diff --git a/Cargo.toml b/Cargo.toml index ba77ba269..b81e5f596 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -449,7 +449,7 @@ serde_with = "3.3.0" humantime = "2.1" humantime-serde = "1.1" rand = "0.8.5" -rustc-hash = "2.0" +rustc-hash = { version = "2.0", default-features = false } schnellru = "0.2" strum = "0.26" rayon = "1.7" diff --git a/crates/ethereum-forks/Cargo.toml b/crates/ethereum-forks/Cargo.toml index 9bdc0c98c..a1d25b571 100644 --- a/crates/ethereum-forks/Cargo.toml +++ b/crates/ethereum-forks/Cargo.toml @@ -25,7 +25,7 @@ serde = { workspace = true, features = ["derive"], optional = true } thiserror-no-std = { workspace = true, default-features = false } once_cell.workspace = true dyn-clone.workspace = true -rustc-hash.workspace = true +rustc-hash = { workspace = true, optional = true } # arbitrary utils arbitrary = { workspace = true, features = ["derive"], optional = true } @@ -39,8 +39,9 @@ proptest.workspace = true proptest-derive.workspace = true [features] -default = ["std", "serde"] +default = ["std", "serde", "rustc-hash"] arbitrary = ["dep:arbitrary", "dep:proptest", "dep:proptest-derive"] optimism = [] serde = ["dep:serde"] -std = ["thiserror-no-std/std"] +std = ["thiserror-no-std/std", "rustc-hash/std"] +rustc-hash = ["dep:rustc-hash"] diff --git a/crates/ethereum-forks/src/display.rs b/crates/ethereum-forks/src/display.rs index d8a2007e4..b0bc06a77 100644 --- a/crates/ethereum-forks/src/display.rs +++ b/crates/ethereum-forks/src/display.rs @@ -1,6 +1,5 @@ #[cfg(not(feature = "std"))] use alloc::{ - collections::BTreeMap, format, string::{String, ToString}, vec::Vec, diff --git a/crates/ethereum-forks/src/hardfork/dev.rs b/crates/ethereum-forks/src/hardfork/dev.rs index 4b422141b..1abc7e75e 100644 --- a/crates/ethereum-forks/src/hardfork/dev.rs +++ b/crates/ethereum-forks/src/hardfork/dev.rs @@ -2,6 +2,9 @@ use crate::{ChainHardforks, EthereumHardfork, ForkCondition}; use alloy_primitives::U256; use once_cell::sync::Lazy; +#[cfg(not(feature = "std"))] +use alloc::vec; + /// Dev hardforks pub static DEV_HARDFORKS: Lazy = Lazy::new(|| { ChainHardforks::new(vec![ diff --git a/crates/ethereum-forks/src/hardfork/ethereum.rs b/crates/ethereum-forks/src/hardfork/ethereum.rs index 7a2618c3c..650e88ad0 100644 --- a/crates/ethereum-forks/src/hardfork/ethereum.rs +++ b/crates/ethereum-forks/src/hardfork/ethereum.rs @@ -9,6 +9,9 @@ use core::{ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, format, string::String}; + hardfork!( /// The name of an Ethereum hardfork. EthereumHardfork { diff --git a/crates/ethereum-forks/src/hardfork/mod.rs b/crates/ethereum-forks/src/hardfork/mod.rs index b6faef6ec..d7bc8c79b 100644 --- a/crates/ethereum-forks/src/hardfork/mod.rs +++ b/crates/ethereum-forks/src/hardfork/mod.rs @@ -15,9 +15,6 @@ use core::{ }; use dyn_clone::DynClone; -#[cfg(not(feature = "std"))] -use alloc::{format, string::String}; - /// Generic hardfork trait. #[auto_impl::auto_impl(&, Box)] pub trait Hardfork: Any + DynClone + Send + Sync + 'static { diff --git a/crates/ethereum-forks/src/hardfork/optimism.rs b/crates/ethereum-forks/src/hardfork/optimism.rs index 6933b7fed..5dc5308ee 100644 --- a/crates/ethereum-forks/src/hardfork/optimism.rs +++ b/crates/ethereum-forks/src/hardfork/optimism.rs @@ -9,6 +9,9 @@ use core::{ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, format, string::String, vec}; + hardfork!( /// The name of an optimism hardfork. /// diff --git a/crates/ethereum-forks/src/hardforks/mod.rs b/crates/ethereum-forks/src/hardforks/mod.rs index 762314f85..b619ab509 100644 --- a/crates/ethereum-forks/src/hardforks/mod.rs +++ b/crates/ethereum-forks/src/hardforks/mod.rs @@ -7,7 +7,13 @@ mod optimism; pub use optimism::OptimismHardforks; use crate::{ForkCondition, Hardfork}; +#[cfg(feature = "std")] use rustc_hash::FxHashMap; +#[cfg(feature = "std")] +use std::collections::hash_map::Entry; + +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, collections::btree_map::Entry, vec::Vec}; /// Generic trait over a set of ordered hardforks pub trait Hardforks: Default + Clone { @@ -33,7 +39,10 @@ pub trait Hardforks: Default + Clone { #[derive(Default, Clone, PartialEq, Eq)] pub struct ChainHardforks { forks: Vec<(Box, ForkCondition)>, + #[cfg(feature = "std")] map: FxHashMap<&'static str, ForkCondition>, + #[cfg(not(feature = "std"))] + map: alloc::collections::BTreeMap<&'static str, ForkCondition>, } impl ChainHardforks { @@ -100,7 +109,7 @@ impl ChainHardforks { /// Inserts `fork` into list, updating with a new [`ForkCondition`] if it already exists. pub fn insert(&mut self, fork: H, condition: ForkCondition) { match self.map.entry(fork.name()) { - std::collections::hash_map::Entry::Occupied(mut entry) => { + Entry::Occupied(mut entry) => { *entry.get_mut() = condition; if let Some((_, inner)) = self.forks.iter_mut().find(|(inner, _)| inner.name() == fork.name()) @@ -108,7 +117,7 @@ impl ChainHardforks { *inner = condition; } } - std::collections::hash_map::Entry::Vacant(entry) => { + Entry::Vacant(entry) => { entry.insert(condition); self.forks.push((Box::new(fork), condition)); }