Skip to main content

reth_storage_api/
stage_checkpoint.rs

1use crate::BlockHashReader;
2use alloc::{string::String, vec::Vec};
3use alloy_eips::BlockNumHash;
4use alloy_primitives::BlockNumber;
5use reth_stages_types::{StageCheckpoint, StageId};
6use reth_storage_errors::provider::{ProviderError, ProviderResult};
7
8/// The trait for fetching stage checkpoint related data.
9#[auto_impl::auto_impl(&, Arc)]
10pub trait StageCheckpointReader: Send {
11    /// Fetch the checkpoint for the given stage.
12    fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>>;
13
14    /// Get stage checkpoint progress.
15    fn get_stage_checkpoint_progress(&self, id: StageId) -> ProviderResult<Option<Vec<u8>>>;
16
17    /// Reads all stage checkpoints and returns a list with the name of the stage and the checkpoint
18    /// data.
19    fn get_all_checkpoints(&self) -> ProviderResult<Vec<(String, StageCheckpoint)>>;
20}
21
22/// Reads the last block that survives a restart.
23#[auto_impl::auto_impl(&, Arc)]
24pub trait DurableBlockReader: StageCheckpointReader + BlockHashReader {
25    /// Returns the number and hash of the last block that is still canonical in the database after
26    /// a restart.
27    ///
28    /// This can be behind
29    /// [`BlockNumReader::last_block_number`](crate::BlockNumReader::last_block_number): static
30    /// files are written before the database transaction that commits the same blocks, and blocks
31    /// above the partial state/trie frontier of the `Finish` stage checkpoint are unwound on
32    /// startup. Returns genesis if there is no `Finish` checkpoint.
33    ///
34    /// The hash is the database's block at that height, never an in-memory canonical block, so it
35    /// identifies the block a restart keeps even while an in-memory reorg below it is not yet
36    /// persisted. Providers that serve in-memory blocks override this to read the database.
37    ///
38    /// The block only survives a host crash or power loss when the database syncs every commit to
39    /// disk. With `--db.sync-mode safe-no-sync`, it survives a process crash, but MDBX may
40    /// discard the latest commits when the database is reopened after a host failure.
41    fn last_durable_block_num_hash(&self) -> ProviderResult<BlockNumHash> {
42        // The Finish checkpoint is written in the same transaction as the blocks it covers.
43        let number = self
44            .get_stage_checkpoint(StageId::Finish)?
45            .map(|checkpoint| {
46                checkpoint
47                    .finish_stage_checkpoint()
48                    .and_then(|finish| finish.partial_state_trie())
49                    .unwrap_or(checkpoint.block_number)
50            })
51            .unwrap_or_default();
52        let hash = self.block_hash(number)?.ok_or(ProviderError::HeaderNotFound(number.into()))?;
53        Ok(BlockNumHash::new(number, hash))
54    }
55}
56
57/// The trait for updating stage checkpoint related data.
58#[auto_impl::auto_impl(&, Arc)]
59pub trait StageCheckpointWriter {
60    /// Save stage checkpoint.
61    fn save_stage_checkpoint(&self, id: StageId, checkpoint: StageCheckpoint)
62        -> ProviderResult<()>;
63
64    /// Save stage checkpoint progress.
65    fn save_stage_checkpoint_progress(
66        &self,
67        id: StageId,
68        checkpoint: Vec<u8>,
69    ) -> ProviderResult<()>;
70
71    /// Update all pipeline sync stage progress.
72    fn update_pipeline_stages(
73        &self,
74        block_number: BlockNumber,
75        drop_stage_checkpoint: bool,
76    ) -> ProviderResult<()>;
77}