From 3a6250f974f316be0b56606ad72aa63e5e24024c Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 2 Aug 2023 19:24:31 +0400 Subject: [PATCH] fix: always set Kademlia mode to server otherwise, connectivity drops significantly. --- client/network/src/discovery.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/client/network/src/discovery.rs b/client/network/src/discovery.rs index c758cb70001b7..8b3e04419f745 100644 --- a/client/network/src/discovery.rs +++ b/client/network/src/discovery.rs @@ -55,6 +55,7 @@ use ip_network::IpNetwork; use libp2p::{ core::{Endpoint, Multiaddr}, kad::{ + self, record::store::{MemoryStore, RecordStore}, GetClosestPeersError, GetRecordOk, Kademlia, KademliaBucketInserts, KademliaConfig, KademliaEvent, QueryId, QueryResult, Quorum, Record, RecordKey, @@ -222,6 +223,9 @@ impl DiscoveryConfig { let store = MemoryStore::new(local_peer_id); let mut kad = Kademlia::with_config(local_peer_id, store, config); + // Always set the mode to server, so that any node can accept incoming Kademlia + // requests. Otherwise, the connectivity degrades significantly. + kad.set_mode(Some(kad::Mode::Server)); for (peer_id, addr) in &permanent_addresses { kad.add_address(peer_id, addr.clone()); @@ -983,7 +987,7 @@ mod tests { upgrade, }, identity::Keypair, - kad, noise, + noise, swarm::{Executor, Swarm, SwarmBuilder, SwarmEvent}, yamux, Multiaddr, }; @@ -1038,17 +1042,6 @@ mod tests { ) .build(); - // Set the Kademlia mode to server so that it can accept incoming requests. - // - // Note: the server mode is set automatically when the node learns its external - // address, but that does not happen in tests => hence we set it manually. - swarm - .behaviour_mut() - .kademlia - .as_mut() - .unwrap() - .set_mode(Some(kad::Mode::Server)); - let listen_addr: Multiaddr = format!("/memory/{}", rand::random::()).parse().unwrap();