Skip to content

Commit

Permalink
feat: support no_std for alloy-eips (#181)
Browse files Browse the repository at this point in the history
* refactor: support no_std for alloy-eips

* fix: disable arbitrary when no_std

* fix: clippy redundant import

---------

Co-authored-by: James <[email protected]>
  • Loading branch information
yjhmelody and prestwich authored Mar 13, 2024
1 parent d0794e5 commit fa155ac
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions crates/eips/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
34 changes: 27 additions & 7 deletions crates/eips/src/eip2718.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,48 @@
//!
//! [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;

/// [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<dyn std::error::Error>),
}

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<alloy_rlp::Error> 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.
///
Expand Down
13 changes: 8 additions & 5 deletions crates/eips/src/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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::<B256>(), 0..=20)"
)
Expand All @@ -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::<AccessListItem>(), 0..=20)"
)
Expand Down
6 changes: 5 additions & 1 deletion crates/eips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/eips/src/merge.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit fa155ac

Please sign in to comment.