mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
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:
@@ -13,13 +13,19 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
||||
.settings(&[clap::AppSettings::ColoredHelp])
|
||||
.arg(
|
||||
Arg::with_name("enr-address")
|
||||
.value_name("IP-ADDRESS")
|
||||
.help("The external IP address/ DNS address to broadcast to other peers on how to reach this node. \
|
||||
If a DNS address is provided, the enr-address is set to the IP address it resolves to and \
|
||||
does not auto-update based on PONG responses in discovery.")
|
||||
.long("enr-address")
|
||||
.value_name("ADDRESS")
|
||||
.help("The IP address/ DNS address to broadcast to other peers on how to reach \
|
||||
this node. If a DNS address is provided, the enr-address is set to the IP \
|
||||
address it resolves to and does not auto-update based on PONG responses in \
|
||||
discovery. Set this only if you are sure other nodes can connect to your \
|
||||
local node on this address. This will update the `ip4` or `ip6` ENR fields \
|
||||
accordingly. To update both, set this flag twice with the different values.")
|
||||
.multiple(true)
|
||||
.max_values(2)
|
||||
.required(true)
|
||||
.takes_value(true)
|
||||
.conflicts_with("network-dir")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("port")
|
||||
@@ -29,11 +35,29 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
||||
.default_value("9000")
|
||||
.takes_value(true)
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("port6")
|
||||
.long("port6")
|
||||
.value_name("PORT")
|
||||
.help("The UDP port to listen on over IpV6 when listening over both Ipv4 and \
|
||||
Ipv6. Defaults to 9090 when required.")
|
||||
.default_value("9090")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("listen-address")
|
||||
.long("listen-address")
|
||||
.value_name("ADDRESS")
|
||||
.help("The address the bootnode will listen for UDP connections.")
|
||||
.help("The address the bootnode will listen for UDP communications. To listen \
|
||||
over IpV4 and IpV6 set this flag twice with the different values.\n\
|
||||
Examples:\n\
|
||||
- --listen-address '0.0.0.0' will listen over Ipv4.\n\
|
||||
- --listen-address '::' will listen over Ipv6.\n\
|
||||
- --listen-address '0.0.0.0' --listen-address '::' will listen over both \
|
||||
Ipv4 and Ipv6. The order of the given addresses is not relevant. However, \
|
||||
multiple Ipv4, or multiple Ipv6 addresses will not be accepted.")
|
||||
.multiple(true)
|
||||
.max_values(2)
|
||||
.default_value("0.0.0.0")
|
||||
.takes_value(true)
|
||||
)
|
||||
@@ -59,6 +83,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
||||
.value_name("PORT")
|
||||
.help("The UDP6 port of the local ENR. Set this only if you are sure other nodes \
|
||||
can connect to your local node on this port over IpV6.")
|
||||
.conflicts_with("network-dir")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -10,7 +10,6 @@ use types::EthSpec;
|
||||
|
||||
pub async fn run<T: EthSpec>(config: BootNodeConfig<T>, log: slog::Logger) {
|
||||
let BootNodeConfig {
|
||||
listen_socket,
|
||||
boot_nodes,
|
||||
local_enr,
|
||||
local_key,
|
||||
@@ -31,7 +30,7 @@ pub async fn run<T: EthSpec>(config: BootNodeConfig<T>, log: slog::Logger) {
|
||||
let pretty_v6_socket = enr_v6_socket.as_ref().map(|addr| addr.to_string());
|
||||
info!(
|
||||
log, "Configuration parameters";
|
||||
"listening_address" => %listen_socket,
|
||||
"listening_address" => ?discv5_config.listen_config,
|
||||
"advertised_v4_address" => ?pretty_v4_socket,
|
||||
"advertised_v6_address" => ?pretty_v6_socket,
|
||||
"eth2" => eth2_field
|
||||
@@ -41,6 +40,7 @@ pub async fn run<T: EthSpec>(config: BootNodeConfig<T>, log: slog::Logger) {
|
||||
|
||||
// build the contactable multiaddr list, adding the p2p protocol
|
||||
info!(log, "Contact information"; "enr" => local_enr.to_base64());
|
||||
info!(log, "Enr details"; "enr" => ?local_enr);
|
||||
info!(log, "Contact information"; "multiaddrs" => ?local_enr.multiaddr_p2p());
|
||||
|
||||
// construct the discv5 server
|
||||
@@ -64,7 +64,7 @@ pub async fn run<T: EthSpec>(config: BootNodeConfig<T>, log: slog::Logger) {
|
||||
}
|
||||
|
||||
// start the server
|
||||
if let Err(e) = discv5.start(listen_socket).await {
|
||||
if let Err(e) = discv5.start().await {
|
||||
slog::crit!(log, "Could not start discv5 server"; "error" => %e);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user