mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore(trie): database trie cursor factory wrapper (#9831)
This commit is contained in:
@ -4,6 +4,7 @@ use reth_primitives::{Address, B256};
|
||||
use reth_trie::{
|
||||
hashed_cursor::{DatabaseHashedCursorFactory, HashedPostStateCursorFactory},
|
||||
proof::Proof,
|
||||
trie_cursor::DatabaseTrieCursorFactory,
|
||||
HashedPostState,
|
||||
};
|
||||
use reth_trie_common::AccountProof;
|
||||
@ -22,10 +23,12 @@ pub trait DatabaseProof<'a, TX> {
|
||||
) -> Result<AccountProof, StateProofError>;
|
||||
}
|
||||
|
||||
impl<'a, TX: DbTx> DatabaseProof<'a, TX> for Proof<&'a TX, DatabaseHashedCursorFactory<'a, TX>> {
|
||||
impl<'a, TX: DbTx> DatabaseProof<'a, TX>
|
||||
for Proof<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>
|
||||
{
|
||||
/// Create a new [Proof] instance from database transaction.
|
||||
fn from_tx(tx: &'a TX) -> Self {
|
||||
Self::new(tx, DatabaseHashedCursorFactory::new(tx))
|
||||
Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
|
||||
}
|
||||
|
||||
fn overlay_account_proof(
|
||||
@ -38,7 +41,7 @@ impl<'a, TX: DbTx> DatabaseProof<'a, TX> for Proof<&'a TX, DatabaseHashedCursorF
|
||||
let sorted = post_state.into_sorted();
|
||||
let hashed_cursor_factory =
|
||||
HashedPostStateCursorFactory::new(DatabaseHashedCursorFactory::new(tx), &sorted);
|
||||
Proof::from_tx(tx)
|
||||
Self::from_tx(tx)
|
||||
.with_hashed_cursor_factory(hashed_cursor_factory)
|
||||
.with_prefix_sets_mut(prefix_sets)
|
||||
.account_proof(address, slots)
|
||||
|
||||
@ -4,6 +4,7 @@ use reth_primitives::{BlockNumber, B256};
|
||||
use reth_trie::{
|
||||
hashed_cursor::{DatabaseHashedCursorFactory, HashedPostStateCursorFactory},
|
||||
prefix_set::PrefixSetLoader,
|
||||
trie_cursor::DatabaseTrieCursorFactory,
|
||||
updates::TrieUpdates,
|
||||
HashedPostState, StateRoot, StateRootProgress,
|
||||
};
|
||||
@ -103,10 +104,10 @@ pub trait DatabaseStateRoot<'a, TX>: Sized {
|
||||
}
|
||||
|
||||
impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
|
||||
for StateRoot<&'a TX, DatabaseHashedCursorFactory<'a, TX>>
|
||||
for StateRoot<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>
|
||||
{
|
||||
fn from_tx(tx: &'a TX) -> Self {
|
||||
Self::new(tx, DatabaseHashedCursorFactory::new(tx))
|
||||
Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
|
||||
}
|
||||
|
||||
fn incremental_root_calculator(
|
||||
@ -145,7 +146,7 @@ impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
|
||||
let prefix_sets = post_state.construct_prefix_sets().freeze();
|
||||
let sorted = post_state.into_sorted();
|
||||
StateRoot::new(
|
||||
tx,
|
||||
DatabaseTrieCursorFactory::new(tx),
|
||||
HashedPostStateCursorFactory::new(DatabaseHashedCursorFactory::new(tx), &sorted),
|
||||
)
|
||||
.with_prefix_sets(prefix_sets)
|
||||
@ -159,7 +160,7 @@ impl<'a, TX: DbTx> DatabaseStateRoot<'a, TX>
|
||||
let prefix_sets = post_state.construct_prefix_sets().freeze();
|
||||
let sorted = post_state.into_sorted();
|
||||
StateRoot::new(
|
||||
tx,
|
||||
DatabaseTrieCursorFactory::new(tx),
|
||||
HashedPostStateCursorFactory::new(DatabaseHashedCursorFactory::new(tx), &sorted),
|
||||
)
|
||||
.with_prefix_sets(prefix_sets)
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
use reth_db_api::transaction::DbTx;
|
||||
use reth_primitives::{Address, B256};
|
||||
use reth_trie::{hashed_cursor::DatabaseHashedCursorFactory, StorageRoot};
|
||||
use reth_trie::{
|
||||
hashed_cursor::DatabaseHashedCursorFactory, trie_cursor::DatabaseTrieCursorFactory, StorageRoot,
|
||||
};
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
use reth_trie::metrics::{TrieRootMetrics, TrieType};
|
||||
@ -15,11 +17,11 @@ pub trait DatabaseStorageRoot<'a, TX> {
|
||||
}
|
||||
|
||||
impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX>
|
||||
for StorageRoot<&'a TX, DatabaseHashedCursorFactory<'a, TX>>
|
||||
for StorageRoot<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>
|
||||
{
|
||||
fn from_tx(tx: &'a TX, address: Address) -> Self {
|
||||
Self::new(
|
||||
tx,
|
||||
DatabaseTrieCursorFactory::new(tx),
|
||||
DatabaseHashedCursorFactory::new(tx),
|
||||
address,
|
||||
#[cfg(feature = "metrics")]
|
||||
@ -29,7 +31,7 @@ impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX>
|
||||
|
||||
fn from_tx_hashed(tx: &'a TX, hashed_address: B256) -> Self {
|
||||
Self::new_hashed(
|
||||
tx,
|
||||
DatabaseTrieCursorFactory::new(tx),
|
||||
DatabaseHashedCursorFactory::new(tx),
|
||||
hashed_address,
|
||||
#[cfg(feature = "metrics")]
|
||||
|
||||
@ -5,7 +5,7 @@ use reth_provider::test_utils::create_test_provider_factory;
|
||||
use reth_trie::{
|
||||
prefix_set::{PrefixSetMut, TriePrefixSets},
|
||||
test_utils::state_root_prehashed,
|
||||
trie_cursor::InMemoryTrieCursorFactory,
|
||||
trie_cursor::{DatabaseTrieCursorFactory, InMemoryTrieCursorFactory},
|
||||
StateRoot,
|
||||
};
|
||||
use reth_trie_common::Nibbles;
|
||||
@ -43,7 +43,11 @@ proptest! {
|
||||
// Compute root with in-memory trie nodes overlay
|
||||
let (state_root, _) = StateRoot::from_tx(provider.tx_ref())
|
||||
.with_prefix_sets(TriePrefixSets { account_prefix_set: changes.freeze(), ..Default::default() })
|
||||
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(provider.tx_ref(), &trie_updates.into_sorted()))
|
||||
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(
|
||||
|
||||
DatabaseTrieCursorFactory::new(provider.tx_ref()),
|
||||
&trie_updates.into_sorted())
|
||||
)
|
||||
.root_with_updates()
|
||||
.unwrap();
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ use reth_trie::{
|
||||
DatabaseHashedCursorFactory, HashedCursorFactory, HashedPostStateCursorFactory,
|
||||
},
|
||||
node_iter::{TrieElement, TrieNodeIter},
|
||||
trie_cursor::TrieCursorFactory,
|
||||
trie_cursor::{DatabaseTrieCursorFactory, TrieCursorFactory},
|
||||
updates::TrieUpdates,
|
||||
walker::TrieWalker,
|
||||
HashBuilder, HashedPostState, Nibbles, StorageRoot, TrieAccount,
|
||||
@ -109,12 +109,13 @@ where
|
||||
let handle =
|
||||
self.blocking_pool.spawn_fifo(move || -> Result<_, AsyncStateRootError> {
|
||||
let provider = view.provider_ro()?;
|
||||
let trie_cursor_factory = DatabaseTrieCursorFactory::new(provider.tx_ref());
|
||||
let hashed_state = HashedPostStateCursorFactory::new(
|
||||
DatabaseHashedCursorFactory::new(provider.tx_ref()),
|
||||
&hashed_state_sorted,
|
||||
);
|
||||
Ok(StorageRoot::new_hashed(
|
||||
provider.tx_ref(),
|
||||
trie_cursor_factory,
|
||||
hashed_state,
|
||||
hashed_address,
|
||||
#[cfg(feature = "metrics")]
|
||||
@ -131,11 +132,11 @@ where
|
||||
|
||||
let provider_ro = self.view.provider_ro()?;
|
||||
let tx = provider_ro.tx_ref();
|
||||
let trie_cursor_factory = DatabaseTrieCursorFactory::new(tx);
|
||||
let hashed_cursor_factory = HashedPostStateCursorFactory::new(
|
||||
DatabaseHashedCursorFactory::new(tx),
|
||||
&hashed_state_sorted,
|
||||
);
|
||||
let trie_cursor_factory = tx;
|
||||
|
||||
let walker = TrieWalker::new(
|
||||
trie_cursor_factory.account_trie_cursor().map_err(ProviderError::Database)?,
|
||||
@ -164,7 +165,7 @@ where
|
||||
None => {
|
||||
tracker.inc_missed_leaves();
|
||||
StorageRoot::new_hashed(
|
||||
trie_cursor_factory,
|
||||
trie_cursor_factory.clone(),
|
||||
hashed_cursor_factory.clone(),
|
||||
hashed_address,
|
||||
#[cfg(feature = "metrics")]
|
||||
|
||||
@ -10,7 +10,7 @@ use reth_trie::{
|
||||
DatabaseHashedCursorFactory, HashedCursorFactory, HashedPostStateCursorFactory,
|
||||
},
|
||||
node_iter::{TrieElement, TrieNodeIter},
|
||||
trie_cursor::TrieCursorFactory,
|
||||
trie_cursor::{DatabaseTrieCursorFactory, TrieCursorFactory},
|
||||
updates::TrieUpdates,
|
||||
walker::TrieWalker,
|
||||
HashBuilder, HashedPostState, Nibbles, StorageRoot, TrieAccount,
|
||||
@ -93,12 +93,13 @@ where
|
||||
.into_par_iter()
|
||||
.map(|(hashed_address, prefix_set)| {
|
||||
let provider_ro = self.view.provider_ro()?;
|
||||
let trie_cursor_factory = DatabaseTrieCursorFactory::new(provider_ro.tx_ref());
|
||||
let hashed_cursor_factory = HashedPostStateCursorFactory::new(
|
||||
DatabaseHashedCursorFactory::new(provider_ro.tx_ref()),
|
||||
&hashed_state_sorted,
|
||||
);
|
||||
let storage_root_result = StorageRoot::new_hashed(
|
||||
provider_ro.tx_ref(),
|
||||
trie_cursor_factory,
|
||||
hashed_cursor_factory,
|
||||
hashed_address,
|
||||
#[cfg(feature = "metrics")]
|
||||
@ -118,7 +119,7 @@ where
|
||||
DatabaseHashedCursorFactory::new(provider_ro.tx_ref()),
|
||||
&hashed_state_sorted,
|
||||
);
|
||||
let trie_cursor_factory = provider_ro.tx_ref();
|
||||
let trie_cursor_factory = DatabaseTrieCursorFactory::new(provider_ro.tx_ref());
|
||||
|
||||
let walker = TrieWalker::new(
|
||||
trie_cursor_factory.account_trie_cursor().map_err(ProviderError::Database)?,
|
||||
@ -145,7 +146,7 @@ where
|
||||
None => {
|
||||
tracker.inc_missed_leaves();
|
||||
StorageRoot::new_hashed(
|
||||
trie_cursor_factory,
|
||||
trie_cursor_factory.clone(),
|
||||
hashed_cursor_factory.clone(),
|
||||
hashed_address,
|
||||
#[cfg(feature = "metrics")]
|
||||
|
||||
@ -13,14 +13,31 @@ use reth_db_api::{
|
||||
use reth_primitives::B256;
|
||||
use reth_trie_common::StorageTrieEntry;
|
||||
|
||||
/// Wrapper struct for database transaction implementing trie cursor factory trait.
|
||||
#[derive(Debug)]
|
||||
pub struct DatabaseTrieCursorFactory<'a, TX>(&'a TX);
|
||||
|
||||
impl<'a, TX> Clone for DatabaseTrieCursorFactory<'a, TX> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, TX> DatabaseTrieCursorFactory<'a, TX> {
|
||||
/// Create new [`DatabaseTrieCursorFactory`].
|
||||
pub const fn new(tx: &'a TX) -> Self {
|
||||
Self(tx)
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of the trie cursor factory for a database transaction.
|
||||
impl<'a, TX: DbTx> TrieCursorFactory for &'a TX {
|
||||
impl<'a, TX: DbTx> TrieCursorFactory for DatabaseTrieCursorFactory<'a, TX> {
|
||||
type AccountTrieCursor = DatabaseAccountTrieCursor<<TX as DbTx>::Cursor<tables::AccountsTrie>>;
|
||||
type StorageTrieCursor =
|
||||
DatabaseStorageTrieCursor<<TX as DbTx>::DupCursor<tables::StoragesTrie>>;
|
||||
|
||||
fn account_trie_cursor(&self) -> Result<Self::AccountTrieCursor, DatabaseError> {
|
||||
Ok(DatabaseAccountTrieCursor::new(self.cursor_read::<tables::AccountsTrie>()?))
|
||||
Ok(DatabaseAccountTrieCursor::new(self.0.cursor_read::<tables::AccountsTrie>()?))
|
||||
}
|
||||
|
||||
fn storage_trie_cursor(
|
||||
@ -28,7 +45,7 @@ impl<'a, TX: DbTx> TrieCursorFactory for &'a TX {
|
||||
hashed_address: B256,
|
||||
) -> Result<Self::StorageTrieCursor, DatabaseError> {
|
||||
Ok(DatabaseStorageTrieCursor::new(
|
||||
self.cursor_dup_read::<tables::StoragesTrie>()?,
|
||||
self.0.cursor_dup_read::<tables::StoragesTrie>()?,
|
||||
hashed_address,
|
||||
))
|
||||
}
|
||||
|
||||
@ -14,11 +14,7 @@ mod subnode;
|
||||
/// Noop trie cursor implementations.
|
||||
pub mod noop;
|
||||
|
||||
pub use self::{
|
||||
database_cursors::{DatabaseAccountTrieCursor, DatabaseStorageTrieCursor},
|
||||
in_memory::*,
|
||||
subnode::CursorSubNode,
|
||||
};
|
||||
pub use self::{database_cursors::*, in_memory::*, subnode::CursorSubNode};
|
||||
|
||||
/// Factory for creating trie cursors.
|
||||
pub trait TrieCursorFactory {
|
||||
|
||||
Reference in New Issue
Block a user