Skip to content

Commit

Permalink
fixed the rayon dependency issue
Browse files Browse the repository at this point in the history
  • Loading branch information
PoulavBhowmick03 committed Feb 5, 2025
1 parent c9fbf6f commit 82c586a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/trie/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
48 changes: 42 additions & 6 deletions crates/trie/common/src/hashedstate.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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<Item = (&'a Address, &'a BundleAccount)>,
) -> Self {
Expand Down Expand Up @@ -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<Item = (&'a Address, &'a BundleAccount)>,
) -> 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::<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);
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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 82c586a

Please sign in to comment.