From f7abf4741ecd82a5fcd8a9bc71970b67f00df017 Mon Sep 17 00:00:00 2001 From: guillaumemichel Date: Wed, 24 Jan 2024 20:38:26 +0100 Subject: [PATCH 1/6] fix: remove default IPFS protocol name --- examples/ipfs-kad/src/main.rs | 8 +++-- misc/server/src/behaviour.rs | 8 +++-- protocols/kad/CHANGELOG.md | 5 ++++ protocols/kad/src/behaviour.rs | 45 ++++++++++++++++++++--------- protocols/kad/src/behaviour/test.rs | 14 ++++----- protocols/kad/src/protocol.rs | 18 ++++++++++++ protocols/kad/tests/client_mode.rs | 3 +- 7 files changed, 75 insertions(+), 26 deletions(-) diff --git a/examples/ipfs-kad/src/main.rs b/examples/ipfs-kad/src/main.rs index 0d11bdd851a..8cdf6f3d546 100644 --- a/examples/ipfs-kad/src/main.rs +++ b/examples/ipfs-kad/src/main.rs @@ -23,11 +23,13 @@ use std::num::NonZeroUsize; use std::ops::Add; use std::time::{Duration, Instant}; +use std::iter; use anyhow::{bail, Result}; use clap::Parser; use futures::StreamExt; -use libp2p::{bytes::BufMut, identity, kad, noise, swarm::SwarmEvent, tcp, yamux, PeerId}; +use libp2p::{bytes::BufMut, identity, kad, noise, tcp, yamux, PeerId}; +use libp2p::swarm::{SwarmEvent, StreamProtocol}; use tracing_subscriber::EnvFilter; const BOOTNODES: [&str; 4] = [ @@ -37,6 +39,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() @@ -56,7 +60,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(iter::once(IPFS_PROTO_NAME).collect()); 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) diff --git a/misc/server/src/behaviour.rs b/misc/server/src/behaviour.rs index ec025e02129..493a8a9cbbc 100644 --- a/misc/server/src/behaviour.rs +++ b/misc/server/src/behaviour.rs @@ -4,7 +4,9 @@ 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::iter; use std::str::FromStr; use std::time::Duration; @@ -15,6 +17,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, @@ -31,7 +35,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(iter::once(IPFS_PROTO_NAME).collect()); // Instantly remove records and provider records. // // TODO: Replace hack with option to disable both. diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index b27a6943659..5ea87d45b8e 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.46.0 -- unreleased + +- Make it mandatory to provide protocol names when creating a `kad::Config`. Deprecate `kad::Config::default()`, replaced by `kad::Config::new(Vec)`. + 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. diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index f7e2fb3a032..6e72a196a00 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -24,7 +24,7 @@ mod test; use crate::addresses::Addresses; use crate::handler::{Handler, HandlerEvent, HandlerIn, RequestId}; -use crate::jobs::*; +use crate::{jobs::*, protocol}; use crate::kbucket::{self, Distance, KBucketsTable, NodeStatus}; use crate::protocol::{ConnectionType, KadPeer, ProtocolConfig}; use crate::query::{Query, QueryConfig, QueryId, QueryPool, QueryPoolState}; @@ -50,6 +50,7 @@ use libp2p_swarm::{ use smallvec::SmallVec; use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; use std::fmt; +use std::iter; use std::num::NonZeroUsize; use std::task::{Context, Poll, Waker}; use std::time::Duration; @@ -184,20 +185,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(iter::once(protocol::DEFAULT_PROTO_NAME).collect()) } } @@ -217,6 +209,29 @@ pub enum Caching { } impl Config { + /// Builds a new `Config` with the given protocol names. + pub fn new(protocol_names: Vec) -> Self { + Config { + kbucket_pending_timeout: Duration::from_secs(60), + query_config: QueryConfig::default(), + protocol_config: ProtocolConfig::new(protocol_names), + 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")] + pub fn default() -> Self { + Default::default() + } + /// Sets custom protocol names. /// /// Kademlia nodes only communicate with other nodes using the same protocol @@ -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) -> &mut Self { self.protocol_config.set_protocol_names(names); self diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 522eebcba92..954d3fd7e12 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -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::{ @@ -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(iter::once(PROTOCOL_NAME).collect()); if rng.gen() { cfg.disjoint_query_paths(true); } @@ -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(iter::once(PROTOCOL_NAME).collect()); config.set_replication_factor(replication_factor); if rng.gen() { config.disjoint_query_paths(true); @@ -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(iter::once(PROTOCOL_NAME).collect()); config.set_replication_factor(replication_factor); if rng.gen() { config.disjoint_query_paths(true); @@ -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(iter::once(PROTOCOL_NAME).collect()).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(iter::once(PROTOCOL_NAME).collect()); config.disjoint_query_paths(true); // I.e. setting the amount disjoint paths to be explored to 2. config.set_parallelism(NonZeroUsize::new(2).unwrap()); @@ -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(iter::once(PROTOCOL_NAME).collect()); cfg.set_kbucket_inserts(BucketInserts::Manual); // 1 -> 2 -> [3 -> ...] let mut swarms = build_connected_nodes_with_config(3, 1, cfg); diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index 7fe2d1130b1..6c663f92b8b 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -144,6 +144,20 @@ pub struct ProtocolConfig { } impl ProtocolConfig { + /// Builds a new `ProtocolConfig` with the given protocol names. + pub fn new(protocol_names: Vec) -> Self { + ProtocolConfig { + protocol_names, + max_packet_size: DEFAULT_MAX_PACKET_SIZE, + } + } + + /// Returns the default configuration. + #[deprecated(note = "Use `ProtocolConfig::new` instead")] + pub fn default() -> Self { + Default::default() + } + /// Returns the configured protocol name. pub fn protocol_names(&self) -> &[StreamProtocol] { &self.protocol_names @@ -151,6 +165,7 @@ impl ProtocolConfig { /// 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) { self.protocol_names = names; } @@ -162,6 +177,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(), diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index f290a36b727..f3a245408a0 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -7,6 +7,7 @@ use libp2p_swarm_test::SwarmExt; use tracing_subscriber::EnvFilter; use Event::*; use MyBehaviourEvent::*; +use std::iter; #[async_std::test] async fn server_gets_added_to_routing_table_by_client() { @@ -179,7 +180,7 @@ impl MyBehaviour { kad: Behaviour::with_config( local_peer_id, MemoryStore::new(local_peer_id), - Config::default(), + Config::new(iter::once(libp2p_kad::PROTOCOL_NAME).collect()), ), } } From 7dbf96a07cf964c257990414620c1f14e73b70fb Mon Sep 17 00:00:00 2001 From: guillaumemichel Date: Thu, 25 Jan 2024 19:05:51 +0100 Subject: [PATCH 2/6] correcting failed tests --- examples/ipfs-kad/src/main.rs | 4 ++-- misc/server/CHANGELOG.md | 6 ++++++ misc/server/Cargo.toml | 2 +- protocols/kad/Cargo.toml | 2 +- protocols/kad/src/behaviour.rs | 5 +++-- protocols/kad/src/behaviour/test.rs | 7 ++++++- protocols/kad/src/protocol.rs | 3 ++- protocols/kad/tests/client_mode.rs | 2 +- 8 files changed, 22 insertions(+), 9 deletions(-) diff --git a/examples/ipfs-kad/src/main.rs b/examples/ipfs-kad/src/main.rs index 8cdf6f3d546..149b86d7cb2 100644 --- a/examples/ipfs-kad/src/main.rs +++ b/examples/ipfs-kad/src/main.rs @@ -20,16 +20,16 @@ #![doc = include_str!("../README.md")] +use std::iter; use std::num::NonZeroUsize; use std::ops::Add; use std::time::{Duration, Instant}; -use std::iter; use anyhow::{bail, Result}; use clap::Parser; use futures::StreamExt; +use libp2p::swarm::{StreamProtocol, SwarmEvent}; use libp2p::{bytes::BufMut, identity, kad, noise, tcp, yamux, PeerId}; -use libp2p::swarm::{SwarmEvent, StreamProtocol}; use tracing_subscriber::EnvFilter; const BOOTNODES: [&str; 4] = [ diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index e4c5dd4a103..c652e40e69b 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.12.6 -- unreleased + +### Changed +- Stop using kad default protocol. + See [PR 5122](https://github.com/libp2p/rust-libp2p/pull/5122) + ## 0.12.5 ### Added diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index 10af2cab4bf..8f1115fbb69 100644 --- a/misc/server/Cargo.toml +++ b/misc/server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-server" -version = "0.12.5" +version = "0.12.6" authors = ["Max Inden "] edition = "2021" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 96f5f10819f..0c1d7ee8bf9 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = { workspace = true } description = "Kademlia protocol for libp2p" -version = "0.45.3" +version = "0.46.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 6e72a196a00..a6065deda92 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -24,7 +24,6 @@ mod test; use crate::addresses::Addresses; use crate::handler::{Handler, HandlerEvent, HandlerIn, RequestId}; -use crate::{jobs::*, protocol}; use crate::kbucket::{self, Distance, KBucketsTable, NodeStatus}; use crate::protocol::{ConnectionType, KadPeer, ProtocolConfig}; use crate::query::{Query, QueryConfig, QueryId, QueryPool, QueryPoolState}; @@ -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}; @@ -186,7 +186,7 @@ pub struct Config { impl Default for Config { /// Returns the default configuration. - /// + /// /// Deprecated: use `Config::new` instead. fn default() -> Self { Self::new(iter::once(protocol::DEFAULT_PROTO_NAME).collect()) @@ -228,6 +228,7 @@ impl Config { /// Returns the default configuration. #[deprecated(note = "Use `Config::new` instead")] + #[allow(clippy::should_implement_trait)] pub fn default() -> Self { Default::default() } diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 954d3fd7e12..8456a55c23e 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -1083,7 +1083,12 @@ fn exp_decr_expiration_overflow() { } // Right shifting a u64 by >63 results in a panic. - prop_no_panic(Config::new(iter::once(PROTOCOL_NAME).collect()).record_ttl.unwrap(), 64); + prop_no_panic( + Config::new(iter::once(PROTOCOL_NAME).collect()) + .record_ttl + .unwrap(), + 64, + ); quickcheck(prop_no_panic as fn(_, _)) } diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index 6c663f92b8b..0f13b73a776 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -154,6 +154,7 @@ impl ProtocolConfig { /// Returns the default configuration. #[deprecated(note = "Use `ProtocolConfig::new` instead")] + #[allow(clippy::should_implement_trait)] pub fn default() -> Self { Default::default() } @@ -178,7 +179,7 @@ impl ProtocolConfig { impl Default for ProtocolConfig { /// Returns the default configuration. - /// + /// /// Deprecated: use `ProtocolConfig::new` instead. fn default() -> Self { ProtocolConfig { diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index f3a245408a0..87912b1a0f9 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -4,10 +4,10 @@ use libp2p_kad::store::MemoryStore; use libp2p_kad::{Behaviour, Config, Event, Mode}; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; +use std::iter; use tracing_subscriber::EnvFilter; use Event::*; use MyBehaviourEvent::*; -use std::iter; #[async_std::test] async fn server_gets_added_to_routing_table_by_client() { From ec77ddcc84837bbb65e168b5d288f2a0c6f4367b Mon Sep 17 00:00:00 2001 From: guillaumemichel Date: Thu, 25 Jan 2024 19:15:23 +0100 Subject: [PATCH 3/6] updated Cargo.toml --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 931c2bcc8db..6452f315dc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2850,7 +2850,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.45.3" +version = "0.46.0" dependencies = [ "arrayvec", "async-std", @@ -3216,7 +3216,7 @@ dependencies = [ [[package]] name = "libp2p-server" -version = "0.12.5" +version = "0.12.6" dependencies = [ "base64 0.21.7", "clap", diff --git a/Cargo.toml b/Cargo.toml index 3f0c0217e9e..3a3ea627b6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.1", path = "protocols/identify" } libp2p-identity = { version = "0.2.8" } -libp2p-kad = { version = "0.45.3", path = "protocols/kad" } +libp2p-kad = { version = "0.46.0", 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" } @@ -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.1", 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.1", path = "swarm" } libp2p-swarm-derive = { version = "=0.34.2", 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. From c17d94a2886b9f8a8c80494e628236ffa0128fef Mon Sep 17 00:00:00 2001 From: guillaumemichel Date: Tue, 30 Jan 2024 20:47:55 +0100 Subject: [PATCH 4/6] updating version number --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/kad/CHANGELOG.md | 2 +- protocols/kad/Cargo.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 832ffff38d2..b44135bd9a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2850,7 +2850,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.46.0" +version = "0.45.4" dependencies = [ "arrayvec", "async-std", diff --git a/Cargo.toml b/Cargo.toml index d71f413c166..ebd0cf7cbad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.46.0", 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" } diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 5ea87d45b8e..31e5b80c345 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.46.0 -- unreleased +## 0.45.4 -- unreleased - Make it mandatory to provide protocol names when creating a `kad::Config`. Deprecate `kad::Config::default()`, replaced by `kad::Config::new(Vec)`. See [PR 5122](https://github.com/libp2p/rust-libp2p/pull/5122). diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 0c1d7ee8bf9..bde0d5f7c84 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = { workspace = true } description = "Kademlia protocol for libp2p" -version = "0.46.0" +version = "0.45.4" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 73b33648cac6c2a21dee0daa4a4b1c9c16b4ed33 Mon Sep 17 00:00:00 2001 From: guillaumemichel Date: Wed, 14 Feb 2024 08:58:12 +0100 Subject: [PATCH 5/6] new() now wants a single protocol --- examples/ipfs-kad/src/main.rs | 3 +-- misc/server/src/behaviour.rs | 3 +-- protocols/kad/src/behaviour.rs | 9 ++++----- protocols/kad/src/behaviour/test.rs | 17 ++++++----------- protocols/kad/src/protocol.rs | 6 +++--- protocols/kad/tests/client_mode.rs | 3 +-- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/examples/ipfs-kad/src/main.rs b/examples/ipfs-kad/src/main.rs index 149b86d7cb2..95921d6fa35 100644 --- a/examples/ipfs-kad/src/main.rs +++ b/examples/ipfs-kad/src/main.rs @@ -20,7 +20,6 @@ #![doc = include_str!("../README.md")] -use std::iter; use std::num::NonZeroUsize; use std::ops::Add; use std::time::{Duration, Instant}; @@ -60,7 +59,7 @@ async fn main() -> Result<()> { .with_dns()? .with_behaviour(|key| { // Create a Kademlia behaviour. - let mut cfg = kad::Config::new(iter::once(IPFS_PROTO_NAME).collect()); + 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) diff --git a/misc/server/src/behaviour.rs b/misc/server/src/behaviour.rs index 493a8a9cbbc..36b18c9798d 100644 --- a/misc/server/src/behaviour.rs +++ b/misc/server/src/behaviour.rs @@ -6,7 +6,6 @@ use libp2p::relay; use libp2p::swarm::behaviour::toggle::Toggle; use libp2p::swarm::{NetworkBehaviour, StreamProtocol}; use libp2p::{identity, Multiaddr, PeerId}; -use std::iter; use std::str::FromStr; use std::time::Duration; @@ -35,7 +34,7 @@ impl Behaviour { enable_autonat: bool, ) -> Self { let kademlia = if enable_kademlia { - let mut kademlia_config = kad::Config::new(iter::once(IPFS_PROTO_NAME).collect()); + let mut kademlia_config = kad::Config::new(IPFS_PROTO_NAME); // Instantly remove records and provider records. // // TODO: Replace hack with option to disable both. diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index a6065deda92..b237fe11dda 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -50,7 +50,6 @@ use libp2p_swarm::{ use smallvec::SmallVec; use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; use std::fmt; -use std::iter; use std::num::NonZeroUsize; use std::task::{Context, Poll, Waker}; use std::time::Duration; @@ -189,7 +188,7 @@ impl Default for Config { /// /// Deprecated: use `Config::new` instead. fn default() -> Self { - Self::new(iter::once(protocol::DEFAULT_PROTO_NAME).collect()) + Self::new(protocol::DEFAULT_PROTO_NAME) } } @@ -209,12 +208,12 @@ pub enum Caching { } impl Config { - /// Builds a new `Config` with the given protocol names. - pub fn new(protocol_names: Vec) -> Self { + /// 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_names), + 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)), diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 8456a55c23e..20378bb6a3f 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -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::new(iter::once(PROTOCOL_NAME).collect()); + let mut cfg = Config::new(PROTOCOL_NAME); if rng.gen() { cfg.disjoint_query_paths(true); } @@ -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::new(iter::once(PROTOCOL_NAME).collect()); + let mut config = Config::new(PROTOCOL_NAME); config.set_replication_factor(replication_factor); if rng.gen() { config.disjoint_query_paths(true); @@ -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::new(iter::once(PROTOCOL_NAME).collect()); + let mut config = Config::new(PROTOCOL_NAME); config.set_replication_factor(replication_factor); if rng.gen() { config.disjoint_query_paths(true); @@ -1083,19 +1083,14 @@ fn exp_decr_expiration_overflow() { } // Right shifting a u64 by >63 results in a panic. - prop_no_panic( - Config::new(iter::once(PROTOCOL_NAME).collect()) - .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::new(iter::once(PROTOCOL_NAME).collect()); + 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()); @@ -1243,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::new(iter::once(PROTOCOL_NAME).collect()); + 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); diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index 0f13b73a776..5abe2089852 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -144,10 +144,10 @@ pub struct ProtocolConfig { } impl ProtocolConfig { - /// Builds a new `ProtocolConfig` with the given protocol names. - pub fn new(protocol_names: Vec) -> Self { + /// Builds a new `ProtocolConfig` with the given protocol name. + pub fn new(protocol_name: StreamProtocol) -> Self { ProtocolConfig { - protocol_names, + protocol_names: vec![protocol_name], max_packet_size: DEFAULT_MAX_PACKET_SIZE, } } diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index 87912b1a0f9..6aceeb27263 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -4,7 +4,6 @@ use libp2p_kad::store::MemoryStore; use libp2p_kad::{Behaviour, Config, Event, Mode}; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use std::iter; use tracing_subscriber::EnvFilter; use Event::*; use MyBehaviourEvent::*; @@ -180,7 +179,7 @@ impl MyBehaviour { kad: Behaviour::with_config( local_peer_id, MemoryStore::new(local_peer_id), - Config::new(iter::once(libp2p_kad::PROTOCOL_NAME).collect()), + Config::new(libp2p_kad::PROTOCOL_NAME), ), } } From 555c196ff70c19b6a4c6cdd9d72197d90433fd58 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 14 Feb 2024 19:01:39 +1100 Subject: [PATCH 6/6] Apply suggestions from code review --- misc/server/CHANGELOG.md | 2 +- protocols/kad/CHANGELOG.md | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index c652e40e69b..484964e27e9 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.12.6 -- unreleased +## 0.12.6 ### Changed - Stop using kad default protocol. diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 31e5b80c345..f7baee2d288 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,6 +1,7 @@ -## 0.45.4 -- unreleased +## 0.45.4 -- Make it mandatory to provide protocol names when creating a `kad::Config`. Deprecate `kad::Config::default()`, replaced by `kad::Config::new(Vec)`. +- 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