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