Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove nat module and use libp2p upnp #4840

Merged
merged 14 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 14 additions & 66 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
5 changes: 3 additions & 2 deletions beacon_node/lighthouse_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ prometheus-client = "0.21.0"
unused_port = { workspace = true }
delay_map = { workspace = true }
void = "1"
libp2p-quic= { version = "0.9.2", features=["tokio"]}
# TODO: remove when importable via libp2p main crate.
libp2p-upnp = { version = "0.1.0", features = ["tokio"] }
libp2p-mplex = "0.40.0"

[dependencies.libp2p]
version = "0.52"
default-features = false
features = ["identify", "yamux", "noise", "gossipsub", "dns", "tcp", "tokio", "plaintext", "secp256k1", "macros", "ecdsa"]
features = ["identify", "yamux", "noise", "gossipsub", "dns", "tcp", "tokio", "plaintext", "secp256k1", "macros", "ecdsa", "quic"]

[dev-dependencies]
slog-term = { workspace = true }
Expand Down
4 changes: 4 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 libp2p::gossipsub;
use libp2p::identify;
use libp2p::swarm::NetworkBehaviour;
use libp2p_upnp::tokio::Behaviour as Upnp;
use types::EthSpec;

use super::api_types::RequestId;
Expand Down Expand Up @@ -34,4 +35,7 @@ where
pub identify: identify::Behaviour,
/// The peer manager that keeps track of peer's reputation and status.
pub peer_manager: PeerManager<TSpec>,

/// Libp2p UPnP port mapping.
pub upnp: Upnp,
}
52 changes: 49 additions & 3 deletions beacon_node/lighthouse_network/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ use libp2p::gossipsub::{
self, IdentTopic as Topic, MessageAcceptance, MessageAuthenticity, MessageId, PublishError,
TopicScoreParams,
};
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 libp2p_upnp;
use slog::{crit, debug, info, o, trace, warn};
use std::path::PathBuf;
use std::pin::Pin;
Expand Down Expand Up @@ -362,6 +362,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
identify,
peer_manager,
connection_limits,
upnp: Default::default(),
}
};

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

fn parse_upnp_event(&mut self, event: libp2p_upnp::Event) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the existing methods for handling events are named with the prefix inject_, perhaps we should align with this naming convention? For example, we could name it inject_upnp_event.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense. Updated, thanks!

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 @@ -1538,6 +1580,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.parse_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 @@ -10,7 +10,6 @@ use libp2p::core::{multiaddr::Multiaddr, muxing::StreamMuxerBox, transport::Boxe
use libp2p::gossipsub;
use libp2p::identity::{secp256k1, Keypair};
use libp2p::{core, noise, yamux, PeerId, Transport, TransportExt};
use libp2p_quic;
use prometheus_client::registry::Registry;
use slog::{debug, warn};
use ssz::Decode;
Expand Down Expand Up @@ -69,9 +68,9 @@ pub fn build_transport(
let (transport, bandwidth) = if quic_support {
// Enables Quic
// The default quic configuration suits us for now.
let quic_config = libp2p_quic::Config::new(&local_private_key);
let quic_config = libp2p::quic::Config::new(&local_private_key);
let (quic, quic_bandwidth) =
libp2p_quic::tokio::Transport::new(quic_config).with_bandwidth_logging();
libp2p::quic::tokio::Transport::new(quic_config).with_bandwidth_logging();
let transport = tcp
.or_transport(quic)
.map(|either_output, _| match either_output {
Expand Down
4 changes: 2 additions & 2 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 = "0.12.1"
igd-next = "0.14.2"
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 Down
Loading