reth_trie_common/trie.rs
1//! Types related to sparse trie nodes and masks.
2
3use crate::Nibbles;
4use alloy_trie::{nodes::TrieNode, TrieMask};
5
6/// Struct for passing around branch node mask information.
7///
8/// Branch nodes can have up to 16 children (one for each nibble).
9/// The masks represent which children are stored in different ways:
10/// - `hash_mask`: Indicates which children are stored as hashes in the database
11/// - `tree_mask`: Indicates which children are complete subtrees stored in the database
12///
13/// These masks are essential for efficient trie traversal and serialization, as they
14/// determine how nodes should be encoded and stored on disk.
15#[derive(Debug, PartialEq, Eq, Clone, Copy)]
16pub struct TrieMasks {
17 /// Branch node hash mask, if any.
18 ///
19 /// When a bit is set, the corresponding child node's hash is stored in the trie.
20 ///
21 /// This mask enables selective hashing of child nodes.
22 pub hash_mask: Option<TrieMask>,
23 /// Branch node tree mask, if any.
24 ///
25 /// When a bit is set, the corresponding child subtree is stored in the database.
26 pub tree_mask: Option<TrieMask>,
27}
28
29impl TrieMasks {
30 /// Helper function, returns both fields `hash_mask` and `tree_mask` as [`None`]
31 pub const fn none() -> Self {
32 Self { hash_mask: None, tree_mask: None }
33 }
34}
35
36/// Carries all information needed by a sparse trie to reveal a particular node.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct ProofTrieNode {
39 /// Path of the node.
40 pub path: Nibbles,
41 /// The node itself.
42 pub node: TrieNode,
43 /// Tree and hash masks for the node, if known.
44 pub masks: TrieMasks,
45}