Skip to content

Commit

Permalink
fix(kad): remove default IPFS protocol name
Browse files Browse the repository at this point in the history
Fixes: #5006.
Related: #5015.

Pull-Request: #5122.
  • Loading branch information
guillaumemichel authored Feb 19, 2024
1 parent 947b884 commit ea9b239
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.46.1", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.44.2", path = "protocols/identify" }
libp2p-identity = { version = "0.2.8" }
libp2p-kad = { version = "0.45.3", path = "protocols/kad" }
libp2p-kad = { version = "0.45.4", path = "protocols/kad" }
libp2p-mdns = { version = "0.45.1", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.14.1", path = "misc/metrics" }
Expand All @@ -100,7 +100,7 @@ libp2p-quic = { version = "0.10.2", path = "transports/quic" }
libp2p-relay = { version = "0.17.1", path = "protocols/relay" }
libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.2", path = "protocols/request-response" }
libp2p-server = { version = "0.12.5", path = "misc/server" }
libp2p-server = { version = "0.12.6", path = "misc/server" }
libp2p-stream = { version = "0.1.0-alpha", path = "protocols/stream" }
libp2p-swarm = { version = "0.44.2", path = "swarm" }
libp2p-swarm-derive = { version = "=0.34.3", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
Expand Down
7 changes: 5 additions & 2 deletions examples/ipfs-kad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use std::time::{Duration, Instant};
use anyhow::{bail, Result};
use clap::Parser;
use futures::StreamExt;
use libp2p::{bytes::BufMut, identity, kad, noise, swarm::SwarmEvent, tcp, yamux, PeerId};
use libp2p::swarm::{StreamProtocol, SwarmEvent};
use libp2p::{bytes::BufMut, identity, kad, noise, tcp, yamux, PeerId};
use tracing_subscriber::EnvFilter;

const BOOTNODES: [&str; 4] = [
Expand All @@ -37,6 +38,8 @@ const BOOTNODES: [&str; 4] = [
"QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
];

const IPFS_PROTO_NAME: StreamProtocol = StreamProtocol::new("/ipfs/kad/1.0.0");

#[tokio::main]
async fn main() -> Result<()> {
let _ = tracing_subscriber::fmt()
Expand All @@ -56,7 +59,7 @@ async fn main() -> Result<()> {
.with_dns()?
.with_behaviour(|key| {
// Create a Kademlia behaviour.
let mut cfg = kad::Config::default();
let mut cfg = kad::Config::new(IPFS_PROTO_NAME);
cfg.set_query_timeout(Duration::from_secs(5 * 60));
let store = kad::store::MemoryStore::new(key.public().to_peer_id());
kad::Behaviour::with_config(key.public().to_peer_id(), store, cfg)
Expand Down
6 changes: 6 additions & 0 deletions misc/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.12.6

### Changed
- Stop using kad default protocol.
See [PR 5122](https://github.com/libp2p/rust-libp2p/pull/5122)

## 0.12.5

### Added
Expand Down
2 changes: 1 addition & 1 deletion misc/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libp2p-server"
version = "0.12.5"
version = "0.12.6"
authors = ["Max Inden <mail@max-inden.de>"]
edition = "2021"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
7 changes: 5 additions & 2 deletions misc/server/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use libp2p::kad;
use libp2p::ping;
use libp2p::relay;
use libp2p::swarm::behaviour::toggle::Toggle;
use libp2p::{identity, swarm::NetworkBehaviour, Multiaddr, PeerId};
use libp2p::swarm::{NetworkBehaviour, StreamProtocol};
use libp2p::{identity, Multiaddr, PeerId};
use std::str::FromStr;
use std::time::Duration;

Expand All @@ -15,6 +16,8 @@ const BOOTNODES: [&str; 4] = [
"QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
];

const IPFS_PROTO_NAME: StreamProtocol = StreamProtocol::new("/ipfs/kad/1.0.0");

#[derive(NetworkBehaviour)]
pub(crate) struct Behaviour {
relay: relay::Behaviour,
Expand All @@ -31,7 +34,7 @@ impl Behaviour {
enable_autonat: bool,
) -> Self {
let kademlia = if enable_kademlia {
let mut kademlia_config = kad::Config::default();
let mut kademlia_config = kad::Config::new(IPFS_PROTO_NAME);
// Instantly remove records and provider records.
//
// TODO: Replace hack with option to disable both.
Expand Down
6 changes: 6 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.45.4

- Make it mandatory to provide protocol names when creating a `kad::Config`.
Deprecate `kad::Config::default()`, replaced by `kad::Config::new(StreamProtocol)`.
See [PR 5122](https://github.com/libp2p/rust-libp2p/pull/5122).

## 0.45.3

- The progress of the close query iterator shall be decided by ANY of the new peers.
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-kad"
edition = "2021"
rust-version = { workspace = true }
description = "Kademlia protocol for libp2p"
version = "0.45.3"
version = "0.45.4"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
45 changes: 31 additions & 14 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ mod test;

use crate::addresses::Addresses;
use crate::handler::{Handler, HandlerEvent, HandlerIn, RequestId};
use crate::jobs::*;
use crate::kbucket::{self, Distance, KBucketsTable, NodeStatus};
use crate::protocol::{ConnectionType, KadPeer, ProtocolConfig};
use crate::query::{Query, QueryConfig, QueryId, QueryPool, QueryPoolState};
Expand All @@ -34,6 +33,7 @@ use crate::record::{
ProviderRecord, Record,
};
use crate::K_VALUE;
use crate::{jobs::*, protocol};
use fnv::{FnvHashMap, FnvHashSet};
use instant::Instant;
use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr};
Expand Down Expand Up @@ -184,20 +184,11 @@ pub struct Config {
}

impl Default for Config {
/// Returns the default configuration.
///
/// Deprecated: use `Config::new` instead.
fn default() -> Self {
Config {
kbucket_pending_timeout: Duration::from_secs(60),
query_config: QueryConfig::default(),
protocol_config: Default::default(),
record_ttl: Some(Duration::from_secs(36 * 60 * 60)),
record_replication_interval: Some(Duration::from_secs(60 * 60)),
record_publication_interval: Some(Duration::from_secs(24 * 60 * 60)),
record_filtering: StoreInserts::Unfiltered,
provider_publication_interval: Some(Duration::from_secs(12 * 60 * 60)),
provider_record_ttl: Some(Duration::from_secs(24 * 60 * 60)),
kbucket_inserts: BucketInserts::OnConnected,
caching: Caching::Enabled { max_peers: 1 },
}
Self::new(protocol::DEFAULT_PROTO_NAME)
}
}

Expand All @@ -217,6 +208,30 @@ pub enum Caching {
}

impl Config {
/// Builds a new `Config` with the given protocol name.
pub fn new(protocol_name: StreamProtocol) -> Self {
Config {
kbucket_pending_timeout: Duration::from_secs(60),
query_config: QueryConfig::default(),
protocol_config: ProtocolConfig::new(protocol_name),
record_ttl: Some(Duration::from_secs(36 * 60 * 60)),
record_replication_interval: Some(Duration::from_secs(60 * 60)),
record_publication_interval: Some(Duration::from_secs(24 * 60 * 60)),
record_filtering: StoreInserts::Unfiltered,
provider_publication_interval: Some(Duration::from_secs(12 * 60 * 60)),
provider_record_ttl: Some(Duration::from_secs(24 * 60 * 60)),
kbucket_inserts: BucketInserts::OnConnected,
caching: Caching::Enabled { max_peers: 1 },
}
}

/// Returns the default configuration.
#[deprecated(note = "Use `Config::new` instead")]
#[allow(clippy::should_implement_trait)]
pub fn default() -> Self {
Default::default()
}

/// Sets custom protocol names.
///
/// Kademlia nodes only communicate with other nodes using the same protocol
Expand All @@ -226,6 +241,8 @@ impl Config {
/// More than one protocol name can be supplied. In this case the node will
/// be able to talk to other nodes supporting any of the provided names.
/// Multiple names must be used with caution to avoid network partitioning.
#[deprecated(note = "Use `Config::new` instead")]
#[allow(deprecated)]
pub fn set_protocol_names(&mut self, names: Vec<StreamProtocol>) -> &mut Self {
self.protocol_config.set_protocol_names(names);
self
Expand Down
14 changes: 7 additions & 7 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::*;

use crate::kbucket::Distance;
use crate::record::{store::MemoryStore, Key};
use crate::{K_VALUE, SHA_256_MH};
use crate::{K_VALUE, PROTOCOL_NAME, SHA_256_MH};
use futures::{executor::block_on, future::poll_fn, prelude::*};
use futures_timer::Delay;
use libp2p_core::{
Expand Down Expand Up @@ -173,7 +173,7 @@ fn bootstrap() {
// or smaller than K_VALUE.
let num_group = rng.gen_range(1..(num_total % K_VALUE.get()) + 2);

let mut cfg = Config::default();
let mut cfg = Config::new(PROTOCOL_NAME);
if rng.gen() {
cfg.disjoint_query_paths(true);
}
Expand Down Expand Up @@ -498,7 +498,7 @@ fn put_record() {
// At least 4 nodes, 1 under test + 3 bootnodes.
let num_total = usize::max(4, replication_factor.get() * 2);

let mut config = Config::default();
let mut config = Config::new(PROTOCOL_NAME);
config.set_replication_factor(replication_factor);
if rng.gen() {
config.disjoint_query_paths(true);
Expand Down Expand Up @@ -867,7 +867,7 @@ fn add_provider() {
// At least 4 nodes, 1 under test + 3 bootnodes.
let num_total = usize::max(4, replication_factor.get() * 2);

let mut config = Config::default();
let mut config = Config::new(PROTOCOL_NAME);
config.set_replication_factor(replication_factor);
if rng.gen() {
config.disjoint_query_paths(true);
Expand Down Expand Up @@ -1083,14 +1083,14 @@ fn exp_decr_expiration_overflow() {
}

// Right shifting a u64 by >63 results in a panic.
prop_no_panic(Config::default().record_ttl.unwrap(), 64);
prop_no_panic(Config::new(PROTOCOL_NAME).record_ttl.unwrap(), 64);

quickcheck(prop_no_panic as fn(_, _))
}

#[test]
fn disjoint_query_does_not_finish_before_all_paths_did() {
let mut config = Config::default();
let mut config = Config::new(PROTOCOL_NAME);
config.disjoint_query_paths(true);
// I.e. setting the amount disjoint paths to be explored to 2.
config.set_parallelism(NonZeroUsize::new(2).unwrap());
Expand Down Expand Up @@ -1238,7 +1238,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() {
/// the routing table with `BucketInserts::Manual`.
#[test]
fn manual_bucket_inserts() {
let mut cfg = Config::default();
let mut cfg = Config::new(PROTOCOL_NAME);
cfg.set_kbucket_inserts(BucketInserts::Manual);
// 1 -> 2 -> [3 -> ...]
let mut swarms = build_connected_nodes_with_config(3, 1, cfg);
Expand Down
19 changes: 19 additions & 0 deletions protocols/kad/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,29 @@ pub struct ProtocolConfig {
}

impl ProtocolConfig {
/// Builds a new `ProtocolConfig` with the given protocol name.
pub fn new(protocol_name: StreamProtocol) -> Self {
ProtocolConfig {
protocol_names: vec![protocol_name],
max_packet_size: DEFAULT_MAX_PACKET_SIZE,
}
}

/// Returns the default configuration.
#[deprecated(note = "Use `ProtocolConfig::new` instead")]
#[allow(clippy::should_implement_trait)]
pub fn default() -> Self {
Default::default()
}

/// Returns the configured protocol name.
pub fn protocol_names(&self) -> &[StreamProtocol] {
&self.protocol_names
}

/// Modifies the protocol names used on the wire. Can be used to create incompatibilities
/// between networks on purpose.
#[deprecated(note = "Use `ProtocolConfig::new` instead")]
pub fn set_protocol_names(&mut self, names: Vec<StreamProtocol>) {
self.protocol_names = names;
}
Expand All @@ -162,6 +178,9 @@ impl ProtocolConfig {
}

impl Default for ProtocolConfig {
/// Returns the default configuration.
///
/// Deprecated: use `ProtocolConfig::new` instead.
fn default() -> Self {
ProtocolConfig {
protocol_names: iter::once(DEFAULT_PROTO_NAME).collect(),
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/tests/client_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl MyBehaviour {
kad: Behaviour::with_config(
local_peer_id,
MemoryStore::new(local_peer_id),
Config::default(),
Config::new(libp2p_kad::PROTOCOL_NAME),
),
}
}
Expand Down

0 comments on commit ea9b239

Please sign in to comment.