Skip to main content

reth_trie_sparse/
provider.rs

1//! Traits and default implementations related to retrieval of blinded trie nodes.
2
3use alloy_primitives::{Bytes, B256};
4use reth_execution_errors::SparseTrieError;
5use reth_trie_common::{Nibbles, TrieMask};
6
7/// Factory for instantiating trie node providers.
8#[auto_impl::auto_impl(&)]
9pub trait TrieNodeProviderFactory {
10    /// Type capable of fetching blinded account nodes.
11    type AccountNodeProvider: TrieNodeProvider;
12    /// Type capable of fetching blinded storage nodes.
13    type StorageNodeProvider: TrieNodeProvider;
14
15    /// Returns blinded account node provider.
16    fn account_node_provider(&self) -> Self::AccountNodeProvider;
17
18    /// Returns blinded storage node provider.
19    fn storage_node_provider(&self, account: B256) -> Self::StorageNodeProvider;
20}
21
22/// Revealed blinded trie node.
23#[derive(PartialEq, Eq, Clone, Debug)]
24pub struct RevealedNode {
25    /// Raw trie node.
26    pub node: Bytes,
27    /// Branch node tree mask, if any.
28    pub tree_mask: Option<TrieMask>,
29    /// Branch node hash mask, if any.
30    pub hash_mask: Option<TrieMask>,
31}
32
33/// Trie node provider for retrieving trie nodes.
34#[auto_impl::auto_impl(&)]
35pub trait TrieNodeProvider {
36    /// Retrieve trie node by path.
37    fn trie_node(&self, path: &Nibbles) -> Result<Option<RevealedNode>, SparseTrieError>;
38}
39
40/// Default trie node provider factory that creates [`DefaultTrieNodeProviderFactory`].
41#[derive(PartialEq, Eq, Clone, Default, Debug)]
42pub struct DefaultTrieNodeProviderFactory;
43
44impl TrieNodeProviderFactory for DefaultTrieNodeProviderFactory {
45    type AccountNodeProvider = DefaultTrieNodeProvider;
46    type StorageNodeProvider = DefaultTrieNodeProvider;
47
48    fn account_node_provider(&self) -> Self::AccountNodeProvider {
49        DefaultTrieNodeProvider
50    }
51
52    fn storage_node_provider(&self, _account: B256) -> Self::StorageNodeProvider {
53        DefaultTrieNodeProvider
54    }
55}
56
57/// Default trie node provider that always returns `Ok(None)`.
58#[derive(PartialEq, Eq, Clone, Default, Debug)]
59pub struct DefaultTrieNodeProvider;
60
61impl TrieNodeProvider for DefaultTrieNodeProvider {
62    fn trie_node(&self, _path: &Nibbles) -> Result<Option<RevealedNode>, SparseTrieError> {
63        Ok(None)
64    }
65}
66
67/// A provider that never reveals nodes from the database.
68///
69/// This is used by `update_leaves` to attempt trie operations without
70/// performing any database lookups. When the trie encounters a blinded node
71/// that would normally trigger a reveal, this provider returns `None`,
72/// causing the operation to fail with a `BlindedNode` error.
73#[derive(PartialEq, Eq, Clone, Copy, Default, Debug)]
74pub struct NoRevealProvider;
75
76impl TrieNodeProvider for NoRevealProvider {
77    fn trie_node(&self, _path: &Nibbles) -> Result<Option<RevealedNode>, SparseTrieError> {
78        Ok(None)
79    }
80}
81
82/// Right pad the path with 0s and return as [`B256`].
83#[inline]
84pub fn pad_path_to_key(path: &Nibbles) -> B256 {
85    let mut padded = path.pack();
86    padded.resize(32, 0);
87    B256::from_slice(&padded)
88}