reth_engine_primitives/
event.rs

1//! Events emitted by the beacon consensus engine.
2
3use crate::ForkchoiceStatus;
4use alloc::boxed::Box;
5use alloy_consensus::BlockHeader;
6use alloy_eips::BlockNumHash;
7use alloy_primitives::B256;
8use alloy_rpc_types_engine::ForkchoiceState;
9use core::{
10    fmt::{Display, Formatter, Result},
11    time::Duration,
12};
13use reth_chain_state::ExecutedBlockWithTrieUpdates;
14use reth_ethereum_primitives::EthPrimitives;
15use reth_primitives_traits::{NodePrimitives, SealedBlock, SealedHeader};
16
17/// Type alias for backwards compat
18#[deprecated(note = "Use ConsensusEngineEvent instead")]
19pub type BeaconConsensusEngineEvent<N> = ConsensusEngineEvent<N>;
20
21/// Events emitted by the consensus engine.
22#[derive(Clone, Debug)]
23pub enum ConsensusEngineEvent<N: NodePrimitives = EthPrimitives> {
24    /// The fork choice state was updated, and the current fork choice status
25    ForkchoiceUpdated(ForkchoiceState, ForkchoiceStatus),
26    /// A block was added to the fork chain.
27    ForkBlockAdded(ExecutedBlockWithTrieUpdates<N>, Duration),
28    /// A new block was received from the consensus engine
29    BlockReceived(BlockNumHash),
30    /// A block was added to the canonical chain, and the elapsed time validating the block
31    CanonicalBlockAdded(ExecutedBlockWithTrieUpdates<N>, Duration),
32    /// A canonical chain was committed, and the elapsed time committing the data
33    CanonicalChainCommitted(Box<SealedHeader<N::BlockHeader>>, Duration),
34    /// The consensus engine processed an invalid block.
35    InvalidBlock(Box<SealedBlock<N::Block>>),
36    /// The consensus engine is involved in live sync, and has specific progress
37    LiveSyncProgress(ConsensusEngineLiveSyncProgress),
38}
39
40impl<N: NodePrimitives> ConsensusEngineEvent<N> {
41    /// Returns the canonical header if the event is a
42    /// [`ConsensusEngineEvent::CanonicalChainCommitted`].
43    pub const fn canonical_header(&self) -> Option<&SealedHeader<N::BlockHeader>> {
44        match self {
45            Self::CanonicalChainCommitted(header, _) => Some(header),
46            _ => None,
47        }
48    }
49}
50
51impl<N> Display for ConsensusEngineEvent<N>
52where
53    N: NodePrimitives<BlockHeader: BlockHeader>,
54{
55    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
56        match self {
57            Self::ForkchoiceUpdated(state, status) => {
58                write!(f, "ForkchoiceUpdated({state:?}, {status:?})")
59            }
60            Self::ForkBlockAdded(block, duration) => {
61                write!(f, "ForkBlockAdded({:?}, {duration:?})", block.recovered_block.num_hash())
62            }
63            Self::CanonicalBlockAdded(block, duration) => {
64                write!(
65                    f,
66                    "CanonicalBlockAdded({:?}, {duration:?})",
67                    block.recovered_block.num_hash()
68                )
69            }
70            Self::CanonicalChainCommitted(block, duration) => {
71                write!(f, "CanonicalChainCommitted({:?}, {duration:?})", block.num_hash())
72            }
73            Self::InvalidBlock(block) => {
74                write!(f, "InvalidBlock({:?})", block.num_hash())
75            }
76            Self::LiveSyncProgress(progress) => {
77                write!(f, "LiveSyncProgress({progress:?})")
78            }
79            Self::BlockReceived(num_hash) => {
80                write!(f, "BlockReceived({num_hash:?})")
81            }
82        }
83    }
84}
85
86/// Progress of the consensus engine during live sync.
87#[derive(Clone, Debug)]
88pub enum ConsensusEngineLiveSyncProgress {
89    /// The consensus engine is downloading blocks from the network.
90    DownloadingBlocks {
91        /// The number of blocks remaining to download.
92        remaining_blocks: u64,
93        /// The target block hash and number to download.
94        target: B256,
95    },
96}