Skip to content

Commit

Permalink
or quic transport with tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Aug 2, 2023
1 parent 61d4ae1 commit 24fe1f2
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 63 deletions.
14 changes: 7 additions & 7 deletions beacon_node/lighthouse_network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ pub struct Config {
/// that no discovery address has been set in the CLI args.
pub enr_address: (Option<Ipv4Addr>, Option<Ipv6Addr>),

/// The udp4 port to broadcast to peers in order to reach back for discovery.
pub enr_udp4_port: Option<u16>,
/// The disc4 port to broadcast to peers in order to reach back for discovery.
pub enr_disc4_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 udp6 port to broadcast to peers in order to reach back for discovery.
pub enr_udp6_port: Option<u16>,
/// The disc6 port to broadcast to peers in order to reach back for discovery.
pub enr_disc6_port: Option<u16>,

/// The tcp6 port to broadcast to peers in order to reach back for libp2p services.
pub enr_tcp6_port: Option<u16>,
Expand Down Expand Up @@ -338,9 +338,9 @@ impl Default for Config {
listen_addresses,
enr_address: (None, None),

enr_udp4_port: None,
enr_disc4_port: None,
enr_tcp4_port: None,
enr_udp6_port: None,
enr_disc6_port: None,
enr_tcp6_port: None,
target_peers: 50,
gs_config,
Expand Down Expand Up @@ -588,4 +588,4 @@ pub const fn is_global_ipv6(addr: &Ipv6Addr) -> bool {
|| is_documentation(addr)
|| is_unique_local(addr)
|| is_unicast_link_local(addr))
}
}
4 changes: 2 additions & 2 deletions beacon_node/lighthouse_network/src/discovery/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ pub fn create_enr_builder_from_config<T: EnrKey>(
builder.ip6(*ip);
}

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

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

Expand Down
24 changes: 12 additions & 12 deletions beacon_node/lighthouse_network/src/service/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::types::{
error, EnrAttestationBitfield, EnrSyncCommitteeBitfield, GossipEncoding, GossipKind,
};
use crate::{GossipTopic, NetworkConfig};
use futures::future::Either;
use libp2p::bandwidth::BandwidthSinks;
use libp2p::core::{multiaddr::Multiaddr, muxing::StreamMuxerBox, transport::Boxed};
use libp2p::gossipsub;
Expand Down Expand Up @@ -51,25 +52,24 @@ pub fn build_transport(
// yamux config
let mut yamux_config = yamux::Config::default();
yamux_config.set_window_update_mode(yamux::WindowUpdateMode::on_read());
let (transport, bandwidth) = transport
let transport = transport
.upgrade(core::upgrade::Version::V1)
.authenticate(generate_noise_config(&local_private_key))
.multiplex(yamux_config)
.timeout(Duration::from_secs(10))
.with_bandwidth_logging();
.timeout(Duration::from_secs(10));

// Enables Quic
/*
// The default quic configuration suits us for now.
let quic_config = libp2p_quic::Config::new(&local_private_key);
let transport = transport.or_transport(libp2p_quic::tokio::Transport::new(quic_config));
// TODO: Get quick to support bandwidth measurements.
*/

let transport = transport.boxed();
let (transport, bandwidth) = transport
.or_transport(libp2p_quic::tokio::Transport::new(quic_config))
.map(|either_output, _| match either_output {
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
})
.with_bandwidth_logging();

Ok((transport, bandwidth))
Ok((transport.boxed(), bandwidth))
}

// Useful helper functions for debugging. Currently not used in the client.
Expand Down Expand Up @@ -274,4 +274,4 @@ pub(crate) fn save_metadata_to_disk<E: EthSpec>(
);
}
}
}
}
4 changes: 2 additions & 2 deletions beacon_node/lighthouse_network/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub fn build_config(port: u16, mut boot_nodes: Vec<Enr>) -> NetworkConfig {
.tempdir()
.unwrap();

config.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, port, port);
config.enr_udp4_port = Some(port);
config.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, port, port, port + 1);
config.enr_disc4_port = Some(port);
config.enr_address = (Some(std::net::Ipv4Addr::LOCALHOST), None);
config.boot_nodes_enr.append(&mut boot_nodes);
config.network_dir = path.into_path();
Expand Down
7 changes: 4 additions & 3 deletions beacon_node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
Arg::with_name("port")
.long("port")
.value_name("PORT")
.help("The TCP/UDP ports to listen on. There are two UDP ports. The discovery UDP port will be set to this value and the Quic UDP port will be set to his value + 1. The discovery port can be modified by the \
.help("The TCP/UDP ports to listen on. There are two UDP ports. \
The discovery UDP port will be set to this value and the Quic UDP port will be set to this value + 1. The discovery port can be modified by the \
--discovery-port flag and the quic port can be modified by the --quic-port flag. If listening over both IPv4 and IPv6 the --port flag \
will apply to the IPv4 address and --port6 to the IPv6 address.")
.default_value("9000")
Expand Down Expand Up @@ -246,7 +247,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
Arg::with_name("disable-discovery")
.long("disable-discovery")
.help("Disables the discv5 discovery protocol. The node will not search for new peers or participate in the discovery protocol.")
.takes_value(false),
.takes_value(false)
.hidden(true)
)
.arg(
Expand Down Expand Up @@ -1165,4 +1166,4 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.takes_value(true)
.possible_values(ProgressiveBalancesMode::VARIANTS)
)
}
}
92 changes: 68 additions & 24 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,22 +869,38 @@ pub fn parse_listening_addresses(
.map_err(|parse_error| format!("Failed to parse --port6 as an integer: {parse_error}"))?
.unwrap_or(9090);

// parse the possible udp ports
let maybe_udp_port = cli_args
// parse the possible discovery ports.
let maybe_disc_port = cli_args
.value_of("discovery-port")
.map(str::parse::<u16>)
.transpose()
.map_err(|parse_error| {
format!("Failed to parse --discovery-port as an integer: {parse_error}")
})?;
let maybe_udp6_port = cli_args
let maybe_disc6_port = cli_args
.value_of("discovery-port6")
.map(str::parse::<u16>)
.transpose()
.map_err(|parse_error| {
format!("Failed to parse --discovery-port6 as an integer: {parse_error}")
})?;

// parse the possible quic ports.
let maybe_quic_port = cli_args
.value_of("quic-port")
.map(str::parse::<u16>)
.transpose()
.map_err(|parse_error| {
format!("Failed to parse --quic-port as an integer: {parse_error}")
})?;
let maybe_quic6_port = cli_args
.value_of("quic-port6")
.map(str::parse::<u16>)
.transpose()
.map_err(|parse_error| {
format!("Failed to parse --quic-port6 as an integer: {parse_error}")
})?;

// Now put everything together
let listening_addresses = match (maybe_ipv4, maybe_ipv6) {
(None, None) => {
Expand All @@ -903,20 +919,26 @@ pub fn parse_listening_addresses(
.transpose()?
.unwrap_or(port);

if maybe_udp6_port.is_some() {
if maybe_disc6_port.is_some() {
warn!(log, "When listening only over IpV6, use the --discovery-port flag. The value of --discovery-port6 will be ignored.")
}
// use zero ports if required. If not, use the specific udp port. If none given, use
// the tcp port.
let udp_port = use_zero_ports
let disc_port = use_zero_ports
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_disc_port)
.unwrap_or(port);

let quic_port = use_zero_ports
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_udp_port)
.unwrap_or(port);

ListenAddress::V6(lighthouse_network::ListenAddr {
addr: ipv6,
udp_port,
quic_port,
disc_port,
tcp_port,
})
}
Expand All @@ -928,16 +950,25 @@ pub fn parse_listening_addresses(
.then(unused_port::unused_tcp4_port)
.transpose()?
.unwrap_or(port);
// use zero ports if required. If not, use the specific udp port. If none given, use
// use zero ports if required. If not, use the specific discovery port. If none given, use
// the tcp port.
let udp_port = use_zero_ports
let disc_port = use_zero_ports
.then(unused_port::unused_udp4_port)
.transpose()?
.or(maybe_udp_port)
.or(maybe_disc_port)
.unwrap_or(port);
// use zero ports if required. If not, use the specific quic port. If none given, use
// the tcp port + 1.
let quic_port = use_zero_ports
.then(unused_port::unused_udp4_port)
.transpose()?
.or(maybe_quic_port)
.unwrap_or(port + 1);

ListenAddress::V4(lighthouse_network::ListenAddr {
addr: ipv4,
udp_port,
disc_port,
quic_port,
tcp_port,
})
}
Expand All @@ -946,31 +977,44 @@ pub fn parse_listening_addresses(
.then(unused_port::unused_tcp4_port)
.transpose()?
.unwrap_or(port);
let ipv4_udp_port = use_zero_ports
let ipv4_disc_port = use_zero_ports
.then(unused_port::unused_udp4_port)
.transpose()?
.or(maybe_udp_port)
.or(maybe_disc_port)
.unwrap_or(ipv4_tcp_port);
let ipv4_quic_port = use_zero_ports
.then(unused_port::unused_udp4_port)
.transpose()?
.or(maybe_quic_port)
.unwrap_or(port + 1);

// Defaults to 9090 when required
let ipv6_tcp_port = use_zero_ports
.then(unused_port::unused_tcp6_port)
.transpose()?
.unwrap_or(port6);
let ipv6_udp_port = use_zero_ports
let ipv6_disc_port = use_zero_ports
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_udp6_port)
.or(maybe_disc6_port)
.unwrap_or(ipv6_tcp_port);
let ipv6_quic_port = use_zero_ports
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_quic6_port)
.unwrap_or(ipv6_tcp_port + 1);

ListenAddress::DualStack(
lighthouse_network::ListenAddr {
addr: ipv4,
udp_port: ipv4_udp_port,
disc_port: ipv4_disc_port,
quic_port: ipv4_quic_port,
tcp_port: ipv4_tcp_port,
},
lighthouse_network::ListenAddr {
addr: ipv6,
udp_port: ipv6_udp_port,
disc_port: ipv6_disc_port,
quic_port: ipv6_quic_port,
tcp_port: ipv6_tcp_port,
},
)
Expand Down Expand Up @@ -1079,7 +1123,7 @@ pub fn set_network_config(
}

if let Some(enr_udp_port_str) = cli_args.value_of("enr-udp-port") {
config.enr_udp4_port = Some(
config.enr_disc4_port = Some(
enr_udp_port_str
.parse::<u16>()
.map_err(|_| format!("Invalid discovery port: {}", enr_udp_port_str))?,
Expand All @@ -1095,7 +1139,7 @@ pub fn set_network_config(
}

if let Some(enr_udp_port_str) = cli_args.value_of("enr-udp6-port") {
config.enr_udp6_port = Some(
config.enr_disc6_port = Some(
enr_udp_port_str
.parse::<u16>()
.map_err(|_| format!("Invalid discovery port: {}", enr_udp_port_str))?,
Expand All @@ -1121,7 +1165,7 @@ pub fn set_network_config(
ipv4_addr.addr
};
config.enr_address.0 = Some(ipv4_enr_addr);
config.enr_udp4_port = Some(ipv4_addr.udp_port);
config.enr_disc4_port = Some(ipv4_addr.disc_port);
}

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

Expand Down Expand Up @@ -1164,11 +1208,11 @@ pub fn set_network_config(
// actually matters. Just use the udp port.

let port = match config.listen_addrs() {
ListenAddress::V4(v4_addr) => v4_addr.udp_port,
ListenAddress::V6(v6_addr) => v6_addr.udp_port,
ListenAddress::V4(v4_addr) => v4_addr.disc_port,
ListenAddress::V6(v6_addr) => v6_addr.disc_port,
ListenAddress::DualStack(v4_addr, _v6_addr) => {
// NOTE: slight preference for ipv4 that I don't think is of importance.
v4_addr.udp_port
v4_addr.disc_port
}
};

Expand Down
14 changes: 7 additions & 7 deletions boot_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ impl<T: EthSpec> BootNodeConfig<T> {

set_network_config(&mut network_config, matches, &data_dir, &logger)?;

// Set the Enr UDP ports to the listening ports if not present.
// Set the Enr Discovery ports to the listening ports if not present.
if let Some(listening_addr_v4) = network_config.listen_addrs().v4() {
network_config.enr_udp4_port = Some(
network_config.enr_disc4_port = Some(
network_config
.enr_udp4_port
.unwrap_or(listening_addr_v4.udp_port),
.enr_disc4_port
.unwrap_or(listening_addr_v4.disc_port),
)
};

if let Some(listening_addr_v6) = network_config.listen_addrs().v6() {
network_config.enr_udp6_port = Some(
network_config.enr_disc6_port = Some(
network_config
.enr_udp6_port
.unwrap_or(listening_addr_v6.udp_port),
.enr_disc6_port
.unwrap_or(listening_addr_v6.disc_port),
)
};

Expand Down
4 changes: 2 additions & 2 deletions lcli/src/generate_bootnode_enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {

let mut config = NetworkConfig::default();
config.enr_address = (Some(ip), None);
config.enr_udp4_port = Some(udp_port);
config.enr_disc4_port = Some(udp_port);
config.enr_tcp6_port = Some(tcp_port);

let secp256k1_keypair = secp256k1::Keypair::generate();
Expand Down Expand Up @@ -57,4 +57,4 @@ pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
.map_err(|e| format!("Unable to write key to {}: {:?}", NETWORK_KEY_FILENAME, e))?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion testing/node_test_rig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn testing_client_config() -> ClientConfig {
// Setting ports to `0` means that the OS will choose some available port.
client_config
.network
.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, 0, 0);
.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, 0, 0, 0);
client_config.network.upnp_enabled = false;
client_config.http_api.enabled = true;
client_config.http_api.listen_port = 0;
Expand Down
Loading

0 comments on commit 24fe1f2

Please sign in to comment.