reth_stages_api/pipeline/
event.rs

1use crate::{
2    stage::{ExecOutput, UnwindInput, UnwindOutput},
3    StageCheckpoint, StageId,
4};
5use alloy_primitives::BlockNumber;
6use std::fmt::{Display, Formatter};
7
8/// An event emitted by a [Pipeline][crate::Pipeline].
9///
10/// It is possible for multiple of these events to be emitted over the duration of a pipeline's
11/// execution since:
12///
13/// - Other stages may ask the pipeline to unwind
14/// - The pipeline will loop indefinitely unless a target block is set
15#[derive(Debug, PartialEq, Eq, Clone)]
16pub enum PipelineEvent {
17    /// Emitted when a stage is about to be prepared for a run.
18    Prepare {
19        /// Pipeline stages progress.
20        pipeline_stages_progress: PipelineStagesProgress,
21        /// The stage that is about to be run.
22        stage_id: StageId,
23        /// The previous checkpoint of the stage.
24        checkpoint: Option<StageCheckpoint>,
25        /// The block number up to which the stage is running, if known.
26        target: Option<BlockNumber>,
27    },
28    /// Emitted when a stage is about to be run.
29    Run {
30        /// Pipeline stages progress.
31        pipeline_stages_progress: PipelineStagesProgress,
32        /// The stage that is about to be run.
33        stage_id: StageId,
34        /// The previous checkpoint of the stage.
35        checkpoint: Option<StageCheckpoint>,
36        /// The block number up to which the stage is running, if known.
37        target: Option<BlockNumber>,
38    },
39    /// Emitted when a stage has run a single time.
40    Ran {
41        /// Pipeline stages progress.
42        pipeline_stages_progress: PipelineStagesProgress,
43        /// The stage that was run.
44        stage_id: StageId,
45        /// The result of executing the stage.
46        result: ExecOutput,
47    },
48    /// Emitted when a stage is about to be unwound.
49    Unwind {
50        /// The stage that is about to be unwound.
51        stage_id: StageId,
52        /// The unwind parameters.
53        input: UnwindInput,
54    },
55    /// Emitted when a stage has been unwound.
56    Unwound {
57        /// The stage that was unwound.
58        stage_id: StageId,
59        /// The result of unwinding the stage.
60        result: UnwindOutput,
61    },
62    /// Emitted when a stage encounters an error either during execution or unwinding.
63    Error {
64        /// The stage that encountered an error.
65        stage_id: StageId,
66    },
67    /// Emitted when a stage was skipped due to it's run conditions not being met:
68    ///
69    /// - The stage might have progressed beyond the point of our target block
70    /// - The stage might not need to be unwound since it has not progressed past the unwind target
71    /// - The stage requires that the pipeline has reached the tip, but it has not done so yet
72    Skipped {
73        /// The stage that was skipped.
74        stage_id: StageId,
75    },
76}
77
78/// Pipeline stages progress.
79#[derive(Debug, PartialEq, Eq, Clone)]
80pub struct PipelineStagesProgress {
81    /// 1-indexed ID of the stage that is about to be run out of total stages in the pipeline.
82    pub current: usize,
83    /// Total number of stages in the pipeline.
84    pub total: usize,
85}
86
87impl Display for PipelineStagesProgress {
88    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
89        write!(f, "{}/{}", self.current, self.total)
90    }
91}