reth_engine_tree/tree/
persistence_state.rs1use alloy_eips::BlockNumHash;
2use alloy_primitives::B256;
3use std::time::Instant;
4use tokio::sync::oneshot;
5use tracing::trace;
6
7#[derive(Default, Debug)]
9pub struct PersistenceState {
10 pub(crate) last_persisted_block: BlockNumHash,
14 pub(crate) rx:
17 Option<(oneshot::Receiver<Option<BlockNumHash>>, Instant, CurrentPersistenceAction)>,
18}
19
20impl PersistenceState {
21 pub(crate) const fn in_progress(&self) -> bool {
24 self.rx.is_some()
25 }
26
27 pub(crate) fn start_remove(
29 &mut self,
30 new_tip_num: u64,
31 rx: oneshot::Receiver<Option<BlockNumHash>>,
32 ) {
33 self.rx =
34 Some((rx, Instant::now(), CurrentPersistenceAction::RemovingBlocks { new_tip_num }));
35 }
36
37 pub(crate) fn start_save(
39 &mut self,
40 highest: BlockNumHash,
41 rx: oneshot::Receiver<Option<BlockNumHash>>,
42 ) {
43 self.rx = Some((rx, Instant::now(), CurrentPersistenceAction::SavingBlocks { highest }));
44 }
45
46 pub(crate) fn current_action(&self) -> Option<&CurrentPersistenceAction> {
49 self.rx.as_ref().map(|rx| &rx.2)
50 }
51
52 pub(crate) fn finish(
54 &mut self,
55 last_persisted_block_hash: B256,
56 last_persisted_block_number: u64,
57 ) {
58 trace!(target: "engine::tree", block= %last_persisted_block_number, hash=%last_persisted_block_hash, "updating persistence state");
59 self.rx = None;
60 self.last_persisted_block =
61 BlockNumHash::new(last_persisted_block_number, last_persisted_block_hash);
62 }
63}
64
65#[derive(Debug, Clone, PartialEq, Eq)]
67pub(crate) enum CurrentPersistenceAction {
68 SavingBlocks {
70 highest: BlockNumHash,
72 },
73 RemovingBlocks {
75 new_tip_num: u64,
77 },
78}