Skip to content

Commit

Permalink
feat: support no_std for ethereum-forks (paradigmxyz#8877)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
kamuik16 and mattsse authored Jun 17, 2024
1 parent 43a49ed commit d131540
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/ethereum-forks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ crc = "3"

# misc
serde = { workspace = true, features = ["derive"], optional = true }
thiserror.workspace = true
thiserror-no-std = { workspace = true, default-features = false }

# arbitrary utils
arbitrary = { workspace = true, features = ["derive"], optional = true }
Expand All @@ -35,7 +35,8 @@ proptest.workspace = true
proptest-derive.workspace = true

[features]
default = ["serde"]
default = ["std", "serde"]
std = ["thiserror-no-std/std"]
serde = ["dep:serde"]
arbitrary = ["dep:arbitrary", "dep:proptest", "dep:proptest-derive"]
optimism = []
21 changes: 13 additions & 8 deletions crates/ethereum-forks/src/forkid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
//! Previously version of Apache licenced [`ethereum-forkid`](https://crates.io/crates/ethereum-forkid).
use crate::Head;
#[cfg(not(feature = "std"))]
use alloc::{
collections::{BTreeMap, BTreeSet},
vec::Vec,
};
use alloy_primitives::{hex, BlockNumber, B256};
use alloy_rlp::{Error as RlpError, *};
#[cfg(any(test, feature = "arbitrary"))]
use arbitrary::Arbitrary;
use core::{
cmp::Ordering,
fmt,
ops::{Add, AddAssign},
};
use crc::*;
#[cfg(any(test, feature = "arbitrary"))]
use proptest_derive::Arbitrary as PropTestArbitrary;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
cmp::Ordering,
collections::{BTreeMap, BTreeSet},
fmt,
ops::{Add, AddAssign},
};
use thiserror::Error;
#[cfg(feature = "std")]
use std::collections::{BTreeMap, BTreeSet};

const CRC_32_IEEE: Crc<u32> = Crc::<u32>::new(&CRC_32_ISO_HDLC);
const TIMESTAMP_BEFORE_ETHEREUM_MAINNET: u64 = 1_300_000_000;
Expand Down Expand Up @@ -174,7 +179,7 @@ impl From<EnrForkIdEntry> for ForkId {
}

/// Reason for rejecting provided `ForkId`.
#[derive(Clone, Copy, Debug, Error, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, thiserror_no_std::Error, PartialEq, Eq, Hash)]
pub enum ValidationError {
/// Remote node is outdated and needs a software update.
#[error(
Expand Down
11 changes: 9 additions & 2 deletions crates/ethereum-forks/src/hardfork.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use alloy_chains::Chain;
use core::{
fmt,
fmt::{Display, Formatter},
str::FromStr,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};

#[cfg(not(feature = "std"))]
use alloc::{format, string::String};

/// Represents the consensus type of a blockchain fork.
///
Expand Down Expand Up @@ -562,7 +569,7 @@ impl FromStr for Hardfork {
}

impl Display for Hardfork {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum-forks/src/head.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloy_primitives::{BlockNumber, B256, U256};
use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt;

/// Describes the current head block.
///
Expand Down
4 changes: 4 additions & 0 deletions crates/ethereum-forks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
// TODO: remove when https://github.com/proptest-rs/proptest/pull/427 is merged
#![allow(unknown_lints, non_local_definitions)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

mod forkid;
mod hardfork;
Expand Down

0 comments on commit d131540

Please sign in to comment.