Skip to content

Commit 4fdc5dc

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 4fdc5dc

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

libp2p/Cargo.toml

+4
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

+4
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

+18-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ pub use protocol::{ConnectionType, KadPeer};
7474
pub use query::QueryId;
7575
pub use record::{store, Key as RecordKey, ProviderRecord, Record};
7676

77+
#[cfg(all(feature = "k-4", not(any(feature = "k-8", feature = "k-12", feature = "k-16"))))]
78+
const K: usize = 4;
79+
#[cfg(all(feature = "k-8", not(any(feature = "k-4", feature = "k-12", feature = "k-16"))))]
80+
const K: usize = 8;
81+
#[cfg(all(feature = "k-12", not(any(feature = "k-4", feature = "k-8", feature = "k-16"))))]
82+
const K: usize = 12;
83+
#[cfg(all(feature = "k-16", not(any(feature = "k-4", feature = "k-8", feature = "k-12"))))]
84+
const K: usize = 16;
85+
// Enable if none of the k-n features are enabled or if all the k-n features are enabled.
86+
#[cfg(any(not(any(feature = "k-4", feature = "k-8", feature = "k-12", feature = "k-16")), all(
87+
feature = "k-4",
88+
feature = "k-8",
89+
feature = "k-12",
90+
feature = "k-16"
91+
)))]
92+
const K: usize = 20;
93+
7794
/// The `k` parameter of the Kademlia specification.
7895
///
7996
/// This parameter determines:
@@ -86,9 +103,7 @@ pub use record::{store, Key as RecordKey, ProviderRecord, Record};
86103
/// The choice of (1) is fixed to this constant. The replication factor is configurable
87104
/// but should generally be no greater than `K_VALUE`. All nodes in a Kademlia
88105
/// 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();
106+
pub const K_VALUE: NonZeroUsize = NonZeroUsize::new(K).unwrap();
92107

93108
/// The `α` parameter of the Kademlia specification.
94109
///

0 commit comments

Comments
 (0)