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: std feature flag for alloy-consensus #461

Merged
merged 4 commits into from
Apr 5, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ The following crates support `no_std`:
- alloy-eips
- alloy-genesis
- alloy-serde
- alloy-consensus

If you would like to add `no_std` support to a crate, please make sure to update
`scripts/check_no_std.sh` as well.
Expand Down
1 change: 1 addition & 0 deletions crates/alloy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ std = [
"alloy-eips?/std",
"alloy-genesis?/std",
"alloy-serde?/std",
"alloy-consensus?/std",
]

# configuration
Expand Down
10 changes: 6 additions & 4 deletions crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ alloy-rlp.workspace = true
alloy-eips.workspace = true
alloy-serde = { workspace = true, optional = true }

sha2 = "0.10"
sha2 = { version = "0.10", default-features = false }

# kzg
thiserror = { workspace = true, optional = true }
c-kzg = { workspace = true, features = ["std", "serde"], optional = true }
c-kzg = { workspace = true, features = ["serde"], optional = true }

# arbitrary
arbitrary = { workspace = true, features = ["derive"], optional = true }
Expand All @@ -37,7 +37,9 @@ tokio = { workspace = true, features = ["macros"] }
serde_json.workspace = true

[features]
default = ["std"]
std = ["alloy-eips/std", "sha2/std", "c-kzg?/std"]
k256 = ["alloy-primitives/k256"]
kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg"]
arbitrary = ["dep:arbitrary", "alloy-eips/arbitrary"]
kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "std"]
arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary"]
serde = ["dep:serde", "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde"]
5 changes: 4 additions & 1 deletion crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use alloy_primitives::{b256, keccak256, Address, BlockNumber, Bloom, Bytes, B256
use alloy_rlp::{
length_of_length, Buf, BufMut, Decodable, Encodable, EMPTY_LIST_CODE, EMPTY_STRING_CODE,
};
use std::mem;
use core::mem;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Ommer root of empty list.
pub const EMPTY_OMMER_ROOT_HASH: B256 =
Expand Down
4 changes: 4 additions & 0 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#![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(not(feature = "std"), no_std)]

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

pub mod constants;

Expand Down
1 change: 1 addition & 0 deletions crates/consensus/src/receipt/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ where
0 => Ok(Self::Legacy(receipt)),
1 => Ok(Self::Eip2930(receipt)),
2 => Ok(Self::Eip1559(receipt)),
3 => Ok(Self::Eip4844(receipt)),
_ => unreachable!(),
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/consensus/src/receipt/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use super::TxReceipt;
use alloy_primitives::{Bloom, Log};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Receipt containing result of transaction execution.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use std::mem;
use core::mem;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use std::mem;
use core::mem;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
Expand Down
11 changes: 7 additions & 4 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ use alloy_primitives::{
keccak256, Address, Bytes, ChainId, FixedBytes, Signature, TxKind, B256, U256,
};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;
use sha2::{Digest, Sha256};
use std::mem;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(not(feature = "kzg"))]
pub use alloy_eips::eip4844::{Blob, Bytes48};
Expand Down Expand Up @@ -1047,8 +1050,8 @@ struct BlobTransactionSidecarRlp {
proofs: Vec<FixedBytes<BYTES_PER_PROOF>>,
}

const _: [(); std::mem::size_of::<BlobTransactionSidecar>()] =
[(); std::mem::size_of::<BlobTransactionSidecarRlp>()];
const _: [(); mem::size_of::<BlobTransactionSidecar>()] =
[(); mem::size_of::<BlobTransactionSidecarRlp>()];

impl BlobTransactionSidecarRlp {
const fn wrap_ref(other: &BlobTransactionSidecar) -> &Self {
Expand All @@ -1058,7 +1061,7 @@ impl BlobTransactionSidecarRlp {

fn unwrap(self) -> BlobTransactionSidecar {
// SAFETY: Same repr and size
unsafe { std::mem::transmute(self) }
unsafe { mem::transmute(self) }
}

fn encode(&self, out: &mut dyn BufMut) {
Expand Down
12 changes: 7 additions & 5 deletions crates/consensus/src/transaction/eip4844/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use alloy_eips::eip4844::Blob;
#[cfg(feature = "kzg")]
use c_kzg::{Blob, KzgCommitment, KzgProof};

use alloy_eips::eip4844::{BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use super::utils::WholeFe;
use alloy_eips::eip4844::{BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB};
use core::cmp;

/// A builder for creating a [`BlobTransactionSidecar`].
///
Expand Down Expand Up @@ -196,7 +199,7 @@ impl SimpleCoder {

let mut res = Vec::with_capacity(num_bytes);
while num_bytes > 0 {
let to_copy = std::cmp::min(31, num_bytes);
let to_copy = cmp::min(31, num_bytes);
let fe = fes.next().ok_or(())?;
res.extend_from_slice(&fe.as_ref()[1..1 + to_copy]);
num_bytes -= to_copy;
Expand All @@ -220,7 +223,7 @@ impl SidecarCoder for SimpleCoder {

// ingest the rest of the data
while !data.is_empty() {
let (left, right) = data.split_at(std::cmp::min(31, data.len()));
let (left, right) = data.split_at(cmp::min(31, data.len()));
builder.ingest_partial_fe(left);
data = right
}
Expand Down Expand Up @@ -386,9 +389,8 @@ where

#[cfg(test)]
mod tests {
use alloy_eips::eip4844::USABLE_BYTES_PER_BLOB;

use super::*;
use alloy_eips::eip4844::USABLE_BYTES_PER_BLOB;

#[test]
fn ingestion_strategy() {
Expand Down
5 changes: 3 additions & 2 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
};
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
use alloy_rlp::{Decodable, Encodable, Header};
use core::mem;

/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], and
/// [2930].
Expand Down Expand Up @@ -43,7 +44,7 @@ impl TryFrom<u8> for TxType {
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
// SAFETY: repr(u8) with explicit discriminant
0..=3 => Ok(unsafe { std::mem::transmute(value) }),
0..=3 => Ok(unsafe { mem::transmute(value) }),
_ => Err(Eip2718Error::UnexpectedType(value)),
}
}
Expand Down Expand Up @@ -256,7 +257,7 @@ mod tests {
use crate::transaction::SignableTransaction;
use alloy_eips::eip2930::{AccessList, AccessListItem};
use alloy_primitives::{hex, Address, Bytes, Signature, TxKind, B256, U256};
use std::{fs, path::PathBuf};
use std::{fs, path::PathBuf, vec, vec::Vec};

#[test]
#[cfg(feature = "k256")]
Expand Down
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{SignableTransaction, Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};
use std::mem;
use core::mem;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Legacy transaction.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
Expand Down
10 changes: 7 additions & 3 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::Signed;
use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256};
use core::any;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

mod eip1559;
pub use eip1559::TxEip1559;
Expand All @@ -25,7 +29,7 @@ mod typed;
pub use typed::TypedTransaction;

/// Represents a minimal EVM transaction.
pub trait Transaction: std::any::Any + Send + Sync + 'static {
pub trait Transaction: any::Any + Send + Sync + 'static {
/// Get `data`.
fn input(&self) -> &[u8];

Expand Down Expand Up @@ -107,8 +111,8 @@ pub trait SignableTransaction<Signature>: Transaction {
// TODO: Remove in favor of dyn trait upcasting (TBD, see https://github.com/rust-lang/rust/issues/65991#issuecomment-1903120162)
#[doc(hidden)]
impl<S: 'static> dyn SignableTransaction<S> {
pub fn __downcast_ref<T: std::any::Any>(&self) -> Option<&T> {
if std::any::Any::type_id(self) == std::any::TypeId::of::<T>() {
pub fn __downcast_ref<T: any::Any>(&self) -> Option<&T> {
if any::Any::type_id(self) == any::TypeId::of::<T>() {
unsafe { Some(&*(self as *const _ as *const T)) }
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion crates/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-eips = { workspace = true, features = ["serde"] }
alloy-json-rpc.workspace = true
alloy-primitives.workspace = true
Expand Down
7 changes: 1 addition & 6 deletions crates/node-bindings/src/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,7 @@ impl Anvil {
Command::new("anvil")
};
cmd.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit());
let mut port = if let Some(port) = self.port {
port
} else {
// let the OS choose a port for us
0
};
let mut port = self.port.unwrap_or_default();
cmd.arg("-p").arg(port.to_string());

if let Some(mnemonic) = self.mnemonic {
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tracing.workspace = true
url = { workspace = true, optional = true }

[dev-dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-node-bindings.workspace = true
alloy-rlp.workspace = true
alloy-signer.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ exclude.workspace = true
# ethereum
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
alloy-consensus = { workspace = true, features = ["std"] }
alloy-rpc-types.workspace = true
alloy-serde.workspace = true
alloy-consensus.workspace = true

# ssz
ethereum_ssz_derive = { workspace = true, optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ alloy-primitives = { workspace = true, features = ["rlp", "serde", "std"] }
alloy-serde.workspace = true
alloy-genesis.workspace=true

alloy-consensus = { workspace = true, features = ["serde"] }
alloy-consensus = { workspace = true, features = ["std", "serde"] }
alloy-eips = {workspace = true, features = ["std", "serde"]}

itertools.workspace = true
Expand Down Expand Up @@ -49,7 +49,7 @@ ssz = ["alloy-primitives/ssz", "alloy-eips/ssz"]

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["rand", "rlp", "serde", "arbitrary"] }
alloy-consensus = { workspace = true, features = ["arbitrary"] }
alloy-consensus = { workspace = true, features = ["std", "arbitrary"] }

arbitrary = { workspace = true, features = ["derive"] }
proptest.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/signer-aws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-network.workspace = true
alloy-primitives.workspace = true
alloy-signer.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/signer-gcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-network.workspace = true
alloy-primitives.workspace = true
alloy-signer.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/signer-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-primitives.workspace = true
alloy-signer.workspace = true

Expand All @@ -28,7 +28,7 @@ alloy-sol-types = { workspace = true, optional = true }
alloy-network.workspace = true

[dev-dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-rlp.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
serial_test.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/signer-trezor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-network.workspace = true
alloy-primitives.workspace = true
alloy-signer.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/signer-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-network.workspace = true
alloy-primitives.workspace = true
alloy-signer.workspace = true
Expand All @@ -37,7 +37,7 @@ yubihsm = { version = "0.42", features = ["secp256k1", "http", "usb"], optional

[dev-dependencies]
serde.workspace = true
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-network.workspace = true
assert_matches.workspace = true
serde_json.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ alloy-sol-types = { workspace = true, optional = true, features = ["std"] }
alloy-dyn-abi = { workspace = true, optional = true, features = ["std", "eip712"] }

[dev-dependencies]
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-network.workspace = true
alloy-signer-wallet.workspace = true
assert_matches.workspace = true
Expand Down
1 change: 1 addition & 0 deletions scripts/check_no_std.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ no_std_packages=(
alloy-eips
alloy-genesis
alloy-serde
alloy-consensus
)

for package in "${no_std_packages[@]}"; do
Expand Down
Loading