diff --git a/.github/assets/check_wasm.sh b/.github/assets/check_wasm.sh index 11e5b5e00..971327f0c 100755 --- a/.github/assets/check_wasm.sh +++ b/.github/assets/check_wasm.sh @@ -70,6 +70,7 @@ exclude_crates=( reth-transaction-pool # c-kzg reth-trie-parallel # tokio reth-testing-utils + reth-network-peers ) # Array to hold the results diff --git a/crates/chainspec/Cargo.toml b/crates/chainspec/Cargo.toml index 67968ee0a..0e56cf2d3 100644 --- a/crates/chainspec/Cargo.toml +++ b/crates/chainspec/Cargo.toml @@ -50,7 +50,8 @@ std = [ "once_cell/std", "alloy-rlp/std", "reth-ethereum-forks/std", - "derive_more/std" + "derive_more/std", + "reth-network-peers/std" ] arbitrary = [ "alloy-chains/arbitrary", diff --git a/crates/net/p2p/Cargo.toml b/crates/net/p2p/Cargo.toml index a72110647..2c61da751 100644 --- a/crates/net/p2p/Cargo.toml +++ b/crates/net/p2p/Cargo.toml @@ -58,5 +58,6 @@ std = [ "alloy-primitives/std", "reth-primitives-traits/std", "alloy-consensus/std", - "derive_more/std" + "derive_more/std", + "reth-network-peers/std" ] diff --git a/crates/net/peers/Cargo.toml b/crates/net/peers/Cargo.toml index 5ac24edea..8ca5faec9 100644 --- a/crates/net/peers/Cargo.toml +++ b/crates/net/peers/Cargo.toml @@ -35,5 +35,13 @@ serde_json.workspace = true tokio = { workspace = true, features = ["net", "macros", "rt"] } [features] +default = ["std"] +std = [ + "alloy-primitives/std", + "alloy-rlp/std", + "secp256k1?/std", + "serde_with/std", + "thiserror/std" +] secp256k1 = ["dep:secp256k1", "enr/secp256k1"] net = ["dep:tokio", "tokio?/net"] diff --git a/crates/net/peers/src/bootnodes/mod.rs b/crates/net/peers/src/bootnodes/mod.rs index 31c91e5d1..b149c108a 100644 --- a/crates/net/peers/src/bootnodes/mod.rs +++ b/crates/net/peers/src/bootnodes/mod.rs @@ -1,6 +1,7 @@ //! Bootnodes for the network use crate::NodeRecord; +use alloc::vec::Vec; mod ethereum; pub use ethereum::*; diff --git a/crates/net/peers/src/lib.rs b/crates/net/peers/src/lib.rs index 1d60994d8..3e2777c2d 100644 --- a/crates/net/peers/src/lib.rs +++ b/crates/net/peers/src/lib.rs @@ -52,9 +52,16 @@ )] #![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; + +use alloc::{ + format, + string::{String, ToString}, +}; use alloy_primitives::B512; -use std::str::FromStr; +use core::str::FromStr; // Re-export PeerId for ease of use. pub use enr::Enr; @@ -137,8 +144,8 @@ impl AnyNode { let node_record = NodeRecord { address: enr .ip4() - .map(std::net::IpAddr::from) - .or_else(|| enr.ip6().map(std::net::IpAddr::from))?, + .map(core::net::IpAddr::from) + .or_else(|| enr.ip6().map(core::net::IpAddr::from))?, tcp_port: enr.tcp4().or_else(|| enr.tcp6())?, udp_port: enr.udp4().or_else(|| enr.udp6())?, id: pk2id(&enr.public_key()), @@ -186,8 +193,8 @@ impl FromStr for AnyNode { } } -impl std::fmt::Display for AnyNode { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Display for AnyNode { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Self::NodeRecord(record) => write!(f, "{record}"), #[cfg(feature = "secp256k1")] diff --git a/crates/net/peers/src/node_record.rs b/crates/net/peers/src/node_record.rs index ed48e242c..15ef5ad85 100644 --- a/crates/net/peers/src/node_record.rs +++ b/crates/net/peers/src/node_record.rs @@ -1,15 +1,18 @@ //! Commonly used `NodeRecord` type for peers. -use std::{ +use crate::PeerId; +use alloc::{ + format, + string::{String, ToString}, +}; +use alloy_rlp::{RlpDecodable, RlpEncodable}; +use core::{ fmt, fmt::Write, net::{IpAddr, Ipv4Addr, SocketAddr}, num::ParseIntError, str::FromStr, }; - -use crate::PeerId; -use alloy_rlp::{RlpDecodable, RlpEncodable}; use serde_with::{DeserializeFromStr, SerializeDisplay}; #[cfg(feature = "secp256k1")] diff --git a/crates/net/peers/src/trusted_peer.rs b/crates/net/peers/src/trusted_peer.rs index aa7e0a015..b87c4d6da 100644 --- a/crates/net/peers/src/trusted_peer.rs +++ b/crates/net/peers/src/trusted_peer.rs @@ -1,14 +1,14 @@ //! `NodeRecord` type that uses a domain instead of an IP. use crate::{NodeRecord, PeerId}; -use serde_with::{DeserializeFromStr, SerializeDisplay}; -use std::{ +use alloc::string::{String, ToString}; +use core::{ fmt::{self, Write}, - io::Error, net::IpAddr, num::ParseIntError, str::FromStr, }; +use serde_with::{DeserializeFromStr, SerializeDisplay}; use url::Host; /// Represents the node record of a trusted peer. The only difference between this and a @@ -45,11 +45,13 @@ impl TrustedPeer { Self { host, tcp_port: port, udp_port: port, id } } + #[cfg(any(test, feature = "std"))] const fn to_node_record(&self, ip: IpAddr) -> NodeRecord { NodeRecord { address: ip, id: self.id, tcp_port: self.tcp_port, udp_port: self.udp_port } } /// Tries to resolve directly to a [`NodeRecord`] if the host is an IP address. + #[cfg(any(test, feature = "std"))] fn try_node_record(&self) -> Result { match &self.host { Host::Ipv4(ip) => Ok(self.to_node_record((*ip).into())), @@ -61,23 +63,24 @@ impl TrustedPeer { /// Resolves the host in a [`TrustedPeer`] to an IP address, returning a [`NodeRecord`]. /// /// This use [`ToSocketAddr`](std::net::ToSocketAddrs) to resolve the host to an IP address. - pub fn resolve_blocking(&self) -> Result { + #[cfg(any(test, feature = "std"))] + pub fn resolve_blocking(&self) -> Result { let domain = match self.try_node_record() { Ok(record) => return Ok(record), Err(domain) => domain, }; // Resolve the domain to an IP address let mut ips = std::net::ToSocketAddrs::to_socket_addrs(&(domain, 0))?; - let ip = ips - .next() - .ok_or_else(|| Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found"))?; + let ip = ips.next().ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found") + })?; Ok(self.to_node_record(ip.ip())) } /// Resolves the host in a [`TrustedPeer`] to an IP address, returning a [`NodeRecord`]. #[cfg(any(test, feature = "net"))] - pub async fn resolve(&self) -> Result { + pub async fn resolve(&self) -> Result { let domain = match self.try_node_record() { Ok(record) => return Ok(record), Err(domain) => domain, @@ -85,9 +88,9 @@ impl TrustedPeer { // Resolve the domain to an IP address let mut ips = tokio::net::lookup_host(format!("{domain}:0")).await?; - let ip = ips - .next() - .ok_or_else(|| Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found"))?; + let ip = ips.next().ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found") + })?; Ok(self.to_node_record(ip.ip())) } diff --git a/crates/optimism/chainspec/Cargo.toml b/crates/optimism/chainspec/Cargo.toml index 7f74156b8..5ccf26607 100644 --- a/crates/optimism/chainspec/Cargo.toml +++ b/crates/optimism/chainspec/Cargo.toml @@ -57,5 +57,6 @@ std = [ "reth-optimism-forks/std", "alloy-consensus/std", "once_cell/std", - "derive_more/std" + "derive_more/std", + "reth-network-peers/std" ]