From 8a0156fe4e3d671ae742cb7966e37a650c1328f0 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 14 Feb 2023 15:49:56 +0100 Subject: [PATCH] feat: add ChainEventSubscriptions trait (#1338) --- crates/interfaces/src/events.rs | 21 +++++++++++++++++++++ crates/interfaces/src/lib.rs | 24 +++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 crates/interfaces/src/events.rs diff --git a/crates/interfaces/src/events.rs b/crates/interfaces/src/events.rs new file mode 100644 index 0000000000..f92f8890f3 --- /dev/null +++ b/crates/interfaces/src/events.rs @@ -0,0 +1,21 @@ +use reth_primitives::{Header, H256}; +use std::sync::Arc; +use tokio::sync::mpsc::UnboundedReceiver; + +/// Type alias for a receiver that receives [NewBlockNotification] +pub type NewBlockNotifications = UnboundedReceiver; + +/// A type that allows to register chain related event subscriptions. +pub trait ChainEventSubscriptions { + /// Get notified when a new block was imported. + fn subscribe_new_blocks(&self) -> NewBlockNotifications; +} + +/// A notification that's emitted when a new block was imported. +#[derive(Clone, Debug)] +pub struct NewBlockNotification { + /// Hash of the block that was imported + pub hash: H256, + /// The block header of the new block + pub header: Arc
, +} diff --git a/crates/interfaces/src/lib.rs b/crates/interfaces/src/lib.rs index 3c42c98f1a..1a29eff82d 100644 --- a/crates/interfaces/src/lib.rs +++ b/crates/interfaces/src/lib.rs @@ -7,29 +7,31 @@ //! Reth interface bindings -/// Block Execution traits. -pub mod executor; - /// Consensus traits. pub mod consensus; -/// Provider error -pub mod provider; - /// Database error pub mod db; +/// Block Execution traits. +pub mod executor; + +/// Possible errors when interacting with the chain. +mod error; +pub use error::{Error, Result}; + +/// Traits for subscribing to events. +pub mod events; + /// P2P traits. pub mod p2p; +/// Provider error +pub mod provider; + /// Syncing related traits. pub mod sync; -/// Possible errors when interacting with the chain. -mod error; - -pub use error::{Error, Result}; - #[cfg(any(test, feature = "test-utils"))] /// Common test helpers for mocking out Consensus, Downloaders and Header Clients. pub mod test_utils;