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

@ -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"]

View File

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

View File

@ -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<ChainHardforks> = Lazy::new(|| {
ChainHardforks::new(vec![

View File

@ -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 {

View File

@ -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 {

View File

@ -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.
///

View File

@ -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<dyn Hardfork>, 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<H: Hardfork>(&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));
}