refactor(trie): move storage root database operations into an extension trait in the reth-db-trie crate (#9721)

This commit is contained in:
Roman Hodulák
2024-07-23 14:58:05 +02:00
committed by GitHub
parent 88c7b2cfa1
commit e7ac0edcf0
4 changed files with 45 additions and 28 deletions

View File

@@ -1,4 +1,7 @@
//! An integration of [`reth-trie`] with [`reth-db`].
mod state;
mod storage;
pub use state::DatabaseStateRoot;
pub use storage::DatabaseStorageRoot;

View 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),
)
}
}

View File

@@ -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,

View File

@@ -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,