Skip to content

Commit abd9965

Browse files
authored
remove nat module and use libp2p upnp (#4840)
* 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
1 parent d36241b commit abd9965

File tree

9 files changed

+151
-323
lines changed

9 files changed

+151
-323
lines changed

Cargo.lock

Lines changed: 26 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ resolver = "2"
9494
edition = "2021"
9595

9696
[workspace.dependencies]
97+
anyhow = "1"
9798
arbitrary = { version = "1", features = ["derive"] }
9899
bincode = "1"
99100
bitvec = "1"

beacon_node/lighthouse_network/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ quick-protobuf-codec = "0.3"
6464
[dependencies.libp2p]
6565
version = "0.53"
6666
default-features = false
67-
features = ["identify", "yamux", "noise", "dns", "tcp", "tokio", "plaintext", "secp256k1", "macros", "ecdsa", "metrics", "quic"]
67+
features = ["identify", "yamux", "noise", "dns", "tcp", "tokio", "plaintext", "secp256k1", "macros", "ecdsa", "metrics", "quic", "upnp"]
6868

6969
[dev-dependencies]
7070
slog-term = { workspace = true }

beacon_node/lighthouse_network/src/service/behaviour.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::types::SnappyTransform;
66
use crate::gossipsub;
77
use libp2p::identify;
88
use libp2p::swarm::NetworkBehaviour;
9+
use libp2p::upnp::tokio::Behaviour as Upnp;
910
use types::EthSpec;
1011

1112
use super::api_types::RequestId;
@@ -32,6 +33,8 @@ where
3233
// NOTE: The id protocol is used for initial interop. This will be removed by mainnet.
3334
/// Provides IP addresses and peer information.
3435
pub identify: identify::Behaviour,
36+
/// Libp2p UPnP port mapping.
37+
pub upnp: Upnp,
3538
/// The routing pub-sub mechanism for eth2.
3639
pub gossipsub: Gossipsub,
3740
}

beacon_node/lighthouse_network/src/service/mod.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ use crate::{error, metrics, Enr, NetworkGlobals, PubsubMessage, TopicHash};
2828
use api_types::{PeerRequestId, Request, RequestId, Response};
2929
use futures::stream::StreamExt;
3030
use gossipsub_scoring_parameters::{lighthouse_gossip_thresholds, PeerScoreSettings};
31-
use libp2p::multiaddr::{Multiaddr, Protocol as MProtocol};
31+
use libp2p::multiaddr::{self, Multiaddr, Protocol as MProtocol};
3232
use libp2p::swarm::{Swarm, SwarmEvent};
33-
use libp2p::PeerId;
34-
use libp2p::{identify, SwarmBuilder};
33+
use libp2p::{identify, PeerId, SwarmBuilder};
3534
use slog::{crit, debug, info, o, trace, warn};
3635
use std::path::PathBuf;
3736
use std::pin::Pin;
@@ -363,6 +362,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
363362
identify,
364363
peer_manager,
365364
connection_limits,
365+
upnp: Default::default(),
366366
}
367367
};
368368

@@ -1601,6 +1601,47 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
16011601
}
16021602
}
16031603

1604+
fn inject_upnp_event(&mut self, event: libp2p::upnp::Event) {
1605+
match event {
1606+
libp2p::upnp::Event::NewExternalAddr(addr) => {
1607+
info!(self.log, "UPnP route established"; "addr" => %addr);
1608+
let mut iter = addr.iter();
1609+
// Skip Ip address.
1610+
iter.next();
1611+
match iter.next() {
1612+
Some(multiaddr::Protocol::Udp(udp_port)) => match iter.next() {
1613+
Some(multiaddr::Protocol::QuicV1) => {
1614+
if let Err(e) = self.discovery_mut().update_enr_quic_port(udp_port) {
1615+
warn!(self.log, "Failed to update ENR"; "error" => e);
1616+
}
1617+
}
1618+
_ => {
1619+
trace!(self.log, "UPnP address mapped multiaddr from unknown transport"; "addr" => %addr)
1620+
}
1621+
},
1622+
Some(multiaddr::Protocol::Tcp(tcp_port)) => {
1623+
if let Err(e) = self.discovery_mut().update_enr_tcp_port(tcp_port) {
1624+
warn!(self.log, "Failed to update ENR"; "error" => e);
1625+
}
1626+
}
1627+
_ => {
1628+
trace!(self.log, "UPnP address mapped multiaddr from unknown transport"; "addr" => %addr);
1629+
}
1630+
}
1631+
}
1632+
libp2p::upnp::Event::ExpiredExternalAddr(_) => {}
1633+
libp2p::upnp::Event::GatewayNotFound => {
1634+
info!(self.log, "UPnP not available");
1635+
}
1636+
libp2p::upnp::Event::NonRoutableGateway => {
1637+
info!(
1638+
self.log,
1639+
"UPnP is available but gateway is not exposed to public network"
1640+
);
1641+
}
1642+
}
1643+
}
1644+
16041645
/* Networking polling */
16051646

16061647
/// Poll the p2p networking stack.
@@ -1623,6 +1664,10 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
16231664
}
16241665
BehaviourEvent::Identify(ie) => self.inject_identify_event(ie),
16251666
BehaviourEvent::PeerManager(pe) => self.inject_pm_event(pe),
1667+
BehaviourEvent::Upnp(e) => {
1668+
self.inject_upnp_event(e);
1669+
None
1670+
}
16261671
BehaviourEvent::ConnectionLimits(le) => void::unreachable(le),
16271672
},
16281673
SwarmEvent::ConnectionEstablished { .. } => None,

beacon_node/lighthouse_network/src/service/utils.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{GossipTopic, NetworkConfig};
88
use futures::future::Either;
99
use libp2p::core::{multiaddr::Multiaddr, muxing::StreamMuxerBox, transport::Boxed};
1010
use libp2p::identity::{secp256k1, Keypair};
11-
use libp2p::quic;
1211
use libp2p::{core, noise, yamux, PeerId, Transport};
1312
use prometheus_client::registry::Registry;
1413
use slog::{debug, warn};
@@ -63,8 +62,8 @@ pub fn build_transport(
6362
let transport = if quic_support {
6463
// Enables Quic
6564
// The default quic configuration suits us for now.
66-
let quic_config = quic::Config::new(&local_private_key);
67-
let quic = quic::tokio::Transport::new(quic_config);
65+
let quic_config = libp2p::quic::Config::new(&local_private_key);
66+
let quic = libp2p::quic::tokio::Transport::new(quic_config);
6867
let transport = tcp
6968
.or_transport(quic)
7069
.map(|either_output, _| match either_output {

beacon_node/network/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ slog-async = { workspace = true }
1414
eth2 = { workspace = true }
1515

1616
[dependencies]
17+
anyhow = { workspace = true }
1718
beacon_chain = { workspace = true }
1819
store = { workspace = true }
1920
lighthouse_network = { workspace = true }
@@ -35,11 +36,10 @@ lazy_static = { workspace = true }
3536
lighthouse_metrics = { workspace = true }
3637
logging = { workspace = true }
3738
task_executor = { workspace = true }
38-
igd-next = "0.14.3"
39+
igd-next = "0.14"
3940
itertools = { workspace = true }
4041
num_cpus = { workspace = true }
4142
lru_cache = { workspace = true }
42-
if-addrs = "0.6.4"
4343
lru = { workspace = true }
4444
strum = { workspace = true }
4545
tokio-util = { workspace = true }
@@ -56,4 +56,4 @@ environment = { workspace = true }
5656
# NOTE: This can be run via cargo build --bin lighthouse --features network/disable-backfill
5757
disable-backfill = []
5858
fork_from_env = ["beacon_chain/fork_from_env"]
59-
portable = ["beacon_chain/portable"]
59+
portable = ["beacon_chain/portable"]

0 commit comments

Comments
 (0)