reth_trie_common/
input.rs

1use crate::{prefix_set::TriePrefixSetsMut, updates::TrieUpdates, HashedPostState};
2
3/// Inputs for trie-related computations.
4#[derive(Default, Debug, Clone)]
5pub struct TrieInput {
6    /// The collection of cached in-memory intermediate trie nodes that
7    /// can be reused for computation.
8    pub nodes: TrieUpdates,
9    /// The in-memory overlay hashed state.
10    pub state: HashedPostState,
11    /// The collection of prefix sets for the computation. Since the prefix sets _always_
12    /// invalidate the in-memory nodes, not all keys from `self.state` might be present here,
13    /// if we have cached nodes for them.
14    pub prefix_sets: TriePrefixSetsMut,
15}
16
17impl TrieInput {
18    /// Create new trie input.
19    pub const fn new(
20        nodes: TrieUpdates,
21        state: HashedPostState,
22        prefix_sets: TriePrefixSetsMut,
23    ) -> Self {
24        Self { nodes, state, prefix_sets }
25    }
26
27    /// Create new trie input from in-memory state. The prefix sets will be constructed and
28    /// set automatically.
29    pub fn from_state(state: HashedPostState) -> Self {
30        let prefix_sets = state.construct_prefix_sets();
31        Self { nodes: TrieUpdates::default(), state, prefix_sets }
32    }
33
34    /// Create new trie input from the provided blocks, from oldest to newest. See the documentation
35    /// for [`Self::extend_with_blocks`] for details.
36    pub fn from_blocks<'a>(
37        blocks: impl IntoIterator<Item = (&'a HashedPostState, &'a TrieUpdates)>,
38    ) -> Self {
39        let mut input = Self::default();
40        input.extend_with_blocks(blocks);
41        input
42    }
43
44    /// Extend the trie input with the provided blocks, from oldest to newest.
45    ///
46    /// For blocks with missing trie updates, the trie input will be extended with prefix sets
47    /// constructed from the state of this block and the state itself, **without** trie updates.
48    pub fn extend_with_blocks<'a>(
49        &mut self,
50        blocks: impl IntoIterator<Item = (&'a HashedPostState, &'a TrieUpdates)>,
51    ) {
52        for (hashed_state, trie_updates) in blocks {
53            self.append_cached_ref(trie_updates, hashed_state);
54        }
55    }
56
57    /// Prepend another trie input to the current one.
58    pub fn prepend_self(&mut self, mut other: Self) {
59        core::mem::swap(&mut self.nodes, &mut other.nodes);
60        self.nodes.extend(other.nodes);
61        core::mem::swap(&mut self.state, &mut other.state);
62        self.state.extend(other.state);
63        // No need to swap prefix sets, as they will be sorted and deduplicated.
64        self.prefix_sets.extend(other.prefix_sets);
65    }
66
67    /// Prepend state to the input and extend the prefix sets.
68    pub fn prepend(&mut self, mut state: HashedPostState) {
69        self.prefix_sets.extend(state.construct_prefix_sets());
70        core::mem::swap(&mut self.state, &mut state);
71        self.state.extend(state);
72    }
73
74    /// Prepend intermediate nodes and state to the input.
75    /// Prefix sets for incoming state will be ignored.
76    pub fn prepend_cached(&mut self, mut nodes: TrieUpdates, mut state: HashedPostState) {
77        core::mem::swap(&mut self.nodes, &mut nodes);
78        self.nodes.extend(nodes);
79        core::mem::swap(&mut self.state, &mut state);
80        self.state.extend(state);
81    }
82
83    /// Append state to the input and extend the prefix sets.
84    pub fn append(&mut self, state: HashedPostState) {
85        self.prefix_sets.extend(state.construct_prefix_sets());
86        self.state.extend(state);
87    }
88
89    /// Append state to the input by reference and extend the prefix sets.
90    pub fn append_ref(&mut self, state: &HashedPostState) {
91        self.prefix_sets.extend(state.construct_prefix_sets());
92        self.state.extend_ref(state);
93    }
94
95    /// Append intermediate nodes and state to the input.
96    /// Prefix sets for incoming state will be ignored.
97    pub fn append_cached(&mut self, nodes: TrieUpdates, state: HashedPostState) {
98        self.nodes.extend(nodes);
99        self.state.extend(state);
100    }
101
102    /// Append intermediate nodes and state to the input by reference.
103    /// Prefix sets for incoming state will be ignored.
104    pub fn append_cached_ref(&mut self, nodes: &TrieUpdates, state: &HashedPostState) {
105        self.nodes.extend_ref(nodes);
106        self.state.extend_ref(state);
107    }
108
109    /// This method clears the trie input nodes, state, and prefix sets.
110    pub fn clear(&mut self) {
111        self.nodes.clear();
112        self.state.clear();
113        self.prefix_sets.clear();
114    }
115
116    /// This method returns a cleared version of this trie input.
117    pub fn cleared(mut self) -> Self {
118        self.clear();
119        self
120    }
121}