mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore(trie): remove from_cache_state (#14028)
This commit is contained in:
@ -11,6 +11,7 @@ pub trait KeyHasher: Default + Clone + Send + Sync + 'static {
|
|||||||
pub struct KeccakKeyHasher;
|
pub struct KeccakKeyHasher;
|
||||||
|
|
||||||
impl KeyHasher for KeccakKeyHasher {
|
impl KeyHasher for KeccakKeyHasher {
|
||||||
|
#[inline]
|
||||||
fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256 {
|
fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256 {
|
||||||
keccak256(bytes)
|
keccak256(bytes)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,7 @@ use itertools::Itertools;
|
|||||||
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
|
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
|
||||||
use reth_primitives::Account;
|
use reth_primitives::Account;
|
||||||
use reth_trie_common::KeyHasher;
|
use reth_trie_common::KeyHasher;
|
||||||
use revm::db::{states::CacheAccount, AccountStatus, BundleAccount};
|
use revm::db::{AccountStatus, BundleAccount};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// Representation of in-memory hashed state.
|
/// Representation of in-memory hashed state.
|
||||||
@ -27,6 +27,7 @@ impl HashedPostState {
|
|||||||
/// Initialize [`HashedPostState`] from bundle state.
|
/// Initialize [`HashedPostState`] from bundle state.
|
||||||
/// Hashes all changed accounts and storage entries that are currently stored in the bundle
|
/// Hashes all changed accounts and storage entries that are currently stored in the bundle
|
||||||
/// state.
|
/// state.
|
||||||
|
#[inline]
|
||||||
pub fn from_bundle_state<'a, KH: KeyHasher>(
|
pub fn from_bundle_state<'a, KH: KeyHasher>(
|
||||||
state: impl IntoParallelIterator<Item = (&'a Address, &'a BundleAccount)>,
|
state: impl IntoParallelIterator<Item = (&'a Address, &'a BundleAccount)>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -52,33 +53,6 @@ impl HashedPostState {
|
|||||||
Self { accounts, storages }
|
Self { accounts, storages }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize [`HashedPostState`] from cached state.
|
|
||||||
/// Hashes all changed accounts and storage entries that are currently stored in cache.
|
|
||||||
pub fn from_cache_state<'a, KH: KeyHasher>(
|
|
||||||
state: impl IntoParallelIterator<Item = (&'a Address, &'a CacheAccount)>,
|
|
||||||
) -> Self {
|
|
||||||
let hashed = state
|
|
||||||
.into_par_iter()
|
|
||||||
.map(|(address, account)| {
|
|
||||||
let hashed_address = KH::hash_key(address);
|
|
||||||
let hashed_account = account.account.as_ref().map(|a| (&a.info).into());
|
|
||||||
let hashed_storage = HashedStorage::from_plain_storage(
|
|
||||||
account.status,
|
|
||||||
account.account.as_ref().map(|a| a.storage.iter()).into_iter().flatten(),
|
|
||||||
);
|
|
||||||
(hashed_address, (hashed_account, hashed_storage))
|
|
||||||
})
|
|
||||||
.collect::<Vec<(B256, (Option<Account>, HashedStorage))>>();
|
|
||||||
|
|
||||||
let mut accounts = HashMap::with_capacity_and_hasher(hashed.len(), Default::default());
|
|
||||||
let mut storages = HashMap::with_capacity_and_hasher(hashed.len(), Default::default());
|
|
||||||
for (address, (account, storage)) in hashed {
|
|
||||||
accounts.insert(address, account);
|
|
||||||
storages.insert(address, storage);
|
|
||||||
}
|
|
||||||
Self { accounts, storages }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct [`HashedPostState`] from a single [`HashedStorage`].
|
/// Construct [`HashedPostState`] from a single [`HashedStorage`].
|
||||||
pub fn from_hashed_storage(hashed_address: B256, storage: HashedStorage) -> Self {
|
pub fn from_hashed_storage(hashed_address: B256, storage: HashedStorage) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -357,10 +331,7 @@ mod tests {
|
|||||||
use alloy_primitives::Bytes;
|
use alloy_primitives::Bytes;
|
||||||
use reth_trie_common::KeccakKeyHasher;
|
use reth_trie_common::KeccakKeyHasher;
|
||||||
use revm::{
|
use revm::{
|
||||||
db::{
|
db::{states::StorageSlot, StorageWithOriginalValues},
|
||||||
states::{plain_account::PlainStorage, StorageSlot},
|
|
||||||
PlainAccount, StorageWithOriginalValues,
|
|
||||||
},
|
|
||||||
primitives::{AccountInfo, Bytecode},
|
primitives::{AccountInfo, Bytecode},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -482,45 +453,6 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_hashed_post_state_from_cache_state() {
|
|
||||||
// Prepare a random Ethereum address.
|
|
||||||
let address = Address::random();
|
|
||||||
|
|
||||||
// Create mock account info.
|
|
||||||
let account_info = AccountInfo {
|
|
||||||
balance: U256::from(500),
|
|
||||||
nonce: 5,
|
|
||||||
code_hash: B256::random(),
|
|
||||||
code: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut storage = PlainStorage::default();
|
|
||||||
storage.insert(U256::from(1), U256::from(35636));
|
|
||||||
|
|
||||||
// Create a `CacheAccount` with the mock account info.
|
|
||||||
let account = CacheAccount {
|
|
||||||
account: Some(PlainAccount { info: account_info.clone(), storage }),
|
|
||||||
status: AccountStatus::Changed,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create a vector of tuples representing the cache state.
|
|
||||||
let state = vec![(&address, &account)];
|
|
||||||
|
|
||||||
// Convert the cache state into a hashed post state.
|
|
||||||
let hashed_state = HashedPostState::from_cache_state::<KeccakKeyHasher>(state);
|
|
||||||
|
|
||||||
// Validate the hashed post state.
|
|
||||||
assert_eq!(hashed_state.accounts.len(), 1);
|
|
||||||
assert_eq!(hashed_state.storages.len(), 1);
|
|
||||||
|
|
||||||
// Validate the account info.
|
|
||||||
assert_eq!(
|
|
||||||
*hashed_state.accounts.get(&keccak256(address)).unwrap(),
|
|
||||||
Some(account_info.into())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hashed_post_state_with_accounts() {
|
fn test_hashed_post_state_with_accounts() {
|
||||||
// Prepare random addresses and mock account info.
|
// Prepare random addresses and mock account info.
|
||||||
|
|||||||
Reference in New Issue
Block a user