reth_trie_db/
witness.rs

1use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
2use alloy_primitives::{map::B256Map, Bytes};
3use reth_db_api::transaction::DbTx;
4use reth_execution_errors::TrieWitnessError;
5use reth_trie::{
6    hashed_cursor::HashedPostStateCursorFactory, trie_cursor::InMemoryTrieCursorFactory,
7    witness::TrieWitness, HashedPostState, TrieInput,
8};
9
10/// Extends [`TrieWitness`] with operations specific for working with a database transaction.
11pub trait DatabaseTrieWitness<'a, TX> {
12    /// Create a new [`TrieWitness`] from database transaction.
13    fn from_tx(tx: &'a TX) -> Self;
14
15    /// Generates trie witness for target state based on [`TrieInput`].
16    fn overlay_witness(
17        tx: &'a TX,
18        input: TrieInput,
19        target: HashedPostState,
20    ) -> Result<B256Map<Bytes>, TrieWitnessError>;
21}
22
23impl<'a, TX: DbTx> DatabaseTrieWitness<'a, TX>
24    for TrieWitness<DatabaseTrieCursorFactory<&'a TX>, DatabaseHashedCursorFactory<&'a TX>>
25{
26    fn from_tx(tx: &'a TX) -> Self {
27        Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
28    }
29
30    fn overlay_witness(
31        tx: &'a TX,
32        input: TrieInput,
33        target: HashedPostState,
34    ) -> Result<B256Map<Bytes>, TrieWitnessError> {
35        let nodes_sorted = input.nodes.into_sorted();
36        let state_sorted = input.state.into_sorted();
37        TrieWitness::new(
38            InMemoryTrieCursorFactory::new(DatabaseTrieCursorFactory::new(tx), &nodes_sorted),
39            HashedPostStateCursorFactory::new(DatabaseHashedCursorFactory::new(tx), &state_sorted),
40        )
41        .with_prefix_sets_mut(input.prefix_sets)
42        .always_include_root_node()
43        .compute(target)
44    }
45}