diff --git a/crates/subspace-networking/src/create.rs b/crates/subspace-networking/src/create.rs index 2a2d92b07b..52604c8faf 100644 --- a/crates/subspace-networking/src/create.rs +++ b/crates/subspace-networking/src/create.rs @@ -35,6 +35,7 @@ use libp2p::multiaddr::Protocol; use libp2p::swarm::SwarmBuilder; use libp2p::yamux::Config as YamuxConfig; use libp2p::{identity, Multiaddr, PeerId, StreamProtocol, TransportError}; +use libp2p_kad::Mode; use parking_lot::Mutex; use std::borrow::Cow; use std::iter::Empty; @@ -224,6 +225,8 @@ pub struct Config { pub special_target_connections: u32, /// Addresses to bootstrap Kademlia network pub bootstrap_addresses: Vec, + /// Kademlia mode. None means "automatic mode". + pub kademlia_mode: Option, } impl fmt::Debug for Config { @@ -337,6 +340,7 @@ where general_target_connections: SWARM_TARGET_CONNECTION_NUMBER, special_target_connections: SWARM_TARGET_CONNECTION_NUMBER, bootstrap_addresses: Vec::new(), + kademlia_mode: Some(Mode::Server), } } } @@ -400,6 +404,7 @@ where general_target_connections, special_target_connections, bootstrap_addresses, + kademlia_mode, } = config; let local_peer_id = peer_id(&keypair); @@ -512,6 +517,7 @@ where general_connection_decision_handler, special_connection_decision_handler, bootstrap_addresses, + kademlia_mode, }); Ok((node, node_runner)) diff --git a/crates/subspace-networking/src/node_runner.rs b/crates/subspace-networking/src/node_runner.rs index 0fdbd1b420..470f829daa 100644 --- a/crates/subspace-networking/src/node_runner.rs +++ b/crates/subspace-networking/src/node_runner.rs @@ -139,6 +139,8 @@ where bootstrap_addresses: Vec, /// Ensures a single bootstrap on run() invocation. bootstrap_command_state: Arc>, + /// Kademlia mode. None means "automatic mode". + kademlia_mode: Option, } // Helper struct for NodeRunner configuration (clippy requirement). @@ -159,6 +161,7 @@ where pub(crate) general_connection_decision_handler: Option, pub(crate) special_connection_decision_handler: Option, pub(crate) bootstrap_addresses: Vec, + pub(crate) kademlia_mode: Option, } impl NodeRunner @@ -180,6 +183,7 @@ where general_connection_decision_handler, special_connection_decision_handler, bootstrap_addresses, + kademlia_mode, }: NodeRunnerConfig, ) -> Self { Self { @@ -206,6 +210,7 @@ where rng: StdRng::seed_from_u64(KADEMLIA_PEERS_ADDRESSES_BATCH_SIZE as u64), // any seed bootstrap_addresses, bootstrap_command_state: Arc::new(AsyncMutex::new(BootstrapCommandState::default())), + kademlia_mode, } } @@ -289,7 +294,8 @@ where self.swarm .behaviour_mut() .kademlia - .set_mode(Some(Mode::Server)); + .set_mode(self.kademlia_mode); + debug!("Kademlia mode set: {:?}.", self.kademlia_mode); let mut bootstrap_step = 0; loop {