From 5a21bda0e8445d70b8b23ffd74858a913598bba7 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 28 Dec 2024 12:49:31 +0100 Subject: [PATCH] chore: add no-std support for eth-wire-types (#13577) --- crates/net/eth-wire-types/Cargo.toml | 18 ++++++++- crates/net/eth-wire-types/src/blocks.rs | 1 + crates/net/eth-wire-types/src/broadcast.rs | 37 +++++++++---------- crates/net/eth-wire-types/src/capability.rs | 3 +- .../eth-wire-types/src/disconnect_reason.rs | 1 + crates/net/eth-wire-types/src/lib.rs | 3 ++ crates/net/eth-wire-types/src/message.rs | 3 +- crates/net/eth-wire-types/src/primitives.rs | 2 +- crates/net/eth-wire-types/src/receipts.rs | 1 + crates/net/eth-wire-types/src/state.rs | 1 + crates/net/eth-wire-types/src/status.rs | 6 +-- crates/net/eth-wire-types/src/transactions.rs | 1 + crates/net/eth-wire-types/src/version.rs | 5 ++- .../net/network/src/transactions/fetcher.rs | 3 +- 14 files changed, 55 insertions(+), 30 deletions(-) diff --git a/crates/net/eth-wire-types/Cargo.toml b/crates/net/eth-wire-types/Cargo.toml index 1fe97f236d..d92e971a6b 100644 --- a/crates/net/eth-wire-types/Cargo.toml +++ b/crates/net/eth-wire-types/Cargo.toml @@ -22,7 +22,7 @@ reth-ethereum-forks.workspace = true # ethereum alloy-chains = { workspace = true, features = ["rlp"] } alloy-eips.workspace = true -alloy-primitives.workspace = true +alloy-primitives = { workspace = true, features = ["map"] } alloy-rlp = { workspace = true, features = ["derive"] } alloy-consensus.workspace = true @@ -46,6 +46,22 @@ proptest-arbitrary-interop.workspace = true rand.workspace = true [features] +default = ["std"] +std = [ + "alloy-chains/std", + "alloy-consensus/std", + "alloy-eips/std", + "alloy-genesis/std", + "alloy-primitives/std", + "alloy-rlp/std", + "bytes/std", + "derive_more/std", + "reth-ethereum-forks/std", + "reth-primitives/std", + "reth-primitives-traits/std", + "serde?/std", + "thiserror/std" +] arbitrary = [ "reth-primitives/arbitrary", "alloy-chains/arbitrary", diff --git a/crates/net/eth-wire-types/src/blocks.rs b/crates/net/eth-wire-types/src/blocks.rs index e6506e86ad..5b4750f299 100644 --- a/crates/net/eth-wire-types/src/blocks.rs +++ b/crates/net/eth-wire-types/src/blocks.rs @@ -2,6 +2,7 @@ //! types. use crate::HeadersDirection; +use alloc::vec::Vec; use alloy_eips::BlockHashOrNumber; use alloy_primitives::B256; use alloy_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper}; diff --git a/crates/net/eth-wire-types/src/broadcast.rs b/crates/net/eth-wire-types/src/broadcast.rs index e6ea1a3a37..b868070982 100644 --- a/crates/net/eth-wire-types/src/broadcast.rs +++ b/crates/net/eth-wire-types/src/broadcast.rs @@ -1,24 +1,19 @@ //! Types for broadcasting new data. use crate::{EthMessage, EthVersion, NetworkPrimitives}; -use alloy_primitives::{Bytes, TxHash, B256, U128}; +use alloc::{sync::Arc, vec::Vec}; +use alloy_primitives::{ + map::{HashMap, HashSet}, + Bytes, TxHash, B256, U128, +}; use alloy_rlp::{ Decodable, Encodable, RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper, }; +use core::mem; use derive_more::{Constructor, Deref, DerefMut, From, IntoIterator}; use reth_codecs_derive::{add_arbitrary_tests, generate_tests}; use reth_primitives::TransactionSigned; use reth_primitives_traits::SignedTransaction; -use std::{ - collections::{HashMap, HashSet}, - mem, - sync::Arc, -}; - -#[cfg(feature = "arbitrary")] -use proptest::{collection::vec, prelude::*}; -#[cfg(feature = "arbitrary")] -use proptest_arbitrary_interop::arb; /// This informs peers of new blocks that have appeared on the network. #[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Default)] @@ -345,17 +340,21 @@ pub struct NewPooledTransactionHashes68 { } #[cfg(feature = "arbitrary")] -impl Arbitrary for NewPooledTransactionHashes68 { +impl proptest::prelude::Arbitrary for NewPooledTransactionHashes68 { type Parameters = (); fn arbitrary_with(_args: ()) -> Self::Strategy { + use proptest::{collection::vec, prelude::*}; // Generate a single random length for all vectors let vec_length = any::().prop_map(|x| x % 100 + 1); // Lengths between 1 and 100 vec_length .prop_flat_map(|len| { // Use the generated length to create vectors of TxType, usize, and B256 - let types_vec = - vec(arb::().prop_map(|ty| ty as u8), len..=len); + let types_vec = vec( + proptest_arbitrary_interop::arb::() + .prop_map(|ty| ty as u8), + len..=len, + ); // Map the usize values to the range 0..131072(0x20000) let sizes_vec = vec(proptest::num::usize::ANY.prop_map(|x| x % 131072), len..=len); @@ -367,7 +366,7 @@ impl Arbitrary for NewPooledTransactionHashes68 { .boxed() } - type Strategy = BoxedStrategy; + type Strategy = proptest::prelude::BoxedStrategy; } impl NewPooledTransactionHashes68 { @@ -496,7 +495,7 @@ impl DedupPayload for NewPooledTransactionHashes68 { fn dedup(self) -> PartiallyValidData { let Self { hashes, mut sizes, mut types } = self; - let mut deduped_data = HashMap::with_capacity(hashes.len()); + let mut deduped_data = HashMap::with_capacity_and_hasher(hashes.len(), Default::default()); for hash in hashes.into_iter().rev() { if let (Some(ty), Some(size)) = (types.pop(), sizes.pop()) { @@ -522,7 +521,7 @@ impl DedupPayload for NewPooledTransactionHashes66 { fn dedup(self) -> PartiallyValidData { let Self(hashes) = self; - let mut deduped_data = HashMap::with_capacity(hashes.len()); + let mut deduped_data = HashMap::with_capacity_and_hasher(hashes.len(), Default::default()); let noop_value: Eth68TxMetadata = None; @@ -699,7 +698,7 @@ impl RequestTxHashes { /// be stored in its entirety like in the future waiting for a /// [`GetPooledTransactions`](crate::GetPooledTransactions) request to resolve. pub fn with_capacity(capacity: usize) -> Self { - Self::new(HashSet::with_capacity(capacity)) + Self::new(HashSet::with_capacity_and_hasher(capacity, Default::default())) } /// Returns an new empty instance. @@ -744,7 +743,7 @@ mod tests { /// Takes as input a struct / encoded hex message pair, ensuring that we encode to the exact hex /// message, and decode to the exact struct. - fn test_encoding_vector( + fn test_encoding_vector( input: (T, &[u8]), ) { let (expected_decoded, expected_encoded) = input; diff --git a/crates/net/eth-wire-types/src/capability.rs b/crates/net/eth-wire-types/src/capability.rs index 5302c9f435..2002a03aea 100644 --- a/crates/net/eth-wire-types/src/capability.rs +++ b/crates/net/eth-wire-types/src/capability.rs @@ -1,10 +1,11 @@ //! All capability related types use crate::EthVersion; +use alloc::{borrow::Cow, string::String, vec::Vec}; use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable}; use bytes::BufMut; +use core::fmt; use reth_codecs_derive::add_arbitrary_tests; -use std::{borrow::Cow, fmt}; /// A message indicating a supported capability and capability version. #[add_arbitrary_tests(rlp)] diff --git a/crates/net/eth-wire-types/src/disconnect_reason.rs b/crates/net/eth-wire-types/src/disconnect_reason.rs index 1792c5e2ac..e6efa0fca8 100644 --- a/crates/net/eth-wire-types/src/disconnect_reason.rs +++ b/crates/net/eth-wire-types/src/disconnect_reason.rs @@ -1,5 +1,6 @@ //! `RLPx` disconnect reason sent to/received from peer +use alloc::vec; use alloy_primitives::bytes::{Buf, BufMut}; use alloy_rlp::{Decodable, Encodable, Header}; use derive_more::Display; diff --git a/crates/net/eth-wire-types/src/lib.rs b/crates/net/eth-wire-types/src/lib.rs index ac7ea55d0b..011173e71a 100644 --- a/crates/net/eth-wire-types/src/lib.rs +++ b/crates/net/eth-wire-types/src/lib.rs @@ -7,6 +7,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 status; pub use status::{Status, StatusBuilder}; diff --git a/crates/net/eth-wire-types/src/message.rs b/crates/net/eth-wire-types/src/message.rs index 308baf48f6..d3c6e5d064 100644 --- a/crates/net/eth-wire-types/src/message.rs +++ b/crates/net/eth-wire-types/src/message.rs @@ -12,9 +12,10 @@ use super::{ NewPooledTransactionHashes68, NodeData, PooledTransactions, Receipts, Status, Transactions, }; use crate::{EthNetworkPrimitives, EthVersion, NetworkPrimitives, SharedTransactions}; +use alloc::{boxed::Box, sync::Arc}; use alloy_primitives::bytes::{Buf, BufMut}; use alloy_rlp::{length_of_length, Decodable, Encodable, Header}; -use std::{fmt::Debug, sync::Arc}; +use core::fmt::Debug; /// [`MAX_MESSAGE_SIZE`] is the maximum cap on the size of a protocol message. // https://github.com/ethereum/go-ethereum/blob/30602163d5d8321fbc68afdcbbaf2362b2641bde/eth/protocols/eth/protocol.go#L50 diff --git a/crates/net/eth-wire-types/src/primitives.rs b/crates/net/eth-wire-types/src/primitives.rs index 0db1dc7df5..6bd6d17531 100644 --- a/crates/net/eth-wire-types/src/primitives.rs +++ b/crates/net/eth-wire-types/src/primitives.rs @@ -2,9 +2,9 @@ use alloy_consensus::{RlpDecodableReceipt, RlpEncodableReceipt, TxReceipt}; use alloy_rlp::{Decodable, Encodable}; +use core::fmt::Debug; use reth_primitives::NodePrimitives; use reth_primitives_traits::{Block, BlockBody, BlockHeader, SignedTransaction}; -use std::fmt::Debug; /// Abstraction over primitive types which might appear in network messages. See /// [`crate::EthMessage`] for more context. diff --git a/crates/net/eth-wire-types/src/receipts.rs b/crates/net/eth-wire-types/src/receipts.rs index 14493505df..c20c237811 100644 --- a/crates/net/eth-wire-types/src/receipts.rs +++ b/crates/net/eth-wire-types/src/receipts.rs @@ -1,5 +1,6 @@ //! Implements the `GetReceipts` and `Receipts` message types. +use alloc::vec::Vec; use alloy_consensus::{RlpDecodableReceipt, RlpEncodableReceipt}; use alloy_primitives::B256; use alloy_rlp::{RlpDecodableWrapper, RlpEncodableWrapper}; diff --git a/crates/net/eth-wire-types/src/state.rs b/crates/net/eth-wire-types/src/state.rs index 57273adc6b..dc1f516105 100644 --- a/crates/net/eth-wire-types/src/state.rs +++ b/crates/net/eth-wire-types/src/state.rs @@ -1,5 +1,6 @@ //! Implements the `GetNodeData` and `NodeData` message types. +use alloc::vec::Vec; use alloy_primitives::{Bytes, B256}; use alloy_rlp::{RlpDecodableWrapper, RlpEncodableWrapper}; use reth_codecs_derive::add_arbitrary_tests; diff --git a/crates/net/eth-wire-types/src/status.rs b/crates/net/eth-wire-types/src/status.rs index e19912481e..157d0ce537 100644 --- a/crates/net/eth-wire-types/src/status.rs +++ b/crates/net/eth-wire-types/src/status.rs @@ -2,10 +2,10 @@ use crate::EthVersion; use alloy_chains::{Chain, NamedChain}; use alloy_primitives::{hex, B256, U256}; use alloy_rlp::{RlpDecodable, RlpEncodable}; +use core::fmt::{Debug, Display}; use reth_chainspec::{EthChainSpec, Hardforks, MAINNET}; use reth_codecs_derive::add_arbitrary_tests; use reth_ethereum_forks::{EthereumHardfork, ForkId, Head}; -use std::fmt::{Debug, Display}; /// The status message is used in the eth protocol handshake to ensure that peers are on the same /// network and are following the same fork. @@ -71,7 +71,7 @@ impl Status { } impl Display for Status { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let hexed_blockhash = hex::encode(self.blockhash); let hexed_genesis = hex::encode(self.genesis); write!( @@ -88,7 +88,7 @@ impl Display for Status { } impl Debug for Status { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let hexed_blockhash = hex::encode(self.blockhash); let hexed_genesis = hex::encode(self.genesis); if f.alternate() { diff --git a/crates/net/eth-wire-types/src/transactions.rs b/crates/net/eth-wire-types/src/transactions.rs index bd77d761e3..788136791e 100644 --- a/crates/net/eth-wire-types/src/transactions.rs +++ b/crates/net/eth-wire-types/src/transactions.rs @@ -1,5 +1,6 @@ //! Implements the `GetPooledTransactions` and `PooledTransactions` message types. +use alloc::vec::Vec; use alloy_eips::eip2718::Encodable2718; use alloy_primitives::B256; use alloy_rlp::{RlpDecodableWrapper, RlpEncodableWrapper}; diff --git a/crates/net/eth-wire-types/src/version.rs b/crates/net/eth-wire-types/src/version.rs index 40d51cb551..93ad8f7e5c 100644 --- a/crates/net/eth-wire-types/src/version.rs +++ b/crates/net/eth-wire-types/src/version.rs @@ -1,9 +1,10 @@ //! Support for representing the version of the `eth` -use std::{fmt, str::FromStr}; - +use crate::alloc::string::ToString; +use alloc::string::String; use alloy_rlp::{Decodable, Encodable, Error as RlpError}; use bytes::BufMut; +use core::{fmt, str::FromStr}; use derive_more::Display; use reth_codecs_derive::add_arbitrary_tests; diff --git a/crates/net/network/src/transactions/fetcher.rs b/crates/net/network/src/transactions/fetcher.rs index b79e78461d..49a0d2abe4 100644 --- a/crates/net/network/src/transactions/fetcher.rs +++ b/crates/net/network/src/transactions/fetcher.rs @@ -1498,8 +1498,7 @@ mod test { assert_ne!(hash, signed_tx_2.hash()) } - let request_hashes = - RequestTxHashes::new(request_hashes.into_iter().collect::>()); + let request_hashes = RequestTxHashes::new(request_hashes.into_iter().collect()); // but response contains tx 1 + another tx let response_txns = PooledTransactions(vec![signed_tx_1.clone(), signed_tx_2]);