reth_stages_types/
lib.rs

1//! Commonly used types for staged sync usage.
2
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
5    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
6    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
7)]
8#![cfg_attr(not(test), warn(unused_crate_dependencies))]
9#![cfg_attr(docsrs, feature(doc_cfg))]
10#![cfg_attr(not(feature = "std"), no_std)]
11
12extern crate alloc;
13
14mod id;
15use alloy_primitives::{BlockHash, BlockNumber};
16pub use id::StageId;
17
18mod checkpoints;
19pub use checkpoints::{
20    AccountHashingCheckpoint, CheckpointBlockRange, EntitiesCheckpoint, ExecutionCheckpoint,
21    HeadersCheckpoint, IndexHistoryCheckpoint, MerkleCheckpoint, StageCheckpoint,
22    StageUnitCheckpoint, StorageHashingCheckpoint, StorageRootMerkleCheckpoint,
23};
24
25mod execution;
26pub use execution::*;
27
28/// Direction and target block for pipeline operations.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum PipelineTarget {
31    /// Target for forward synchronization, indicating a block hash to sync to.
32    Sync(BlockHash),
33    /// Target for backward unwinding, indicating a block number to unwind to.
34    Unwind(BlockNumber),
35}
36
37impl PipelineTarget {
38    /// Returns the target block hash for forward synchronization, if applicable.
39    ///
40    /// # Returns
41    ///
42    /// - `Some(BlockHash)`: The target block hash for forward synchronization.
43    /// - `None`: If the target is for backward unwinding.
44    pub const fn sync_target(self) -> Option<BlockHash> {
45        match self {
46            Self::Sync(hash) => Some(hash),
47            Self::Unwind(_) => None,
48        }
49    }
50
51    /// Returns the target block number for backward unwinding, if applicable.
52    ///
53    /// # Returns
54    ///
55    /// - `Some(BlockNumber)`: The target block number for backward unwinding.
56    /// - `None`: If the target is for forward synchronization.
57    pub const fn unwind_target(self) -> Option<BlockNumber> {
58        match self {
59            Self::Sync(_) => None,
60            Self::Unwind(number) => Some(number),
61        }
62    }
63}
64
65impl From<BlockHash> for PipelineTarget {
66    fn from(hash: BlockHash) -> Self {
67        Self::Sync(hash)
68    }
69}
70
71impl core::fmt::Display for PipelineTarget {
72    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73        match self {
74            Self::Sync(block) => {
75                write!(f, "Sync({block})")
76            }
77            Self::Unwind(block) => write!(f, "Unwind({block})"),
78        }
79    }
80}