mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(trie): database trie witness (#9913)
This commit is contained in:
@ -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
crates/trie/db/src/witness.rs
Normal file
46
crates/trie/db/src/witness.rs
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user