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.
8pub trait BlindedProviderFactory {
9    /// Type capable of fetching blinded account nodes.
10    type AccountNodeProvider: BlindedProvider;
11    /// Type capable of fetching blinded storage nodes.
12    type StorageNodeProvider: BlindedProvider;
13
14    /// Returns blinded account node provider.
15    fn account_node_provider(&self) -> Self::AccountNodeProvider;
16
17    /// Returns blinded storage node provider.
18    fn storage_node_provider(&self, account: B256) -> Self::StorageNodeProvider;
19}
20
21/// Revealed blinded trie node.
22#[derive(Debug)]
23pub struct RevealedNode {
24    /// Raw trie node.
25    pub node: Bytes,
26    /// Branch node tree mask, if any.
27    pub tree_mask: Option<TrieMask>,
28    /// Branch node hash mask, if any.
29    pub hash_mask: Option<TrieMask>,
30}
31
32/// Trie node provider for retrieving blinded nodes.
33pub trait BlindedProvider {
34    /// Retrieve blinded node by path.
35    fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<RevealedNode>, SparseTrieError>;
36}
37
38/// Default blinded node provider factory that creates [`DefaultBlindedProvider`].
39#[derive(PartialEq, Eq, Clone, Default, Debug)]
40pub struct DefaultBlindedProviderFactory;
41
42impl BlindedProviderFactory for DefaultBlindedProviderFactory {
43    type AccountNodeProvider = DefaultBlindedProvider;
44    type StorageNodeProvider = DefaultBlindedProvider;
45
46    fn account_node_provider(&self) -> Self::AccountNodeProvider {
47        DefaultBlindedProvider
48    }
49
50    fn storage_node_provider(&self, _account: B256) -> Self::StorageNodeProvider {
51        DefaultBlindedProvider
52    }
53}
54
55/// Default blinded node provider that always returns `Ok(None)`.
56#[derive(PartialEq, Eq, Clone, Default, Debug)]
57pub struct DefaultBlindedProvider;
58
59impl BlindedProvider for DefaultBlindedProvider {
60    fn blinded_node(&mut self, _path: &Nibbles) -> Result<Option<RevealedNode>, SparseTrieError> {
61        Ok(None)
62    }
63}
64
65/// Right pad the path with 0s and return as [`B256`].
66#[inline]
67pub fn pad_path_to_key(path: &Nibbles) -> B256 {
68    let mut padded = path.pack();
69    padded.resize(32, 0);
70    B256::from_slice(&padded)
71}