diff --git a/crates/trie/common/Cargo.toml b/crates/trie/common/Cargo.toml index eb4ff90c150a..4f3bef0a4177 100644 --- a/crates/trie/common/Cargo.toml +++ b/crates/trie/common/Cargo.toml @@ -42,7 +42,7 @@ plain_hasher = { version = "0.2", optional = true } arbitrary = { workspace = true, features = ["derive"], optional = true } # misc -rayon.workspace = true +rayon = { workspace = true, optional = true } [dev-dependencies] reth-primitives-traits = { workspace = true, features = ["serde"] } diff --git a/crates/trie/common/src/hashedstate.rs b/crates/trie/common/src/hashedstate.rs index e54ab6e25096..4d917bb01034 100644 --- a/crates/trie/common/src/hashedstate.rs +++ b/crates/trie/common/src/hashedstate.rs @@ -1,20 +1,24 @@ -#![cfg_attr(not(feature = "std"), no_std)] - use crate::{ prefix_set::{PrefixSetMut, TriePrefixSetsMut}, - KeyHasher, Nibbles, + Nibbles, }; -use alloc::{borrow::Cow, vec::Vec}; use alloy_primitives::{ keccak256, map::{hash_map, B256HashMap, B256HashSet, HashMap, HashSet}, Address, B256, U256, }; use itertools::Itertools; -use rayon::prelude::{IntoParallelIterator, ParallelIterator}; -use reth_primitives_traits::Account; +use crate::KeyHasher; +use reth_primitives_traits::Account; use revm::db::{AccountStatus, BundleAccount}; +use std::borrow::Cow; + +#[cfg(feature = "rayon")] +pub use rayon::*; + +#[cfg(feature = "rayon")] +use rayon::prelude::{IntoParallelIterator, ParallelIterator}; /// Representation of in-memory hashed state. #[derive(PartialEq, Eq, Clone, Default, Debug)] @@ -30,6 +34,7 @@ impl HashedPostState { /// Hashes all changed accounts and storage entries that are currently stored in the bundle /// state. #[inline] + #[cfg(feature = "rayon")] pub fn from_bundle_state<'a, KH: KeyHasher>( state: impl IntoParallelIterator, ) -> Self { @@ -57,6 +62,37 @@ impl HashedPostState { Self { accounts, storages } } + /// Initialize [`HashedPostState`] from bundle state. + /// Hashes all changed accounts and storage entries that are currently stored in the bundle + /// state. + #[cfg(not(feature = "rayon"))] + pub fn from_bundle_state<'a, KH: KeyHasher>( + state: impl IntoIterator, + ) -> Self { + let hashed = state + .into_iter() + .map(|(address, account)| { + let hashed_address = KH::hash_key(address); + let hashed_account = account.info.as_ref().map(Into::into); + let hashed_storage = HashedStorage::from_plain_storage( + account.status, + account.storage.iter().map(|(slot, value)| (slot, &value.present_value)), + ); + (hashed_address, (hashed_account, hashed_storage)) + }) + .collect::, 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); + if !storage.is_empty() { + storages.insert(address, storage); + } + } + Self { accounts, storages } + } + /// Construct [`HashedPostState`] from a single [`HashedStorage`]. pub fn from_hashed_storage(hashed_address: B256, storage: HashedStorage) -> Self { Self { diff --git a/crates/trie/trie/Cargo.toml b/crates/trie/trie/Cargo.toml index 867474d0f3da..909787b2f2a1 100644 --- a/crates/trie/trie/Cargo.toml +++ b/crates/trie/trie/Cargo.toml @@ -33,7 +33,7 @@ alloy-trie.workspace = true tracing.workspace = true # misc -rayon.workspace = true +rayon = { workspace = true, optional = true } auto_impl.workspace = true itertools.workspace = true