Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/trie/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
mod proof;
mod state;
mod storage;
mod witness;

pub use proof::DatabaseProof;
pub use state::DatabaseStateRoot;
pub use storage::DatabaseStorageRoot;
pub use witness::DatabaseTrieWitness;
46 changes: 46 additions & 0 deletions crates/trie/db/src/witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use reth_db_api::transaction::DbTx;
use reth_execution_errors::TrieWitnessError;
use reth_primitives::{Bytes, B256};
use reth_trie::{
hashed_cursor::{DatabaseHashedCursorFactory, HashedPostStateCursorFactory},
trie_cursor::DatabaseTrieCursorFactory,
witness::TrieWitness,
HashedPostState,
};
use std::collections::HashMap;

/// Extends [`TrieWitness`] with operations specific for working with a database transaction.
pub trait DatabaseTrieWitness<'a, TX> {
/// Create a new [`TrieWitness`] from database transaction.
fn from_tx(tx: &'a TX) -> Self;

/// Generates trie witness for target state on top of this [`HashedPostState`].
fn overlay_witness(
tx: &'a TX,
post_state: HashedPostState,
target: HashedPostState,
) -> Result<HashMap<B256, Bytes>, TrieWitnessError>;
}

impl<'a, TX: DbTx> DatabaseTrieWitness<'a, TX>
for TrieWitness<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>
{
fn from_tx(tx: &'a TX) -> Self {
Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
}

fn overlay_witness(
tx: &'a TX,
post_state: HashedPostState,
target: HashedPostState,
) -> Result<HashMap<B256, Bytes>, TrieWitnessError> {
let prefix_sets = post_state.construct_prefix_sets();
let sorted = post_state.into_sorted();
let hashed_cursor_factory =
HashedPostStateCursorFactory::new(DatabaseHashedCursorFactory::new(tx), &sorted);
Self::from_tx(tx)
.with_hashed_cursor_factory(hashed_cursor_factory)
.with_prefix_sets_mut(prefix_sets)
.compute(target)
}
}