From f3b7401b436fc55edf5f7658e6236804008eb139 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 15 Nov 2023 16:07:29 +0200 Subject: [PATCH] Infer `enable_autonat` from `external_addresses` --- .../subspace-networking/examples/benchmark.rs | 1 - .../examples/random-walker.rs | 1 - crates/subspace-networking/src/constructor.rs | 20 ++++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/subspace-networking/examples/benchmark.rs b/crates/subspace-networking/examples/benchmark.rs index d0aaaa37bd..4216851029 100644 --- a/crates/subspace-networking/examples/benchmark.rs +++ b/crates/subspace-networking/examples/benchmark.rs @@ -274,7 +274,6 @@ pub async fn configure_dsn( allow_non_global_addresses_in_dht: enable_private_ips, request_response_protocols: vec![PieceByIndexRequestHandler::create(|_, _| async { None })], bootstrap_addresses, - enable_autonat: false, max_pending_outgoing_connections: pending_out_peers, max_established_outgoing_connections: out_peers, ..default_config diff --git a/crates/subspace-networking/examples/random-walker.rs b/crates/subspace-networking/examples/random-walker.rs index 6526427b4c..b56c45cbac 100644 --- a/crates/subspace-networking/examples/random-walker.rs +++ b/crates/subspace-networking/examples/random-walker.rs @@ -367,7 +367,6 @@ async fn configure_dsn( allow_non_global_addresses_in_dht: enable_private_ips, request_response_protocols: vec![PieceByIndexRequestHandler::create(|_, _| async { None })], bootstrap_addresses, - enable_autonat: false, max_pending_outgoing_connections: pending_out_peers, max_established_outgoing_connections: out_peers, ..default_config diff --git a/crates/subspace-networking/src/constructor.rs b/crates/subspace-networking/src/constructor.rs index e3bb66bf96..28a2d367c0 100644 --- a/crates/subspace-networking/src/constructor.rs +++ b/crates/subspace-networking/src/constructor.rs @@ -257,10 +257,6 @@ pub struct Config { /// Known external addresses to the local peer. The addresses will be added on the swarm start /// and enable peer to notify others about its reachable address. pub external_addresses: Vec, - /// Enable autonat protocol. Helps detecting whether we're behind the firewall. - /// - /// NOTE: Ignored and implied to be `false` in case `external_addresses` is not empty. - pub enable_autonat: bool, /// Defines whether we should run blocking Kademlia bootstrap() operation before other requests. pub disable_bootstrap_on_start: bool, } @@ -382,7 +378,6 @@ where bootstrap_addresses: Vec::new(), kademlia_mode: KademliaMode::Static(Mode::Client), external_addresses: Vec::new(), - enable_autonat: true, disable_bootstrap_on_start: false, } } @@ -453,7 +448,6 @@ where bootstrap_addresses, kademlia_mode, external_addresses, - enable_autonat, disable_bootstrap_on_start, } = config; let local_peer_id = peer_id(&keypair); @@ -506,7 +500,7 @@ where ..ConnectedPeersConfig::default() } }), - autonat: (enable_autonat && external_addresses.is_empty()).then(|| AutonatConfig { + autonat: external_addresses.is_empty().then(|| AutonatConfig { use_connected: true, only_global_ips: !config.allow_non_global_addresses_in_dht, confidence_max: AUTONAT_MAX_CONFIDENCE, @@ -514,8 +508,16 @@ where }), }); - if let KademliaMode::Static(mode) = kademlia_mode { - behaviour.kademlia.set_mode(Some(mode)); + match (kademlia_mode, external_addresses.is_empty()) { + (KademliaMode::Static(mode), _) => { + behaviour.kademlia.set_mode(Some(mode)); + } + (KademliaMode::Dynamic, false) => { + behaviour.kademlia.set_mode(Some(Mode::Server)); + } + _ => { + // Autonat will figure it out + } }; let temporary_bans = Arc::new(Mutex::new(TemporaryBans::new(