Skip to content

Commit af0abea

Browse files
committed
feat: allow setting Kad K_VALUE using feat flag
Introduces new feature flags to change the `K_VALUE` for Kademlia. The new feat flags are: `k-4`, `k-8`, `k-12` and `k-16`.
1 parent 92d48a5 commit af0abea

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

libp2p/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ floodsub = ["dep:libp2p-floodsub"]
6464
gossipsub = ["dep:libp2p-gossipsub", "libp2p-metrics?/gossipsub"]
6565
identify = ["dep:libp2p-identify", "libp2p-metrics?/identify"]
6666
json = ["libp2p-request-response?/json"]
67+
k-4 = ["kad"]
68+
k-8 = ["kad"]
69+
k-12 = ["kad"]
70+
k-16 = ["kad"]
6771
kad = ["dep:libp2p-kad", "libp2p-metrics?/kad"]
6872
macros = ["libp2p-swarm/macros"]
6973
mdns = ["dep:libp2p-mdns"]

protocols/kad/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] }
4545

4646
[features]
4747
serde = ["dep:serde", "bytes/serde"]
48+
k-4 = []
49+
k-8 = []
50+
k-12 = []
51+
k-16 = []
4852

4953
# Passing arguments to the docsrs builder in order to properly document cfg's.
5054
# More information: https://docs.rs/about/builds#cross-compiling

protocols/kad/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ pub use protocol::{ConnectionType, KadPeer};
7474
pub use query::QueryId;
7575
pub use record::{store, Key as RecordKey, ProviderRecord, Record};
7676

77+
#[cfg(feature = "k-4")]
78+
const K: usize = 4;
79+
#[cfg(feature = "k-8")]
80+
const K: usize = 8;
81+
#[cfg(feature = "k-12")]
82+
const K: usize = 12;
83+
#[cfg(feature = "k-16")]
84+
const K: usize = 16;
85+
#[cfg(not(any(feature = "k-4", feature = "k-8", feature = "k-12", feature = "k-16")))]
86+
const K: usize = 20;
87+
7788
/// The `k` parameter of the Kademlia specification.
7889
///
7990
/// This parameter determines:
@@ -86,9 +97,7 @@ pub use record::{store, Key as RecordKey, ProviderRecord, Record};
8697
/// The choice of (1) is fixed to this constant. The replication factor is configurable
8798
/// but should generally be no greater than `K_VALUE`. All nodes in a Kademlia
8899
/// DHT should agree on the choices made for (1) and (2).
89-
///
90-
/// The current value is `20`.
91-
pub const K_VALUE: NonZeroUsize = NonZeroUsize::new(20).unwrap();
100+
pub const K_VALUE: NonZeroUsize = NonZeroUsize::new(K).unwrap();
92101

93102
/// The `α` parameter of the Kademlia specification.
94103
///

0 commit comments

Comments
 (0)