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 target: BlockNumber,
13 /// The block that caused the unwind.
14 bad_block: Box<BlockWithParent>,
15 },
16 /// The pipeline made progress.
17 Continue {
18 /// Block number reached by the stage.
19 block_number: BlockNumber,
20 },
21 /// Pipeline made no progress
22 NoProgress {
23 /// Block number reached by the stage.
24 block_number: Option<BlockNumber>,
25 },
26}
27
28impl ControlFlow {
29 /// Whether the pipeline should continue executing stages.
30 pub const fn should_continue(&self) -> bool {
31 matches!(self, Self::Continue { .. } | Self::NoProgress { .. })
32 }
33
34 /// Returns true if the control flow is unwind.
35 pub const fn is_unwind(&self) -> bool {
36 matches!(self, Self::Unwind { .. })
37 }
38
39 /// Returns the pipeline block number the stage reached, if the state is not `Unwind`.
40 pub const fn block_number(&self) -> Option<BlockNumber> {
41 match self {
42 Self::Unwind { .. } => None,
43 Self::Continue { block_number } => Some(*block_number),
44 Self::NoProgress { block_number } => *block_number,
45 }
46 }
47}