no_std in reth-ethereum-forks (#9589)

This commit is contained in:
nk_ysg
2024-07-18 02:18:27 +08:00
committed by GitHub
parent 5c4ce3bc6a
commit 0376be622d
8 changed files with 25 additions and 10 deletions

View File

@ -449,7 +449,7 @@ serde_with = "3.3.0"
humantime = "2.1" humantime = "2.1"
humantime-serde = "1.1" humantime-serde = "1.1"
rand = "0.8.5" rand = "0.8.5"
rustc-hash = "2.0" rustc-hash = { version = "2.0", default-features = false }
schnellru = "0.2" schnellru = "0.2"
strum = "0.26" strum = "0.26"
rayon = "1.7" rayon = "1.7"

View File

@ -25,7 +25,7 @@ serde = { workspace = true, features = ["derive"], optional = true }
thiserror-no-std = { workspace = true, default-features = false } thiserror-no-std = { workspace = true, default-features = false }
once_cell.workspace = true once_cell.workspace = true
dyn-clone.workspace = true dyn-clone.workspace = true
rustc-hash.workspace = true rustc-hash = { workspace = true, optional = true }
# arbitrary utils # arbitrary utils
arbitrary = { workspace = true, features = ["derive"], optional = true } arbitrary = { workspace = true, features = ["derive"], optional = true }
@ -39,8 +39,9 @@ proptest.workspace = true
proptest-derive.workspace = true proptest-derive.workspace = true
[features] [features]
default = ["std", "serde"] default = ["std", "serde", "rustc-hash"]
arbitrary = ["dep:arbitrary", "dep:proptest", "dep:proptest-derive"] arbitrary = ["dep:arbitrary", "dep:proptest", "dep:proptest-derive"]
optimism = [] optimism = []
serde = ["dep:serde"] serde = ["dep:serde"]
std = ["thiserror-no-std/std"] std = ["thiserror-no-std/std", "rustc-hash/std"]
rustc-hash = ["dep:rustc-hash"]

View File

@ -1,6 +1,5 @@
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
use alloc::{ use alloc::{
collections::BTreeMap,
format, format,
string::{String, ToString}, string::{String, ToString},
vec::Vec, vec::Vec,

View File

@ -2,6 +2,9 @@ use crate::{ChainHardforks, EthereumHardfork, ForkCondition};
use alloy_primitives::U256; use alloy_primitives::U256;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
#[cfg(not(feature = "std"))]
use alloc::vec;
/// Dev hardforks /// Dev hardforks
pub static DEV_HARDFORKS: Lazy<ChainHardforks> = Lazy::new(|| { pub static DEV_HARDFORKS: Lazy<ChainHardforks> = Lazy::new(|| {
ChainHardforks::new(vec![ ChainHardforks::new(vec![

View File

@ -9,6 +9,9 @@ use core::{
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::String};
hardfork!( hardfork!(
/// The name of an Ethereum hardfork. /// The name of an Ethereum hardfork.
EthereumHardfork { EthereumHardfork {

View File

@ -15,9 +15,6 @@ use core::{
}; };
use dyn_clone::DynClone; use dyn_clone::DynClone;
#[cfg(not(feature = "std"))]
use alloc::{format, string::String};
/// Generic hardfork trait. /// Generic hardfork trait.
#[auto_impl::auto_impl(&, Box)] #[auto_impl::auto_impl(&, Box)]
pub trait Hardfork: Any + DynClone + Send + Sync + 'static { pub trait Hardfork: Any + DynClone + Send + Sync + 'static {

View File

@ -9,6 +9,9 @@ use core::{
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::String, vec};
hardfork!( hardfork!(
/// The name of an optimism hardfork. /// The name of an optimism hardfork.
/// ///

View File

@ -7,7 +7,13 @@ mod optimism;
pub use optimism::OptimismHardforks; pub use optimism::OptimismHardforks;
use crate::{ForkCondition, Hardfork}; use crate::{ForkCondition, Hardfork};
#[cfg(feature = "std")]
use rustc_hash::FxHashMap; 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 /// Generic trait over a set of ordered hardforks
pub trait Hardforks: Default + Clone { pub trait Hardforks: Default + Clone {
@ -33,7 +39,10 @@ pub trait Hardforks: Default + Clone {
#[derive(Default, Clone, PartialEq, Eq)] #[derive(Default, Clone, PartialEq, Eq)]
pub struct ChainHardforks { pub struct ChainHardforks {
forks: Vec<(Box<dyn Hardfork>, ForkCondition)>, forks: Vec<(Box<dyn Hardfork>, ForkCondition)>,
#[cfg(feature = "std")]
map: FxHashMap<&'static str, ForkCondition>, map: FxHashMap<&'static str, ForkCondition>,
#[cfg(not(feature = "std"))]
map: alloc::collections::BTreeMap<&'static str, ForkCondition>,
} }
impl ChainHardforks { impl ChainHardforks {
@ -100,7 +109,7 @@ impl ChainHardforks {
/// Inserts `fork` into list, updating with a new [`ForkCondition`] if it already exists. /// Inserts `fork` into list, updating with a new [`ForkCondition`] if it already exists.
pub fn insert<H: Hardfork>(&mut self, fork: H, condition: ForkCondition) { pub fn insert<H: Hardfork>(&mut self, fork: H, condition: ForkCondition) {
match self.map.entry(fork.name()) { match self.map.entry(fork.name()) {
std::collections::hash_map::Entry::Occupied(mut entry) => { Entry::Occupied(mut entry) => {
*entry.get_mut() = condition; *entry.get_mut() = condition;
if let Some((_, inner)) = if let Some((_, inner)) =
self.forks.iter_mut().find(|(inner, _)| inner.name() == fork.name()) self.forks.iter_mut().find(|(inner, _)| inner.name() == fork.name())
@ -108,7 +117,7 @@ impl ChainHardforks {
*inner = condition; *inner = condition;
} }
} }
std::collections::hash_map::Entry::Vacant(entry) => { Entry::Vacant(entry) => {
entry.insert(condition); entry.insert(condition);
self.forks.push((Box::new(fork), condition)); self.forks.push((Box::new(fork), condition));
} }