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

refactor: remove UPnP feature flag (compile by default) #2631

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion ant-networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ version = "0.3.4"
default = []
loud = []
open-metrics = ["libp2p/metrics", "prometheus-client", "hyper", "sysinfo"]
upnp = ["libp2p/upnp"]

[dependencies]
aes-gcm-siv = "0.11.1"
Expand All @@ -37,6 +36,7 @@ itertools = "~0.12.1"
libp2p = { version = "0.54.1", features = [
"tokio",
"dns",
"upnp",
"kad",
"macros",
"request-response",
Expand Down
33 changes: 6 additions & 27 deletions ant-networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ use ant_protocol::{
};
use futures::future::Either;
use futures::StreamExt;
#[cfg(feature = "upnp")]
use libp2p::swarm::behaviour::toggle::Toggle;
use libp2p::{core::muxing::StreamMuxerBox, relay};
use libp2p::{core::muxing::StreamMuxerBox, relay, swarm::behaviour::toggle::Toggle};
use libp2p::{
identity::Keypair,
kad::{self, QueryId, Quorum, Record, RecordKey, K_VALUE},
Expand Down Expand Up @@ -228,7 +226,6 @@ pub(super) struct NodeBehaviour {
pub(super) blocklist:
libp2p::allow_block_list::Behaviour<libp2p::allow_block_list::BlockedPeers>,
pub(super) identify: libp2p::identify::Behaviour,
#[cfg(feature = "upnp")]
pub(super) upnp: Toggle<libp2p::upnp::tokio::Behaviour>,
pub(super) relay_client: libp2p::relay::client::Behaviour,
pub(super) relay_server: libp2p::relay::Behaviour,
Expand All @@ -249,7 +246,6 @@ pub struct NetworkBuilder {
#[cfg(feature = "open-metrics")]
metrics_server_port: Option<u16>,
request_timeout: Option<Duration>,
#[cfg(feature = "upnp")]
upnp: bool,
}

Expand All @@ -267,7 +263,6 @@ impl NetworkBuilder {
#[cfg(feature = "open-metrics")]
metrics_server_port: None,
request_timeout: None,
#[cfg(feature = "upnp")]
upnp: false,
}
}
Expand Down Expand Up @@ -305,7 +300,6 @@ impl NetworkBuilder {
self.metrics_server_port = port;
}

#[cfg(feature = "upnp")]
pub fn upnp(&mut self, upnp: bool) {
self.upnp = upnp;
}
Expand Down Expand Up @@ -395,17 +389,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)?;
Expand Down Expand Up @@ -437,14 +424,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);

(network, net_event_recv, driver)
}
Expand All @@ -456,7 +437,7 @@ impl NetworkBuilder {
record_store_cfg: Option<NodeRecordStoreConfig>,
is_client: bool,
req_res_protocol: ProtocolSupport,
#[cfg(feature = "upnp")] upnp: bool,
upnp: bool,
) -> (Network, mpsc::Receiver<NetworkEvent>, SwarmDriver) {
let identify_protocol_str = IDENTIFY_PROTOCOL_STR
.read()
Expand Down Expand Up @@ -613,7 +594,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())
Expand All @@ -639,7 +619,6 @@ impl NetworkBuilder {
blocklist: libp2p::allow_block_list::Behaviour::default(),
relay_client: relay_behaviour,
relay_server,
#[cfg(feature = "upnp")]
upnp,
request_response,
kademlia,
Expand Down
2 changes: 0 additions & 2 deletions ant-networking/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Request, Response>),
Kademlia(libp2p::kad::Event),
Expand All @@ -51,7 +50,6 @@ pub(super) enum NodeEvent {
Void(void::Void),
}

#[cfg(feature = "upnp")]
impl From<libp2p::upnp::Event> for NodeEvent {
fn from(event: libp2p::upnp::Event) -> Self {
NodeEvent::Upnp(event)
Expand Down
1 change: 0 additions & 1 deletion ant-networking/src/event/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions ant-networking/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<upnp::UpnpEventLabels, Counter>,

// metrics from ant-networking
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -209,7 +205,6 @@ impl NetworkMetricsRecorder {
);
let network_metrics = Self {
libp2p_metrics,
#[cfg(feature = "upnp")]
upnp_events,

records_stored,
Expand Down
3 changes: 1 addition & 2 deletions ant-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.4" }
Expand Down
2 changes: 0 additions & 2 deletions ant-node/src/bin/antnode/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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,

Expand Down Expand Up @@ -318,7 +317,6 @@ fn main() -> Result<()> {
node_socket_addr,
opt.peers.local,
root_dir,
#[cfg(feature = "upnp")]
opt.upnp,
);
node_builder.initial_peers(initial_peers);
Expand Down
5 changes: 1 addition & 4 deletions ant-node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub struct NodeBuilder {
metrics_server_port: Option<u16>,
/// Enable hole punching for nodes connecting from home networks.
is_behind_home_network: bool,
#[cfg(feature = "upnp")]
upnp: bool,
}

Expand All @@ -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,
Expand All @@ -122,7 +121,6 @@ impl NodeBuilder {
#[cfg(feature = "open-metrics")]
metrics_server_port: None,
is_behind_home_network: false,
#[cfg(feature = "upnp")]
upnp,
}
}
Expand Down Expand Up @@ -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) =
Expand Down
1 change: 0 additions & 1 deletion ant-node/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading