mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-30 09:38:24 -05:00
chore: add no-std support for eth-wire-types (#13577)
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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::<usize>().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::<reth_primitives::TxType>().prop_map(|ty| ty as u8), len..=len);
|
||||
let types_vec = vec(
|
||||
proptest_arbitrary_interop::arb::<reth_primitives::TxType>()
|
||||
.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<Self>;
|
||||
type Strategy = proptest::prelude::BoxedStrategy<Self>;
|
||||
}
|
||||
|
||||
impl NewPooledTransactionHashes68 {
|
||||
@@ -496,7 +495,7 @@ impl DedupPayload for NewPooledTransactionHashes68 {
|
||||
fn dedup(self) -> PartiallyValidData<Self::Value> {
|
||||
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<Self::Value> {
|
||||
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<T: Encodable + Decodable + PartialEq + std::fmt::Debug>(
|
||||
fn test_encoding_vector<T: Encodable + Decodable + PartialEq + core::fmt::Debug>(
|
||||
input: (T, &[u8]),
|
||||
) {
|
||||
let (expected_decoded, expected_encoded) = input;
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -1498,8 +1498,7 @@ mod test {
|
||||
assert_ne!(hash, signed_tx_2.hash())
|
||||
}
|
||||
|
||||
let request_hashes =
|
||||
RequestTxHashes::new(request_hashes.into_iter().collect::<HashSet<_>>());
|
||||
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]);
|
||||
|
||||
Reference in New Issue
Block a user