Skip to content

Commit

Permalink
use trusted peer struct for bootnodes
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymousGiga committed Jul 3, 2024
1 parent 4a4a65e commit 3d32c0e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 22 additions & 1 deletion crates/net/peers/src/node_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use std::{
str::FromStr,
};

use crate::PeerId;
use crate::{PeerId, TrustedPeer};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use serde_with::{DeserializeFromStr, SerializeDisplay};

#[cfg(feature = "secp256k1")]
use enr::Enr;
use url::Host;

/// Represents a ENR in discovery.
///
Expand Down Expand Up @@ -196,6 +197,26 @@ impl FromStr for NodeRecord {
}
}

impl From<TrustedPeer> for NodeRecord {
fn from(trust: TrustedPeer) -> Self {
use std::net::{SocketAddr, ToSocketAddrs};
let lookup_host = |host: &str| -> Option<SocketAddr> {
let mut addrs = host.to_socket_addrs().expect("No IP found");
addrs.next()
};

let address = match trust.host {
Host::Ipv4(ip) => IpAddr::V4(ip),
Host::Ipv6(ip) => IpAddr::V6(ip),
Host::Domain(domain) => {
let ip = lookup_host(&format!("{domain}:0")).expect("No IP found");
ip.ip()
}
};
Self { address, tcp_port: trust.tcp_port, udp_port: trust.udp_port, id: trust.id }
}
}

#[cfg(feature = "secp256k1")]
impl TryFrom<&Enr<secp256k1::SecretKey>> for NodeRecord {
type Error = NodeRecordParseError;
Expand Down
5 changes: 3 additions & 2 deletions crates/node/core/src/args/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct NetworkArgs {
///
/// Will fall back to a network-specific default if not specified.
#[arg(long, value_delimiter = ',')]
pub bootnodes: Option<Vec<NodeRecord>>,
pub bootnodes: Option<Vec<TrustedPeer>>,

/// Amount of DNS resolution requests retries to perform when peering.
#[arg(long, default_value_t = 0)]
Expand Down Expand Up @@ -130,9 +130,10 @@ impl NetworkArgs {
secret_key: SecretKey,
default_peers_file: PathBuf,
) -> NetworkConfigBuilder {
let chain_bootnodes = self
let chain_bootnodes: Vec<NodeRecord> = self
.bootnodes
.clone()
.map(|bootnodes| bootnodes.into_iter().map(Into::into).collect())
.unwrap_or_else(|| chain_spec.bootnodes().unwrap_or_else(mainnet_nodes));
let peers_file = self.peers_file.clone().unwrap_or(default_peers_file);

Expand Down

0 comments on commit 3d32c0e

Please sign in to comment.