mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-27 16:18:08 -05:00
refactor(trie): move storage root database operations into an extension trait in the reth-db-trie crate (#9721)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
//! An integration of [`reth-trie`] with [`reth-db`].
|
||||
|
||||
mod state;
|
||||
mod storage;
|
||||
|
||||
pub use state::DatabaseStateRoot;
|
||||
pub use storage::DatabaseStorageRoot;
|
||||
|
||||
39
crates/trie/db/src/storage.rs
Normal file
39
crates/trie/db/src/storage.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use reth_db_api::transaction::DbTx;
|
||||
use reth_primitives::{Address, B256};
|
||||
use reth_trie::{hashed_cursor::DatabaseHashedCursorFactory, StorageRoot};
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
use reth_trie::metrics::{TrieRootMetrics, TrieType};
|
||||
|
||||
/// Extends [`StorageRoot`] with operations specific for working with a database transaction.
|
||||
pub trait DatabaseStorageRoot<'a, TX> {
|
||||
/// Create a new storage root calculator from database transaction and raw address.
|
||||
fn from_tx(tx: &'a TX, address: Address) -> Self;
|
||||
|
||||
/// Create a new storage root calculator from database transaction and hashed address.
|
||||
fn from_tx_hashed(tx: &'a TX, hashed_address: B256) -> Self;
|
||||
}
|
||||
|
||||
impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX>
|
||||
for StorageRoot<&'a TX, DatabaseHashedCursorFactory<'a, TX>>
|
||||
{
|
||||
fn from_tx(tx: &'a TX, address: Address) -> Self {
|
||||
Self::new(
|
||||
tx,
|
||||
DatabaseHashedCursorFactory::new(tx),
|
||||
address,
|
||||
#[cfg(feature = "metrics")]
|
||||
TrieRootMetrics::new(TrieType::Storage),
|
||||
)
|
||||
}
|
||||
|
||||
fn from_tx_hashed(tx: &'a TX, hashed_address: B256) -> Self {
|
||||
Self::new_hashed(
|
||||
tx,
|
||||
DatabaseHashedCursorFactory::new(tx),
|
||||
hashed_address,
|
||||
#[cfg(feature = "metrics")]
|
||||
TrieRootMetrics::new(TrieType::Storage),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ use reth_trie::{
|
||||
BranchNodeCompact, StateRoot, StorageRoot, TrieMask,
|
||||
};
|
||||
use reth_trie_common::triehash::KeccakHasher;
|
||||
use reth_trie_db::DatabaseStateRoot;
|
||||
use reth_trie_db::{DatabaseStateRoot, DatabaseStorageRoot};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
ops::Mul,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
hashed_cursor::{DatabaseHashedCursorFactory, HashedCursorFactory, HashedStorageCursor},
|
||||
hashed_cursor::{HashedCursorFactory, HashedStorageCursor},
|
||||
node_iter::{TrieElement, TrieNodeIter},
|
||||
prefix_set::{PrefixSet, TriePrefixSets},
|
||||
progress::{IntermediateStateRootState, StateRootProgress},
|
||||
@@ -10,13 +10,12 @@ use crate::{
|
||||
HashBuilder, Nibbles, TrieAccount,
|
||||
};
|
||||
use alloy_rlp::{BufMut, Encodable};
|
||||
use reth_db_api::transaction::DbTx;
|
||||
use reth_execution_errors::{StateRootError, StorageRootError};
|
||||
use reth_primitives::{constants::EMPTY_ROOT_HASH, keccak256, Address, B256};
|
||||
use tracing::trace;
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
use crate::metrics::{StateRootMetrics, TrieRootMetrics, TrieType};
|
||||
use crate::metrics::{StateRootMetrics, TrieRootMetrics};
|
||||
|
||||
/// `StateRoot` is used to compute the root node of a state trie.
|
||||
#[derive(Debug)]
|
||||
@@ -363,30 +362,6 @@ impl<T, H> StorageRoot<T, H> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, TX: DbTx> StorageRoot<&'a TX, DatabaseHashedCursorFactory<'a, TX>> {
|
||||
/// Create a new storage root calculator from database transaction and raw address.
|
||||
pub fn from_tx(tx: &'a TX, address: Address) -> Self {
|
||||
Self::new(
|
||||
tx,
|
||||
DatabaseHashedCursorFactory::new(tx),
|
||||
address,
|
||||
#[cfg(feature = "metrics")]
|
||||
TrieRootMetrics::new(TrieType::Storage),
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a new storage root calculator from database transaction and hashed address.
|
||||
pub fn from_tx_hashed(tx: &'a TX, hashed_address: B256) -> Self {
|
||||
Self::new_hashed(
|
||||
tx,
|
||||
DatabaseHashedCursorFactory::new(tx),
|
||||
hashed_address,
|
||||
#[cfg(feature = "metrics")]
|
||||
TrieRootMetrics::new(TrieType::Storage),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, H> StorageRoot<T, H>
|
||||
where
|
||||
T: TrieCursorFactory,
|
||||
|
||||
Reference in New Issue
Block a user