Skip to content

Commit

Permalink
remove nat module and use libp2p upnp (#4840)
Browse files Browse the repository at this point in the history
* remove nat module and use libp2p upnp

* update Cargo.lock

* remove no longer used dependencies

* restore nat module refactored

* log successful mapping

* only activate upnp if config enabled

reduce logs to debug!

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into libp2p-nat

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into libp2p-nat

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into libp2p-nat

* address review

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into libp2p-nat

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into libp2p-nat

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into libp2p-nat

* address review
  • Loading branch information
jxs authored Feb 27, 2024
1 parent d36241b commit abd9965
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 323 deletions.
73 changes: 26 additions & 47 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ resolver = "2"
edition = "2021"

[workspace.dependencies]
anyhow = "1"
arbitrary = { version = "1", features = ["derive"] }
bincode = "1"
bitvec = "1"
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ quick-protobuf-codec = "0.3"
[dependencies.libp2p]
version = "0.53"
default-features = false
features = ["identify", "yamux", "noise", "dns", "tcp", "tokio", "plaintext", "secp256k1", "macros", "ecdsa", "metrics", "quic"]
features = ["identify", "yamux", "noise", "dns", "tcp", "tokio", "plaintext", "secp256k1", "macros", "ecdsa", "metrics", "quic", "upnp"]

[dev-dependencies]
slog-term = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/lighthouse_network/src/service/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::types::SnappyTransform;
use crate::gossipsub;
use libp2p::identify;
use libp2p::swarm::NetworkBehaviour;
use libp2p::upnp::tokio::Behaviour as Upnp;
use types::EthSpec;

use super::api_types::RequestId;
Expand All @@ -32,6 +33,8 @@ where
// NOTE: The id protocol is used for initial interop. This will be removed by mainnet.
/// Provides IP addresses and peer information.
pub identify: identify::Behaviour,
/// Libp2p UPnP port mapping.
pub upnp: Upnp,
/// The routing pub-sub mechanism for eth2.
pub gossipsub: Gossipsub,
}
51 changes: 48 additions & 3 deletions beacon_node/lighthouse_network/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ use crate::{error, metrics, Enr, NetworkGlobals, PubsubMessage, TopicHash};
use api_types::{PeerRequestId, Request, RequestId, Response};
use futures::stream::StreamExt;
use gossipsub_scoring_parameters::{lighthouse_gossip_thresholds, PeerScoreSettings};
use libp2p::multiaddr::{Multiaddr, Protocol as MProtocol};
use libp2p::multiaddr::{self, Multiaddr, Protocol as MProtocol};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::PeerId;
use libp2p::{identify, SwarmBuilder};
use libp2p::{identify, PeerId, SwarmBuilder};
use slog::{crit, debug, info, o, trace, warn};
use std::path::PathBuf;
use std::pin::Pin;
Expand Down Expand Up @@ -363,6 +362,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
identify,
peer_manager,
connection_limits,
upnp: Default::default(),
}
};

Expand Down Expand Up @@ -1601,6 +1601,47 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
}
}

fn inject_upnp_event(&mut self, event: libp2p::upnp::Event) {
match event {
libp2p::upnp::Event::NewExternalAddr(addr) => {
info!(self.log, "UPnP route established"; "addr" => %addr);
let mut iter = addr.iter();
// Skip Ip address.
iter.next();
match iter.next() {
Some(multiaddr::Protocol::Udp(udp_port)) => match iter.next() {
Some(multiaddr::Protocol::QuicV1) => {
if let Err(e) = self.discovery_mut().update_enr_quic_port(udp_port) {
warn!(self.log, "Failed to update ENR"; "error" => e);
}
}
_ => {
trace!(self.log, "UPnP address mapped multiaddr from unknown transport"; "addr" => %addr)
}
},
Some(multiaddr::Protocol::Tcp(tcp_port)) => {
if let Err(e) = self.discovery_mut().update_enr_tcp_port(tcp_port) {
warn!(self.log, "Failed to update ENR"; "error" => e);
}
}
_ => {
trace!(self.log, "UPnP address mapped multiaddr from unknown transport"; "addr" => %addr);
}
}
}
libp2p::upnp::Event::ExpiredExternalAddr(_) => {}
libp2p::upnp::Event::GatewayNotFound => {
info!(self.log, "UPnP not available");
}
libp2p::upnp::Event::NonRoutableGateway => {
info!(
self.log,
"UPnP is available but gateway is not exposed to public network"
);
}
}
}

/* Networking polling */

/// Poll the p2p networking stack.
Expand All @@ -1623,6 +1664,10 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
}
BehaviourEvent::Identify(ie) => self.inject_identify_event(ie),
BehaviourEvent::PeerManager(pe) => self.inject_pm_event(pe),
BehaviourEvent::Upnp(e) => {
self.inject_upnp_event(e);
None
}
BehaviourEvent::ConnectionLimits(le) => void::unreachable(le),
},
SwarmEvent::ConnectionEstablished { .. } => None,
Expand Down
5 changes: 2 additions & 3 deletions beacon_node/lighthouse_network/src/service/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{GossipTopic, NetworkConfig};
use futures::future::Either;
use libp2p::core::{multiaddr::Multiaddr, muxing::StreamMuxerBox, transport::Boxed};
use libp2p::identity::{secp256k1, Keypair};
use libp2p::quic;
use libp2p::{core, noise, yamux, PeerId, Transport};
use prometheus_client::registry::Registry;
use slog::{debug, warn};
Expand Down Expand Up @@ -63,8 +62,8 @@ pub fn build_transport(
let transport = if quic_support {
// Enables Quic
// The default quic configuration suits us for now.
let quic_config = quic::Config::new(&local_private_key);
let quic = quic::tokio::Transport::new(quic_config);
let quic_config = libp2p::quic::Config::new(&local_private_key);
let quic = libp2p::quic::tokio::Transport::new(quic_config);
let transport = tcp
.or_transport(quic)
.map(|either_output, _| match either_output {
Expand Down
6 changes: 3 additions & 3 deletions beacon_node/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ slog-async = { workspace = true }
eth2 = { workspace = true }

[dependencies]
anyhow = { workspace = true }
beacon_chain = { workspace = true }
store = { workspace = true }
lighthouse_network = { workspace = true }
Expand All @@ -35,11 +36,10 @@ lazy_static = { workspace = true }
lighthouse_metrics = { workspace = true }
logging = { workspace = true }
task_executor = { workspace = true }
igd-next = "0.14.3"
igd-next = "0.14"
itertools = { workspace = true }
num_cpus = { workspace = true }
lru_cache = { workspace = true }
if-addrs = "0.6.4"
lru = { workspace = true }
strum = { workspace = true }
tokio-util = { workspace = true }
Expand All @@ -56,4 +56,4 @@ environment = { workspace = true }
# NOTE: This can be run via cargo build --bin lighthouse --features network/disable-backfill
disable-backfill = []
fork_from_env = ["beacon_chain/fork_from_env"]
portable = ["beacon_chain/portable"]
portable = ["beacon_chain/portable"]
Loading

0 comments on commit abd9965

Please sign in to comment.