use std::{
collections::HashSet,
fmt::Debug,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
};
use alloy_primitives::Bytes;
use derive_more::Display;
use discv5::{
multiaddr::{Multiaddr, Protocol},
ListenConfig,
};
use reth_ethereum_forks::{EnrForkIdEntry, ForkId};
use reth_network_peers::NodeRecord;
use tracing::warn;
use crate::{enr::discv4_id_to_multiaddr_id, filter::MustNotIncludeKeys, NetworkStackId};
pub const DEFAULT_DISCOVERY_V5_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED;
pub const DEFAULT_DISCOVERY_V5_ADDR_IPV6: Ipv6Addr = Ipv6Addr::UNSPECIFIED;
pub const DEFAULT_DISCOVERY_V5_PORT: u16 = 9200;
pub const DEFAULT_DISCOVERY_V5_LISTEN_CONFIG: ListenConfig =
ListenConfig::Ipv4 { ip: DEFAULT_DISCOVERY_V5_ADDR, port: DEFAULT_DISCOVERY_V5_PORT };
pub const DEFAULT_SECONDS_LOOKUP_INTERVAL: u64 = 60;
pub const DEFAULT_COUNT_BOOTSTRAP_LOOKUPS: u64 = 100;
pub const DEFAULT_SECONDS_BOOTSTRAP_LOOKUP_INTERVAL: u64 = 5;
#[derive(Debug)]
pub struct ConfigBuilder {
discv5_config: Option<discv5::Config>,
bootstrap_nodes: HashSet<BootNode>,
fork: Option<(&'static [u8], ForkId)>,
tcp_socket: SocketAddr,
other_enr_kv_pairs: Vec<(&'static [u8], Bytes)>,
lookup_interval: Option<u64>,
bootstrap_lookup_interval: Option<u64>,
bootstrap_lookup_countdown: Option<u64>,
discovered_peer_filter: Option<MustNotIncludeKeys>,
}
impl ConfigBuilder {
pub fn new_from(discv5_config: Config) -> Self {
let Config {
discv5_config,
bootstrap_nodes,
fork,
tcp_socket,
other_enr_kv_pairs,
lookup_interval,
bootstrap_lookup_interval,
bootstrap_lookup_countdown,
discovered_peer_filter,
} = discv5_config;
Self {
discv5_config: Some(discv5_config),
bootstrap_nodes,
fork: fork.map(|(key, fork_id)| (key, fork_id.fork_id)),
tcp_socket,
other_enr_kv_pairs,
lookup_interval: Some(lookup_interval),
bootstrap_lookup_interval: Some(bootstrap_lookup_interval),
bootstrap_lookup_countdown: Some(bootstrap_lookup_countdown),
discovered_peer_filter: Some(discovered_peer_filter),
}
}
pub fn discv5_config(mut self, discv5_config: discv5::Config) -> Self {
self.discv5_config = Some(discv5_config);
self
}
pub fn add_signed_boot_nodes(mut self, nodes: impl IntoIterator<Item = discv5::Enr>) -> Self {
self.bootstrap_nodes.extend(nodes.into_iter().map(BootNode::Enr));
self
}
pub fn add_cl_serialized_signed_boot_nodes(mut self, enrs: &str) -> Self {
let bootstrap_nodes = &mut self.bootstrap_nodes;
for node in enrs.split(&[',']).flat_map(|record| record.trim().parse::<discv5::Enr>()) {
bootstrap_nodes.insert(BootNode::Enr(node));
}
self
}
pub fn add_unsigned_boot_nodes(mut self, enodes: impl IntoIterator<Item = NodeRecord>) -> Self {
for node in enodes {
if let Ok(node) = BootNode::from_unsigned(node) {
self.bootstrap_nodes.insert(node);
}
}
self
}
pub fn add_serialized_unsigned_boot_nodes(mut self, enodes: &[&str]) -> Self {
for node in enodes {
if let Ok(node) = node.parse() {
if let Ok(node) = BootNode::from_unsigned(node) {
self.bootstrap_nodes.insert(node);
}
}
}
self
}
pub const fn fork(mut self, fork_key: &'static [u8], fork_id: ForkId) -> Self {
self.fork = Some((fork_key, fork_id));
self
}
pub const fn tcp_socket(mut self, socket: SocketAddr) -> Self {
self.tcp_socket = socket;
self
}
pub fn add_enr_kv_pair(mut self, key: &'static [u8], value: Bytes) -> Self {
self.other_enr_kv_pairs.push((key, value));
self
}
pub const fn lookup_interval(mut self, seconds: u64) -> Self {
self.lookup_interval = Some(seconds);
self
}
pub const fn bootstrap_lookup_interval(mut self, seconds: u64) -> Self {
self.bootstrap_lookup_interval = Some(seconds);
self
}
pub const fn bootstrap_lookup_countdown(mut self, counts: u64) -> Self {
self.bootstrap_lookup_countdown = Some(counts);
self
}
pub fn must_not_include_keys(mut self, not_keys: &[&'static [u8]]) -> Self {
let mut filter = self.discovered_peer_filter.unwrap_or_default();
filter.add_disallowed_keys(not_keys);
self.discovered_peer_filter = Some(filter);
self
}
pub fn build(self) -> Config {
let Self {
discv5_config,
bootstrap_nodes,
fork,
tcp_socket,
other_enr_kv_pairs,
lookup_interval,
bootstrap_lookup_interval,
bootstrap_lookup_countdown,
discovered_peer_filter,
} = self;
let mut discv5_config = discv5_config.unwrap_or_else(|| {
discv5::ConfigBuilder::new(DEFAULT_DISCOVERY_V5_LISTEN_CONFIG).build()
});
discv5_config.listen_config =
amend_listen_config_wrt_rlpx(&discv5_config.listen_config, tcp_socket.ip());
let fork = fork.map(|(key, fork_id)| (key, fork_id.into()));
let lookup_interval = lookup_interval.unwrap_or(DEFAULT_SECONDS_LOOKUP_INTERVAL);
let bootstrap_lookup_interval =
bootstrap_lookup_interval.unwrap_or(DEFAULT_SECONDS_BOOTSTRAP_LOOKUP_INTERVAL);
let bootstrap_lookup_countdown =
bootstrap_lookup_countdown.unwrap_or(DEFAULT_COUNT_BOOTSTRAP_LOOKUPS);
let discovered_peer_filter = discovered_peer_filter
.unwrap_or_else(|| MustNotIncludeKeys::new(&[NetworkStackId::ETH2]));
Config {
discv5_config,
bootstrap_nodes,
fork,
tcp_socket,
other_enr_kv_pairs,
lookup_interval,
bootstrap_lookup_interval,
bootstrap_lookup_countdown,
discovered_peer_filter,
}
}
}
#[derive(Clone, Debug)]
pub struct Config {
pub(super) discv5_config: discv5::Config,
pub(super) bootstrap_nodes: HashSet<BootNode>,
pub(super) fork: Option<(&'static [u8], EnrForkIdEntry)>,
pub(super) tcp_socket: SocketAddr,
pub(super) other_enr_kv_pairs: Vec<(&'static [u8], Bytes)>,
pub(super) lookup_interval: u64,
pub(super) bootstrap_lookup_interval: u64,
pub(super) bootstrap_lookup_countdown: u64,
pub(super) discovered_peer_filter: MustNotIncludeKeys,
}
impl Config {
pub fn builder(rlpx_tcp_socket: SocketAddr) -> ConfigBuilder {
ConfigBuilder {
discv5_config: None,
bootstrap_nodes: HashSet::default(),
fork: None,
tcp_socket: rlpx_tcp_socket,
other_enr_kv_pairs: Vec::new(),
lookup_interval: None,
bootstrap_lookup_interval: None,
bootstrap_lookup_countdown: None,
discovered_peer_filter: None,
}
}
pub fn insert_boot_node(&mut self, boot_node: BootNode) {
self.bootstrap_nodes.insert(boot_node);
}
pub fn insert_unsigned_boot_node(&mut self, node_record: NodeRecord) {
let _ = BootNode::from_unsigned(node_record).map(|node| self.insert_boot_node(node));
}
pub fn extend_unsigned_boot_nodes(
&mut self,
node_records: impl IntoIterator<Item = NodeRecord>,
) {
for node_record in node_records {
self.insert_unsigned_boot_node(node_record);
}
}
pub fn discovery_socket(&self) -> SocketAddr {
match self.discv5_config.listen_config {
ListenConfig::Ipv4 { ip, port } => (ip, port).into(),
ListenConfig::Ipv6 { ip, port } => (ip, port).into(),
ListenConfig::DualStack { ipv6, ipv6_port, .. } => (ipv6, ipv6_port).into(),
}
}
pub const fn rlpx_socket(&self) -> &SocketAddr {
&self.tcp_socket
}
}
pub const fn ipv4(listen_config: &ListenConfig) -> Option<SocketAddrV4> {
match listen_config {
ListenConfig::Ipv4 { ip, port } |
ListenConfig::DualStack { ipv4: ip, ipv4_port: port, .. } => {
Some(SocketAddrV4::new(*ip, *port))
}
ListenConfig::Ipv6 { .. } => None,
}
}
pub const fn ipv6(listen_config: &ListenConfig) -> Option<SocketAddrV6> {
match listen_config {
ListenConfig::Ipv4 { .. } => None,
ListenConfig::Ipv6 { ip, port } |
ListenConfig::DualStack { ipv6: ip, ipv6_port: port, .. } => {
Some(SocketAddrV6::new(*ip, *port, 0, 0))
}
}
}
pub fn amend_listen_config_wrt_rlpx(
listen_config: &ListenConfig,
rlpx_addr: IpAddr,
) -> ListenConfig {
let discv5_socket_ipv4 = ipv4(listen_config);
let discv5_socket_ipv6 = ipv6(listen_config);
let discv5_port_ipv4 =
discv5_socket_ipv4.map(|socket| socket.port()).unwrap_or(DEFAULT_DISCOVERY_V5_PORT);
let discv5_addr_ipv4 = discv5_socket_ipv4.map(|socket| *socket.ip());
let discv5_port_ipv6 =
discv5_socket_ipv6.map(|socket| socket.port()).unwrap_or(DEFAULT_DISCOVERY_V5_PORT);
let discv5_addr_ipv6 = discv5_socket_ipv6.map(|socket| *socket.ip());
let (discv5_socket_ipv4, discv5_socket_ipv6) = discv5_sockets_wrt_rlpx_addr(
rlpx_addr,
discv5_addr_ipv4,
discv5_port_ipv4,
discv5_addr_ipv6,
discv5_port_ipv6,
);
ListenConfig::from_two_sockets(discv5_socket_ipv4, discv5_socket_ipv6)
}
pub fn discv5_sockets_wrt_rlpx_addr(
rlpx_addr: IpAddr,
discv5_addr_ipv4: Option<Ipv4Addr>,
discv5_port_ipv4: u16,
discv5_addr_ipv6: Option<Ipv6Addr>,
discv5_port_ipv6: u16,
) -> (Option<SocketAddrV4>, Option<SocketAddrV6>) {
match rlpx_addr {
IpAddr::V4(rlpx_addr) => {
let discv5_socket_ipv6 =
discv5_addr_ipv6.map(|ip| SocketAddrV6::new(ip, discv5_port_ipv6, 0, 0));
if let Some(discv5_addr) = discv5_addr_ipv4 {
if discv5_addr != rlpx_addr {
warn!(target: "net::discv5",
%discv5_addr,
%rlpx_addr,
"Overwriting discv5 IPv4 address with RLPx IPv4 address, limited to one advertised IP address per IP version"
);
}
}
(Some(SocketAddrV4::new(rlpx_addr, discv5_port_ipv4)), discv5_socket_ipv6)
}
IpAddr::V6(rlpx_addr) => {
let discv5_socket_ipv4 =
discv5_addr_ipv4.map(|ip| SocketAddrV4::new(ip, discv5_port_ipv4));
if let Some(discv5_addr) = discv5_addr_ipv6 {
if discv5_addr != rlpx_addr {
warn!(target: "net::discv5",
%discv5_addr,
%rlpx_addr,
"Overwriting discv5 IPv6 address with RLPx IPv6 address, limited to one advertised IP address per IP version"
);
}
}
(discv5_socket_ipv4, Some(SocketAddrV6::new(rlpx_addr, discv5_port_ipv6, 0, 0)))
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Display)]
pub enum BootNode {
#[display("{_0}")]
Enode(Multiaddr),
#[display("{_0:?}")]
Enr(discv5::Enr),
}
impl BootNode {
pub fn from_unsigned(node_record: NodeRecord) -> Result<Self, secp256k1::Error> {
let NodeRecord { address, udp_port, id, .. } = node_record;
let mut multi_address = Multiaddr::empty();
match address {
IpAddr::V4(ip) => multi_address.push(Protocol::Ip4(ip)),
IpAddr::V6(ip) => multi_address.push(Protocol::Ip6(ip)),
}
multi_address.push(Protocol::Udp(udp_port));
let id = discv4_id_to_multiaddr_id(id)?;
multi_address.push(Protocol::P2p(id));
Ok(Self::Enode(multi_address))
}
}
#[cfg(test)]
mod test {
use super::*;
use alloy_primitives::hex;
use std::net::SocketAddrV4;
const MULTI_ADDRESSES: &str = "/ip4/184.72.129.189/udp/30301/p2p/16Uiu2HAmSG2hdLwyQHQmG4bcJBgD64xnW63WMTLcrNq6KoZREfGb,/ip4/3.231.11.52/udp/30301/p2p/16Uiu2HAmMy4V8bi3XP7KDfSLQcLACSvTLroRRwEsTyFUKo8NCkkp,/ip4/54.198.153.150/udp/30301/p2p/16Uiu2HAmSVsb7MbRf1jg3Dvd6a3n5YNqKQwn1fqHCFgnbqCsFZKe,/ip4/3.220.145.177/udp/30301/p2p/16Uiu2HAm74pBDGdQ84XCZK27GRQbGFFwQ7RsSqsPwcGmCR3Cwn3B,/ip4/3.231.138.188/udp/30301/p2p/16Uiu2HAmMnTiJwgFtSVGV14ZNpwAvS1LUoF4pWWeNtURuV6C3zYB";
const BOOT_NODES_OP_MAINNET_AND_BASE_MAINNET: &[&str] = &[
"enode://ca2774c3c401325850b2477fd7d0f27911efbf79b1e8b335066516e2bd8c4c9e0ba9696a94b1cb030a88eac582305ff55e905e64fb77fe0edcd70a4e5296d3ec@34.65.175.185:30305",
"enode://dd751a9ef8912be1bfa7a5e34e2c3785cc5253110bd929f385e07ba7ac19929fb0e0c5d93f77827291f4da02b2232240fbc47ea7ce04c46e333e452f8656b667@34.65.107.0:30305",
"enode://c5d289b56a77b6a2342ca29956dfd07aadf45364dde8ab20d1dc4efd4d1bc6b4655d902501daea308f4d8950737a4e93a4dfedd17b49cd5760ffd127837ca965@34.65.202.239:30305",
"enode://87a32fd13bd596b2ffca97020e31aef4ddcc1bbd4b95bb633d16c1329f654f34049ed240a36b449fda5e5225d70fe40bc667f53c304b71f8e68fc9d448690b51@3.231.138.188:30301",
"enode://ca21ea8f176adb2e229ce2d700830c844af0ea941a1d8152a9513b966fe525e809c3a6c73a2c18a12b74ed6ec4380edf91662778fe0b79f6a591236e49e176f9@184.72.129.189:30301",
"enode://acf4507a211ba7c1e52cdf4eef62cdc3c32e7c9c47998954f7ba024026f9a6b2150cd3f0b734d9c78e507ab70d59ba61dfe5c45e1078c7ad0775fb251d7735a2@3.220.145.177:30301",
"enode://8a5a5006159bf079d06a04e5eceab2a1ce6e0f721875b2a9c96905336219dbe14203d38f70f3754686a6324f786c2f9852d8c0dd3adac2d080f4db35efc678c5@3.231.11.52:30301",
"enode://cdadbe835308ad3557f9a1de8db411da1a260a98f8421d62da90e71da66e55e98aaa8e90aa7ce01b408a54e4bd2253d701218081ded3dbe5efbbc7b41d7cef79@54.198.153.150:30301"
];
#[test]
fn parse_boot_nodes() {
const OP_SEPOLIA_CL_BOOTNODES: &str ="enr:-J64QBwRIWAco7lv6jImSOjPU_W266lHXzpAS5YOh7WmgTyBZkgLgOwo_mxKJq3wz2XRbsoBItbv1dCyjIoNq67mFguGAYrTxM42gmlkgnY0gmlwhBLSsHKHb3BzdGFja4S0lAUAiXNlY3AyNTZrMaEDmoWSi8hcsRpQf2eJsNUx-sqv6fH4btmo2HsAzZFAKnKDdGNwgiQGg3VkcIIkBg,enr:-J64QFa3qMsONLGphfjEkeYyF6Jkil_jCuJmm7_a42ckZeUQGLVzrzstZNb1dgBp1GGx9bzImq5VxJLP-BaptZThGiWGAYrTytOvgmlkgnY0gmlwhGsV-zeHb3BzdGFja4S0lAUAiXNlY3AyNTZrMaEDahfSECTIS_cXyZ8IyNf4leANlZnrsMEWTkEYxf4GMCmDdGNwgiQGg3VkcIIkBg";
let config = Config::builder((Ipv4Addr::UNSPECIFIED, 30303).into())
.add_cl_serialized_signed_boot_nodes(OP_SEPOLIA_CL_BOOTNODES)
.build();
let socket_1 = "18.210.176.114:9222".parse::<SocketAddrV4>().unwrap();
let socket_2 = "107.21.251.55:9222".parse::<SocketAddrV4>().unwrap();
for node in config.bootstrap_nodes {
let BootNode::Enr(node) = node else { panic!() };
assert!(
socket_1 == node.udp4_socket().unwrap() && socket_1 == node.tcp4_socket().unwrap() ||
socket_2 == node.udp4_socket().unwrap() &&
socket_2 == node.tcp4_socket().unwrap()
);
assert_eq!("84b4940500", hex::encode(node.get_raw_rlp("opstack").unwrap()));
}
}
#[test]
fn parse_enodes() {
let config = Config::builder((Ipv4Addr::UNSPECIFIED, 30303).into())
.add_serialized_unsigned_boot_nodes(BOOT_NODES_OP_MAINNET_AND_BASE_MAINNET)
.build();
let bootstrap_nodes =
config.bootstrap_nodes.into_iter().map(|node| format!("{node}")).collect::<Vec<_>>();
for node in MULTI_ADDRESSES.split(&[',']) {
assert!(bootstrap_nodes.contains(&node.to_string()));
}
}
#[test]
fn overwrite_ipv4_addr() {
let rlpx_addr: Ipv4Addr = "192.168.0.1".parse().unwrap();
let listen_config = DEFAULT_DISCOVERY_V5_LISTEN_CONFIG;
let amended_config = amend_listen_config_wrt_rlpx(&listen_config, rlpx_addr.into());
let config_socket_ipv4 = ipv4(&amended_config).unwrap();
assert_eq!(*config_socket_ipv4.ip(), rlpx_addr);
assert_eq!(config_socket_ipv4.port(), DEFAULT_DISCOVERY_V5_PORT);
assert_eq!(ipv6(&amended_config), ipv6(&listen_config));
}
#[test]
fn overwrite_ipv6_addr() {
let rlpx_addr: Ipv6Addr = "fe80::1".parse().unwrap();
let listen_config = DEFAULT_DISCOVERY_V5_LISTEN_CONFIG;
let amended_config = amend_listen_config_wrt_rlpx(&listen_config, rlpx_addr.into());
let config_socket_ipv6 = ipv6(&amended_config).unwrap();
assert_eq!(*config_socket_ipv6.ip(), rlpx_addr);
assert_eq!(config_socket_ipv6.port(), DEFAULT_DISCOVERY_V5_PORT);
assert_eq!(ipv4(&amended_config), ipv4(&listen_config));
}
}