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

Commit

Permalink
expose kademlia replication factor through node CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
klbrvik committed Jun 15, 2023
1 parent 075ba84 commit dd87e54
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
9 changes: 8 additions & 1 deletion client/cli/src/params/network_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sc_service::{
config::{Multiaddr, MultiaddrWithPeerId},
ChainSpec, ChainType,
};
use std::{borrow::Cow, path::PathBuf};
use std::{borrow::Cow, num::NonZeroUsize, path::PathBuf};

/// Parameters used to create the network configuration.
#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -121,6 +121,12 @@ pub struct NetworkParams {
#[arg(long)]
pub kademlia_disjoint_query_paths: bool,

/// Kademlia replication factor determines to how many closest peers a record is replicated to.
/// Default value is 20. Discovery mechanism requires successful replication to all
/// `kademlia_replication_factor` peers to consider record successfully put.
#[arg(long)]
pub kademlia_replication_factor: Option<NonZeroUsize>,

/// Join the IPFS network and serve transactions over bitswap protocol.
#[arg(long)]
pub ipfs_server: bool,
Expand Down Expand Up @@ -233,6 +239,7 @@ impl NetworkParams {
enable_dht_random_walk: !self.reserved_only,
allow_non_globals_in_dht,
kademlia_disjoint_query_paths: self.kademlia_disjoint_query_paths,
kademlia_replication_factor: self.kademlia_replication_factor,
yamux_window_size: None,
ipfs_server: self.ipfs_server,
sync_mode: self.sync.into(),
Expand Down
5 changes: 5 additions & 0 deletions client/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use std::{
io::{self, Write},
iter,
net::Ipv4Addr,
num::NonZeroUsize,
path::{Path, PathBuf},
pin::Pin,
str::{self, FromStr},
Expand Down Expand Up @@ -605,6 +606,9 @@ pub struct NetworkConfiguration {
/// the presence of potentially adversarial nodes.
pub kademlia_disjoint_query_paths: bool,

/// None will end up using default Kadmelia value.
pub kademlia_replication_factor: Option<NonZeroUsize>,

/// Enable serving block data over IPFS bitswap.
pub ipfs_server: bool,

Expand Down Expand Up @@ -656,6 +660,7 @@ impl NetworkConfiguration {
enable_dht_random_walk: true,
allow_non_globals_in_dht: false,
kademlia_disjoint_query_paths: false,
kademlia_replication_factor: None,
yamux_window_size: None,
ipfs_server: false,
}
Expand Down
13 changes: 13 additions & 0 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct DiscoveryConfig {
enable_mdns: bool,
kademlia_disjoint_query_paths: bool,
kademlia_protocols: Vec<Vec<u8>>,
kademlia_replication_factor: Option<NonZeroUsize>,
}

impl DiscoveryConfig {
Expand All @@ -116,6 +117,7 @@ impl DiscoveryConfig {
enable_mdns: false,
kademlia_disjoint_query_paths: false,
kademlia_protocols: Vec::new(),
kademlia_replication_factor: None,
}
}

Expand Down Expand Up @@ -182,6 +184,11 @@ impl DiscoveryConfig {
self
}

pub fn with_kademlia_replication_factor(&mut self, value: NonZeroUsize) -> &mut Self {
self.kademlia_replication_factor = Some(value);
self
}

/// Create a `DiscoveryBehaviour` from this config.
pub fn finish(self) -> DiscoveryBehaviour {
let Self {
Expand All @@ -194,10 +201,16 @@ 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();

if let Some(replication_factor) = kademlia_replication_factor {
config.set_replication_factor(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
Expand Down
8 changes: 7 additions & 1 deletion client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,13 @@ where
network_config.kademlia_disjoint_query_paths,
);

match network_config.transport {
if let Some(kademlia_replication_factor) =
params.network_config.kademlia_replication_factor
{
config.with_kademlia_replication_factor(kademlia_replication_factor);
}

match params.network_config.transport {
TransportConfig::MemoryOnly => {
config.with_mdns(false);
config.allow_private_ip(false);
Expand Down
13 changes: 11 additions & 2 deletions frame/democracy/src/migrations/unlock_and_unreserve_all_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,17 @@ where

log::info!(target: LOG_TARGET, "Total accounts: {:?}", all_accounts.len());
log::info!(target: LOG_TARGET, "Total stake to unlock: {:?}", total_stake_to_unlock);
log::info!(target: LOG_TARGET, "Total deposit to unreserve: {:?}", total_deposits_to_unreserve);
log::info!(target: LOG_TARGET, "Bugged deposits: {}/{}", bugged_deposits, account_deposits.len());
log::info!(
target: LOG_TARGET,
"Total deposit to unreserve: {:?}",
total_deposits_to_unreserve
);
log::info!(
target: LOG_TARGET,
"Bugged deposits: {}/{}",
bugged_deposits,
account_deposits.len()
);

Ok(account_reserved_before.encode())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,20 @@ where
let total_stake_to_unlock = account_staked_sums.clone().into_values().sum::<BalanceOf<T>>();
let total_deposits_to_unreserve =
account_deposited_sums.clone().into_values().sum::<BalanceOf<T>>();
log::info!(target: LOG_TARGET, "Total accounts: {:?}", all_accounts.len());
log::info!(target: LOG_TARGET, "Total accounts: {:?}", all_accounts.len());
log::info!(target: LOG_TARGET, "Total stake to unlock: {:?}", total_stake_to_unlock);
log::info!(target: LOG_TARGET, "Total deposit to unreserve: {:?}", total_deposits_to_unreserve);
log::info!(
target: LOG_TARGET,
"Total deposit to unreserve: {:?}",
total_deposits_to_unreserve
);
if bugged_deposits > 0 {
log::warn!(target: LOG_TARGET, "Bugged deposits: {}/{}", bugged_deposits, all_accounts.len());
log::warn!(
target: LOG_TARGET,
"Bugged deposits: {}/{}",
bugged_deposits,
all_accounts.len()
);
}

Ok(account_reserved_before.encode())
Expand Down

0 comments on commit dd87e54

Please sign in to comment.