mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-19 21:04:41 +00:00
Upgrade to tokio 0.3 (#1839)
## Description This PR updates Lighthouse to tokio 0.3. It includes a number of dependency updates and some structural changes as to how we create and spawn tasks. This also brings with it a number of various improvements: - Discv5 update - Libp2p update - Fix for recompilation issues - Improved UPnP port mapping handling - Futures dependency update - Log downgrade to traces for rejecting peers when we've reached our max Co-authored-by: blacktemplar <blacktemplar@a1.net>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
use crate::{NetworkConfig, NetworkMessage};
|
||||
use if_addrs::get_if_addrs;
|
||||
use slog::{debug, info, warn};
|
||||
use slog::{debug, info};
|
||||
use std::net::{IpAddr, SocketAddr, SocketAddrV4};
|
||||
use tokio::sync::mpsc;
|
||||
use types::EthSpec;
|
||||
@@ -70,6 +70,8 @@ pub fn construct_upnp_mappings<T: EthSpec>(
|
||||
Some(v) => v,
|
||||
};
|
||||
|
||||
debug!(log, "UPnP Local IP Discovered"; "ip" => ?local_ip);
|
||||
|
||||
match local_ip {
|
||||
IpAddr::V4(address) => {
|
||||
let libp2p_socket = SocketAddrV4::new(address, config.tcp_port);
|
||||
@@ -78,53 +80,39 @@ pub fn construct_upnp_mappings<T: EthSpec>(
|
||||
// one.
|
||||
// I've found this to be more reliable. If multiple users are behind a single
|
||||
// router, they should ideally try to set different port numbers.
|
||||
let tcp_socket = match gateway.add_port(
|
||||
let tcp_socket = add_port_mapping(
|
||||
&gateway,
|
||||
igd::PortMappingProtocol::TCP,
|
||||
libp2p_socket.port(),
|
||||
libp2p_socket,
|
||||
0,
|
||||
"lighthouse-tcp",
|
||||
) {
|
||||
Err(e) => {
|
||||
info!(log, "UPnP TCP route not set"; "error" => %e);
|
||||
None
|
||||
}
|
||||
Ok(_) => {
|
||||
info!(log, "UPnP TCP route established"; "external_socket" => format!("{}:{}", external_ip.as_ref().map(|ip| ip.to_string()).unwrap_or_else(|_| "".into()), config.tcp_port));
|
||||
external_ip
|
||||
.as_ref()
|
||||
.map(|ip| SocketAddr::new(ip.clone().into(), config.tcp_port))
|
||||
.ok()
|
||||
}
|
||||
};
|
||||
"tcp",
|
||||
&log,
|
||||
).and_then(|_| {
|
||||
let external_socket = external_ip.as_ref().map(|ip| SocketAddr::new(ip.clone().into(), config.tcp_port)).map_err(|_| ());
|
||||
info!(log, "UPnP TCP route established"; "external_socket" => format!("{}:{}", external_socket.as_ref().map(|ip| ip.to_string()).unwrap_or_else(|_| "".into()), config.tcp_port));
|
||||
external_socket
|
||||
}).ok();
|
||||
|
||||
let udp_socket = if !config.disable_discovery {
|
||||
let discovery_socket = SocketAddrV4::new(address, config.udp_port);
|
||||
match gateway.add_port(
|
||||
add_port_mapping(
|
||||
&gateway,
|
||||
igd::PortMappingProtocol::UDP,
|
||||
discovery_socket.port(),
|
||||
discovery_socket,
|
||||
0,
|
||||
"lighthouse-udp",
|
||||
) {
|
||||
Err(e) => {
|
||||
info!(log, "UPnP UDP route not set"; "error" => %e);
|
||||
None
|
||||
}
|
||||
Ok(_) => {
|
||||
info!(log, "UPnP UDP route established"; "external_socket" => format!("{}:{}", external_ip.as_ref().map(|ip| ip.to_string()).unwrap_or_else(|_| "".into()), config.tcp_port));
|
||||
external_ip
|
||||
.map(|ip| SocketAddr::new(ip.into(), config.tcp_port))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
"udp",
|
||||
&log,
|
||||
).and_then(|_| {
|
||||
let external_socket = external_ip
|
||||
.map(|ip| SocketAddr::new(ip.into(), config.udp_port)).map_err(|_| ());
|
||||
info!(log, "UPnP UDP route established"; "external_socket" => format!("{}:{}", external_socket.as_ref().map(|ip| ip.to_string()).unwrap_or_else(|_| "".into()), config.udp_port));
|
||||
external_socket
|
||||
}).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// report any updates to the network service.
|
||||
network_send.send(NetworkMessage::UPnPMappingEstablished{ tcp_socket, udp_socket })
|
||||
.unwrap_or_else(|e| warn!(log, "Could not send message to the network service"; "error" => %e));
|
||||
.unwrap_or_else(|e| debug!(log, "Could not send message to the network service"; "error" => %e));
|
||||
}
|
||||
_ => debug!(log, "UPnP no routes constructed. IPv6 not supported"),
|
||||
}
|
||||
@@ -132,6 +120,50 @@ pub fn construct_upnp_mappings<T: EthSpec>(
|
||||
};
|
||||
}
|
||||
|
||||
/// Sets up a port mapping for a protocol returning the mapped port if successful.
|
||||
fn add_port_mapping(
|
||||
gateway: &igd::Gateway,
|
||||
protocol: igd::PortMappingProtocol,
|
||||
socket: SocketAddrV4,
|
||||
protocol_string: &'static str,
|
||||
log: &slog::Logger,
|
||||
) -> Result<(), ()> {
|
||||
// We add specific port mappings rather than getting the router to arbitrary assign
|
||||
// one.
|
||||
// I've found this to be more reliable. If multiple users are behind a single
|
||||
// router, they should ideally try to set different port numbers.
|
||||
let mapping_string = &format!("lighthouse-{}", protocol_string);
|
||||
for _ in 0..2 {
|
||||
match gateway.add_port(protocol, socket.port(), socket, 0, mapping_string) {
|
||||
Err(e) => {
|
||||
match e {
|
||||
igd::AddPortError::PortInUse => {
|
||||
// Try and remove and re-create
|
||||
debug!(log, "UPnP port in use, attempting to remap"; "protocol" => protocol_string, "port" => socket.port());
|
||||
match gateway.remove_port(protocol, socket.port()) {
|
||||
Ok(()) => {
|
||||
debug!(log, "UPnP Removed port mapping"; "protocol" => protocol_string, "port" => socket.port())
|
||||
}
|
||||
Err(e) => {
|
||||
debug!(log, "UPnP Port remove failure"; "protocol" => protocol_string, "port" => socket.port(), "error" => %e);
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
}
|
||||
e => {
|
||||
info!(log, "UPnP TCP route not set"; "error" => %e);
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(_) => {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(())
|
||||
}
|
||||
|
||||
/// Removes the specified TCP and UDP port mappings.
|
||||
pub fn remove_mappings(tcp_port: Option<u16>, udp_port: Option<u16>, log: &slog::Logger) {
|
||||
if tcp_port.is_some() || udp_port.is_some() {
|
||||
|
||||
Reference in New Issue
Block a user