Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit fe1f8ba

Browse files
klbrvikbkchr
andauthored
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 <[email protected]> * wrap CLI value as Option * make kademlia replication factor non-optional --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent e72781c commit fe1f8ba

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

client/cli/src/params/network_params.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use sc_service::{
2828
config::{Multiaddr, MultiaddrWithPeerId},
2929
ChainSpec, ChainType,
3030
};
31-
use std::{borrow::Cow, path::PathBuf};
31+
use std::{borrow::Cow, num::NonZeroUsize, path::PathBuf};
3232

3333
/// Parameters used to create the network configuration.
3434
#[derive(Debug, Clone, Args)]
@@ -121,6 +121,13 @@ pub struct NetworkParams {
121121
#[arg(long)]
122122
pub kademlia_disjoint_query_paths: bool,
123123

124+
/// Kademlia replication factor determines to how many closest peers a record is replicated to.
125+
///
126+
/// Discovery mechanism requires successful replication to all
127+
/// `kademlia_replication_factor` peers to consider record successfully put.
128+
#[arg(long, default_value = "20")]
129+
pub kademlia_replication_factor: NonZeroUsize,
130+
124131
/// Join the IPFS network and serve transactions over bitswap protocol.
125132
#[arg(long)]
126133
pub ipfs_server: bool,
@@ -233,6 +240,7 @@ impl NetworkParams {
233240
enable_dht_random_walk: !self.reserved_only,
234241
allow_non_globals_in_dht,
235242
kademlia_disjoint_query_paths: self.kademlia_disjoint_query_paths,
243+
kademlia_replication_factor: self.kademlia_replication_factor,
236244
yamux_window_size: None,
237245
ipfs_server: self.ipfs_server,
238246
sync_mode: self.sync.into(),

client/network/src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//! See the documentation of [`Params`].
2323
2424
pub use crate::{
25+
discovery::DEFAULT_KADEMLIA_REPLICATION_FACTOR,
2526
protocol::NotificationsSink,
2627
request_responses::{
2728
IncomingRequest, OutgoingResponse, ProtocolConfig as RequestResponseConfig,
@@ -50,6 +51,7 @@ use std::{
5051
io::{self, Write},
5152
iter,
5253
net::Ipv4Addr,
54+
num::NonZeroUsize,
5355
path::{Path, PathBuf},
5456
pin::Pin,
5557
str::{self, FromStr},
@@ -605,6 +607,12 @@ pub struct NetworkConfiguration {
605607
/// the presence of potentially adversarial nodes.
606608
pub kademlia_disjoint_query_paths: bool,
607609

610+
/// Kademlia replication factor determines to how many closest peers a record is replicated to.
611+
///
612+
/// Discovery mechanism requires successful replication to all
613+
/// `kademlia_replication_factor` peers to consider record successfully put.
614+
pub kademlia_replication_factor: NonZeroUsize,
615+
608616
/// Enable serving block data over IPFS bitswap.
609617
pub ipfs_server: bool,
610618

@@ -656,6 +664,8 @@ impl NetworkConfiguration {
656664
enable_dht_random_walk: true,
657665
allow_non_globals_in_dht: false,
658666
kademlia_disjoint_query_paths: false,
667+
kademlia_replication_factor: NonZeroUsize::new(DEFAULT_KADEMLIA_REPLICATION_FACTOR)
668+
.expect("value is a constant; constant is non-zero; qed."),
659669
yamux_window_size: None,
660670
ipfs_server: false,
661671
}

client/network/src/discovery.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ use std::{
8787
/// a given address.
8888
const MAX_KNOWN_EXTERNAL_ADDRESSES: usize = 32;
8989

90+
/// Default value for Kademlia replication factor which determines to how many closest peers a
91+
/// record is replicated to.
92+
pub const DEFAULT_KADEMLIA_REPLICATION_FACTOR: usize = 20;
93+
9094
/// `DiscoveryBehaviour` configuration.
9195
///
9296
/// Note: In order to discover nodes or load and store values via Kademlia one has to add
@@ -101,6 +105,7 @@ pub struct DiscoveryConfig {
101105
enable_mdns: bool,
102106
kademlia_disjoint_query_paths: bool,
103107
kademlia_protocols: Vec<Vec<u8>>,
108+
kademlia_replication_factor: NonZeroUsize,
104109
}
105110

106111
impl DiscoveryConfig {
@@ -116,6 +121,8 @@ impl DiscoveryConfig {
116121
enable_mdns: false,
117122
kademlia_disjoint_query_paths: false,
118123
kademlia_protocols: Vec::new(),
124+
kademlia_replication_factor: NonZeroUsize::new(DEFAULT_KADEMLIA_REPLICATION_FACTOR)
125+
.expect("value is a constant; constant is non-zero; qed."),
119126
}
120127
}
121128

@@ -182,6 +189,12 @@ impl DiscoveryConfig {
182189
self
183190
}
184191

192+
/// Sets Kademlia replication factor.
193+
pub fn with_kademlia_replication_factor(&mut self, value: NonZeroUsize) -> &mut Self {
194+
self.kademlia_replication_factor = value;
195+
self
196+
}
197+
185198
/// Create a `DiscoveryBehaviour` from this config.
186199
pub fn finish(self) -> DiscoveryBehaviour {
187200
let Self {
@@ -194,10 +207,13 @@ impl DiscoveryConfig {
194207
enable_mdns,
195208
kademlia_disjoint_query_paths,
196209
kademlia_protocols,
210+
kademlia_replication_factor,
197211
} = self;
198212

199213
let kademlia = if !kademlia_protocols.is_empty() {
200214
let mut config = KademliaConfig::default();
215+
216+
config.set_replication_factor(kademlia_replication_factor);
201217
config.set_protocol_names(kademlia_protocols.into_iter().map(Into::into).collect());
202218
// By default Kademlia attempts to insert all peers into its routing table once a
203219
// dialing attempt succeeds. In order to control which peer is added, disable the

client/network/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ where
320320
config.use_kademlia_disjoint_query_paths(
321321
network_config.kademlia_disjoint_query_paths,
322322
);
323+
config.with_kademlia_replication_factor(network_config.kademlia_replication_factor);
323324

324325
match network_config.transport {
325326
TransportConfig::MemoryOnly => {

0 commit comments

Comments
 (0)