diff --git a/Cargo.lock b/Cargo.lock index 4506011256b..a7750ab7e6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2851,7 +2851,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.45.3" +version = "0.45.4" dependencies = [ "arrayvec", "async-std", @@ -3217,7 +3217,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 0624c44b390..3d7097f9a88 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.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" } @@ -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. diff --git a/examples/ipfs-kad/src/main.rs b/examples/ipfs-kad/src/main.rs index 0d11bdd851a..95921d6fa35 100644 --- a/examples/ipfs-kad/src/main.rs +++ b/examples/ipfs-kad/src/main.rs @@ -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] = [ @@ -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() @@ -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) diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index e4c5dd4a103..484964e27e9 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -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 diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index f300e177b6c..9653ddc5d76 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/misc/server/src/behaviour.rs b/misc/server/src/behaviour.rs index ec025e02129..36b18c9798d 100644 --- a/misc/server/src/behaviour.rs +++ b/misc/server/src/behaviour.rs @@ -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; @@ -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, @@ -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. diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index b27a6943659..f7baee2d288 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -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. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 96f5f10819f..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.45.3" +version = "0.45.4" 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 f7e2fb3a032..b237fe11dda 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::*; 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}; @@ -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) } } @@ -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 @@ -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..20378bb6a3f 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(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::default(); + 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::default(); + let mut config = Config::new(PROTOCOL_NAME); 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(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()); @@ -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); diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index 7fe2d1130b1..5abe2089852 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -144,6 +144,21 @@ 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 @@ -151,6 +166,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 +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(), diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index f290a36b727..6aceeb27263 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -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), ), } }