Update discv5 to expand ipv6 support (#4319)

Done in different PRs so that they can reviewed independently, as it's likely this won't be merged before I leave

Includes resolution for #4080 
- [ ] #4299
- [ ] #4318
- [ ] #4320 

Co-authored-by: Diva M <divma@protonmail.com>
Co-authored-by: Age Manning <Age@AgeManning.com>
This commit is contained in:
Divma
2023-06-13 01:25:05 +00:00
parent 62a2413ade
commit 2639e67e90
12 changed files with 425 additions and 170 deletions

View File

@@ -2,7 +2,6 @@ use beacon_node::{get_data_dir, set_network_config};
use clap::ArgMatches;
use eth2_network_config::Eth2NetworkConfig;
use lighthouse_network::discovery::create_enr_builder_from_config;
use lighthouse_network::discv5::IpMode;
use lighthouse_network::discv5::{enr::CombinedKey, Discv5Config, Enr};
use lighthouse_network::{
discovery::{load_enr_from_disk, use_or_load_enr},
@@ -10,13 +9,12 @@ use lighthouse_network::{
};
use serde_derive::{Deserialize, Serialize};
use ssz::Encode;
use std::net::SocketAddr;
use std::net::{SocketAddrV4, SocketAddrV6};
use std::{marker::PhantomData, path::PathBuf};
use types::EthSpec;
/// A set of configuration parameters for the bootnode, established from CLI arguments.
pub struct BootNodeConfig<T: EthSpec> {
pub listen_socket: SocketAddr,
// TODO: Generalise to multiaddr
pub boot_nodes: Vec<Enr>,
pub local_enr: Enr,
@@ -81,31 +79,6 @@ impl<T: EthSpec> BootNodeConfig<T> {
network_config.discv5_config.enr_update = false;
}
// the address to listen on
let listen_socket = match network_config.listen_addrs().clone() {
lighthouse_network::ListenAddress::V4(v4_addr) => {
// Set explicitly as ipv4 otherwise
network_config.discv5_config.ip_mode = IpMode::Ip4;
v4_addr.udp_socket_addr()
}
lighthouse_network::ListenAddress::V6(v6_addr) => {
// create ipv6 sockets and enable ipv4 mapped addresses.
network_config.discv5_config.ip_mode = IpMode::Ip6 {
enable_mapped_addresses: false,
};
v6_addr.udp_socket_addr()
}
lighthouse_network::ListenAddress::DualStack(_v4_addr, v6_addr) => {
// create ipv6 sockets and enable ipv4 mapped addresses.
network_config.discv5_config.ip_mode = IpMode::Ip6 {
enable_mapped_addresses: true,
};
v6_addr.udp_socket_addr()
}
};
let private_key = load_private_key(&network_config, &logger);
let local_key = CombinedKey::from_libp2p(&private_key)?;
@@ -143,7 +116,7 @@ impl<T: EthSpec> BootNodeConfig<T> {
let mut builder = create_enr_builder_from_config(&network_config, enable_tcp);
// If we know of the ENR field, add it to the initial construction
if let Some(enr_fork_bytes) = enr_fork {
builder.add_value("eth2", enr_fork_bytes.as_slice());
builder.add_value("eth2", &enr_fork_bytes);
}
builder
.build(&local_key)
@@ -155,7 +128,6 @@ impl<T: EthSpec> BootNodeConfig<T> {
};
Ok(BootNodeConfig {
listen_socket,
boot_nodes,
local_enr,
local_key,
@@ -170,7 +142,8 @@ impl<T: EthSpec> BootNodeConfig<T> {
/// Its fields are a subset of the fields of `BootNodeConfig`, some of them are copied from `Discv5Config`.
#[derive(Serialize, Deserialize)]
pub struct BootNodeConfigSerialization {
pub listen_socket: SocketAddr,
pub ipv4_listen_socket: Option<SocketAddrV4>,
pub ipv6_listen_socket: Option<SocketAddrV6>,
// TODO: Generalise to multiaddr
pub boot_nodes: Vec<Enr>,
pub local_enr: Enr,
@@ -183,7 +156,6 @@ impl BootNodeConfigSerialization {
/// relevant fields of `config`
pub fn from_config_ref<T: EthSpec>(config: &BootNodeConfig<T>) -> Self {
let BootNodeConfig {
listen_socket,
boot_nodes,
local_enr,
local_key: _,
@@ -191,8 +163,27 @@ impl BootNodeConfigSerialization {
phantom: _,
} = config;
let (ipv4_listen_socket, ipv6_listen_socket) = match discv5_config.listen_config {
lighthouse_network::discv5::ListenConfig::Ipv4 { ip, port } => {
(Some(SocketAddrV4::new(ip, port)), None)
}
lighthouse_network::discv5::ListenConfig::Ipv6 { ip, port } => {
(None, Some(SocketAddrV6::new(ip, port, 0, 0)))
}
lighthouse_network::discv5::ListenConfig::DualStack {
ipv4,
ipv4_port,
ipv6,
ipv6_port,
} => (
Some(SocketAddrV4::new(ipv4, ipv4_port)),
Some(SocketAddrV6::new(ipv6, ipv6_port, 0, 0)),
),
};
BootNodeConfigSerialization {
listen_socket: *listen_socket,
ipv4_listen_socket,
ipv6_listen_socket,
boot_nodes: boot_nodes.clone(),
local_enr: local_enr.clone(),
disable_packet_filter: !discv5_config.enable_packet_filter,