reth_engine_tree/tree/persistence_state.rs
1//! Persistence state management for background database operations.
2//!
3//! This module manages the state of background tasks that persist cached data
4//! to the database. The persistence system works asynchronously to avoid blocking
5//! block execution while ensuring data durability.
6//!
7//! ## Background Persistence
8//!
9//! The execution engine maintains an in-memory cache of state changes that need
10//! to be persisted to disk. Rather than writing synchronously (which would slow
11//! down block processing), persistence happens in background tasks.
12//!
13//! ## Persistence Actions
14//!
15//! - **Saving Blocks**: Persist newly executed blocks and their state changes
16//! - **Removing Blocks**: Remove invalid blocks during chain reorganizations
17//!
18//! ## Coordination
19//!
20//! The [`PersistenceState`] tracks ongoing persistence operations and coordinates
21//! between the main execution thread and background persistence workers.
22
23use crate::persistence::PersistenceResult;
24use alloy_eips::BlockNumHash;
25use reth_concurrency::sync::{channel::Receiver, time::FastInstant as Instant};
26use tracing::trace;
27
28/// The state of the persistence task.
29#[derive(Debug)]
30pub struct PersistenceState {
31 /// Hash and number of the highest block whose non-state/trie outputs are persisted.
32 ///
33 /// This tracks the highest canonical block with durable block/static-file/plain-state data.
34 pub(crate) last_persisted_block: BlockNumHash,
35 /// Hash and number of the highest block whose state/trie outputs were processed for
36 /// persistence.
37 pub(crate) last_state_trie_persisted_block: BlockNumHash,
38 /// Receiver end of channel where the result of the persistence task will be
39 /// sent when done. A None value means there's no persistence task in progress.
40 pub(crate) rx: Option<(Receiver<PersistenceResult>, Instant, CurrentPersistenceAction)>,
41}
42
43impl PersistenceState {
44 /// Determines if there is a persistence task in progress by checking if the
45 /// receiver is set.
46 pub(crate) const fn in_progress(&self) -> bool {
47 self.rx.is_some()
48 }
49
50 /// Sets the state for a block removal operation.
51 pub(crate) fn start_remove(&mut self, new_tip_num: u64, rx: Receiver<PersistenceResult>) {
52 self.rx =
53 Some((rx, Instant::now(), CurrentPersistenceAction::RemovingBlocks { new_tip_num }));
54 }
55
56 /// Sets the state for a block save operation.
57 pub(crate) fn start_save(&mut self, highest: BlockNumHash, rx: Receiver<PersistenceResult>) {
58 self.rx = Some((rx, Instant::now(), CurrentPersistenceAction::SavingBlocks { highest }));
59 }
60
61 /// Returns the current persistence action. If there is no persistence task in progress, then
62 /// this returns `None`.
63 #[cfg(test)]
64 pub(crate) fn current_action(&self) -> Option<&CurrentPersistenceAction> {
65 self.rx.as_ref().map(|rx| &rx.2)
66 }
67
68 /// Sets state for a finished persistence task.
69 pub(crate) fn finish(
70 &mut self,
71 last_persisted_block: BlockNumHash,
72 last_state_trie_persisted_block: BlockNumHash,
73 ) {
74 trace!(
75 target: "engine::tree",
76 last_persisted_block = %last_persisted_block.number,
77 last_state_trie_persisted_block = %last_state_trie_persisted_block.number,
78 "updating persistence state"
79 );
80 self.rx = None;
81 self.last_persisted_block = last_persisted_block;
82 self.last_state_trie_persisted_block = last_state_trie_persisted_block;
83 }
84}
85
86/// The currently running persistence action.
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub(crate) enum CurrentPersistenceAction {
89 /// The persistence task is saving blocks.
90 SavingBlocks {
91 /// The highest block being saved.
92 highest: BlockNumHash,
93 },
94 /// The persistence task is removing blocks.
95 RemovingBlocks {
96 /// The tip, above which we are removing blocks.
97 new_tip_num: u64,
98 },
99}