diff --git a/Cargo.toml b/Cargo.toml index 2542186c7b1..a3e6f2e2356 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,10 +88,11 @@ rand = "0.8" reqwest = { version = "0.11", default-features = false } semver = "1.0" thiserror = "1.0" +thiserror-no-std = "2.0.2" url = "2.5" ## serde -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1.0", features = ["derive"], default-features = false } serde_json = "1.0" ## misc-testing diff --git a/crates/eips/Cargo.toml b/crates/eips/Cargo.toml index 0466594e2c5..c808c69a592 100644 --- a/crates/eips/Cargo.toml +++ b/crates/eips/Cargo.toml @@ -12,13 +12,11 @@ repository.workspace = true exclude.workspace = true [dependencies] -alloy-primitives = { workspace = true, features = ["rlp"] } +alloy-primitives = { workspace = true, features = ["rlp"], default-features = false } alloy-rlp = { workspace = true, features = ["derive"] } -thiserror.workspace = true - # serde -serde = { workspace = true, optional = true } +serde = { workspace = true, default-features = false, optional = true } # arbitrary arbitrary = { workspace = true, features = ["derive"], optional = true } @@ -31,5 +29,12 @@ proptest = { workspace = true } proptest-derive = { workspace = true } [features] +default = ["std"] +std = [ + "alloy-primitives/std", + "alloy-rlp/std", + + "serde/std", +] serde = ["dep:serde", "alloy-primitives/serde"] -arbitrary = ["dep:arbitrary", "dep:proptest-derive", "dep:proptest", "alloy-primitives/arbitrary"] +arbitrary = ["std", "dep:arbitrary", "dep:proptest-derive", "dep:proptest", "alloy-primitives/arbitrary"] diff --git a/crates/eips/src/eip2718.rs b/crates/eips/src/eip2718.rs index 79b26002950..e6206ff38b7 100644 --- a/crates/eips/src/eip2718.rs +++ b/crates/eips/src/eip2718.rs @@ -2,8 +2,15 @@ //! //! [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 +#[cfg(not(feature = "std"))] +use crate::alloc::{vec, vec::Vec}; + use alloy_primitives::{keccak256, Sealed, B256}; use alloy_rlp::{BufMut, Header, EMPTY_STRING_CODE}; +use core::{ + fmt, + fmt::{Display, Formatter}, +}; // https://eips.ethereum.org/EIPS/eip-2718#transactiontype-only-goes-up-to-0x7f const TX_TYPE_BYTE_MAX: u8 = 0x7f; @@ -11,19 +18,32 @@ const TX_TYPE_BYTE_MAX: u8 = 0x7f; /// [EIP-2718] decoding errors. /// /// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 -#[derive(thiserror::Error, Debug)] +#[derive(Debug, Copy, Clone)] pub enum Eip2718Error { /// Rlp error from [`alloy_rlp`]. - #[error(transparent)] - RlpError(#[from] alloy_rlp::Error), + RlpError(alloy_rlp::Error), /// Got an unexpected type flag while decoding. - #[error("Unexpected type flag. Got {0}.")] UnexpectedType(u8), - /// Some other error occurred. - #[error(transparent)] - Custom(#[from] Box), } +impl Display for Eip2718Error { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::RlpError(err) => write!(f, "{err}"), + Self::UnexpectedType(t) => write!(f, "Unexpected type flag. Got {t}."), + } + } +} + +impl From for Eip2718Error { + fn from(err: alloy_rlp::Error) -> Self { + Self::RlpError(err) + } +} + +#[cfg(feature = "std")] +impl std::error::Error for Eip2718Error {} + /// Decoding trait for [EIP-2718] envelopes. These envelopes wrap a transaction /// or a receipt with a type flag. /// diff --git a/crates/eips/src/eip2930.rs b/crates/eips/src/eip2930.rs index 4ca18c51f8b..3f9a82fc8d7 100644 --- a/crates/eips/src/eip2930.rs +++ b/crates/eips/src/eip2930.rs @@ -4,15 +4,18 @@ #![allow(unknown_lints, non_local_definitions)] +#[cfg(not(feature = "std"))] +pub(crate) use alloc::vec::Vec; + use alloy_primitives::{Address, B256, U256}; use alloy_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper}; -use std::mem; +use core::mem; /// A list of addresses and storage keys that the transaction plans to access. /// Accesses outside the list are possible, but become more expensive. #[derive(Clone, Debug, PartialEq, Eq, Hash, Default, RlpDecodable, RlpEncodable)] #[cfg_attr( - any(test, feature = "arbitrary"), + all(any(test, feature = "arbitrary"), feature = "std"), derive(proptest_derive::Arbitrary, arbitrary::Arbitrary) )] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -22,7 +25,7 @@ pub struct AccessListItem { pub address: Address, /// Keys of storage that would be loaded at the start of execution #[cfg_attr( - any(test, feature = "arbitrary"), + all(any(test, feature = "arbitrary"), feature = "std"), proptest( strategy = "proptest::collection::vec(proptest::arbitrary::any::(), 0..=20)" ) @@ -41,12 +44,12 @@ impl AccessListItem { /// AccessList as defined in EIP-2930 #[derive(Clone, Debug, PartialEq, Eq, Hash, Default, RlpDecodableWrapper, RlpEncodableWrapper)] #[cfg_attr( - any(test, feature = "arbitrary"), + all(any(test, feature = "arbitrary"), feature = "std"), derive(proptest_derive::Arbitrary, arbitrary::Arbitrary) )] pub struct AccessList( #[cfg_attr( - any(test, feature = "arbitrary"), + all(any(test, feature = "arbitrary"), feature = "std"), proptest( strategy = "proptest::collection::vec(proptest::arbitrary::any::(), 0..=20)" ) diff --git a/crates/eips/src/lib.rs b/crates/eips/src/lib.rs index 5935ff12ec3..77e0ca8edcf 100644 --- a/crates/eips/src/lib.rs +++ b/crates/eips/src/lib.rs @@ -11,9 +11,13 @@ clippy::missing_const_for_fn, rustdoc::all )] -#![cfg_attr(not(test), warn(unused_crate_dependencies))] #![deny(unused_must_use, rust_2018_idioms)] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(feature = "arbitrary", cfg(feature = "std"))] +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; pub mod eip1559; pub use eip1559::calc_next_block_base_fee; diff --git a/crates/eips/src/merge.rs b/crates/eips/src/merge.rs index 026440baa49..76fd09d11db 100644 --- a/crates/eips/src/merge.rs +++ b/crates/eips/src/merge.rs @@ -1,6 +1,6 @@ //! Constants related to the beacon chain consensus. -use std::time::Duration; +use core::time::Duration; /// An EPOCH is a series of 32 slots. pub const EPOCH_SLOTS: u64 = 32; diff --git a/crates/rpc-types/Cargo.toml b/crates/rpc-types/Cargo.toml index 1a0673ba6c0..2716292744e 100644 --- a/crates/rpc-types/Cargo.toml +++ b/crates/rpc-types/Cargo.toml @@ -14,7 +14,7 @@ exclude.workspace = true [dependencies] # ethereum alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] } -alloy-primitives = { workspace = true, features = ["rlp", "serde"] } +alloy-primitives = { workspace = true, features = ["rlp", "serde", "std"] } alloy-serde.workspace = true ethereum_ssz_derive = { workspace = true, optional = true }