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;
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/// Trie node provider for retrieving blinded nodes.
22pub trait BlindedProvider {
23    /// The error type for the provider.
24    type Error: Into<SparseTrieError>;
25
26    /// Retrieve blinded node by path.
27    fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, Self::Error>;
28}
29
30/// Default blinded node provider factory that creates [`DefaultBlindedProvider`].
31#[derive(PartialEq, Eq, Clone, Default, Debug)]
32pub struct DefaultBlindedProviderFactory;
33
34impl BlindedProviderFactory for DefaultBlindedProviderFactory {
35    type AccountNodeProvider = DefaultBlindedProvider;
36    type StorageNodeProvider = DefaultBlindedProvider;
37
38    fn account_node_provider(&self) -> Self::AccountNodeProvider {
39        DefaultBlindedProvider
40    }
41
42    fn storage_node_provider(&self, _account: B256) -> Self::StorageNodeProvider {
43        DefaultBlindedProvider
44    }
45}
46
47/// Default blinded node provider that always returns `Ok(None)`.
48#[derive(PartialEq, Eq, Clone, Default, Debug)]
49pub struct DefaultBlindedProvider;
50
51impl BlindedProvider for DefaultBlindedProvider {
52    type Error = SparseTrieError;
53
54    fn blinded_node(&mut self, _path: &Nibbles) -> Result<Option<Bytes>, Self::Error> {
55        Ok(None)
56    }
57}
58
59/// Right pad the path with 0s and return as [`B256`].
60#[inline]
61pub fn pad_path_to_key(path: &Nibbles) -> B256 {
62    let mut padded = path.pack();
63    padded.resize(32, 0);
64    B256::from_slice(&padded)
65}