reth_stages_api/pipeline/
ctrl.rs

1use alloy_eips::eip1898::BlockWithParent;
2use alloy_primitives::BlockNumber;
3
4/// Determines the control flow during pipeline execution.
5///
6/// See [`Pipeline::run_loop`](crate::Pipeline::run_loop) for more information.
7#[derive(Debug, Clone, Eq, PartialEq)]
8pub enum ControlFlow {
9    /// An unwind was requested and must be performed before continuing.
10    Unwind {
11        /// The block to unwind to.
12        ///
13        /// This marks the highest block to which the stage should unwind to.
14        /// For example, unwinding to block 10, should remove all data for blocks above 10 (>=11).
15        target: BlockNumber,
16        /// The block that caused the unwind.
17        bad_block: Box<BlockWithParent>,
18    },
19    /// The pipeline made progress.
20    Continue {
21        /// Block number reached by the stage.
22        block_number: BlockNumber,
23    },
24    /// Pipeline made no progress
25    NoProgress {
26        /// Block number reached by the stage.
27        block_number: Option<BlockNumber>,
28    },
29}
30
31impl ControlFlow {
32    /// Whether the pipeline should continue executing stages.
33    pub const fn should_continue(&self) -> bool {
34        matches!(self, Self::Continue { .. } | Self::NoProgress { .. })
35    }
36
37    /// Returns true if the control flow is unwind.
38    pub const fn is_unwind(&self) -> bool {
39        matches!(self, Self::Unwind { .. })
40    }
41
42    /// Returns the pipeline block number the stage reached, if the state is not `Unwind`.
43    pub const fn block_number(&self) -> Option<BlockNumber> {
44        match self {
45            Self::Unwind { .. } => None,
46            Self::Continue { block_number } => Some(*block_number),
47            Self::NoProgress { block_number } => *block_number,
48        }
49    }
50}