Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support no_std for alloy-eips #181

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment on lines +21 to 22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we should move this error type and keep the custom variant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we should move this error type and keep the custom variant

i want to how tohandle the related trait

/// 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>),
Comment on lines -22 to -24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think eventually we'd need this, so perhaps the better question for this PR is, does this type even belong in here?

perhaps we should rather move this to the consensus crate instead, I believe it's better suited in there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?
Move all eip error type to consensus crate? and add Error type to trait such as Decodable2718?

Copy link
Contributor Author

@yjhmelody yjhmelody Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that might be a break change, which the current one is not? Maybe it should be improved in subsequent versions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing a variant is a breaking change as well, I'd really prefer moving the error. imo it does not belong in the eip crate

}

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
Loading