Skip to main content

reth_trie_db/
proof.rs

1use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory, TrieTableAdapter};
2use alloy_primitives::{keccak256, map::HashMap, Address, B256};
3use reth_db_api::transaction::DbTx;
4use reth_execution_errors::StateProofError;
5use reth_trie::{
6    hashed_cursor::HashedPostStateCursorFactory,
7    proof::{Proof, StorageProof},
8    trie_cursor::InMemoryTrieCursorFactory,
9    HashedPostStateSorted, HashedStorage, StorageMultiProof, TrieInputSorted,
10};
11
12/// Extends [`Proof`] with operations specific for working with a database transaction.
13pub trait DatabaseProof<'a> {
14    /// Associated type for the database transaction.
15    type Tx;
16
17    /// Create a new [`Proof`] instance from database transaction.
18    fn from_tx(tx: &'a Self::Tx) -> Self;
19}
20
21impl<'a, TX: DbTx, A: TrieTableAdapter> DatabaseProof<'a>
22    for Proof<DatabaseTrieCursorFactory<&'a TX, A>, DatabaseHashedCursorFactory<&'a TX>>
23{
24    type Tx = TX;
25
26    fn from_tx(tx: &'a Self::Tx) -> Self {
27        Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
28    }
29}
30
31/// Extends [`StorageProof`] with operations specific for working with a database transaction.
32pub trait DatabaseStorageProof<'a, TX> {
33    /// Generates the storage proof for target slot based on [`HashedStorage`].
34    fn overlay_storage_proof(
35        tx: &'a TX,
36        address: Address,
37        slot: B256,
38        storage: HashedStorage,
39    ) -> Result<reth_trie::StorageProof, StateProofError>;
40
41    /// Generates the storage multiproof for target slots based on [`TrieInputSorted`].
42    fn overlay_storage_multiproof(
43        tx: &'a TX,
44        address: Address,
45        slots: &[B256],
46        input: TrieInputSorted,
47    ) -> Result<StorageMultiProof, StateProofError>;
48}
49
50impl<'a, TX: DbTx, A: TrieTableAdapter> DatabaseStorageProof<'a, TX>
51    for StorageProof<DatabaseTrieCursorFactory<&'a TX, A>, DatabaseHashedCursorFactory<&'a TX>>
52{
53    fn overlay_storage_proof(
54        tx: &'a TX,
55        address: Address,
56        slot: B256,
57        storage: HashedStorage,
58    ) -> Result<reth_trie::StorageProof, StateProofError> {
59        let hashed_address = keccak256(address);
60        let prefix_set = storage.construct_prefix_set();
61        let state_sorted = HashedPostStateSorted::new(
62            Default::default(),
63            HashMap::from_iter([(hashed_address, storage.into_sorted())]),
64        );
65        StorageProof::new(
66            DatabaseTrieCursorFactory::<_, A>::new(tx),
67            HashedPostStateCursorFactory::new(DatabaseHashedCursorFactory::new(tx), &state_sorted),
68            address,
69        )
70        .with_prefix_set_mut(prefix_set)
71        .storage_proof(slot)
72    }
73
74    fn overlay_storage_multiproof(
75        tx: &'a TX,
76        address: Address,
77        slots: &[B256],
78        mut input: TrieInputSorted,
79    ) -> Result<StorageMultiProof, StateProofError> {
80        let hashed_address = keccak256(address);
81        let targets = slots.iter().map(keccak256).collect();
82        StorageProof::new(
83            InMemoryTrieCursorFactory::new(
84                DatabaseTrieCursorFactory::<_, A>::new(tx),
85                &input.nodes,
86            ),
87            HashedPostStateCursorFactory::new(DatabaseHashedCursorFactory::new(tx), &input.state),
88            address,
89        )
90        .with_prefix_set_mut(
91            input.prefix_sets.storage_prefix_sets.remove(&hashed_address).unwrap_or_default(),
92        )
93        .storage_multiproof(targets)
94    }
95}