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