Skip to main content

anvil/eth/backend/
notifications.rs

1//! Notifications emitted from the backed
2
3use alloy_consensus::Header;
4use alloy_primitives::B256;
5use alloy_rpc_types::Log;
6use futures::channel::mpsc::UnboundedReceiver;
7use std::sync::Arc;
8
9/// A notification that's emitted when the canonical chain was updated.
10#[derive(Clone, Debug)]
11pub enum ChainNotification {
12    /// A new block was added to the canonical chain.
13    Block(NewBlockNotification),
14    /// Logs of blocks that were removed from the canonical chain due to a reorg.
15    ///
16    /// The logs are marked as `removed` and retain the metadata of the removed blocks they were
17    /// originally included in.
18    RemovedLogs(Arc<Vec<Log>>),
19}
20
21impl ChainNotification {
22    /// Returns the [`NewBlockNotification`] if this notification is for an imported block.
23    pub const fn as_new_block(&self) -> Option<&NewBlockNotification> {
24        match self {
25            Self::Block(block) => Some(block),
26            Self::RemovedLogs(_) => None,
27        }
28    }
29}
30
31/// A notification that's emitted when a new block was imported
32#[derive(Clone, Debug)]
33pub struct NewBlockNotification {
34    /// Hash of the imported block
35    pub hash: B256,
36    /// block header
37    pub header: Arc<Header>,
38}
39
40/// Type alias for a receiver that receives [ChainNotification]
41pub type ChainNotifications = UnboundedReceiver<ChainNotification>;