reth_trie_common/
trie.rs

1//! Types related to sparse trie nodes and masks.
2
3use crate::Nibbles;
4use alloy_primitives::map::HashMap;
5use alloy_trie::{nodes::TrieNode, TrieMask};
6
7/// Branch node masks containing `hash_mask` and `tree_mask`.
8///
9/// Consolidates `hash_mask` and `tree_mask` into a single struct, reducing `HashMap` overhead
10/// when storing masks by path. Instead of two separate `HashMap<Nibbles, TrieMask>`,
11/// we use a single `HashMap<Nibbles, BranchNodeMasks>`.
12#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
13pub struct BranchNodeMasks {
14    /// Hash mask indicating which children are stored as hashes.
15    pub hash_mask: TrieMask,
16    /// Tree mask indicating which children are complete subtrees.
17    pub tree_mask: TrieMask,
18}
19
20impl BranchNodeMasks {
21    /// Creates masks from optional hash and tree masks.
22    ///
23    /// Returns `None` if both masks are `None`, otherwise defaults missing masks to empty.
24    pub fn from_optional(hash_mask: Option<TrieMask>, tree_mask: Option<TrieMask>) -> Option<Self> {
25        match (hash_mask, tree_mask) {
26            (Some(h), Some(t)) => Some(Self { hash_mask: h, tree_mask: t }),
27            (Some(h), None) => Some(Self { hash_mask: h, tree_mask: TrieMask::default() }),
28            (None, Some(t)) => Some(Self { hash_mask: TrieMask::default(), tree_mask: t }),
29            (None, None) => None,
30        }
31    }
32}
33
34/// Map from nibble path to branch node masks.
35pub type BranchNodeMasksMap = HashMap<Nibbles, BranchNodeMasks>;
36
37/// Carries all information needed by a sparse trie to reveal a particular node.
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct ProofTrieNode {
40    /// Path of the node.
41    pub path: Nibbles,
42    /// The node itself.
43    pub node: TrieNode,
44    /// Tree and hash masks for the node, if known.
45    /// Both masks are always set together (from database branch nodes).
46    pub masks: Option<BranchNodeMasks>,
47}