reth_trie/proof/
blinded.rs

1use super::{Proof, StorageProof};
2use crate::{hashed_cursor::HashedCursorFactory, trie_cursor::TrieCursorFactory};
3use alloy_primitives::{
4    map::{HashMap, HashSet},
5    Bytes, B256,
6};
7use reth_execution_errors::{SparseTrieError, SparseTrieErrorKind};
8use reth_trie_common::{prefix_set::TriePrefixSetsMut, Nibbles};
9use reth_trie_sparse::blinded::{pad_path_to_key, BlindedProvider, BlindedProviderFactory};
10use std::sync::Arc;
11
12/// Factory for instantiating providers capable of retrieving blinded trie nodes via proofs.
13#[derive(Debug)]
14pub struct ProofBlindedProviderFactory<T, H> {
15    /// The cursor factory for traversing trie nodes.
16    trie_cursor_factory: T,
17    /// The factory for hashed cursors.
18    hashed_cursor_factory: H,
19    /// A set of prefix sets that have changes.
20    prefix_sets: Arc<TriePrefixSetsMut>,
21}
22
23impl<T, H> ProofBlindedProviderFactory<T, H> {
24    /// Create new proof-based blinded provider factory.
25    pub const fn new(
26        trie_cursor_factory: T,
27        hashed_cursor_factory: H,
28        prefix_sets: Arc<TriePrefixSetsMut>,
29    ) -> Self {
30        Self { trie_cursor_factory, hashed_cursor_factory, prefix_sets }
31    }
32}
33
34impl<T, H> BlindedProviderFactory for ProofBlindedProviderFactory<T, H>
35where
36    T: TrieCursorFactory + Clone + Send + Sync,
37    H: HashedCursorFactory + Clone + Send + Sync,
38{
39    type AccountNodeProvider = ProofBlindedAccountProvider<T, H>;
40    type StorageNodeProvider = ProofBlindedStorageProvider<T, H>;
41
42    fn account_node_provider(&self) -> Self::AccountNodeProvider {
43        ProofBlindedAccountProvider {
44            trie_cursor_factory: self.trie_cursor_factory.clone(),
45            hashed_cursor_factory: self.hashed_cursor_factory.clone(),
46            prefix_sets: self.prefix_sets.clone(),
47        }
48    }
49
50    fn storage_node_provider(&self, account: B256) -> Self::StorageNodeProvider {
51        ProofBlindedStorageProvider {
52            trie_cursor_factory: self.trie_cursor_factory.clone(),
53            hashed_cursor_factory: self.hashed_cursor_factory.clone(),
54            prefix_sets: self.prefix_sets.clone(),
55            account,
56        }
57    }
58}
59
60/// Blinded provider for retrieving account trie nodes by path.
61#[derive(Debug)]
62pub struct ProofBlindedAccountProvider<T, H> {
63    /// The cursor factory for traversing trie nodes.
64    trie_cursor_factory: T,
65    /// The factory for hashed cursors.
66    hashed_cursor_factory: H,
67    /// A set of prefix sets that have changes.
68    prefix_sets: Arc<TriePrefixSetsMut>,
69}
70
71impl<T, H> ProofBlindedAccountProvider<T, H> {
72    /// Create new proof-based blinded account node provider.
73    pub const fn new(
74        trie_cursor_factory: T,
75        hashed_cursor_factory: H,
76        prefix_sets: Arc<TriePrefixSetsMut>,
77    ) -> Self {
78        Self { trie_cursor_factory, hashed_cursor_factory, prefix_sets }
79    }
80}
81
82impl<T, H> BlindedProvider for ProofBlindedAccountProvider<T, H>
83where
84    T: TrieCursorFactory + Clone + Send + Sync,
85    H: HashedCursorFactory + Clone + Send + Sync,
86{
87    type Error = SparseTrieError;
88
89    fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, Self::Error> {
90        let targets = HashMap::from_iter([(pad_path_to_key(path), HashSet::default())]);
91        let proof =
92            Proof::new(self.trie_cursor_factory.clone(), self.hashed_cursor_factory.clone())
93                .with_prefix_sets_mut(self.prefix_sets.as_ref().clone())
94                .multiproof(targets.into())
95                .map_err(|error| SparseTrieErrorKind::Other(Box::new(error)))?;
96
97        Ok(proof.account_subtree.into_inner().remove(path))
98    }
99}
100
101/// Blinded provider for retrieving storage trie nodes by path.
102#[derive(Debug)]
103pub struct ProofBlindedStorageProvider<T, H> {
104    /// The cursor factory for traversing trie nodes.
105    trie_cursor_factory: T,
106    /// The factory for hashed cursors.
107    hashed_cursor_factory: H,
108    /// A set of prefix sets that have changes.
109    prefix_sets: Arc<TriePrefixSetsMut>,
110    /// Target account.
111    account: B256,
112}
113
114impl<T, H> ProofBlindedStorageProvider<T, H> {
115    /// Create new proof-based blinded storage node provider.
116    pub const fn new(
117        trie_cursor_factory: T,
118        hashed_cursor_factory: H,
119        prefix_sets: Arc<TriePrefixSetsMut>,
120        account: B256,
121    ) -> Self {
122        Self { trie_cursor_factory, hashed_cursor_factory, prefix_sets, account }
123    }
124}
125
126impl<T, H> BlindedProvider for ProofBlindedStorageProvider<T, H>
127where
128    T: TrieCursorFactory + Clone + Send + Sync,
129    H: HashedCursorFactory + Clone + Send + Sync,
130{
131    type Error = SparseTrieError;
132
133    fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, Self::Error> {
134        let targets = HashSet::from_iter([pad_path_to_key(path)]);
135        let storage_prefix_set =
136            self.prefix_sets.storage_prefix_sets.get(&self.account).cloned().unwrap_or_default();
137        let proof = StorageProof::new_hashed(
138            self.trie_cursor_factory.clone(),
139            self.hashed_cursor_factory.clone(),
140            self.account,
141        )
142        .with_prefix_set_mut(storage_prefix_set)
143        .storage_multiproof(targets)
144        .map_err(|error| SparseTrieErrorKind::Other(Box::new(error)))?;
145
146        Ok(proof.subtree.into_inner().remove(path))
147    }
148}