Skip to content

Commit

Permalink
Fix ephemeral ports appearing in external addresses due to no listen …
Browse files Browse the repository at this point in the history
…addresses known yet
  • Loading branch information
nazar-pc committed Nov 17, 2023
1 parent 76aaa2f commit 51f9fa0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
38 changes: 20 additions & 18 deletions crates/subspace-networking/src/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -581,24 +583,24 @@ where
let shared_weak = Arc::downgrade(&shared);

let node = Node::new(shared);
let node_runner =
NodeRunner::<LocalRecordProvider>::new(NodeRunnerConfig::<LocalRecordProvider> {
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))
}
23 changes: 23 additions & 0 deletions crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Command>,
swarm: Swarm<Behavior<LocalOnlyRecordStore<LocalRecordProvider>>>,
shared_weak: Weak<Shared>,
Expand Down Expand Up @@ -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<Command>,
pub(crate) swarm: Swarm<Behavior<LocalOnlyRecordStore<LocalRecordProvider>>>,
pub(crate) shared_weak: Weak<Shared>,
Expand All @@ -176,6 +180,7 @@ where
pub(crate) fn new(
NodeRunnerConfig {
allow_non_global_addresses_in_dht,
is_listening,
command_receiver,
swarm,
shared_weak,
Expand Down Expand Up @@ -206,6 +211,7 @@ where

Self {
allow_non_global_addresses_in_dht,
is_listening,
command_receiver,
swarm,
shared_weak,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 51f9fa0

Please sign in to comment.