From 744348c12543baff94f327fdbe3641c6621b476b Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Tue, 14 Jan 2025 16:19:19 +0100 Subject: [PATCH 1/2] refactor: remove UPnP (compile by default) --- ant-networking/Cargo.toml | 1 - ant-networking/src/driver.rs | 29 +++++------------------------ ant-networking/src/event/mod.rs | 2 -- ant-networking/src/event/swarm.rs | 1 - ant-networking/src/metrics/mod.rs | 5 ----- ant-node/Cargo.toml | 3 +-- ant-node/src/bin/antnode/main.rs | 2 -- ant-node/src/node.rs | 5 +---- ant-node/src/python.rs | 1 - 9 files changed, 7 insertions(+), 42 deletions(-) diff --git a/ant-networking/Cargo.toml b/ant-networking/Cargo.toml index efa470d678..3f287266bc 100644 --- a/ant-networking/Cargo.toml +++ b/ant-networking/Cargo.toml @@ -13,7 +13,6 @@ version = "0.3.3" default = [] loud = [] open-metrics = ["libp2p/metrics", "prometheus-client", "hyper", "sysinfo"] -upnp = ["libp2p/upnp"] [dependencies] aes-gcm-siv = "0.11.1" diff --git a/ant-networking/src/driver.rs b/ant-networking/src/driver.rs index 1498554479..09700ae9ba 100644 --- a/ant-networking/src/driver.rs +++ b/ant-networking/src/driver.rs @@ -255,7 +255,6 @@ pub(super) struct NodeBehaviour { pub(super) identify: libp2p::identify::Behaviour, /// mDNS behaviour to use in local mode pub(super) mdns: Toggle, - #[cfg(feature = "upnp")] pub(super) upnp: Toggle, pub(super) relay_client: libp2p::relay::client::Behaviour, pub(super) relay_server: libp2p::relay::Behaviour, @@ -276,7 +275,6 @@ pub struct NetworkBuilder { #[cfg(feature = "open-metrics")] metrics_server_port: Option, request_timeout: Option, - #[cfg(feature = "upnp")] upnp: bool, } @@ -294,7 +292,6 @@ impl NetworkBuilder { #[cfg(feature = "open-metrics")] metrics_server_port: None, request_timeout: None, - #[cfg(feature = "upnp")] upnp: false, } } @@ -332,7 +329,6 @@ impl NetworkBuilder { self.metrics_server_port = port; } - #[cfg(feature = "upnp")] pub fn upnp(&mut self, upnp: bool) { self.upnp = upnp; } @@ -422,17 +418,10 @@ impl NetworkBuilder { }; let listen_addr = self.listen_addr; - #[cfg(feature = "upnp")] let upnp = self.upnp; - let (network, events_receiver, mut swarm_driver) = self.build( - kad_cfg, - Some(store_cfg), - false, - ProtocolSupport::Full, - #[cfg(feature = "upnp")] - upnp, - )?; + let (network, events_receiver, mut swarm_driver) = + self.build(kad_cfg, Some(store_cfg), false, ProtocolSupport::Full, upnp)?; // Listen on the provided address let listen_socket_addr = listen_addr.ok_or(NetworkError::ListenAddressNotProvided)?; @@ -464,14 +453,8 @@ impl NetworkBuilder { // How many nodes _should_ store data. .set_replication_factor(REPLICATION_FACTOR); - let (network, net_event_recv, driver) = self.build( - kad_cfg, - None, - true, - ProtocolSupport::Outbound, - #[cfg(feature = "upnp")] - false, - )?; + let (network, net_event_recv, driver) = + self.build(kad_cfg, None, true, ProtocolSupport::Outbound, false)?; Ok((network, net_event_recv, driver)) } @@ -483,7 +466,7 @@ impl NetworkBuilder { record_store_cfg: Option, is_client: bool, req_res_protocol: ProtocolSupport, - #[cfg(feature = "upnp")] upnp: bool, + upnp: bool, ) -> Result<(Network, mpsc::Receiver, SwarmDriver)> { let identify_protocol_str = IDENTIFY_PROTOCOL_STR .read() @@ -656,7 +639,6 @@ impl NetworkBuilder { libp2p::identify::Behaviour::new(cfg) }; - #[cfg(feature = "upnp")] let upnp = if !self.local && !is_client && upnp { debug!("Enabling UPnP port opening behavior"); Some(libp2p::upnp::tokio::Behaviour::default()) @@ -682,7 +664,6 @@ impl NetworkBuilder { blocklist: libp2p::allow_block_list::Behaviour::default(), relay_client: relay_behaviour, relay_server, - #[cfg(feature = "upnp")] upnp, request_response, kademlia, diff --git a/ant-networking/src/event/mod.rs b/ant-networking/src/event/mod.rs index 8489d47516..d4465212be 100644 --- a/ant-networking/src/event/mod.rs +++ b/ant-networking/src/event/mod.rs @@ -41,7 +41,6 @@ type KBucketStatus = (usize, usize, usize, usize, Vec<(usize, usize, u32)>); /// NodeEvent enum #[derive(CustomDebug)] pub(super) enum NodeEvent { - #[cfg(feature = "upnp")] Upnp(libp2p::upnp::Event), MsgReceived(libp2p::request_response::Event), Kademlia(libp2p::kad::Event), @@ -52,7 +51,6 @@ pub(super) enum NodeEvent { Void(void::Void), } -#[cfg(feature = "upnp")] impl From for NodeEvent { fn from(event: libp2p::upnp::Event) -> Self { NodeEvent::Upnp(event) diff --git a/ant-networking/src/event/swarm.rs b/ant-networking/src/event/swarm.rs index e6eef4e576..7df6cd7d1c 100644 --- a/ant-networking/src/event/swarm.rs +++ b/ant-networking/src/event/swarm.rs @@ -68,7 +68,6 @@ impl SwarmDriver { } } } - #[cfg(feature = "upnp")] SwarmEvent::Behaviour(NodeEvent::Upnp(upnp_event)) => { #[cfg(feature = "open-metrics")] if let Some(metrics_recorder) = &self.metrics_recorder { diff --git a/ant-networking/src/metrics/mod.rs b/ant-networking/src/metrics/mod.rs index 372327bb03..a61dd2e054 100644 --- a/ant-networking/src/metrics/mod.rs +++ b/ant-networking/src/metrics/mod.rs @@ -9,7 +9,6 @@ // Implementation to record `libp2p::upnp::Event` metrics mod bad_node; pub mod service; -#[cfg(feature = "upnp")] mod upnp; use std::sync::atomic::AtomicU64; @@ -37,7 +36,6 @@ pub(crate) struct NetworkMetricsRecorder { // Must directly call self.libp2p_metrics.record(libp2p_event) with Recorder trait in scope. But since we have // re-implemented the trait for the wrapper struct, we can instead call self.record(libp2p_event) libp2p_metrics: Libp2pMetrics, - #[cfg(feature = "upnp")] upnp_events: Family, // metrics from ant-networking @@ -127,9 +125,7 @@ impl NetworkMetricsRecorder { bad_peers_count.clone(), ); - #[cfg(feature = "upnp")] let upnp_events = Family::default(); - #[cfg(feature = "upnp")] sub_registry.register( "upnp_events", "Events emitted by the UPnP behaviour", @@ -209,7 +205,6 @@ impl NetworkMetricsRecorder { ); let network_metrics = Self { libp2p_metrics, - #[cfg(feature = "upnp")] upnp_events, records_stored, diff --git a/ant-node/Cargo.toml b/ant-node/Cargo.toml index 37203c57fd..948f0bfcc2 100644 --- a/ant-node/Cargo.toml +++ b/ant-node/Cargo.toml @@ -14,13 +14,12 @@ name = "antnode" path = "src/bin/antnode/main.rs" [features] -default = ["upnp", "open-metrics"] +default = ["open-metrics"] extension-module = ["pyo3/extension-module"] loud = ["ant-networking/loud"] # loud mode: print important messages to console nightly = [] open-metrics = ["ant-networking/open-metrics", "prometheus-client"] otlp = ["ant-logging/otlp"] -upnp = ["ant-networking/upnp"] [dependencies] ant-bootstrap = { path = "../ant-bootstrap", version = "0.1.3" } diff --git a/ant-node/src/bin/antnode/main.rs b/ant-node/src/bin/antnode/main.rs index 2be7543dae..acf6e73ee7 100644 --- a/ant-node/src/bin/antnode/main.rs +++ b/ant-node/src/bin/antnode/main.rs @@ -84,7 +84,6 @@ struct Opt { home_network: bool, /// Try to use UPnP to open a port in the home router and allow incoming connections. - #[cfg(feature = "upnp")] #[clap(long, default_value_t = false)] upnp: bool, @@ -317,7 +316,6 @@ fn main() -> Result<()> { node_socket_addr, opt.peers.local, root_dir, - #[cfg(feature = "upnp")] opt.upnp, ); node_builder.initial_peers(initial_peers); diff --git a/ant-node/src/node.rs b/ant-node/src/node.rs index ba83779c84..d06ca6d674 100644 --- a/ant-node/src/node.rs +++ b/ant-node/src/node.rs @@ -94,7 +94,6 @@ pub struct NodeBuilder { metrics_server_port: Option, /// Enable hole punching for nodes connecting from home networks. is_behind_home_network: bool, - #[cfg(feature = "upnp")] upnp: bool, } @@ -108,7 +107,7 @@ impl NodeBuilder { addr: SocketAddr, local: bool, root_dir: PathBuf, - #[cfg(feature = "upnp")] upnp: bool, + upnp: bool, ) -> Self { Self { bootstrap_cache: None, @@ -122,7 +121,6 @@ impl NodeBuilder { #[cfg(feature = "open-metrics")] metrics_server_port: None, is_behind_home_network: false, - #[cfg(feature = "upnp")] upnp, } } @@ -184,7 +182,6 @@ impl NodeBuilder { network_builder.bootstrap_cache(cache); } - #[cfg(feature = "upnp")] network_builder.upnp(self.upnp); let (network, network_event_receiver, swarm_driver) = diff --git a/ant-node/src/python.rs b/ant-node/src/python.rs index 2571b0d13f..dbd08c8511 100644 --- a/ant-node/src/python.rs +++ b/ant-node/src/python.rs @@ -100,7 +100,6 @@ impl AntNode { node_socket_addr, local, root_dir.unwrap_or_else(|| PathBuf::from(".")), - #[cfg(feature = "upnp")] false, ); node_builder.initial_peers(initial_peers); From 11719e574895299616f32fd43162512ae35fa9b6 Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Tue, 14 Jan 2025 17:06:43 +0100 Subject: [PATCH 2/2] fix: enable upnp feature in libp2p --- ant-networking/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/ant-networking/Cargo.toml b/ant-networking/Cargo.toml index 3f287266bc..d5704c7216 100644 --- a/ant-networking/Cargo.toml +++ b/ant-networking/Cargo.toml @@ -38,6 +38,7 @@ libp2p = { version = "0.54.1", features = [ "tokio", "dns", "mdns", + "upnp", "kad", "macros", "request-response",