expose setting kademlia replication factor through node CLI (#14391)

* expose kademlia replication factor through node CLI

* set default CLI flag value for kademlia_replication_factor

Co-authored-by: Bastian Köcher <git@kchr.de>

* wrap CLI value as Option

* make kademlia replication factor non-optional

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
klbrvik
2023-06-21 23:21:54 +01:00
committed by GitHub
parent e783d98437
commit 1d2f16ab1f
4 changed files with 36 additions and 1 deletions
+16
View File
@@ -87,6 +87,10 @@ use std::{
/// a given address.
const MAX_KNOWN_EXTERNAL_ADDRESSES: usize = 32;
/// Default value for Kademlia replication factor which determines to how many closest peers a
/// record is replicated to.
pub const DEFAULT_KADEMLIA_REPLICATION_FACTOR: usize = 20;
/// `DiscoveryBehaviour` configuration.
///
/// Note: In order to discover nodes or load and store values via Kademlia one has to add
@@ -101,6 +105,7 @@ pub struct DiscoveryConfig {
enable_mdns: bool,
kademlia_disjoint_query_paths: bool,
kademlia_protocols: Vec<Vec<u8>>,
kademlia_replication_factor: NonZeroUsize,
}
impl DiscoveryConfig {
@@ -116,6 +121,8 @@ impl DiscoveryConfig {
enable_mdns: false,
kademlia_disjoint_query_paths: false,
kademlia_protocols: Vec::new(),
kademlia_replication_factor: NonZeroUsize::new(DEFAULT_KADEMLIA_REPLICATION_FACTOR)
.expect("value is a constant; constant is non-zero; qed."),
}
}
@@ -182,6 +189,12 @@ impl DiscoveryConfig {
self
}
/// Sets Kademlia replication factor.
pub fn with_kademlia_replication_factor(&mut self, value: NonZeroUsize) -> &mut Self {
self.kademlia_replication_factor = value;
self
}
/// Create a `DiscoveryBehaviour` from this config.
pub fn finish(self) -> DiscoveryBehaviour {
let Self {
@@ -194,10 +207,13 @@ impl DiscoveryConfig {
enable_mdns,
kademlia_disjoint_query_paths,
kademlia_protocols,
kademlia_replication_factor,
} = self;
let kademlia = if !kademlia_protocols.is_empty() {
let mut config = KademliaConfig::default();
config.set_replication_factor(kademlia_replication_factor);
config.set_protocol_names(kademlia_protocols.into_iter().map(Into::into).collect());
// By default Kademlia attempts to insert all peers into its routing table once a
// dialing attempt succeeds. In order to control which peer is added, disable the