reth_trie_sparse/trie.rs
1use crate::{
2 provider::TrieNodeProvider, LeafUpdate, ParallelSparseTrie, SparseTrie as SparseTrieTrait,
3 SparseTrieUpdates,
4};
5use alloc::{borrow::Cow, boxed::Box, vec::Vec};
6use alloy_primitives::{map::B256Map, B256};
7use reth_execution_errors::{SparseTrieErrorKind, SparseTrieResult};
8use reth_trie_common::{BranchNodeMasks, Nibbles, RlpNode, TrieMask, TrieNodeV2};
9use tracing::instrument;
10
11/// A sparse trie that is either in a "blind" state (no nodes are revealed, root node hash is
12/// unknown) or in a "revealed" state (root node has been revealed and the trie can be updated).
13///
14/// In blind mode the trie does not contain any decoded node data, which saves memory but
15/// prevents direct access to node contents. The revealed mode stores decoded nodes along
16/// with additional information such as values, allowing direct manipulation.
17///
18/// The sparse trie design is optimised for:
19/// 1. Memory efficiency - only revealed nodes are loaded into memory
20/// 2. Update tracking - changes to the trie structure can be tracked and selectively persisted
21/// 3. Incremental operations - nodes can be revealed as needed without loading the entire trie.
22/// This is what gives rise to the notion of a "sparse" trie.
23#[derive(PartialEq, Eq, Debug, Clone)]
24pub enum RevealableSparseTrie<T = ParallelSparseTrie> {
25 /// The trie is blind -- no nodes have been revealed
26 ///
27 /// This is the default state. In this state, the trie cannot be directly queried or modified
28 /// until nodes are revealed.
29 ///
30 /// In this state the `RevealableSparseTrie` can optionally carry with it a cleared
31 /// sparse trie. This allows for reusing the trie's allocations between payload executions.
32 Blind(Option<Box<T>>),
33 /// Some nodes in the Trie have been revealed.
34 ///
35 /// In this state, the trie can be queried and modified for the parts
36 /// that have been revealed. Other parts remain blind and require revealing
37 /// before they can be accessed.
38 Revealed(Box<T>),
39}
40
41impl<T: Default> Default for RevealableSparseTrie<T> {
42 fn default() -> Self {
43 Self::Blind(None)
44 }
45}
46
47impl<T: SparseTrieTrait + Default> RevealableSparseTrie<T> {
48 /// Creates a new revealed but empty sparse trie with `SparseNode::Empty` as root node.
49 pub fn revealed_empty() -> Self {
50 Self::Revealed(Box::default())
51 }
52
53 /// Reveals the root node, converting a blind trie into a revealed one.
54 ///
55 /// If the trie is blinded, its root node is replaced with `root`.
56 ///
57 /// The `masks` are used to determine how the node's children are stored.
58 /// The `retain_updates` flag controls whether changes to the trie structure
59 /// should be tracked.
60 ///
61 /// # Returns
62 ///
63 /// A mutable reference to the underlying [`RevealableSparseTrie`](SparseTrieTrait).
64 pub fn reveal_root(
65 &mut self,
66 root: TrieNodeV2,
67 masks: Option<BranchNodeMasks>,
68 retain_updates: bool,
69 ) -> SparseTrieResult<&mut T> {
70 // if `Blind`, we initialize the revealed trie with the given root node, using a
71 // pre-allocated trie if available.
72 if self.is_blind() {
73 let mut revealed_trie = if let Self::Blind(Some(cleared_trie)) = core::mem::take(self) {
74 cleared_trie
75 } else {
76 Box::default()
77 };
78
79 revealed_trie.set_root(root, masks, retain_updates)?;
80 *self = Self::Revealed(revealed_trie);
81 }
82
83 Ok(self.as_revealed_mut().unwrap())
84 }
85}
86
87impl<T: SparseTrieTrait> RevealableSparseTrie<T> {
88 /// Creates a new blind sparse trie.
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use reth_trie_sparse::{provider::DefaultTrieNodeProvider, RevealableSparseTrie};
94 ///
95 /// let trie = <RevealableSparseTrie>::blind();
96 /// assert!(trie.is_blind());
97 /// let trie = <RevealableSparseTrie>::default();
98 /// assert!(trie.is_blind());
99 /// ```
100 pub const fn blind() -> Self {
101 Self::Blind(None)
102 }
103
104 /// Creates a new blind sparse trie, clearing and later reusing the given
105 /// [`RevealableSparseTrie`](SparseTrieTrait).
106 pub fn blind_from(mut trie: T) -> Self {
107 trie.clear();
108 Self::Blind(Some(Box::new(trie)))
109 }
110
111 /// Returns `true` if the sparse trie has no revealed nodes.
112 pub const fn is_blind(&self) -> bool {
113 matches!(self, Self::Blind(_))
114 }
115
116 /// Returns `true` if the sparse trie is revealed.
117 pub const fn is_revealed(&self) -> bool {
118 matches!(self, Self::Revealed(_))
119 }
120
121 /// Returns an immutable reference to the underlying revealed sparse trie.
122 ///
123 /// Returns `None` if the trie is blinded.
124 pub const fn as_revealed_ref(&self) -> Option<&T> {
125 if let Self::Revealed(revealed) = self {
126 Some(revealed)
127 } else {
128 None
129 }
130 }
131
132 /// Returns a mutable reference to the underlying revealed sparse trie.
133 ///
134 /// Returns `None` if the trie is blinded.
135 pub fn as_revealed_mut(&mut self) -> Option<&mut T> {
136 if let Self::Revealed(revealed) = self {
137 Some(revealed)
138 } else {
139 None
140 }
141 }
142
143 /// Wipes the trie by removing all nodes and values,
144 /// and resetting the trie to only contain an empty root node.
145 ///
146 /// Note: This method will error if the trie is blinded.
147 pub fn wipe(&mut self) -> SparseTrieResult<()> {
148 let revealed = self.as_revealed_mut().ok_or(SparseTrieErrorKind::Blind)?;
149 revealed.wipe();
150 Ok(())
151 }
152
153 /// Calculates the root hash of the trie.
154 ///
155 /// This will update any remaining dirty nodes before computing the root hash.
156 /// "dirty" nodes are nodes that need their hashes to be recomputed because one or more of their
157 /// children's hashes have changed.
158 ///
159 /// # Returns
160 ///
161 /// - `Some(B256)` with the calculated root hash if the trie is revealed.
162 /// - `None` if the trie is still blind.
163 pub fn root(&mut self) -> Option<B256> {
164 Some(self.as_revealed_mut()?.root())
165 }
166
167 /// Returns true if the root node is cached and does not need any recomputation.
168 pub fn is_root_cached(&self) -> bool {
169 self.as_revealed_ref().is_some_and(|trie| trie.is_root_cached())
170 }
171
172 /// Returns the root hash along with any accumulated update information.
173 ///
174 /// This is useful for when you need both the root hash and information about
175 /// what nodes were modified, which can be used to efficiently update
176 /// an external database.
177 ///
178 /// # Returns
179 ///
180 /// An `Option` tuple consisting of:
181 /// - The trie root hash (`B256`).
182 /// - A [`SparseTrieUpdates`] structure containing information about updated nodes.
183 /// - `None` if the trie is still blind.
184 pub fn root_with_updates(&mut self) -> Option<(B256, SparseTrieUpdates)> {
185 let revealed = self.as_revealed_mut()?;
186 Some((revealed.root(), revealed.take_updates()))
187 }
188
189 /// Clears this trie, setting it to a blind state.
190 ///
191 /// If this instance was revealed, or was itself a `Blind` with a pre-allocated
192 /// [`RevealableSparseTrie`](SparseTrieTrait), this will set to `Blind` carrying a cleared
193 /// pre-allocated [`RevealableSparseTrie`](SparseTrieTrait).
194 #[inline]
195 pub fn clear(&mut self) {
196 *self = match core::mem::replace(self, Self::blind()) {
197 s @ Self::Blind(_) => s,
198 Self::Revealed(mut trie) => {
199 trie.clear();
200 Self::Blind(Some(trie))
201 }
202 };
203 }
204
205 /// Updates (or inserts) a leaf at the given key path with the specified RLP-encoded value.
206 ///
207 /// # Errors
208 ///
209 /// Returns an error if the trie is still blind, or if the update fails.
210 #[instrument(level = "trace", target = "trie::sparse", skip_all)]
211 pub fn update_leaf(
212 &mut self,
213 path: Nibbles,
214 value: Vec<u8>,
215 provider: impl TrieNodeProvider,
216 ) -> SparseTrieResult<()> {
217 let revealed = self.as_revealed_mut().ok_or(SparseTrieErrorKind::Blind)?;
218 revealed.update_leaf(path, value, provider)?;
219 Ok(())
220 }
221
222 /// Removes a leaf node at the specified key path.
223 ///
224 /// # Errors
225 ///
226 /// Returns an error if the trie is still blind, or if the leaf cannot be removed
227 #[instrument(level = "trace", target = "trie::sparse", skip_all)]
228 pub fn remove_leaf(
229 &mut self,
230 path: &Nibbles,
231 provider: impl TrieNodeProvider,
232 ) -> SparseTrieResult<()> {
233 let revealed = self.as_revealed_mut().ok_or(SparseTrieErrorKind::Blind)?;
234 revealed.remove_leaf(path, provider)?;
235 Ok(())
236 }
237
238 /// Shrinks the capacity of the sparse trie's node storage.
239 /// Works for both revealed and blind tries with allocated storage.
240 pub fn shrink_nodes_to(&mut self, size: usize) {
241 match self {
242 Self::Blind(Some(trie)) | Self::Revealed(trie) => {
243 trie.shrink_nodes_to(size);
244 }
245 _ => {}
246 }
247 }
248
249 /// Shrinks the capacity of the sparse trie's value storage.
250 /// Works for both revealed and blind tries with allocated storage.
251 pub fn shrink_values_to(&mut self, size: usize) {
252 match self {
253 Self::Blind(Some(trie)) | Self::Revealed(trie) => {
254 trie.shrink_values_to(size);
255 }
256 _ => {}
257 }
258 }
259}
260
261impl<T: SparseTrieTrait + Default> RevealableSparseTrie<T> {
262 /// Applies batch leaf updates to the sparse trie.
263 ///
264 /// For blind tries, all updates are kept in the map and proof targets are emitted
265 /// for every key (with `min_len = 0` since nothing is revealed).
266 ///
267 /// For revealed tries, delegates to the inner implementation which will:
268 /// - Apply updates where possible
269 /// - Keep blocked updates in the map
270 /// - Emit proof targets for blinded paths
271 pub fn update_leaves(
272 &mut self,
273 updates: &mut B256Map<LeafUpdate>,
274 mut proof_required_fn: impl FnMut(B256, u8),
275 ) -> SparseTrieResult<()> {
276 match self {
277 Self::Blind(_) => {
278 // Nothing is revealed - emit proof targets for all keys with min_len = 0
279 for key in updates.keys() {
280 proof_required_fn(*key, 0);
281 }
282 // All updates remain in the map for retry after proofs are fetched
283 Ok(())
284 }
285 Self::Revealed(trie) => trie.update_leaves(updates, proof_required_fn),
286 }
287 }
288}
289
290/// Enum representing sparse trie node type.
291#[derive(Debug, Clone, Copy, PartialEq, Eq)]
292pub enum SparseNodeType {
293 /// Empty trie node.
294 Empty,
295 /// A placeholder that stores only the hash for a node that has not been fully revealed.
296 Hash,
297 /// Sparse leaf node.
298 Leaf,
299 /// Sparse extension node.
300 Extension {
301 /// A flag indicating whether the extension node should be stored in the database.
302 store_in_db_trie: Option<bool>,
303 },
304 /// Sparse branch node.
305 Branch {
306 /// A flag indicating whether the branch node should be stored in the database.
307 store_in_db_trie: Option<bool>,
308 },
309}
310
311impl SparseNodeType {
312 /// Returns true if the node is a hash node.
313 pub const fn is_hash(&self) -> bool {
314 matches!(self, Self::Hash)
315 }
316
317 /// Returns true if the node is a branch node.
318 pub const fn is_branch(&self) -> bool {
319 matches!(self, Self::Branch { .. })
320 }
321
322 /// Returns true if the node should be stored in the database.
323 pub const fn store_in_db_trie(&self) -> Option<bool> {
324 match *self {
325 Self::Extension { store_in_db_trie } | Self::Branch { store_in_db_trie } => {
326 store_in_db_trie
327 }
328 _ => None,
329 }
330 }
331}
332
333/// Enum representing trie nodes in sparse trie.
334#[derive(Debug, Clone, PartialEq, Eq)]
335pub enum SparseNode {
336 /// Empty trie node.
337 Empty,
338 /// Sparse leaf node with remaining key suffix.
339 Leaf {
340 /// Remaining key suffix for the leaf node.
341 key: Nibbles,
342 /// Tracker for the node's state, e.g. cached `RlpNode` tracking.
343 state: SparseNodeState,
344 },
345 /// Sparse extension node with key.
346 Extension {
347 /// The key slice stored by this extension node.
348 key: Nibbles,
349 /// Tracker for the node's state, e.g. cached `RlpNode` tracking.
350 state: SparseNodeState,
351 },
352 /// Sparse branch node with state mask.
353 Branch {
354 /// The bitmask representing children present in the branch node.
355 state_mask: TrieMask,
356 /// Tracker for the node's state, e.g. cached `RlpNode` tracking.
357 state: SparseNodeState,
358 /// The mask of the children that are blinded.
359 blinded_mask: TrieMask,
360 /// The hashes of the children that are blinded.
361 blinded_hashes: Box<[B256; 16]>,
362 },
363}
364
365impl SparseNode {
366 /// Create new [`SparseNode::Branch`] from state mask and blinded nodes.
367 #[cfg(test)]
368 pub fn new_branch(state_mask: TrieMask, blinded_children: &[(u8, B256)]) -> Self {
369 let mut blinded_mask = TrieMask::default();
370 let mut blinded_hashes = Box::new([B256::ZERO; 16]);
371
372 for (nibble, hash) in blinded_children {
373 blinded_mask.set_bit(*nibble);
374 blinded_hashes[*nibble as usize] = *hash;
375 }
376 Self::Branch { state_mask, state: SparseNodeState::Dirty, blinded_mask, blinded_hashes }
377 }
378
379 /// Create new [`SparseNode::Branch`] with two bits set.
380 pub fn new_split_branch(bit_a: u8, bit_b: u8) -> Self {
381 let state_mask = TrieMask::new(
382 // set bits for both children
383 (1u16 << bit_a) | (1u16 << bit_b),
384 );
385 Self::Branch {
386 state_mask,
387 state: SparseNodeState::Dirty,
388 blinded_mask: TrieMask::default(),
389 blinded_hashes: Box::new([B256::ZERO; 16]),
390 }
391 }
392
393 /// Create new [`SparseNode::Extension`] from the key slice.
394 pub const fn new_ext(key: Nibbles) -> Self {
395 Self::Extension { key, state: SparseNodeState::Dirty }
396 }
397
398 /// Create new [`SparseNode::Leaf`] from leaf key and value.
399 pub const fn new_leaf(key: Nibbles) -> Self {
400 Self::Leaf { key, state: SparseNodeState::Dirty }
401 }
402
403 /// Returns the cached [`RlpNode`] of the node, if it's available.
404 pub fn cached_rlp_node(&self) -> Option<Cow<'_, RlpNode>> {
405 match &self {
406 Self::Empty => None,
407 Self::Leaf { state, .. } |
408 Self::Extension { state, .. } |
409 Self::Branch { state, .. } => state.cached_rlp_node().map(Cow::Borrowed),
410 }
411 }
412
413 /// Returns the cached hash of the node, if it's available.
414 pub fn cached_hash(&self) -> Option<B256> {
415 match &self {
416 Self::Empty => None,
417 Self::Leaf { state, .. } |
418 Self::Extension { state, .. } |
419 Self::Branch { state, .. } => state.cached_hash(),
420 }
421 }
422
423 /// Sets the hash of the node for testing purposes.
424 ///
425 /// For [`SparseNode::Empty`] nodes, this method panics.
426 #[cfg(any(test, feature = "test-utils"))]
427 pub fn set_state(&mut self, new_state: SparseNodeState) {
428 match self {
429 Self::Empty => {
430 panic!("Cannot set hash for Empty or Hash nodes")
431 }
432 Self::Leaf { state, .. } |
433 Self::Extension { state, .. } |
434 Self::Branch { state, .. } => {
435 *state = new_state;
436 }
437 }
438 }
439
440 /// Sets the state of the node and returns a new node with the same state.
441 #[cfg(any(test, feature = "test-utils"))]
442 pub fn with_state(mut self, state: SparseNodeState) -> Self {
443 self.set_state(state);
444 self
445 }
446
447 /// Returns the memory size of this node in bytes.
448 pub const fn memory_size(&self) -> usize {
449 match self {
450 Self::Empty | Self::Branch { .. } => core::mem::size_of::<Self>(),
451 Self::Leaf { key, .. } | Self::Extension { key, .. } => {
452 core::mem::size_of::<Self>() + key.len()
453 }
454 }
455 }
456}
457
458/// Tracks the current state of a node in the trie, specifically regarding whether it's been updated
459/// or not.
460#[derive(Debug, Clone, PartialEq, Eq)]
461pub enum SparseNodeState {
462 /// The node has been updated and its new `RlpNode` has not yet been calculated.
463 ///
464 /// If a node is dirty and has children (branches or extensions) then at least once child must
465 /// also be dirty.
466 Dirty,
467 /// The node has a cached `RlpNode`, either from being revealed or computed after an update.
468 Cached {
469 /// The RLP node which is used to represent this node in its parent. Usually this is the
470 /// RLP encoding of the node's hash, except for when the node RLP encodes to <32
471 /// bytes.
472 rlp_node: RlpNode,
473 /// Flag indicating if this node is cached in the database.
474 ///
475 /// NOTE for extension nodes this actually indicates the node's child branch is in the
476 /// database, not the extension itself.
477 store_in_db_trie: Option<bool>,
478 },
479}
480
481impl SparseNodeState {
482 /// Returns the cached [`RlpNode`] of the node, if it's available.
483 pub const fn cached_rlp_node(&self) -> Option<&RlpNode> {
484 match self {
485 Self::Cached { rlp_node, .. } => Some(rlp_node),
486 Self::Dirty => None,
487 }
488 }
489
490 /// Returns the cached hash of the node, if it's available.
491 pub fn cached_hash(&self) -> Option<B256> {
492 self.cached_rlp_node().and_then(|n| n.as_hash())
493 }
494
495 /// Returns whether or not this node is stored in the db, or None if it's not known.
496 pub const fn store_in_db_trie(&self) -> Option<bool> {
497 match self {
498 Self::Cached { store_in_db_trie, .. } => *store_in_db_trie,
499 Self::Dirty => None,
500 }
501 }
502}
503
504/// RLP node stack item.
505#[derive(Clone, PartialEq, Eq, Debug)]
506pub struct RlpNodeStackItem {
507 /// Path to the node.
508 pub path: Nibbles,
509 /// RLP node.
510 pub rlp_node: RlpNode,
511 /// Type of the node.
512 pub node_type: SparseNodeType,
513}
514
515impl SparseTrieUpdates {
516 /// Create new wiped sparse trie updates.
517 pub fn wiped() -> Self {
518 Self { wiped: true, ..Default::default() }
519 }
520
521 /// Clears the updates, but keeps the backing data structures allocated.
522 ///
523 /// Sets `wiped` to `false`.
524 pub fn clear(&mut self) {
525 self.updated_nodes.clear();
526 self.removed_nodes.clear();
527 self.wiped = false;
528 }
529
530 /// Extends the updates with another set of updates.
531 pub fn extend(&mut self, other: Self) {
532 self.updated_nodes.extend(other.updated_nodes);
533 self.removed_nodes.extend(other.removed_nodes);
534 self.wiped |= other.wiped;
535 }
536}