Skip to content

Commit

Permalink
feat: Port alloy-consensus to no_std
Browse files Browse the repository at this point in the history
enable `std` feature everywhere
  • Loading branch information
clabby committed Apr 4, 2024
1 parent 01efe93 commit d296ccf
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 34 deletions.
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
8 changes: 5 additions & 3 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", "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"]
7 changes: 7 additions & 0 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
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
7 changes: 7 additions & 0 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
8 changes: 8 additions & 0 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down Expand Up @@ -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};

Expand Down
14 changes: 11 additions & 3 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -1047,8 +1054,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 +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) {
Expand Down Expand Up @@ -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};

Expand Down
16 changes: 12 additions & 4 deletions crates/consensus/src/transaction/eip4844/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
}
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 9 additions & 2 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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].
///
Expand Down Expand Up @@ -43,7 +48,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 @@ -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")]
Expand Down
7 changes: 7 additions & 0 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
14 changes: 11 additions & 3 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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];

Expand Down Expand Up @@ -107,8 +115,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 All @@ -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
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
Loading

0 comments on commit d296ccf

Please sign in to comment.