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                IntermediateStorageRootState {
51                    state: IntermediateRootState {
52                        hash_builder: HashBuilder::from(checkpoint.state),
53                        walker_stack: checkpoint
54                            .walker_stack
55                            .into_iter()
56                            .map(CursorSubNode::from)
57                            .collect(),
58                        last_hashed_key: checkpoint.last_storage_key,
59                    },
60                    account: Account {
61                        nonce: checkpoint.account_nonce,
62                        balance: checkpoint.account_balance,
63                        bytecode_hash: Some(checkpoint.account_bytecode_hash),
64                    },
65                }
66            }),
67        }
68    }
69}
70
71/// The intermediate state of a state root computation, whether account or storage root.
72#[derive(Debug)]
73pub struct IntermediateRootState {
74    /// Previously constructed hash builder.
75    pub hash_builder: HashBuilder,
76    /// Previously recorded walker stack.
77    pub walker_stack: Vec<CursorSubNode>,
78    /// The last hashed key processed.
79    pub last_hashed_key: B256,
80}
81
82/// The progress of a storage root calculation.
83#[derive(Debug)]
84pub enum StorageRootProgress {
85    /// The complete storage root computation with updates and computed root.
86    Complete(B256, usize, StorageTrieUpdates),
87    /// The intermediate progress of state root computation.
88    /// Contains the walker stack, the hash builder, and the trie updates.
89    Progress(Box<IntermediateRootState>, usize, StorageTrieUpdates),
90}