Skip to content

Commit

Permalink
Add QUIC to default ENR builder and fight tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AgeManning committed Aug 10, 2023
1 parent c7c1749 commit 14b42c1
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 84 deletions.
23 changes: 15 additions & 8 deletions beacon_node/lighthouse_network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,24 @@ pub struct Config {
/// that no discovery address has been set in the CLI args.
pub enr_address: (Option<Ipv4Addr>, Option<Ipv6Addr>),

/// The disc4 port to broadcast to peers in order to reach back for discovery.
pub enr_disc4_port: Option<u16>,
/// The udp port to broadcast to peers in order to reach back for discovery.
pub enr_udp4_port: Option<u16>,

/// The UDP port to broadcast to peers in order to reach back for quic libp2p services.
pub enr_quic4_port: Option<u16>,

/// The tcp4 port to broadcast to peers in order to reach back for libp2p services.
pub enr_tcp4_port: Option<u16>,

/// The disc6 port to broadcast to peers in order to reach back for discovery.
pub enr_disc6_port: Option<u16>,
/// The udp6 port to broadcast to peers in order to reach back for discovery.
pub enr_udp6_port: Option<u16>,

/// The tcp6 port to broadcast to peers in order to reach back for libp2p services.
pub enr_tcp6_port: Option<u16>,

/// The UDP port to broadcast to peers in order to reach back for quic libp2p services.
pub enr_quic6_port: Option<u16>,

/// Target number of connected peers.
pub target_peers: usize,

Expand Down Expand Up @@ -333,10 +339,11 @@ impl Default for Config {
network_dir,
listen_addresses,
enr_address: (None, None),

enr_disc4_port: None,
enr_udp4_port: None,
enr_quic4_port: None,
enr_tcp4_port: None,
enr_disc6_port: None,
enr_udp6_port: None,
enr_quic6_port: None,
enr_tcp6_port: None,
target_peers: 50,
gs_config,
Expand Down Expand Up @@ -591,4 +598,4 @@ pub const fn is_global_ipv6(addr: &Ipv6Addr) -> bool {
|| is_documentation(addr)
|| is_unique_local(addr)
|| is_unicast_link_local(addr))
}
}
35 changes: 30 additions & 5 deletions beacon_node/lighthouse_network/src/discovery/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::path::Path;
use std::str::FromStr;
use types::{EnrForkId, EthSpec};

use super::enr_ext::{EnrExt, QUIC6_ENR_KEY, QUIC_ENR_KEY};

/// The ENR field specifying the fork id.
pub const ETH2_ENR_KEY: &str = "eth2";
/// The ENR field specifying the attestation subnet bitfield.
Expand Down Expand Up @@ -142,7 +144,7 @@ pub fn build_or_load_enr<T: EthSpec>(

pub fn create_enr_builder_from_config<T: EnrKey>(
config: &NetworkConfig,
enable_tcp: bool,
enable_libp2p: bool,
) -> EnrBuilder<T> {
let mut builder = EnrBuilder::new("v4");
let (maybe_ipv4_address, maybe_ipv6_address) = &config.enr_address;
Expand All @@ -155,15 +157,35 @@ pub fn create_enr_builder_from_config<T: EnrKey>(
builder.ip6(*ip);
}

if let Some(udp4_port) = config.enr_disc4_port {
if let Some(udp4_port) = config.enr_udp4_port {
builder.udp4(udp4_port);
}

if let Some(udp6_port) = config.enr_disc6_port {
if let Some(udp6_port) = config.enr_udp6_port {
builder.udp6(udp6_port);
}

if enable_tcp {
// Add QUIC fields to the ENR.
// If `enable_libp2p` is disabled, then we should not support QUIC in the ENR either.
if enable_libp2p && !config.disable_quic_support {
// If we are listening on ipv4, add the quic ipv4 port
if let Some(quic4_port) = config
.enr_quic4_port
.or_else(|| config.listen_addrs().v4().map(|v4_addr| v4_addr.quic_port))
{
builder.add_value(QUIC_ENR_KEY, &quic4_port);
}

// If we are listening on ipv6, add the quic ipv6 port
if let Some(quic6_port) = config
.enr_quic6_port
.or_else(|| config.listen_addrs().v6().map(|v6_addr| v6_addr.quic_port))
{
builder.add_value(QUIC6_ENR_KEY, &quic6_port);
}
}

if enable_libp2p {
// If the ENR port is not set, and we are listening over that ip version, use the listening port instead.
let tcp4_port = config
.enr_tcp4_port
Expand Down Expand Up @@ -218,6 +240,9 @@ fn compare_enr(local_enr: &Enr, disk_enr: &Enr) -> bool {
// tcp ports must match
&& local_enr.tcp4() == disk_enr.tcp4()
&& local_enr.tcp6() == disk_enr.tcp6()
// quic ports must match
&& local_enr.quic() == disk_enr.quic()
&& local_enr.quic6() == disk_enr.quic6()
// must match on the same fork
&& local_enr.get(ETH2_ENR_KEY) == disk_enr.get(ETH2_ENR_KEY)
// take preference over disk udp port if one is not specified
Expand Down Expand Up @@ -258,4 +283,4 @@ pub fn save_enr_to_disk(dir: &Path, enr: &Enr, log: &slog::Logger) {
);
}
}
}
}
6 changes: 3 additions & 3 deletions beacon_node/lighthouse_network/src/discovery/enr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use libp2p::core::multiaddr::Protocol;
use libp2p::identity::{ed25519, secp256k1, KeyType, Keypair, PublicKey};
use tiny_keccak::{Hasher, Keccak};

const QUIC_ENR_KEY: &str = "quic";
const QUIC6_ENR_KEY: &str = "quic6";
pub const QUIC_ENR_KEY: &str = "quic";
pub const QUIC6_ENR_KEY: &str = "quic6";

/// Extend ENR for libp2p types.
pub trait EnrExt {
Expand Down Expand Up @@ -397,4 +397,4 @@ mod tests {

assert_eq!(enr.node_id(), node_id);
}
}
}
23 changes: 14 additions & 9 deletions beacon_node/lighthouse_network/src/listen_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,31 @@ impl ListenAddress {

/// Returns the addresses the Swarm will listen on, given the setup.
pub fn listen_addresses(&self) -> impl Iterator<Item = Multiaddr> {
let v4_tcp_multiaddrs = self
let v4_tcp_multiaddr = self
.v4()
.map(|v4_addr| Multiaddr::from(v4_addr.addr).with(Protocol::Tcp(v4_addr.tcp_port)));

let v4_quic_multiaddrs = self.v4().map(|v4_addr| {
let v4_quic_multiaddr = self.v4().map(|v4_addr| {
Multiaddr::from(v4_addr.addr)
.with(Protocol::Udp(v4_addr.quic_port))
.with(Protocol::QuicV1)
});

let v6_tcp_multiaddrs = self
let v6_quic_multiaddr = self.v6().map(|v6_addr| {
Multiaddr::from(v6_addr.addr)
.with(Protocol::Udp(v6_addr.quic_port))
.with(Protocol::QuicV1)
});

let v6_tcp_multiaddr = self
.v6()
.map(|v6_addr| Multiaddr::from(v6_addr.addr).with(Protocol::Tcp(v6_addr.tcp_port)));

// TODO: Add QUIC IPv6 multiaddr once it is supported

v4_tcp_multiaddrs
v4_tcp_multiaddr
.into_iter()
.chain(v4_quic_multiaddrs)
.chain(v6_tcp_multiaddrs)
.chain(v4_quic_multiaddr)
.chain(v6_quic_multiaddr)
.chain(v6_tcp_multiaddr)
}

#[cfg(test)]
Expand Down Expand Up @@ -123,4 +128,4 @@ impl slog::KV for ListenAddress {
}
slog::Result::Ok(())
}
}
}
26 changes: 21 additions & 5 deletions beacon_node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,39 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long("enr-udp-port")
.value_name("PORT")
.help("The UDP4 port of the local ENR. Set this only if you are sure other nodes \
can connect to your local node on this port over IpV4.")
can connect to your local node on this port over IPv4.")
.takes_value(true),
)
.arg(
Arg::with_name("enr-quic-port")
.long("enr-quic-port")
.value_name("PORT")
.help("The quic UDP4 port that will be set on the local ENR. Set this only if you are sure other nodes \
can connect to your local node on this port over IPv4.")
.takes_value(true),
)
.arg(
Arg::with_name("enr-udp6-port")
.long("enr-udp6-port")
.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.")
can connect to your local node on this port over IPv6.")
.takes_value(true),
)
.arg(
Arg::with_name("enr-quic6-port")
.long("enr-quic6-port")
.value_name("PORT")
.help("The quic UDP6 port that will be set on the local ENR. Set this only if you are sure other nodes \
can connect to your local node on this port over IPv6.")
.takes_value(true),
)
.arg(
Arg::with_name("enr-tcp-port")
.long("enr-tcp-port")
.value_name("PORT")
.help("The TCP4 port of the local ENR. Set this only if you are sure other nodes \
can connect to your local node on this port over IpV4. The --port flag is \
can connect to your local node on this port over IPv4. The --port flag is \
used if this is not set.")
.takes_value(true),
)
Expand All @@ -200,7 +216,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long("enr-tcp6-port")
.value_name("PORT")
.help("The TCP6 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. The --port6 flag is \
can connect to your local node on this port over IPv6. The --port6 flag is \
used if this is not set.")
.takes_value(true),
)
Expand Down Expand Up @@ -1166,4 +1182,4 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.takes_value(true)
.possible_values(ProgressiveBalancesMode::VARIANTS)
)
}
}
41 changes: 27 additions & 14 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn get_config<E: EthSpec>(

let data_dir_ref = client_config.data_dir().clone();

println!("SET NETWORK CONFIG");
set_network_config(&mut client_config.network, cli_args, &data_dir_ref, log)?;

/*
Expand Down Expand Up @@ -916,13 +917,21 @@ pub fn set_network_config(
}

if let Some(enr_udp_port_str) = cli_args.value_of("enr-udp-port") {
config.enr_disc4_port = Some(
config.enr_udp4_port = Some(
enr_udp_port_str
.parse::<u16>()
.map_err(|_| format!("Invalid discovery port: {}", enr_udp_port_str))?,
);
}

if let Some(enr_quic_port_str) = cli_args.value_of("enr-quic-port") {
config.enr_quic4_port = Some(
enr_quic_port_str
.parse::<u16>()
.map_err(|_| format!("Invalid quic port: {}", enr_quic_port_str))?,
);
}

if let Some(enr_tcp_port_str) = cli_args.value_of("enr-tcp-port") {
config.enr_tcp4_port = Some(
enr_tcp_port_str
Expand All @@ -932,13 +941,21 @@ pub fn set_network_config(
}

if let Some(enr_udp_port_str) = cli_args.value_of("enr-udp6-port") {
config.enr_disc6_port = Some(
config.enr_udp6_port = Some(
enr_udp_port_str
.parse::<u16>()
.map_err(|_| format!("Invalid discovery port: {}", enr_udp_port_str))?,
);
}

if let Some(enr_quic_port_str) = cli_args.value_of("enr-quic6-port") {
config.enr_quic6_port = Some(
enr_quic_port_str
.parse::<u16>()
.map_err(|_| format!("Invalid quic port: {}", enr_quic_port_str))?,
);
}

if let Some(enr_tcp_port_str) = cli_args.value_of("enr-tcp6-port") {
config.enr_tcp6_port = Some(
enr_tcp_port_str
Expand All @@ -958,7 +975,7 @@ pub fn set_network_config(
ipv4_addr.addr
};
config.enr_address.0 = Some(ipv4_enr_addr);
config.enr_disc4_port = Some(ipv4_addr.disc_port);
config.enr_udp4_port = Some(ipv4_addr.disc_port);
}

if let Some(ipv6_addr) = config.listen_addrs().v6().cloned() {
Expand All @@ -968,7 +985,7 @@ pub fn set_network_config(
ipv6_addr.addr
};
config.enr_address.1 = Some(ipv6_enr_addr);
config.enr_disc6_port = Some(ipv6_addr.disc_port);
config.enr_udp6_port = Some(ipv6_addr.disc_port);
}
}

Expand Down Expand Up @@ -1132,6 +1149,7 @@ pub fn parse_listening_addresses(
cli_args: &ArgMatches,
log: &Logger,
) -> Result<ListenAddress, String> {
dbg!("PARSE");
let listen_addresses_str = cli_args
.values_of("listen-address")
.expect("--listen_addresses has a default value");
Expand Down Expand Up @@ -1215,6 +1233,8 @@ pub fn parse_listening_addresses(
format!("Failed to parse --quic6-port as an integer: {parse_error}")
})?;

println!("{:?}", maybe_disc6_port);
println!("{:?}", (maybe_ipv4, maybe_ipv6));
// Now put everything together
let listening_addresses = match (maybe_ipv4, maybe_ipv6) {
(None, None) => {
Expand All @@ -1237,18 +1257,9 @@ pub fn parse_listening_addresses(
warn!(log, "When listening only over IPv6, use the --discovery-port flag. The value of --discovery-port6 will be ignored.")
}

// TODO: Remove this warning once https://github.com/libp2p/rust-libp2p/issues/4165
// is resolved.
if maybe_quic_port.is_some() || maybe_quic6_port.is_some() {
warn!(log, "QUIC is currently disabled over Ipv6.")
}

// TODO: Once QUIC is supported over IPv6, uncomment this.
/*
if maybe_quic6_port.is_some() {
warn!(log, "When listening only over IPv6, use the --quic-port flag. The value of --quic-port6 will be ignored.")
}
*/

// use zero ports if required. If not, use the specific udp port. If none given, use
// the tcp port.
Expand Down Expand Up @@ -1322,11 +1333,13 @@ pub fn parse_listening_addresses(
.then(unused_port::unused_tcp6_port)
.transpose()?
.unwrap_or(port6);
println!("{:?}", maybe_disc6_port);
let ipv6_disc_port = use_zero_ports
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_disc6_port)
.unwrap_or(ipv6_tcp_port);
println!("{:?}", ipv6_disc_port);
let ipv6_quic_port = use_zero_ports
.then(unused_port::unused_udp6_port)
.transpose()?
Expand Down Expand Up @@ -1425,4 +1438,4 @@ where
.into_iter()
.next()
.ok_or(format!("Must provide at least one value to {}", flag_name))
}
}
Loading

0 comments on commit 14b42c1

Please sign in to comment.