Skip to main content

reth_trie_sparse/arena/
nodes.rs

1use super::{
2    branch_child_idx::{BranchChildIdx, BranchChildIter},
3    ArenaSparseSubtrie, BranchChild, NodeArena,
4};
5use alloc::{boxed::Box, vec::Vec};
6use alloy_primitives::{keccak256, B256};
7use alloy_trie::{BranchNodeCompact, TrieMask};
8use reth_trie_common::{BranchNodeMasks, Nibbles, ProofTrieNodeV2, RlpNode, TrieNodeV2};
9use smallvec::SmallVec;
10use strum::AsRefStr;
11
12use crate::TrieNodeEpoch;
13
14/// Tracks whether a node's RLP encoding is cached or needs recomputation.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub(super) enum ArenaSparseNodeState {
17    /// The node has been revealed but its RLP encoding is not cached.
18    Revealed,
19    /// The node has a cached RLP encoding that is still valid.
20    Cached {
21        /// The cached RLP-encoded representation of the node.
22        rlp_node: RlpNode,
23        /// The newest tracked modification epoch for this node or its descendants.
24        epoch: TrieNodeEpoch,
25    },
26    /// The node has been modified and its RLP encoding needs recomputation.
27    Dirty,
28}
29
30impl ArenaSparseNodeState {
31    /// Converts into a [`Self::Dirty`] if it's not already.
32    pub(super) const fn to_dirty(&self) -> Self {
33        Self::Dirty
34    }
35
36    /// Returns the [`RlpNode`] cached on the state, if there is one.
37    pub(super) const fn cached_rlp_node(&self) -> Option<&RlpNode> {
38        match self {
39            Self::Cached { rlp_node, .. } => Some(rlp_node),
40            _ => None,
41        }
42    }
43
44    /// Returns the cached epoch, if this node is cached.
45    pub(super) const fn cached_epoch(&self) -> Option<TrieNodeEpoch> {
46        match self {
47            Self::Cached { epoch, .. } => Some(*epoch),
48            _ => None,
49        }
50    }
51}
52
53/// The branch-specific data stored in an [`ArenaSparseNode::Branch`].
54#[derive(Debug, Clone)]
55pub(super) struct ArenaSparseNodeBranch {
56    /// Cached or dirty state of this node.
57    pub(super) state: ArenaSparseNodeState,
58    /// Revealed or blinded children, packed densely. The `state_mask` tracks which
59    /// nibble positions have entries in this `SmallVec`, which is sized to hold the maximum
60    /// of 16 children inline and therefore never spills.
61    pub(super) children: SmallVec<[BranchChild; 16]>,
62    /// Bitmask indicating which of the 16 child slots are occupied (have an entry
63    /// in `children`).
64    pub(super) state_mask: TrieMask,
65    /// The short key (extension key) for this branch. When non-empty, the node's path is the
66    /// path of the parent extension node with this short key.
67    pub(super) short_key: Nibbles,
68    /// Tree mask and hash mask for database persistence (`TrieUpdates`).
69    pub(super) branch_masks: BranchNodeMasks,
70}
71
72impl ArenaSparseNodeBranch {
73    /// Unsets the bit for `nibble` in `state_mask`, `hash_mask`, and `tree_mask`.
74    pub(super) const fn unset_child_bit(&mut self, nibble: u8) {
75        self.state_mask.unset_bit(nibble);
76    }
77
78    /// Inserts a child at `nibble`, updating the state mask, children array, and marking the
79    /// branch as dirty.
80    pub(super) fn set_child(&mut self, nibble: u8, child: BranchChild) {
81        let insert_pos = BranchChildIdx::insertion_point(self.state_mask, nibble);
82        self.state_mask.set_bit(nibble);
83        self.children.insert(insert_pos.get(), child);
84        self.state = ArenaSparseNodeState::Dirty;
85    }
86
87    /// Removes the child at `nibble`, updating the state mask, children array, and marking the
88    /// branch as dirty.
89    ///
90    /// # Panics
91    ///
92    /// Panics if `nibble` is not set in the state mask.
93    pub(super) fn remove_child(&mut self, nibble: u8) {
94        let child_idx =
95            BranchChildIdx::new(self.state_mask, nibble).expect("nibble not found in state_mask");
96        self.children.remove(child_idx.get());
97        self.unset_child_bit(nibble);
98        self.state = ArenaSparseNodeState::Dirty;
99    }
100
101    /// Returns the sibling child in a branch with exactly 2 children.
102    ///
103    /// # Panics
104    ///
105    /// Panics (debug) if the branch does not have exactly 2 children, or if `nibble` is not set.
106    pub(super) fn sibling_child(&self, nibble: u8) -> BranchChild {
107        debug_assert_eq!(
108            self.state_mask.count_bits(),
109            2,
110            "sibling_child requires exactly 2 children"
111        );
112        let child_idx =
113            BranchChildIdx::new(self.state_mask, nibble).expect("nibble not found in state_mask");
114        // With exactly 2 children the dense array has indices 0 and 1.
115        self.children[1 - child_idx.get()]
116    }
117
118    /// Iterates over `(nibble, child)` pairs in nibble order.
119    pub(super) fn child_iter(&self) -> impl Iterator<Item = (u8, BranchChild)> + '_ {
120        BranchChildIter::new(self.state_mask).map(|(idx, nibble)| (nibble, self.children[idx]))
121    }
122
123    /// Returns a [`BranchNodeCompact`] from this branch's masks and children hashes.
124    pub(super) fn branch_node_compact(&self, arena: &NodeArena) -> BranchNodeCompact {
125        let mut hashes = Vec::with_capacity(self.branch_masks.hash_mask.count_bits() as usize);
126        for (nibble, child) in self.child_iter() {
127            if self.branch_masks.hash_mask.is_bit_set(nibble) {
128                let hash = match child.revealed_index() {
129                    Some(child_idx) => arena[child_idx].cached_hash(),
130                    None => arena.blinded(child).as_hash().expect("blinded child must be a hash"),
131                };
132                hashes.push(hash);
133            }
134        }
135        BranchNodeCompact::new(
136            self.state_mask,
137            self.branch_masks.tree_mask,
138            self.branch_masks.hash_mask,
139            hashes,
140            None,
141        )
142    }
143}
144
145/// A node in the arena-based sparse trie.
146#[derive(Debug, Clone, AsRefStr)]
147pub(super) enum ArenaSparseNode {
148    /// Indicates a trie with no nodes.
149    EmptyRoot {
150        /// Cached or dirty state of this node. Its cached RLP is always the empty root hash.
151        state: ArenaSparseNodeState,
152    },
153    /// A branch node with up to 16 children.
154    Branch(ArenaSparseNodeBranch),
155    /// A leaf node containing a value.
156    Leaf {
157        /// Cached or dirty state of this node.
158        state: ArenaSparseNodeState,
159        /// The RLP-encoded leaf value.
160        value: Vec<u8>,
161        /// The remaining key suffix for this leaf.
162        key: Nibbles,
163    },
164    /// A subtrie that can be taken for parallel processing.
165    Subtrie(Box<ArenaSparseSubtrie>),
166    /// Placeholder for a subtrie that has been temporarily taken for parallel operations.
167    TakenSubtrie,
168    /// Tombstone for a vacated slot. [`NodeArena::remove`](super::NodeArena::remove) also puts
169    /// the slot on the arena's free list, [`NodeArena::drain_node`](super::NodeArena::drain_node)
170    /// does not.
171    Free,
172}
173
174impl ArenaSparseNode {
175    /// Returns the state of an `EmptyRoot`, `Branch`, `Leaf`, or `Subtrie` root node, or `None` for
176    /// other types.
177    pub(super) fn state_ref(&self) -> Option<&ArenaSparseNodeState> {
178        match self {
179            Self::EmptyRoot { state } | Self::Leaf { state, .. } => Some(state),
180            Self::Branch(b) => Some(&b.state),
181            Self::Subtrie(s) => s.arena[s.root].state_ref(),
182            _ => None,
183        }
184    }
185
186    /// Returns a mutable reference to the state of an `EmptyRoot`, `Branch`, or `Leaf` node.
187    ///
188    /// # Panics
189    ///
190    /// Panics if called on a non-EmptyRoot/Branch/Leaf node.
191    pub(super) fn state_mut(&mut self) -> &mut ArenaSparseNodeState {
192        match self {
193            Self::EmptyRoot { state } | Self::Leaf { state, .. } => state,
194            Self::Branch(b) => &mut b.state,
195            _ => panic!("state_mut called on non-EmptyRoot/Branch/Leaf node"),
196        }
197    }
198
199    /// Returns `true` if this node's RLP encoding is cached.
200    pub(super) fn is_cached(&self) -> bool {
201        self.state_ref().is_some_and(|s| matches!(s, ArenaSparseNodeState::Cached { .. }))
202    }
203
204    /// Returns the short key of the branch or leaf, or None.
205    pub(super) const fn short_key(&self) -> Option<&Nibbles> {
206        match self {
207            Self::Branch(b) => Some(&b.short_key),
208            Self::Leaf { key, .. } => Some(key),
209            _ => None,
210        }
211    }
212
213    /// Returns a reference to the branch data.
214    ///
215    /// # Panics
216    ///
217    /// Panics if this is not a `Branch` node.
218    pub(super) fn branch_ref(&self) -> &ArenaSparseNodeBranch {
219        match self {
220            Self::Branch(b) => b,
221            _ => panic!("branch_ref called on non-Branch node {self:?}"),
222        }
223    }
224
225    /// Returns a mutable reference to the branch data.
226    ///
227    /// # Panics
228    ///
229    /// Panics if this is not a `Branch` node.
230    pub(super) fn branch_mut(&mut self) -> &mut ArenaSparseNodeBranch {
231        match self {
232            Self::Branch(b) => b,
233            _ => panic!("branch_mut called on non-Branch node {self:?}"),
234        }
235    }
236
237    /// Returns a reference to the subtrie if this is a `Subtrie` node, or `None`.
238    #[cfg(all(debug_assertions, not(feature = "dst")))]
239    pub(super) const fn as_subtrie(&self) -> Option<&ArenaSparseSubtrie> {
240        match self {
241            Self::Subtrie(s) => Some(s),
242            _ => None,
243        }
244    }
245
246    /// Returns the branch data if this node (or its subtrie root) is a branch, or `None`.
247    pub(super) fn as_branch(&self) -> Option<&ArenaSparseNodeBranch> {
248        match self {
249            Self::Branch(b) => Some(b),
250            Self::Subtrie(s) => s.arena[s.root].as_branch(),
251            _ => None,
252        }
253    }
254
255    /// Returns `true` if this node should contribute a set bit in its parent's `hash_mask`.
256    ///
257    /// That is, if the node is a branch with no short key (no extension) whose cached
258    /// RLP is a hash (>= 32 bytes). Small branches whose RLP is embedded don't get a
259    /// `hash_mask` bit.
260    pub(super) fn hash_mask_bit(&self) -> bool {
261        self.as_branch().is_some_and(|b| {
262            b.short_key.is_empty() &&
263                b.state.cached_rlp_node().expect("branch's RlpNode must be cached").is_hash()
264        })
265    }
266
267    /// Returns `true` if this node should contribute a set bit in its parent's `tree_mask`.
268    ///
269    /// That is, if the node is a branch with any non-empty `branch_masks`.
270    pub(super) fn tree_mask_bit(&self) -> bool {
271        self.as_branch().is_some_and(|b| !b.branch_masks.is_empty())
272    }
273
274    /// Returns the cached hash of this node. Panics if the node's state is not `Cached`.
275    ///
276    /// If the `RlpNode` is already a hash (>= 32 bytes encoded), returns it directly.
277    /// Otherwise keccak-hashes the RLP encoding to produce the hash. This handles the
278    /// case where a branch's RLP is small enough to be embedded rather than hashed.
279    pub(super) fn cached_hash(&self) -> B256 {
280        let rlp_node = match self {
281            Self::Branch(ArenaSparseNodeBranch { state, .. }) | Self::Leaf { state, .. } => state
282                .cached_rlp_node()
283                .expect("cached_hash called on non-Cached branch or leaf: {self:?}"),
284            Self::Subtrie(s) => return s.arena[s.root].cached_hash(),
285            _ => panic!("cached_hash called on {self:?}"),
286        };
287        rlp_node.as_hash().unwrap_or_else(|| keccak256(rlp_node.as_slice()))
288    }
289}
290
291impl ArenaSparseNode {
292    /// Converts a [`ProofTrieNodeV2`] into an [`ArenaSparseNode`], storing the RLP of a branch's
293    /// unrevealed children in `arena`'s blinded side table.
294    ///
295    /// # Panics
296    ///
297    /// Panics if the node is an `Extension`, which should have been merged into a branch
298    /// by [`TrieNodeV2`].
299    pub(super) fn from_proof_node(arena: &mut NodeArena, proof_node: ProofTrieNodeV2) -> Self {
300        let ProofTrieNodeV2 { node, masks, .. } = proof_node;
301        match node {
302            TrieNodeV2::EmptyRoot => Self::EmptyRoot { state: ArenaSparseNodeState::Revealed },
303            TrieNodeV2::Leaf(leaf) => Self::Leaf {
304                state: ArenaSparseNodeState::Revealed,
305                key: leaf.key,
306                value: leaf.value,
307            },
308            TrieNodeV2::Branch(branch) => {
309                let children = branch.stack[..branch.state_mask.count_bits() as usize]
310                    .iter()
311                    .map(|rlp| arena.insert_blinded(rlp.clone()))
312                    .collect();
313                Self::Branch(ArenaSparseNodeBranch {
314                    state: ArenaSparseNodeState::Revealed,
315                    children,
316                    state_mask: branch.state_mask,
317                    short_key: branch.key,
318                    branch_masks: masks.unwrap_or_default(),
319                })
320            }
321            TrieNodeV2::Extension(_) => {
322                panic!("Extension nodes should be merged into branches by TrieNodeV2")
323            }
324        }
325    }
326}