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