reth_trie_sparse/
blinded.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 blinded node providers.
8#[auto_impl::auto_impl(&)]
9pub trait BlindedProviderFactory {
10    /// Type capable of fetching blinded account nodes.
11    type AccountNodeProvider: BlindedProvider;
12    /// Type capable of fetching blinded storage nodes.
13    type StorageNodeProvider: BlindedProvider;
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 blinded nodes.
34#[auto_impl::auto_impl(&)]
35pub trait BlindedProvider {
36    /// Retrieve blinded node by path.
37    fn blinded_node(&self, path: &Nibbles) -> Result<Option<RevealedNode>, SparseTrieError>;
38}
39
40/// Default blinded node provider factory that creates [`DefaultBlindedProvider`].
41#[derive(PartialEq, Eq, Clone, Default, Debug)]
42pub struct DefaultBlindedProviderFactory;
43
44impl BlindedProviderFactory for DefaultBlindedProviderFactory {
45    type AccountNodeProvider = DefaultBlindedProvider;
46    type StorageNodeProvider = DefaultBlindedProvider;
47
48    fn account_node_provider(&self) -> Self::AccountNodeProvider {
49        DefaultBlindedProvider
50    }
51
52    fn storage_node_provider(&self, _account: B256) -> Self::StorageNodeProvider {
53        DefaultBlindedProvider
54    }
55}
56
57/// Default blinded node provider that always returns `Ok(None)`.
58#[derive(PartialEq, Eq, Clone, Default, Debug)]
59pub struct DefaultBlindedProvider;
60
61impl BlindedProvider for DefaultBlindedProvider {
62    fn blinded_node(&self, _path: &Nibbles) -> Result<Option<RevealedNode>, SparseTrieError> {
63        Ok(None)
64    }
65}
66
67/// Right pad the path with 0s and return as [`B256`].
68#[inline]
69pub fn pad_path_to_key(path: &Nibbles) -> B256 {
70    let mut padded = path.pack();
71    padded.resize(32, 0);
72    B256::from_slice(&padded)
73}