From 122c4b187af3c621bdd10f829d44538300c0dae0 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 6 Sep 2024 17:58:02 -0400 Subject: [PATCH] feat: no_std network primitives (#1248) --- .github/workflows/no_std.yml | 2 +- Cargo.toml | 2 +- README.md | 2 +- crates/network-primitives/Cargo.toml | 4 ++++ crates/network-primitives/src/block.rs | 12 ++++++++---- crates/network-primitives/src/lib.rs | 3 +++ crates/rpc-types-eth/Cargo.toml | 4 ++-- 7 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.github/workflows/no_std.yml b/.github/workflows/no_std.yml index 9d16d58084d..b9af136c279 100644 --- a/.github/workflows/no_std.yml +++ b/.github/workflows/no_std.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: false matrix: - crate: ["alloy-eips", "alloy-consensus", "alloy-genesis"] + crate: ["alloy-eips", "alloy-consensus", "alloy-genesis", "alloy-network-primitives"] steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable diff --git a/Cargo.toml b/Cargo.toml index 65955d7f263..f016321c610 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -124,7 +124,7 @@ auto_impl = "1.2" base64 = "0.22" bimap = "0.6" home = "0.5" -itertools = "0.13" +itertools = { version = "0.13", default-features = false } once_cell = { version = "1.19", default-features = false } pin-project = "1.1" rand = "0.8" diff --git a/README.md b/README.md index cde535f8cb7..c3eeecec195 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ When updating this, also update: - .github/workflows/ci.yml --> -The current MSRV (minimum supported rust version) is 1.78. +The current MSRV (minimum supported rust version) is 1.79. Alloy will keep a rolling MSRV policy of **at least** two versions behind the latest stable release (so if the latest stable release is 1.58, we would diff --git a/crates/network-primitives/Cargo.toml b/crates/network-primitives/Cargo.toml index a56fb3e6239..bd107082ef3 100644 --- a/crates/network-primitives/Cargo.toml +++ b/crates/network-primitives/Cargo.toml @@ -26,3 +26,7 @@ serde.workspace = true [dev-dependencies] rand.workspace = true + +[features] +default = ["std"] +std = ["alloy-primitives/std"] diff --git a/crates/network-primitives/src/block.rs b/crates/network-primitives/src/block.rs index 1ec52f338ba..f817d2988b9 100644 --- a/crates/network-primitives/src/block.rs +++ b/crates/network-primitives/src/block.rs @@ -1,6 +1,9 @@ use alloy_primitives::B256; use serde::{Deserialize, Serialize}; +use alloc::{vec, vec::Vec}; +use core::slice; + use crate::TransactionResponse; /// Block Transactions depending on the boolean attribute of `eth_getBlockBy*`, @@ -69,10 +72,10 @@ impl BlockTransactions { /// Returns an iterator over the transactions (if any). This will be empty if the block is not /// full. - pub fn into_transactions(self) -> std::vec::IntoIter { + pub fn into_transactions(self) -> vec::IntoIter { match self { Self::Full(txs) => txs.into_iter(), - _ => std::vec::IntoIter::default(), + _ => vec::IntoIter::default(), } } @@ -149,8 +152,8 @@ pub struct BlockTransactionHashes<'a, T>(BlockTransactionHashesInner<'a, T>); #[derive(Clone, Debug)] enum BlockTransactionHashesInner<'a, T> { - Hashes(std::slice::Iter<'a, B256>), - Full(std::slice::Iter<'a, T>), + Hashes(slice::Iter<'a, B256>), + Full(slice::Iter<'a, T>), Uncle, } @@ -209,6 +212,7 @@ impl DoubleEndedIterator for BlockTransactionHashes<'_, } } +#[cfg(feature = "std")] impl<'a, T: TransactionResponse> std::iter::FusedIterator for BlockTransactionHashes<'a, T> {} /// Determines how the `transactions` field of block should be filled. diff --git a/crates/network-primitives/src/lib.rs b/crates/network-primitives/src/lib.rs index d1534307e4a..6012668a3dd 100644 --- a/crates/network-primitives/src/lib.rs +++ b/crates/network-primitives/src/lib.rs @@ -5,6 +5,9 @@ )] #![cfg_attr(not(test), warn(unused_crate_dependencies))] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; mod traits; pub use traits::{BlockResponse, HeaderResponse, ReceiptResponse, TransactionResponse}; diff --git a/crates/rpc-types-eth/Cargo.toml b/crates/rpc-types-eth/Cargo.toml index 1d40c304c78..e1f36e74051 100644 --- a/crates/rpc-types-eth/Cargo.toml +++ b/crates/rpc-types-eth/Cargo.toml @@ -23,8 +23,8 @@ alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] } alloy-primitives = { workspace = true, features = ["rlp", "serde", "std"] } alloy-serde.workspace = true -alloy-consensus = { workspace = true, features = ["std", "serde"] } -alloy-eips = { workspace = true, features = ["std", "serde"] } +alloy-consensus = { workspace = true, features = ["serde", "std"] } +alloy-eips = { workspace = true, features = ["serde", "std"] } alloy-network-primitives.workspace = true