Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/kad: Support multiple protocol names #2846

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions protocols/kad/CHANGELOG.md
@@ -1,5 +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).
dmitry-markin marked this conversation as resolved.
Show resolved Hide resolved

- Update to `libp2p-swarm` `v0.39.0`.

# 0.39.0
Expand Down
16 changes: 10 additions & 6 deletions protocols/kad/src/behaviour.rs
Expand Up @@ -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<Cow<'static, [u8]>>) -> &mut Self {
dmitry-markin marked this conversation as resolved.
Show resolved Hide resolved
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
}

Expand Down Expand Up @@ -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.
Expand Down
17 changes: 9 additions & 8 deletions protocols/kad/src/protocol.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -142,21 +143,21 @@ impl From<KadPeer> 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]>,
dmitry-markin marked this conversation as resolved.
Show resolved Hide resolved
/// 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<Cow<'static, [u8]>>) {
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.
Expand All @@ -168,18 +169,18 @@ 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,
}
}
}

impl UpgradeInfo for KademliaProtocolConfig {
type Info = Cow<'static, [u8]>;
type InfoIter = iter::Once<Self::Info>;
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()
}
}

Expand Down