forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[event] trigger events processing (MystenLabs#2326)
* rebase & comments * format
- Loading branch information
Showing
10 changed files
with
151 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::authority::{AuthorityStore, AuthorityStoreWrapper}; | ||
use crate::streamer::Streamer; | ||
use chrono::prelude::*; | ||
use move_bytecode_utils::module_cache::SyncModuleCache; | ||
use std::convert::TryFrom; | ||
use std::sync::Arc; | ||
use sui_types::{ | ||
error::{SuiError, SuiResult}, | ||
event::{Event, EventEnvelope}, | ||
messages::TransactionEffects, | ||
}; | ||
use tokio::sync::mpsc::{self, Sender}; | ||
use tracing::{debug, error}; | ||
|
||
const EVENT_DISPATCH_BUFFER_SIZE: usize = 1000; | ||
|
||
pub fn get_unixtime_ms() -> u64 { | ||
let ts_ms = Utc::now().timestamp_millis(); | ||
u64::try_from(ts_ms).expect("Travelling in time machine") | ||
} | ||
|
||
pub struct EventHandler { | ||
module_cache: SyncModuleCache<AuthorityStoreWrapper>, | ||
streamer_queue: Sender<EventEnvelope>, | ||
} | ||
|
||
impl EventHandler { | ||
pub fn new(validator_store: Arc<AuthorityStore>) -> Self { | ||
let (tx, rx) = mpsc::channel::<EventEnvelope>(EVENT_DISPATCH_BUFFER_SIZE); | ||
Streamer::spawn(rx); | ||
Self { | ||
module_cache: SyncModuleCache::new(AuthorityStoreWrapper(validator_store)), | ||
streamer_queue: tx, | ||
} | ||
} | ||
|
||
pub async fn process_events(&self, effects: &TransactionEffects) { | ||
// serializely dispatch event processing to honor events' orders. | ||
for event in &effects.events { | ||
if let Err(e) = self.process_event(event).await { | ||
error!(error =? e, "Failed to send EventEnvolope to dispatch"); | ||
} | ||
} | ||
} | ||
|
||
pub async fn process_event(&self, event: &Event) -> SuiResult { | ||
let envolope = match event { | ||
Event::MoveEvent { .. } => { | ||
debug!(event =? event, "Process MoveEvent."); | ||
match event.extract_move_struct(&self.module_cache) { | ||
Ok(Some(move_struct)) => EventEnvelope::new( | ||
get_unixtime_ms(), | ||
None, | ||
event.clone(), | ||
Some(move_struct), | ||
), | ||
Ok(None) => unreachable!("Expect a MoveStruct from a MoveEvent."), | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
_ => EventEnvelope::new(get_unixtime_ms(), None, event.clone(), None), | ||
}; | ||
|
||
// TODO store events here | ||
|
||
self.streamer_queue | ||
.send(envolope) | ||
.await | ||
.map_err(|e| SuiError::EventFailedToDispatch { | ||
error: e.to_string(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use sui_types::event::EventEnvelope; | ||
use tokio::{sync::mpsc::Receiver, task::JoinHandle}; | ||
use tracing::debug; | ||
|
||
pub struct Streamer { | ||
event_queue: Receiver<EventEnvelope>, | ||
} | ||
|
||
impl Streamer { | ||
pub fn spawn(rx: Receiver<EventEnvelope>) -> JoinHandle<()> { | ||
tokio::spawn(async move { Self { event_queue: rx }.stream().await }) | ||
} | ||
|
||
pub async fn stream(&mut self) { | ||
while let Some(event_envelope) = self.event_queue.recv().await { | ||
debug!(event_envelope =? event_envelope, "Get event"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters