mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-23 23:04:53 +00:00
Add Experimental QUIC support (#4577)
## Issue Addressed #4402 ## Proposed Changes This PR adds QUIC support to Lighthouse. As this is not officially spec'd this will only work between lighthouse <-> lighthouse connections. We attempt a QUIC connection (if the node advertises it) and if it fails we fallback to TCP. This should be a backwards compatible modification. We want to test this functionality on live networks to observe any improvements in bandwidth/latency. NOTE: This also removes the websockets transport as I believe no one is really using it. It should be mentioned in our release however. Co-authored-by: João Oliveira <hello@jxs.pt>
This commit is contained in:
@@ -914,15 +914,15 @@ 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()
|
||||
@@ -930,6 +930,24 @@ pub fn parse_listening_addresses(
|
||||
format!("Failed to parse --discovery-port6 as an integer: {parse_error}")
|
||||
})?;
|
||||
|
||||
// parse the possible quic port.
|
||||
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}")
|
||||
})?;
|
||||
|
||||
// parse the possible quic port.
|
||||
let maybe_quic6_port = cli_args
|
||||
.value_of("quic-port6")
|
||||
.map(str::parse::<u16>)
|
||||
.transpose()
|
||||
.map_err(|parse_error| {
|
||||
format!("Failed to parse --quic6-port as an integer: {parse_error}")
|
||||
})?;
|
||||
|
||||
// Now put everything together
|
||||
let listening_addresses = match (maybe_ipv4, maybe_ipv6) {
|
||||
(None, None) => {
|
||||
@@ -940,7 +958,7 @@ pub fn parse_listening_addresses(
|
||||
// A single ipv6 address was provided. Set the ports
|
||||
|
||||
if cli_args.is_present("port6") {
|
||||
warn!(log, "When listening only over IpV6, use the --port flag. The value of --port6 will be ignored.")
|
||||
warn!(log, "When listening only over IPv6, use the --port flag. The value of --port6 will be ignored.")
|
||||
}
|
||||
// use zero ports if required. If not, use the given port.
|
||||
let tcp_port = use_zero_ports
|
||||
@@ -948,20 +966,32 @@ pub fn parse_listening_addresses(
|
||||
.transpose()?
|
||||
.unwrap_or(port);
|
||||
|
||||
if maybe_udp6_port.is_some() {
|
||||
warn!(log, "When listening only over IpV6, use the --discovery-port flag. The value of --discovery-port6 will be ignored.")
|
||||
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.")
|
||||
}
|
||||
|
||||
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.
|
||||
let udp_port = use_zero_ports
|
||||
let disc_port = use_zero_ports
|
||||
.then(unused_port::unused_udp6_port)
|
||||
.transpose()?
|
||||
.or(maybe_udp_port)
|
||||
.or(maybe_disc_port)
|
||||
.unwrap_or(port);
|
||||
|
||||
let quic_port = use_zero_ports
|
||||
.then(unused_port::unused_udp6_port)
|
||||
.transpose()?
|
||||
.or(maybe_quic_port)
|
||||
.unwrap_or(port + 1);
|
||||
|
||||
ListenAddress::V6(lighthouse_network::ListenAddr {
|
||||
addr: ipv6,
|
||||
udp_port,
|
||||
quic_port,
|
||||
disc_port,
|
||||
tcp_port,
|
||||
})
|
||||
}
|
||||
@@ -973,16 +1003,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,
|
||||
})
|
||||
}
|
||||
@@ -991,31 +1030,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,
|
||||
},
|
||||
)
|
||||
@@ -1131,6 +1183,14 @@ pub fn set_network_config(
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
@@ -1147,6 +1207,14 @@ pub fn set_network_config(
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
@@ -1156,9 +1224,9 @@ pub fn set_network_config(
|
||||
}
|
||||
|
||||
if cli_args.is_present("enr-match") {
|
||||
// Match the Ip and UDP port in the enr.
|
||||
// Match the IP and UDP port in the ENR.
|
||||
|
||||
// set the enr address to localhost if the address is unspecified
|
||||
// Set the ENR address to localhost if the address is unspecified.
|
||||
if let Some(ipv4_addr) = config.listen_addrs().v4().cloned() {
|
||||
let ipv4_enr_addr = if ipv4_addr.addr == Ipv4Addr::UNSPECIFIED {
|
||||
Ipv4Addr::LOCALHOST
|
||||
@@ -1166,7 +1234,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_udp4_port = Some(ipv4_addr.disc_port);
|
||||
}
|
||||
|
||||
if let Some(ipv6_addr) = config.listen_addrs().v6().cloned() {
|
||||
@@ -1176,7 +1244,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_udp6_port = Some(ipv6_addr.disc_port);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1209,11 +1277,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
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1272,6 +1340,10 @@ pub fn set_network_config(
|
||||
warn!(log, "Discovery is disabled. New peers will not be found");
|
||||
}
|
||||
|
||||
if cli_args.is_present("disable-quic") {
|
||||
config.disable_quic_support = true;
|
||||
}
|
||||
|
||||
if cli_args.is_present("disable-upnp") {
|
||||
config.upnp_enabled = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user