From d296ccf7196f691a2028dbe9445c71103f075e16 Mon Sep 17 00:00:00 2001 From: clabby Date: Thu, 4 Apr 2024 13:24:51 -0400 Subject: [PATCH] feat: Port `alloy-consensus` to `no_std` enable `std` feature everywhere --- crates/alloy/Cargo.toml | 1 + crates/consensus/Cargo.toml | 8 +++++--- crates/consensus/src/header.rs | 7 +++++++ crates/consensus/src/lib.rs | 4 ++++ crates/consensus/src/receipt/mod.rs | 1 + crates/consensus/src/receipt/receipts.rs | 3 +++ crates/consensus/src/transaction/eip1559.rs | 7 +++++++ crates/consensus/src/transaction/eip2930.rs | 8 ++++++++ crates/consensus/src/transaction/eip4844.rs | 14 +++++++++++--- .../consensus/src/transaction/eip4844/builder.rs | 16 ++++++++++++---- crates/consensus/src/transaction/envelope.rs | 11 +++++++++-- crates/consensus/src/transaction/legacy.rs | 7 +++++++ crates/consensus/src/transaction/mod.rs | 14 +++++++++++--- crates/network/Cargo.toml | 2 +- crates/node-bindings/src/anvil.rs | 7 +------ crates/provider/Cargo.toml | 2 +- crates/rpc-types-engine/Cargo.toml | 2 +- crates/rpc-types/Cargo.toml | 4 ++-- crates/signer-aws/Cargo.toml | 2 +- crates/signer-gcp/Cargo.toml | 2 +- crates/signer-ledger/Cargo.toml | 4 ++-- crates/signer-trezor/Cargo.toml | 2 +- crates/signer-wallet/Cargo.toml | 4 ++-- crates/signer/Cargo.toml | 2 +- 24 files changed, 100 insertions(+), 34 deletions(-) diff --git a/crates/alloy/Cargo.toml b/crates/alloy/Cargo.toml index 92c11b2e7668..f7d055d4e4e3 100644 --- a/crates/alloy/Cargo.toml +++ b/crates/alloy/Cargo.toml @@ -68,6 +68,7 @@ std = [ "alloy-eips?/std", "alloy-genesis?/std", "alloy-serde?/std", + "alloy-consensus?/std", ] # configuration diff --git a/crates/consensus/Cargo.toml b/crates/consensus/Cargo.toml index 09740eb5dc4e..aa4c0f327fcc 100644 --- a/crates/consensus/Cargo.toml +++ b/crates/consensus/Cargo.toml @@ -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 } @@ -37,7 +37,9 @@ tokio = { workspace = true, features = ["macros"] } serde_json.workspace = true [features] +default = ["std"] +std = ["alloy-eips/std", "c-kzg/std", "sha2/std"] k256 = ["alloy-primitives/k256"] -kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg"] +kzg = ["std", "dep:c-kzg", "dep:thiserror", "alloy-eips/kzg"] arbitrary = ["dep:arbitrary", "alloy-eips/arbitrary"] serde = ["dep:serde", "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde"] diff --git a/crates/consensus/src/header.rs b/crates/consensus/src/header.rs index ea316c53255f..c20debdce5ab 100644 --- a/crates/consensus/src/header.rs +++ b/crates/consensus/src/header.rs @@ -7,6 +7,13 @@ 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, }; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::mem; + +#[cfg(feature = "std")] use std::mem; /// Ommer root of empty list. diff --git a/crates/consensus/src/lib.rs b/crates/consensus/src/lib.rs index 62744f1efc19..77bef5a6c9e7 100644 --- a/crates/consensus/src/lib.rs +++ b/crates/consensus/src/lib.rs @@ -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; diff --git a/crates/consensus/src/receipt/mod.rs b/crates/consensus/src/receipt/mod.rs index 08acf7475247..a41de5ccbf73 100644 --- a/crates/consensus/src/receipt/mod.rs +++ b/crates/consensus/src/receipt/mod.rs @@ -34,6 +34,7 @@ pub trait TxReceipt { #[cfg(test)] mod tests { use super::*; + use alloc::vec; use alloy_eips::eip2718::Encodable2718; use alloy_primitives::{address, b256, bytes, hex, Bytes, LogData}; use alloy_rlp::{Decodable, Encodable}; diff --git a/crates/consensus/src/receipt/receipts.rs b/crates/consensus/src/receipt/receipts.rs index a473ef4198ff..db9eb3afc222 100644 --- a/crates/consensus/src/receipt/receipts.rs +++ b/crates/consensus/src/receipt/receipts.rs @@ -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))] diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index f3010c03ade0..956b17586168 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -2,6 +2,13 @@ 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}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::mem; + +#[cfg(feature = "std")] use std::mem; /// A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)). diff --git a/crates/consensus/src/transaction/eip2930.rs b/crates/consensus/src/transaction/eip2930.rs index e0ef9b252ac5..c6adcbc900b6 100644 --- a/crates/consensus/src/transaction/eip2930.rs +++ b/crates/consensus/src/transaction/eip2930.rs @@ -2,6 +2,13 @@ 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}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::mem; + +#[cfg(feature = "std")] use std::mem; /// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)). @@ -304,6 +311,7 @@ impl Decodable for TxEip2930 { mod tests { use super::TxEip2930; use crate::{SignableTransaction, TxEnvelope}; + use alloc::{vec, vec::Vec}; use alloy_primitives::{Address, Bytes, Signature, TxKind, U256}; use alloy_rlp::{Decodable, Encodable}; diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index 28e15ae32caa..6034fc4c1685 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -14,6 +14,13 @@ use alloy_primitives::{ }; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; use sha2::{Digest, Sha256}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::mem; + +#[cfg(feature = "std")] use std::mem; #[cfg(not(feature = "kzg"))] @@ -1047,8 +1054,8 @@ struct BlobTransactionSidecarRlp { proofs: Vec>, } -const _: [(); std::mem::size_of::()] = - [(); std::mem::size_of::()]; +const _: [(); mem::size_of::()] = + [(); mem::size_of::()]; impl BlobTransactionSidecarRlp { const fn wrap_ref(other: &BlobTransactionSidecar) -> &Self { @@ -1058,7 +1065,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) { @@ -1095,6 +1102,7 @@ pub(crate) fn kzg_to_versioned_hash(commitment: &[u8]) -> B256 { mod tests { use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar}; use crate::{SignableTransaction, TxEnvelope}; + use alloc::{vec, vec::Vec}; use alloy_primitives::{Signature, U256}; use alloy_rlp::{Decodable, Encodable}; diff --git a/crates/consensus/src/transaction/eip4844/builder.rs b/crates/consensus/src/transaction/eip4844/builder.rs index 0a6af7af1e38..6c2cfdc6f9dd 100644 --- a/crates/consensus/src/transaction/eip4844/builder.rs +++ b/crates/consensus/src/transaction/eip4844/builder.rs @@ -3,6 +3,14 @@ use alloy_eips::eip4844::Blob; #[cfg(feature = "kzg")] use c_kzg::{Blob, KzgCommitment, KzgProof}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::cmp; + +#[cfg(feature = "std")] +use std::cmp; + use alloy_eips::eip4844::{BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB}; use super::utils::WholeFe; @@ -196,7 +204,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; @@ -220,7 +228,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 } @@ -386,9 +394,9 @@ where #[cfg(test)] mod tests { - use alloy_eips::eip4844::USABLE_BYTES_PER_BLOB; - use super::*; + use alloc::vec; + use alloy_eips::eip4844::USABLE_BYTES_PER_BLOB; #[test] fn ingestion_strategy() { diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index ebd057ddfda3..7f40347d1b0f 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -4,6 +4,11 @@ use crate::{ use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718}; use alloy_rlp::{Decodable, Encodable, Header}; +#[cfg(not(feature = "std"))] +use core::mem; +#[cfg(feature = "std")] +use std::mem; + /// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], and /// [2930]. /// @@ -43,7 +48,7 @@ impl TryFrom for TxType { fn try_from(value: u8) -> Result { 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)), } } @@ -252,11 +257,13 @@ impl Encodable2718 for TxEnvelope { #[cfg(test)] mod tests { + extern crate std; + use super::*; 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")] diff --git a/crates/consensus/src/transaction/legacy.rs b/crates/consensus/src/transaction/legacy.rs index 02f80bf97af7..e7feea4c50ab 100644 --- a/crates/consensus/src/transaction/legacy.rs +++ b/crates/consensus/src/transaction/legacy.rs @@ -1,6 +1,13 @@ 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}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::mem; + +#[cfg(feature = "std")] use std::mem; /// Legacy transaction. diff --git a/crates/consensus/src/transaction/mod.rs b/crates/consensus/src/transaction/mod.rs index a7fe001c3609..1d36d82805fc 100644 --- a/crates/consensus/src/transaction/mod.rs +++ b/crates/consensus/src/transaction/mod.rs @@ -1,6 +1,14 @@ use crate::Signed; use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use core::any; + +#[cfg(feature = "std")] +use std::any; + mod eip1559; pub use eip1559::TxEip1559; @@ -25,7 +33,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]; @@ -107,8 +115,8 @@ pub trait SignableTransaction: Transaction { // TODO: Remove in favor of dyn trait upcasting (TBD, see https://github.com/rust-lang/rust/issues/65991#issuecomment-1903120162) #[doc(hidden)] impl dyn SignableTransaction { - pub fn __downcast_ref(&self) -> Option<&T> { - if std::any::Any::type_id(self) == std::any::TypeId::of::() { + pub fn __downcast_ref(&self) -> Option<&T> { + if any::Any::type_id(self) == any::TypeId::of::() { unsafe { Some(&*(self as *const _ as *const T)) } } else { None diff --git a/crates/network/Cargo.toml b/crates/network/Cargo.toml index 637c51dc1a85..f7ecf349280a 100644 --- a/crates/network/Cargo.toml +++ b/crates/network/Cargo.toml @@ -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 diff --git a/crates/node-bindings/src/anvil.rs b/crates/node-bindings/src/anvil.rs index a1e9a8d39503..ed8b66d9c18f 100644 --- a/crates/node-bindings/src/anvil.rs +++ b/crates/node-bindings/src/anvil.rs @@ -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 { diff --git a/crates/provider/Cargo.toml b/crates/provider/Cargo.toml index 34840f0ab35d..f0bff8071877 100644 --- a/crates/provider/Cargo.toml +++ b/crates/provider/Cargo.toml @@ -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 diff --git a/crates/rpc-types-engine/Cargo.toml b/crates/rpc-types-engine/Cargo.toml index 1f6840024174..6318d09cd0de 100644 --- a/crates/rpc-types-engine/Cargo.toml +++ b/crates/rpc-types-engine/Cargo.toml @@ -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 } diff --git a/crates/rpc-types/Cargo.toml b/crates/rpc-types/Cargo.toml index 49835554c2a0..6a96a6304a3a 100644 --- a/crates/rpc-types/Cargo.toml +++ b/crates/rpc-types/Cargo.toml @@ -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 @@ -43,7 +43,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 diff --git a/crates/signer-aws/Cargo.toml b/crates/signer-aws/Cargo.toml index a525c65f3ffa..84eeb3700114 100644 --- a/crates/signer-aws/Cargo.toml +++ b/crates/signer-aws/Cargo.toml @@ -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 diff --git a/crates/signer-gcp/Cargo.toml b/crates/signer-gcp/Cargo.toml index 890c305f3bdb..6af633490e5f 100644 --- a/crates/signer-gcp/Cargo.toml +++ b/crates/signer-gcp/Cargo.toml @@ -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 diff --git a/crates/signer-ledger/Cargo.toml b/crates/signer-ledger/Cargo.toml index 6cd98772f780..c7a321c6f628 100644 --- a/crates/signer-ledger/Cargo.toml +++ b/crates/signer-ledger/Cargo.toml @@ -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 @@ -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 diff --git a/crates/signer-trezor/Cargo.toml b/crates/signer-trezor/Cargo.toml index 19698cb07473..2a9236eca6e3 100644 --- a/crates/signer-trezor/Cargo.toml +++ b/crates/signer-trezor/Cargo.toml @@ -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 diff --git a/crates/signer-wallet/Cargo.toml b/crates/signer-wallet/Cargo.toml index bdc2ad073a5b..a0a371c662de 100644 --- a/crates/signer-wallet/Cargo.toml +++ b/crates/signer-wallet/Cargo.toml @@ -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 @@ -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 diff --git a/crates/signer/Cargo.toml b/crates/signer/Cargo.toml index 778d76874ab1..a1fcebba8add 100644 --- a/crates/signer/Cargo.toml +++ b/crates/signer/Cargo.toml @@ -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