feat: add std feature to network-peers (#13078)

This commit is contained in:
Matthias Seitz
2024-12-02 21:56:21 +01:00
committed by GitHub
parent 156984b377
commit 65193bdaf3
9 changed files with 49 additions and 23 deletions

View File

@ -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"
]

View File

@ -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"]

View File

@ -1,6 +1,7 @@
//! Bootnodes for the network
use crate::NodeRecord;
use alloc::vec::Vec;
mod ethereum;
pub use ethereum::*;

View File

@ -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")]

View File

@ -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")]

View File

@ -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<NodeRecord, &str> {
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<NodeRecord, Error> {
#[cfg(any(test, feature = "std"))]
pub fn resolve_blocking(&self) -> Result<NodeRecord, std::io::Error> {
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<NodeRecord, Error> {
pub async fn resolve(&self) -> Result<NodeRecord, std::io::Error> {
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()))
}