From 51f9fa0b66b0e24ad6f77adbb81113ce6756b6a3 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Fri, 17 Nov 2023 12:37:14 +0200 Subject: [PATCH] Fix ephemeral ports appearing in external addresses due to no listen addresses known yet --- crates/subspace-networking/src/constructor.rs | 38 ++++++++++--------- crates/subspace-networking/src/node_runner.rs | 23 +++++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/crates/subspace-networking/src/constructor.rs b/crates/subspace-networking/src/constructor.rs index 28a2d367c0..9f9bf9b84c 100644 --- a/crates/subspace-networking/src/constructor.rs +++ b/crates/subspace-networking/src/constructor.rs @@ -546,6 +546,8 @@ where }) .build(); + let is_listening = !listen_on.is_empty(); + // Setup listen_on addresses for mut addr in listen_on { if let Err(error) = swarm.listen_on(addr.clone()) { @@ -581,24 +583,24 @@ where let shared_weak = Arc::downgrade(&shared); let node = Node::new(shared); - let node_runner = - NodeRunner::::new(NodeRunnerConfig:: { - allow_non_global_addresses_in_dht, - command_receiver, - swarm, - shared_weak, - next_random_query_interval: initial_random_query_interval, - networking_parameters_registry: networking_parameters_registry - .unwrap_or(StubNetworkingParametersManager.boxed()), - reserved_peers: strip_peer_id(reserved_peers).into_iter().collect(), - temporary_bans, - metrics, - protocol_version, - general_connection_decision_handler, - special_connection_decision_handler, - bootstrap_addresses, - disable_bootstrap_on_start, - }); + let node_runner = NodeRunner::new(NodeRunnerConfig { + allow_non_global_addresses_in_dht, + is_listening, + command_receiver, + swarm, + shared_weak, + next_random_query_interval: initial_random_query_interval, + networking_parameters_registry: networking_parameters_registry + .unwrap_or(StubNetworkingParametersManager.boxed()), + reserved_peers: strip_peer_id(reserved_peers).into_iter().collect(), + temporary_bans, + metrics, + protocol_version, + general_connection_decision_handler, + special_connection_decision_handler, + bootstrap_addresses, + disable_bootstrap_on_start, + }); Ok((node, node_runner)) } diff --git a/crates/subspace-networking/src/node_runner.rs b/crates/subspace-networking/src/node_runner.rs index 71c75fdb2c..d7754e9cd7 100644 --- a/crates/subspace-networking/src/node_runner.rs +++ b/crates/subspace-networking/src/node_runner.rs @@ -102,6 +102,8 @@ where { /// Should non-global addresses be added to the DHT? allow_non_global_addresses_in_dht: bool, + /// Whether node is listening on some addresses + is_listening: bool, command_receiver: mpsc::Receiver, swarm: Swarm>>, shared_weak: Weak, @@ -154,6 +156,8 @@ where LocalRecordProvider: constructor::LocalRecordProvider + Send + Sync + 'static, { pub(crate) allow_non_global_addresses_in_dht: bool, + /// Whether node is listening on some addresses + pub(crate) is_listening: bool, pub(crate) command_receiver: mpsc::Receiver, pub(crate) swarm: Swarm>>, pub(crate) shared_weak: Weak, @@ -176,6 +180,7 @@ where pub(crate) fn new( NodeRunnerConfig { allow_non_global_addresses_in_dht, + is_listening, command_receiver, swarm, shared_weak, @@ -206,6 +211,7 @@ where Self { allow_non_global_addresses_in_dht, + is_listening, command_receiver, swarm, shared_weak, @@ -236,6 +242,23 @@ where /// Drives the main networking future forward. pub async fn run(&mut self) { + if self.is_listening { + // Wait for listen addresses, otherwise we will get ephemeral addresses in external address candidates that + // we do not want + loop { + if self.swarm.listeners().next().is_some() { + break; + } + + if let Some(swarm_event) = self.swarm.next().await { + self.register_event_metrics(&swarm_event); + self.handle_swarm_event(swarm_event).await; + } else { + break; + } + } + } + if !self.disable_bootstrap_on_start { self.bootstrap().await; } else {