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_rpc_types_engine::ForkchoiceState;
8use core::{
9    fmt::{Display, Formatter, Result},
10    time::Duration,
11};
12use reth_chain_state::ExecutedBlock;
13use reth_ethereum_primitives::EthPrimitives;
14use reth_primitives_traits::{NodePrimitives, SealedBlock, SealedHeader};
15
16/// Type alias for backwards compat
17#[deprecated(note = "Use ConsensusEngineEvent instead")]
18pub type BeaconConsensusEngineEvent<N> = ConsensusEngineEvent<N>;
19
20/// Events emitted by the consensus engine.
21#[derive(Clone, Debug)]
22pub enum ConsensusEngineEvent<N: NodePrimitives = EthPrimitives> {
23    /// The fork choice state was updated, and the current fork choice status
24    ForkchoiceUpdated(ForkchoiceState, ForkchoiceStatus),
25    /// A block was added to the fork chain.
26    ForkBlockAdded(ExecutedBlock<N>, Duration),
27    /// A new block was received from the consensus engine
28    BlockReceived(BlockNumHash),
29    /// A block was added to the canonical chain, and the elapsed time validating the block
30    CanonicalBlockAdded(ExecutedBlock<N>, Duration),
31    /// A canonical chain was committed, and the elapsed time committing the data
32    CanonicalChainCommitted(Box<SealedHeader<N::BlockHeader>>, Duration),
33    /// The consensus engine processed an invalid block.
34    InvalidBlock(Box<SealedBlock<N::Block>>),
35}
36
37impl<N: NodePrimitives> ConsensusEngineEvent<N> {
38    /// Returns the canonical header if the event is a
39    /// [`ConsensusEngineEvent::CanonicalChainCommitted`].
40    pub const fn canonical_header(&self) -> Option<&SealedHeader<N::BlockHeader>> {
41        match self {
42            Self::CanonicalChainCommitted(header, _) => Some(header),
43            _ => None,
44        }
45    }
46}
47
48impl<N> Display for ConsensusEngineEvent<N>
49where
50    N: NodePrimitives<BlockHeader: BlockHeader>,
51{
52    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
53        match self {
54            Self::ForkchoiceUpdated(state, status) => {
55                write!(f, "ForkchoiceUpdated({state:?}, {status:?})")
56            }
57            Self::ForkBlockAdded(block, duration) => {
58                write!(f, "ForkBlockAdded({:?}, {duration:?})", block.recovered_block.num_hash())
59            }
60            Self::CanonicalBlockAdded(block, duration) => {
61                write!(
62                    f,
63                    "CanonicalBlockAdded({:?}, {duration:?})",
64                    block.recovered_block.num_hash()
65                )
66            }
67            Self::CanonicalChainCommitted(block, duration) => {
68                write!(f, "CanonicalChainCommitted({:?}, {duration:?})", block.num_hash())
69            }
70            Self::InvalidBlock(block) => {
71                write!(f, "InvalidBlock({:?})", block.num_hash())
72            }
73            Self::BlockReceived(num_hash) => {
74                write!(f, "BlockReceived({num_hash:?})")
75            }
76        }
77    }
78}