Skip to main content

reth_trie/
progress.rs

1use crate::{
2    hash_builder::HashBuilder,
3    trie_cursor::CursorSubNode,
4    updates::{StorageTrieUpdates, TrieUpdates},
5};
6use alloy_primitives::B256;
7use reth_primitives_traits::Account;
8use reth_stages_types::MerkleCheckpoint;
9
10/// The progress of the state root computation.
11#[derive(Debug)]
12pub enum StateRootProgress {
13    /// The complete state root computation with updates, the total number of entries walked, and
14    /// the computed root.
15    Complete(B256, usize, TrieUpdates),
16    /// The intermediate progress of state root computation.
17    /// Contains the walker stack, the hash builder, and the trie updates.
18    ///
19    /// Also contains any progress in an inner storage root computation.
20    Progress(Box<IntermediateStateRootState>, usize, TrieUpdates),
21}
22
23/// The intermediate state of the state root computation.
24#[derive(Debug)]
25pub struct IntermediateStateRootState {
26    /// The intermediate account root state.
27    pub account_root_state: IntermediateRootState,
28    /// The intermediate storage root state with account data.
29    pub storage_root_state: Option<IntermediateStorageRootState>,
30}
31
32/// The intermediate state of a storage root computation along with the account.
33#[derive(Debug)]
34pub struct IntermediateStorageRootState {
35    /// The intermediate storage trie state.
36    pub state: IntermediateRootState,
37    /// The account for which the storage root is being computed.
38    pub account: Account,
39}
40
41impl From<MerkleCheckpoint> for IntermediateStateRootState {
42    fn from(value: MerkleCheckpoint) -> Self {
43        Self {
44            account_root_state: IntermediateRootState {
45                hash_builder: HashBuilder::from(value.state),
46                walker_stack: value.walker_stack.into_iter().map(CursorSubNode::from).collect(),
47                last_hashed_key: value.last_account_key,
48            },
49            storage_root_state: value.storage_root_checkpoint.map(|checkpoint| {
50                let account = checkpoint.account();
51                IntermediateStorageRootState {
52                    state: IntermediateRootState {
53                        hash_builder: HashBuilder::from(checkpoint.state),
54                        walker_stack: checkpoint
55                            .walker_stack
56                            .into_iter()
57                            .map(CursorSubNode::from)
58                            .collect(),
59                        last_hashed_key: checkpoint.last_storage_key,
60                    },
61                    account,
62                }
63            }),
64        }
65    }
66}
67
68/// The intermediate state of a state root computation, whether account or storage root.
69#[derive(Debug)]
70pub struct IntermediateRootState {
71    /// Previously constructed hash builder.
72    pub hash_builder: HashBuilder,
73    /// Previously recorded walker stack.
74    pub walker_stack: Vec<CursorSubNode>,
75    /// The last hashed key processed.
76    pub last_hashed_key: B256,
77}
78
79/// The progress of a storage root calculation.
80#[derive(Debug)]
81pub enum StorageRootProgress {
82    /// The complete storage root computation with updates and computed root.
83    Complete(B256, usize, StorageTrieUpdates),
84    /// The intermediate progress of state root computation.
85    /// Contains the walker stack, the hash builder, and the trie updates.
86    Progress(Box<IntermediateRootState>, usize, StorageTrieUpdates),
87}