diff --git a/crates/trie/db/src/lib.rs b/crates/trie/db/src/lib.rs index ce49539fd4..16efa9fdb9 100644 --- a/crates/trie/db/src/lib.rs +++ b/crates/trie/db/src/lib.rs @@ -15,12 +15,10 @@ mod witness; pub use hashed_cursor::{ DatabaseHashedAccountCursor, DatabaseHashedCursorFactory, DatabaseHashedStorageCursor, }; -pub use prefix_set::{load_prefix_sets_with_provider, PrefixSetLoader}; +pub use prefix_set::load_prefix_sets_with_provider; pub use proof::{DatabaseProof, DatabaseStorageProof}; pub use state::{DatabaseHashedPostState, DatabaseStateRoot}; -pub use storage::{ - hashed_storage_from_reverts_with_provider, DatabaseHashedStorage, DatabaseStorageRoot, -}; +pub use storage::{hashed_storage_from_reverts_with_provider, DatabaseStorageRoot}; pub use trie_cursor::{ DatabaseAccountTrieCursor, DatabaseStorageTrieCursor, DatabaseTrieCursorFactory, }; diff --git a/crates/trie/db/src/prefix_set.rs b/crates/trie/db/src/prefix_set.rs index eec1bad362..b0368477f4 100644 --- a/crates/trie/db/src/prefix_set.rs +++ b/crates/trie/db/src/prefix_set.rs @@ -2,16 +2,12 @@ use alloy_primitives::{ map::{HashMap, HashSet}, BlockNumber, B256, }; -use core::{ - marker::PhantomData, - ops::{Deref, RangeInclusive}, -}; +use core::ops::RangeInclusive; use reth_db_api::{ cursor::DbCursorRO, models::{AccountBeforeTx, BlockNumberAddress}, tables, transaction::DbTx, - DatabaseError, }; use reth_primitives_traits::StorageEntry; use reth_storage_api::{ChangeSetReader, DBProvider, StorageChangeSetReader}; @@ -21,71 +17,6 @@ use reth_trie::{ KeyHasher, Nibbles, }; -/// A wrapper around a database transaction that loads prefix sets within a given block range. -#[derive(Debug)] -pub struct PrefixSetLoader<'a, TX, KH>(&'a TX, PhantomData); - -impl<'a, TX, KH> PrefixSetLoader<'a, TX, KH> { - /// Create a new loader. - pub const fn new(tx: &'a TX) -> Self { - Self(tx, PhantomData) - } -} - -impl Deref for PrefixSetLoader<'_, TX, KH> { - type Target = TX; - - fn deref(&self) -> &Self::Target { - self.0 - } -} - -impl PrefixSetLoader<'_, TX, KH> { - /// Load all account and storage changes for the given block range. - pub fn load(self, range: RangeInclusive) -> Result { - // Initialize prefix sets. - let mut account_prefix_set = PrefixSetMut::default(); - let mut storage_prefix_sets = HashMap::::default(); - let mut destroyed_accounts = HashSet::default(); - - // Walk account changeset and insert account prefixes. - let mut account_changeset_cursor = self.cursor_read::()?; - let mut account_hashed_state_cursor = self.cursor_read::()?; - for account_entry in account_changeset_cursor.walk_range(range.clone())? { - let (_, AccountBeforeTx { address, .. }) = account_entry?; - let hashed_address = KH::hash_key(address); - account_prefix_set.insert(Nibbles::unpack(hashed_address)); - - if account_hashed_state_cursor.seek_exact(hashed_address)?.is_none() { - destroyed_accounts.insert(hashed_address); - } - } - - // Walk storage changeset and insert storage prefixes as well as account prefixes if missing - // from the account prefix set. - let mut storage_cursor = self.cursor_dup_read::()?; - let storage_range = BlockNumberAddress::range(range); - for storage_entry in storage_cursor.walk_range(storage_range)? { - let (BlockNumberAddress((_, address)), StorageEntry { key, .. }) = storage_entry?; - let hashed_address = KH::hash_key(address); - account_prefix_set.insert(Nibbles::unpack(hashed_address)); - storage_prefix_sets - .entry(hashed_address) - .or_default() - .insert(Nibbles::unpack(KH::hash_key(key))); - } - - Ok(TriePrefixSets { - account_prefix_set: account_prefix_set.freeze(), - storage_prefix_sets: storage_prefix_sets - .into_iter() - .map(|(k, v)| (k, v.freeze())) - .collect(), - destroyed_accounts, - }) - } -} - /// Load prefix sets using a provider that implements [`ChangeSetReader`]. This function can read /// changesets from both static files and database. pub fn load_prefix_sets_with_provider( diff --git a/crates/trie/db/src/storage.rs b/crates/trie/db/src/storage.rs index b4614eb15b..cbaad8d031 100644 --- a/crates/trie/db/src/storage.rs +++ b/crates/trie/db/src/storage.rs @@ -1,8 +1,6 @@ use crate::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory}; use alloy_primitives::{keccak256, map::hash_map, Address, BlockNumber, B256}; -use reth_db_api::{ - cursor::DbCursorRO, models::BlockNumberAddress, tables, transaction::DbTx, DatabaseError, -}; +use reth_db_api::{models::BlockNumberAddress, transaction::DbTx}; use reth_execution_errors::StorageRootError; use reth_storage_api::{BlockNumReader, StorageChangeSetReader}; use reth_storage_errors::provider::ProviderResult; @@ -29,13 +27,6 @@ pub trait DatabaseStorageRoot<'a, TX> { ) -> Result; } -/// Extends [`HashedStorage`] with operations specific for working with a database transaction. -pub trait DatabaseHashedStorage: Sized { - /// Initializes [`HashedStorage`] from reverts. Iterates over storage reverts from the specified - /// block up to the current tip and aggregates them into hashed storage in reverse. - fn from_reverts(tx: &TX, address: Address, from: BlockNumber) -> Result; -} - /// Initializes [`HashedStorage`] from reverts using a provider. pub fn hashed_storage_from_reverts_with_provider

( provider: &P, @@ -110,20 +101,3 @@ impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX> .root() } } - -impl DatabaseHashedStorage for HashedStorage { - fn from_reverts(tx: &TX, address: Address, from: BlockNumber) -> Result { - let mut storage = Self::new(false); - let mut storage_changesets_cursor = tx.cursor_read::()?; - for entry in storage_changesets_cursor.walk_range(BlockNumberAddress((from, address))..)? { - let (BlockNumberAddress((_, storage_address)), storage_change) = entry?; - if storage_address == address { - let hashed_slot = keccak256(storage_change.key); - if let hash_map::Entry::Vacant(entry) = storage.storage.entry(hashed_slot) { - entry.insert(storage_change.value); - } - } - } - Ok(storage) - } -}