mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-02-19 03:04:27 -05:00
chore(trie): remove unused direct MDBX changeset readers (#21580)
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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<KH>);
|
||||
|
||||
impl<'a, TX, KH> PrefixSetLoader<'a, TX, KH> {
|
||||
/// Create a new loader.
|
||||
pub const fn new(tx: &'a TX) -> Self {
|
||||
Self(tx, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<TX, KH> Deref for PrefixSetLoader<'_, TX, KH> {
|
||||
type Target = TX;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<TX: DbTx, KH: KeyHasher> PrefixSetLoader<'_, TX, KH> {
|
||||
/// Load all account and storage changes for the given block range.
|
||||
pub fn load(self, range: RangeInclusive<BlockNumber>) -> Result<TriePrefixSets, DatabaseError> {
|
||||
// Initialize prefix sets.
|
||||
let mut account_prefix_set = PrefixSetMut::default();
|
||||
let mut storage_prefix_sets = HashMap::<B256, PrefixSetMut>::default();
|
||||
let mut destroyed_accounts = HashSet::default();
|
||||
|
||||
// Walk account changeset and insert account prefixes.
|
||||
let mut account_changeset_cursor = self.cursor_read::<tables::AccountChangeSets>()?;
|
||||
let mut account_hashed_state_cursor = self.cursor_read::<tables::HashedAccounts>()?;
|
||||
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::<tables::StorageChangeSets>()?;
|
||||
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<Provider, KH>(
|
||||
|
||||
@@ -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<B256, StorageRootError>;
|
||||
}
|
||||
|
||||
/// Extends [`HashedStorage`] with operations specific for working with a database transaction.
|
||||
pub trait DatabaseHashedStorage<TX>: 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<Self, DatabaseError>;
|
||||
}
|
||||
|
||||
/// Initializes [`HashedStorage`] from reverts using a provider.
|
||||
pub fn hashed_storage_from_reverts_with_provider<P>(
|
||||
provider: &P,
|
||||
@@ -110,20 +101,3 @@ impl<'a, TX: DbTx> DatabaseStorageRoot<'a, TX>
|
||||
.root()
|
||||
}
|
||||
}
|
||||
|
||||
impl<TX: DbTx> DatabaseHashedStorage<TX> for HashedStorage {
|
||||
fn from_reverts(tx: &TX, address: Address, from: BlockNumber) -> Result<Self, DatabaseError> {
|
||||
let mut storage = Self::new(false);
|
||||
let mut storage_changesets_cursor = tx.cursor_read::<tables::StorageChangeSets>()?;
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user