reth_beacon_consensus/engine/
event.rs

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