Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
name: Test ${{ matrix.crate }}
runs-on: ${{ fromJSON(
github.repository == 'libp2p/rust-libp2p' && (
(contains(fromJSON('["libp2p-webrtc", "libp2p"]'), matrix.crate) && '["self-hosted", "linux", "x64", "2xlarge"]') ||
(contains(fromJSON('["libp2p-quic", "libp2p-perf"]'), matrix.crate) && '["self-hosted", "linux", "x64", "xlarge"]') ||
'["self-hosted", "linux", "x64", "large"]'
(contains(fromJSON('["libp2p-webrtc", "libp2p"]'), matrix.crate) && '["self-hosted", "linux", "x64", "2xlarge"]') ||
(contains(fromJSON('["libp2p-quic", "libp2p-perf"]'), matrix.crate) && '["self-hosted", "linux", "x64", "xlarge"]') ||
'["self-hosted", "linux", "x64", "large"]'
) || '"ubuntu-latest"') }}
timeout-minutes: 10
needs: gather_published_crates
Expand All @@ -47,7 +47,25 @@ jobs:
save-if: false

- name: Run all tests
run: cargo test --package "$CRATE" --all-features
run: |
if [ "$CRATE" = "libp2p-kad" ]; then
echo "Running tests for libp2p-kad with k-20 (default)"
cargo test --package libp2p-kad --all-features

echo "Running tests for libp2p-kad with k-4"
cargo test --package libp2p-kad --features serde,k-4

echo "Running tests for libp2p-kad with k-8"
cargo test --package libp2p-kad --features serde,k-8

echo "Running tests for libp2p-kad with k-12"
cargo test --package libp2p-kad --features serde,k-12

echo "Running tests for libp2p-kad with k-16"
cargo test --package libp2p-kad --features serde,k-16
else
cargo test --package "$CRATE" --all-features
fi

- name: Check if we compile without any features activated
run: cargo build --package "$CRATE" --no-default-features
Expand Down Expand Up @@ -338,7 +356,7 @@ jobs:

- name: Ensure `full` feature contains all features
run: |
ALL_FEATURES=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "libp2p") | .features | keys | map(select(. != "full")) | sort | join(" ")')
ALL_FEATURES=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "libp2p") | .features | keys | map(select(. != "full" and . != "k-4" and . != "k-8" and . != "k-12" and . != "k-16")) | sort | join(" ")')
FULL_FEATURE=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "libp2p") | .features["full"] | sort | join(" ")')

echo "$ALL_FEATURES";
Expand Down Expand Up @@ -382,10 +400,10 @@ jobs:

- name: Ensure generated files are unmodified # https://stackoverflow.com/a/5737794
run: |
git_status=$(git status --porcelain)

echo $git_status
test -z "$git_status"
git_status=$(git status --porcelain)
echo $git_status
test -z "$git_status"

ensure-lockfile-uptodate:
name: Ensure that `Cargo.lock` is up-to-date
Expand All @@ -398,7 +416,7 @@ jobs:
cargo-deny:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: EmbarkStudios/cargo-deny-action@v2
with:
command: check advisories bans licenses sources
- uses: actions/checkout@v4
- uses: EmbarkStudios/cargo-deny-action@v2
with:
command: check advisories bans licenses sources
4 changes: 4 additions & 0 deletions libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ floodsub = ["dep:libp2p-floodsub"]
gossipsub = ["dep:libp2p-gossipsub", "libp2p-metrics?/gossipsub"]
identify = ["dep:libp2p-identify", "libp2p-metrics?/identify"]
json = ["libp2p-request-response?/json"]
k-4 = ["kad"]
k-8 = ["kad"]
k-12 = ["kad"]
k-16 = ["kad"]
kad = ["dep:libp2p-kad", "libp2p-metrics?/kad"]
macros = ["libp2p-swarm/macros"]
mdns = ["dep:libp2p-mdns"]
Expand Down
4 changes: 4 additions & 0 deletions protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] }

[features]
serde = ["dep:serde", "bytes/serde"]
k-4 = []
k-8 = []
k-12 = []
k-16 = []

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
31 changes: 28 additions & 3 deletions protocols/kad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ pub use protocol::{ConnectionType, KadPeer};
pub use query::QueryId;
pub use record::{store, Key as RecordKey, ProviderRecord, Record};

#[cfg(all(
feature = "k-4",
not(any(feature = "k-8", feature = "k-12", feature = "k-16"))
))]
const K: usize = 4;
#[cfg(all(
feature = "k-8",
not(any(feature = "k-4", feature = "k-12", feature = "k-16"))
))]
const K: usize = 8;
#[cfg(all(
feature = "k-12",
not(any(feature = "k-4", feature = "k-8", feature = "k-16"))
))]
const K: usize = 12;
#[cfg(all(
feature = "k-16",
not(any(feature = "k-4", feature = "k-8", feature = "k-12"))
))]
const K: usize = 16;
// Enable if none of the k-n features are enabled or if all the k-n features are enabled.
#[cfg(any(
not(any(feature = "k-4", feature = "k-8", feature = "k-12", feature = "k-16")),
all(feature = "k-4", feature = "k-8", feature = "k-12", feature = "k-16")
))]
const K: usize = 20;

/// The `k` parameter of the Kademlia specification.
///
/// This parameter determines:
Expand All @@ -86,9 +113,7 @@ pub use record::{store, Key as RecordKey, ProviderRecord, Record};
/// The choice of (1) is fixed to this constant. The replication factor is configurable
/// but should generally be no greater than `K_VALUE`. All nodes in a Kademlia
/// DHT should agree on the choices made for (1) and (2).
///
/// The current value is `20`.
pub const K_VALUE: NonZeroUsize = NonZeroUsize::new(20).unwrap();
pub const K_VALUE: NonZeroUsize = NonZeroUsize::new(K).unwrap();

/// The `α` parameter of the Kademlia specification.
///
Expand Down
Loading