From b4d9119ec7718ad55448dd02137cd6d49297e21b Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 25 Aug 2022 14:33:21 +0300 Subject: [PATCH 1/4] Support multiple Kademlia protocol names --- Cargo.toml | 2 +- protocols/kad/CHANGELOG.md | 6 ++++++ protocols/kad/Cargo.toml | 2 +- protocols/kad/src/behaviour.rs | 16 ++++++++++------ protocols/kad/src/protocol.rs | 17 +++++++++-------- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 75867ddbf4f..9d92ba51b08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,7 +82,7 @@ libp2p-core = { version = "0.35.0", path = "core", default-features = false } libp2p-dcutr = { version = "0.5.0", path = "protocols/dcutr", optional = true } libp2p-floodsub = { version = "0.38.0", path = "protocols/floodsub", optional = true } libp2p-identify = { version = "0.38.0", path = "protocols/identify", optional = true } -libp2p-kad = { version = "0.39.0", path = "protocols/kad", optional = true } +libp2p-kad = { version = "0.39.1", path = "protocols/kad", optional = true } libp2p-metrics = { version = "0.8.0", path = "misc/metrics", optional = true } libp2p-mplex = { version = "0.35.0", path = "muxers/mplex", optional = true } libp2p-noise = { version = "0.38.0", path = "transports/noise", optional = true } diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 09f2035ef2b..9715c4f3329 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.39.1 [unreleased] + +- Add support for multiple protocol names. Update `Kademlia`, `KademliaConfig`, + and `KademliaProtocolConfig` accordingly. + See [issue 2837](https://github.com/libp2p/rust-libp2p/issues/2837). + # 0.39.0 - Update to `libp2p-swarm` `v0.38.0`. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index d1910dccbbb..a40f93a0327 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = "1.56.1" description = "Kademlia protocol for libp2p" -version = "0.39.0" +version = "0.39.1" 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 f40932ad0d3..9260a86fee7 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -214,13 +214,17 @@ impl Default for KademliaConfig { } impl KademliaConfig { - /// Sets a custom protocol name. + /// Sets custom protocol names. /// /// Kademlia nodes only communicate with other nodes using the same protocol - /// name. Using a custom name therefore allows to segregate the DHT from + /// name. Using custom name(s) therefore allows to segregate the DHT from /// others, if that is desired. - pub fn set_protocol_name(&mut self, name: impl Into>) -> &mut Self { - self.protocol_config.set_protocol_name(name); + /// + /// 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. + pub fn set_protocol_names(&mut self, names: SmallVec<[Cow<'static, [u8]>; 2]>) -> &mut Self { + self.protocol_config.set_protocol_names(names); self } @@ -403,8 +407,8 @@ where } /// Get the protocol name of this kademlia instance. - pub fn protocol_name(&self) -> &[u8] { - self.protocol_config.protocol_name() + pub fn protocol_names(&self) -> &[Cow<'static, [u8]>] { + self.protocol_config.protocol_names() } /// Creates a new `Kademlia` network behaviour with the given configuration. diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index 656917b54f6..f82da3b8187 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -36,6 +36,7 @@ use instant::Instant; use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use libp2p_core::{Multiaddr, PeerId}; use prost::Message; +use smallvec::SmallVec; use std::{borrow::Cow, convert::TryFrom, time::Duration}; use std::{io, iter}; use unsigned_varint::codec; @@ -142,21 +143,21 @@ impl From for proto::message::Peer { // `OutboundUpgrade` to be just a single message #[derive(Debug, Clone)] pub struct KademliaProtocolConfig { - protocol_name: Cow<'static, [u8]>, + protocol_names: SmallVec<[Cow<'static, [u8]>; 2]>, /// Maximum allowed size of a packet. max_packet_size: usize, } impl KademliaProtocolConfig { /// Returns the configured protocol name. - pub fn protocol_name(&self) -> &[u8] { - &self.protocol_name + pub fn protocol_names(&self) -> &[Cow<'static, [u8]>] { + &self.protocol_names } /// Modifies the protocol name used on the wire. Can be used to create incompatibilities /// between networks on purpose. - pub fn set_protocol_name(&mut self, name: impl Into>) { - self.protocol_name = name.into(); + pub fn set_protocol_names(&mut self, names: SmallVec<[Cow<'static, [u8]>; 2]>) { + self.protocol_names = names; } /// Modifies the maximum allowed size of a single Kademlia packet. @@ -168,7 +169,7 @@ impl KademliaProtocolConfig { impl Default for KademliaProtocolConfig { fn default() -> Self { KademliaProtocolConfig { - protocol_name: Cow::Borrowed(DEFAULT_PROTO_NAME), + protocol_names: iter::once(Cow::Borrowed(DEFAULT_PROTO_NAME)).collect(), max_packet_size: DEFAULT_MAX_PACKET_SIZE, } } @@ -176,10 +177,10 @@ impl Default for KademliaProtocolConfig { impl UpgradeInfo for KademliaProtocolConfig { type Info = Cow<'static, [u8]>; - type InfoIter = iter::Once; + type InfoIter = smallvec::IntoIter<[Self::Info; 2]>; fn protocol_info(&self) -> Self::InfoIter { - iter::once(self.protocol_name.clone()) + self.protocol_names.clone().into_iter() } } From 1fd48c3aa688ddd1fd62140837dc2ed01dae4ca9 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Mon, 29 Aug 2022 15:09:29 +0300 Subject: [PATCH 2/4] minor: changelog fix --- protocols/kad/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 2774541915e..56ca73c5072 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,4 +1,3 @@ -======= # 0.40.0 [unreleased] - Add support for multiple protocol names. Update `Kademlia`, `KademliaConfig`, From e267eaf1a3cf1eb0c4e1db1c1b99eae290d94e30 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Tue, 30 Aug 2022 13:14:01 +0300 Subject: [PATCH 3/4] PR review fixes --- protocols/kad/CHANGELOG.md | 5 +++-- protocols/kad/src/behaviour.rs | 12 +++++++++++- protocols/kad/src/protocol.rs | 16 +++++++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 56ca73c5072..23b3b764636 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,8 +1,9 @@ # 0.40.0 [unreleased] - Add support for multiple protocol names. Update `Kademlia`, `KademliaConfig`, - and `KademliaProtocolConfig` accordingly. - See [issue 2837](https://github.com/libp2p/rust-libp2p/issues/2837). + and `KademliaProtocolConfig` accordingly. [See Issue 2837]. [See PR 2846]. + [Issue 2837]: https://github.com/libp2p/rust-libp2p/issues/2837 + [PR 2846]: https://github.com/libp2p/rust-libp2p/pull/2846 - Update to `libp2p-swarm` `v0.39.0`. diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 9260a86fee7..feba94f550e 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -223,11 +223,21 @@ impl KademliaConfig { /// 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. - pub fn set_protocol_names(&mut self, names: SmallVec<[Cow<'static, [u8]>; 2]>) -> &mut Self { + pub fn set_protocol_names(&mut self, names: Vec>) -> &mut Self { self.protocol_config.set_protocol_names(names); self } + /// Sets a custom protocol name. + /// + /// Kademlia nodes only communicate with other nodes using the same protocol + /// name. Using a custom name therefore allows to segregate the DHT from + /// others, if that is desired. + #[deprecated(since = "0.40.0", note = "use `set_protocol_names()` instead")] + pub fn set_protocol_name(&mut self, name: impl Into>) -> &mut Self { + self.set_protocol_names(std::iter::once(name.into()).collect()) + } + /// Sets the timeout for a single query. /// /// > **Note**: A single query usually comprises at least as many requests diff --git a/protocols/kad/src/protocol.rs b/protocols/kad/src/protocol.rs index f82da3b8187..3c00a5059f2 100644 --- a/protocols/kad/src/protocol.rs +++ b/protocols/kad/src/protocol.rs @@ -36,7 +36,6 @@ use instant::Instant; use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use libp2p_core::{Multiaddr, PeerId}; use prost::Message; -use smallvec::SmallVec; use std::{borrow::Cow, convert::TryFrom, time::Duration}; use std::{io, iter}; use unsigned_varint::codec; @@ -143,7 +142,7 @@ impl From for proto::message::Peer { // `OutboundUpgrade` to be just a single message #[derive(Debug, Clone)] pub struct KademliaProtocolConfig { - protocol_names: SmallVec<[Cow<'static, [u8]>; 2]>, + protocol_names: Vec>, /// Maximum allowed size of a packet. max_packet_size: usize, } @@ -154,12 +153,19 @@ impl KademliaProtocolConfig { &self.protocol_names } - /// Modifies the protocol name used on the wire. Can be used to create incompatibilities + /// Modifies the protocol names used on the wire. Can be used to create incompatibilities /// between networks on purpose. - pub fn set_protocol_names(&mut self, names: SmallVec<[Cow<'static, [u8]>; 2]>) { + pub fn set_protocol_names(&mut self, names: Vec>) { self.protocol_names = names; } + /// Sets single protocol name used on the wire. Can be used to create incompatibilities + /// between networks on purpose. + #[deprecated(since = "0.40.0", note = "use `set_protocol_names()` instead")] + pub fn set_protocol_name(&mut self, name: impl Into>) { + self.set_protocol_names(std::iter::once(name.into()).collect()); + } + /// Modifies the maximum allowed size of a single Kademlia packet. pub fn set_max_packet_size(&mut self, size: usize) { self.max_packet_size = size; @@ -177,7 +183,7 @@ impl Default for KademliaProtocolConfig { impl UpgradeInfo for KademliaProtocolConfig { type Info = Cow<'static, [u8]>; - type InfoIter = smallvec::IntoIter<[Self::Info; 2]>; + type InfoIter = std::vec::IntoIter; fn protocol_info(&self) -> Self::InfoIter { self.protocol_names.clone().into_iter() From 7a1ad1fb765c702ad559dd9463d5ce63e679daae Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 2 Sep 2022 08:52:24 +0200 Subject: [PATCH 4/4] Update protocols/kad/CHANGELOG.md --- protocols/kad/CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 23b3b764636..a3788bf260f 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,12 +1,13 @@ # 0.40.0 [unreleased] - Add support for multiple protocol names. Update `Kademlia`, `KademliaConfig`, - and `KademliaProtocolConfig` accordingly. [See Issue 2837]. [See PR 2846]. - [Issue 2837]: https://github.com/libp2p/rust-libp2p/issues/2837 - [PR 2846]: https://github.com/libp2p/rust-libp2p/pull/2846 + and `KademliaProtocolConfig` accordingly. See [Issue 2837]. See [PR 2846]. - Update to `libp2p-swarm` `v0.39.0`. +[Issue 2837]: https://github.com/libp2p/rust-libp2p/issues/2837 +[PR 2846]: https://github.com/libp2p/rust-libp2p/pull/2846 + # 0.39.0 - Update to `libp2p-swarm` `v0.38.0`.