mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add std feature to network-peers (#13078)
This commit is contained in:
1
.github/assets/check_wasm.sh
vendored
1
.github/assets/check_wasm.sh
vendored
@ -70,6 +70,7 @@ exclude_crates=(
|
|||||||
reth-transaction-pool # c-kzg
|
reth-transaction-pool # c-kzg
|
||||||
reth-trie-parallel # tokio
|
reth-trie-parallel # tokio
|
||||||
reth-testing-utils
|
reth-testing-utils
|
||||||
|
reth-network-peers
|
||||||
)
|
)
|
||||||
|
|
||||||
# Array to hold the results
|
# Array to hold the results
|
||||||
|
|||||||
@ -50,7 +50,8 @@ std = [
|
|||||||
"once_cell/std",
|
"once_cell/std",
|
||||||
"alloy-rlp/std",
|
"alloy-rlp/std",
|
||||||
"reth-ethereum-forks/std",
|
"reth-ethereum-forks/std",
|
||||||
"derive_more/std"
|
"derive_more/std",
|
||||||
|
"reth-network-peers/std"
|
||||||
]
|
]
|
||||||
arbitrary = [
|
arbitrary = [
|
||||||
"alloy-chains/arbitrary",
|
"alloy-chains/arbitrary",
|
||||||
|
|||||||
@ -58,5 +58,6 @@ std = [
|
|||||||
"alloy-primitives/std",
|
"alloy-primitives/std",
|
||||||
"reth-primitives-traits/std",
|
"reth-primitives-traits/std",
|
||||||
"alloy-consensus/std",
|
"alloy-consensus/std",
|
||||||
"derive_more/std"
|
"derive_more/std",
|
||||||
|
"reth-network-peers/std"
|
||||||
]
|
]
|
||||||
|
|||||||
@ -35,5 +35,13 @@ serde_json.workspace = true
|
|||||||
tokio = { workspace = true, features = ["net", "macros", "rt"] }
|
tokio = { workspace = true, features = ["net", "macros", "rt"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
std = [
|
||||||
|
"alloy-primitives/std",
|
||||||
|
"alloy-rlp/std",
|
||||||
|
"secp256k1?/std",
|
||||||
|
"serde_with/std",
|
||||||
|
"thiserror/std"
|
||||||
|
]
|
||||||
secp256k1 = ["dep:secp256k1", "enr/secp256k1"]
|
secp256k1 = ["dep:secp256k1", "enr/secp256k1"]
|
||||||
net = ["dep:tokio", "tokio?/net"]
|
net = ["dep:tokio", "tokio?/net"]
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
//! Bootnodes for the network
|
//! Bootnodes for the network
|
||||||
|
|
||||||
use crate::NodeRecord;
|
use crate::NodeRecord;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
mod ethereum;
|
mod ethereum;
|
||||||
pub use ethereum::*;
|
pub use ethereum::*;
|
||||||
|
|||||||
@ -52,9 +52,16 @@
|
|||||||
)]
|
)]
|
||||||
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
|
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
|
||||||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
|
#![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 alloy_primitives::B512;
|
||||||
use std::str::FromStr;
|
use core::str::FromStr;
|
||||||
|
|
||||||
// Re-export PeerId for ease of use.
|
// Re-export PeerId for ease of use.
|
||||||
pub use enr::Enr;
|
pub use enr::Enr;
|
||||||
@ -137,8 +144,8 @@ impl AnyNode {
|
|||||||
let node_record = NodeRecord {
|
let node_record = NodeRecord {
|
||||||
address: enr
|
address: enr
|
||||||
.ip4()
|
.ip4()
|
||||||
.map(std::net::IpAddr::from)
|
.map(core::net::IpAddr::from)
|
||||||
.or_else(|| enr.ip6().map(std::net::IpAddr::from))?,
|
.or_else(|| enr.ip6().map(core::net::IpAddr::from))?,
|
||||||
tcp_port: enr.tcp4().or_else(|| enr.tcp6())?,
|
tcp_port: enr.tcp4().or_else(|| enr.tcp6())?,
|
||||||
udp_port: enr.udp4().or_else(|| enr.udp6())?,
|
udp_port: enr.udp4().or_else(|| enr.udp6())?,
|
||||||
id: pk2id(&enr.public_key()),
|
id: pk2id(&enr.public_key()),
|
||||||
@ -186,8 +193,8 @@ impl FromStr for AnyNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for AnyNode {
|
impl core::fmt::Display for AnyNode {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::NodeRecord(record) => write!(f, "{record}"),
|
Self::NodeRecord(record) => write!(f, "{record}"),
|
||||||
#[cfg(feature = "secp256k1")]
|
#[cfg(feature = "secp256k1")]
|
||||||
|
|||||||
@ -1,15 +1,18 @@
|
|||||||
//! Commonly used `NodeRecord` type for peers.
|
//! 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,
|
||||||
fmt::Write,
|
fmt::Write,
|
||||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||||
num::ParseIntError,
|
num::ParseIntError,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::PeerId;
|
|
||||||
use alloy_rlp::{RlpDecodable, RlpEncodable};
|
|
||||||
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||||
|
|
||||||
#[cfg(feature = "secp256k1")]
|
#[cfg(feature = "secp256k1")]
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
//! `NodeRecord` type that uses a domain instead of an IP.
|
//! `NodeRecord` type that uses a domain instead of an IP.
|
||||||
|
|
||||||
use crate::{NodeRecord, PeerId};
|
use crate::{NodeRecord, PeerId};
|
||||||
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
use alloc::string::{String, ToString};
|
||||||
use std::{
|
use core::{
|
||||||
fmt::{self, Write},
|
fmt::{self, Write},
|
||||||
io::Error,
|
|
||||||
net::IpAddr,
|
net::IpAddr,
|
||||||
num::ParseIntError,
|
num::ParseIntError,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||||
use url::Host;
|
use url::Host;
|
||||||
|
|
||||||
/// Represents the node record of a trusted peer. The only difference between this and a
|
/// 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 }
|
Self { host, tcp_port: port, udp_port: port, id }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(test, feature = "std"))]
|
||||||
const fn to_node_record(&self, ip: IpAddr) -> NodeRecord {
|
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 }
|
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.
|
/// 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> {
|
fn try_node_record(&self) -> Result<NodeRecord, &str> {
|
||||||
match &self.host {
|
match &self.host {
|
||||||
Host::Ipv4(ip) => Ok(self.to_node_record((*ip).into())),
|
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`].
|
/// 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.
|
/// 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() {
|
let domain = match self.try_node_record() {
|
||||||
Ok(record) => return Ok(record),
|
Ok(record) => return Ok(record),
|
||||||
Err(domain) => domain,
|
Err(domain) => domain,
|
||||||
};
|
};
|
||||||
// Resolve the domain to an IP address
|
// Resolve the domain to an IP address
|
||||||
let mut ips = std::net::ToSocketAddrs::to_socket_addrs(&(domain, 0))?;
|
let mut ips = std::net::ToSocketAddrs::to_socket_addrs(&(domain, 0))?;
|
||||||
let ip = ips
|
let ip = ips.next().ok_or_else(|| {
|
||||||
.next()
|
std::io::Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found")
|
||||||
.ok_or_else(|| Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found"))?;
|
})?;
|
||||||
|
|
||||||
Ok(self.to_node_record(ip.ip()))
|
Ok(self.to_node_record(ip.ip()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves the host in a [`TrustedPeer`] to an IP address, returning a [`NodeRecord`].
|
/// Resolves the host in a [`TrustedPeer`] to an IP address, returning a [`NodeRecord`].
|
||||||
#[cfg(any(test, feature = "net"))]
|
#[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() {
|
let domain = match self.try_node_record() {
|
||||||
Ok(record) => return Ok(record),
|
Ok(record) => return Ok(record),
|
||||||
Err(domain) => domain,
|
Err(domain) => domain,
|
||||||
@ -85,9 +88,9 @@ impl TrustedPeer {
|
|||||||
|
|
||||||
// Resolve the domain to an IP address
|
// Resolve the domain to an IP address
|
||||||
let mut ips = tokio::net::lookup_host(format!("{domain}:0")).await?;
|
let mut ips = tokio::net::lookup_host(format!("{domain}:0")).await?;
|
||||||
let ip = ips
|
let ip = ips.next().ok_or_else(|| {
|
||||||
.next()
|
std::io::Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found")
|
||||||
.ok_or_else(|| Error::new(std::io::ErrorKind::AddrNotAvailable, "No IP found"))?;
|
})?;
|
||||||
|
|
||||||
Ok(self.to_node_record(ip.ip()))
|
Ok(self.to_node_record(ip.ip()))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -57,5 +57,6 @@ std = [
|
|||||||
"reth-optimism-forks/std",
|
"reth-optimism-forks/std",
|
||||||
"alloy-consensus/std",
|
"alloy-consensus/std",
|
||||||
"once_cell/std",
|
"once_cell/std",
|
||||||
"derive_more/std"
|
"derive_more/std",
|
||||||
|
"reth-network-peers/std"
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user