reth_engine_tree/tree/
persistence_state.rs

1use alloy_eips::BlockNumHash;
2use alloy_primitives::B256;
3use std::{collections::VecDeque, time::Instant};
4use tokio::sync::oneshot;
5use tracing::{debug, trace};
6
7/// The state of the persistence task.
8#[derive(Default, Debug)]
9pub struct PersistenceState {
10    /// Hash and number of the last block persisted.
11    ///
12    /// This tracks the chain height that is persisted on disk
13    pub(crate) last_persisted_block: BlockNumHash,
14    /// Receiver end of channel where the result of the persistence task will be
15    /// sent when done. A None value means there's no persistence task in progress.
16    pub(crate) rx: Option<(oneshot::Receiver<Option<BlockNumHash>>, Instant)>,
17    /// The block above which blocks should be removed from disk, because there has been an on disk
18    /// reorg.
19    pub(crate) remove_above_state: VecDeque<u64>,
20}
21
22impl PersistenceState {
23    /// Determines if there is a persistence task in progress by checking if the
24    /// receiver is set.
25    pub(crate) const fn in_progress(&self) -> bool {
26        self.rx.is_some()
27    }
28
29    /// Sets state for a started persistence task.
30    pub(crate) fn start(&mut self, rx: oneshot::Receiver<Option<BlockNumHash>>) {
31        self.rx = Some((rx, Instant::now()));
32    }
33
34    /// Sets the `remove_above_state`, to the new tip number specified, only if it is less than the
35    /// current `last_persisted_block_number`.
36    pub(crate) fn schedule_removal(&mut self, new_tip_num: u64) {
37        debug!(target: "engine::tree", ?new_tip_num, prev_remove_state=?self.remove_above_state, last_persisted_block=?self.last_persisted_block, "Scheduling removal");
38        self.remove_above_state.push_back(new_tip_num);
39    }
40
41    /// Sets state for a finished persistence task.
42    pub(crate) fn finish(
43        &mut self,
44        last_persisted_block_hash: B256,
45        last_persisted_block_number: u64,
46    ) {
47        trace!(target: "engine::tree", block= %last_persisted_block_number, hash=%last_persisted_block_hash, "updating persistence state");
48        self.rx = None;
49        self.last_persisted_block =
50            BlockNumHash::new(last_persisted_block_number, last_persisted_block_hash);
51    }
52}