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    /// Prepend state to the input and extend the prefix sets.
35    pub fn prepend(&mut self, mut state: HashedPostState) {
36        self.prefix_sets.extend(state.construct_prefix_sets());
37        core::mem::swap(&mut self.state, &mut state);
38        self.state.extend(state);
39    }
40
41    /// Prepend intermediate nodes and state to the input.
42    /// Prefix sets for incoming state will be ignored.
43    pub fn prepend_cached(&mut self, mut nodes: TrieUpdates, mut state: HashedPostState) {
44        core::mem::swap(&mut self.nodes, &mut nodes);
45        self.nodes.extend(nodes);
46        core::mem::swap(&mut self.state, &mut state);
47        self.state.extend(state);
48    }
49
50    /// Append state to the input and extend the prefix sets.
51    pub fn append(&mut self, state: HashedPostState) {
52        self.prefix_sets.extend(state.construct_prefix_sets());
53        self.state.extend(state);
54    }
55
56    /// Append state to the input by reference and extend the prefix sets.
57    pub fn append_ref(&mut self, state: &HashedPostState) {
58        self.prefix_sets.extend(state.construct_prefix_sets());
59        self.state.extend_ref(state);
60    }
61
62    /// Append intermediate nodes and state to the input.
63    /// Prefix sets for incoming state will be ignored.
64    pub fn append_cached(&mut self, nodes: TrieUpdates, state: HashedPostState) {
65        self.nodes.extend(nodes);
66        self.state.extend(state);
67    }
68
69    /// Append intermediate nodes and state to the input by reference.
70    /// Prefix sets for incoming state will be ignored.
71    pub fn append_cached_ref(&mut self, nodes: &TrieUpdates, state: &HashedPostState) {
72        self.nodes.extend_ref(nodes);
73        self.state.extend_ref(state);
74    }
75}