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-trie-parallel # tokio
|
||||
reth-testing-utils
|
||||
reth-network-peers
|
||||
)
|
||||
|
||||
# Array to hold the results
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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"
|
||||
]
|
||||
|
||||
@ -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"]
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
//! Bootnodes for the network
|
||||
|
||||
use crate::NodeRecord;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
mod ethereum;
|
||||
pub use ethereum::*;
|
||||
|
||||
@ -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")]
|
||||
|
||||
@ -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")]
|
||||
|
||||
@ -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()))
|
||||
}
|
||||
|
||||
@ -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"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user