Skip to content

Commit cee199a

Browse files
protocols/kad: Support multiple protocol names (#2846)
Add support for multiple Kademlia protocol names to allow protocol name upgrades.
1 parent 89f898c commit cee199a

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

protocols/kad/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# 0.40.0 [unreleased]
22

3+
- Add support for multiple protocol names. Update `Kademlia`, `KademliaConfig`,
4+
and `KademliaProtocolConfig` accordingly. See [Issue 2837]. See [PR 2846].
5+
36
- Update to `libp2p-swarm` `v0.39.0`.
47

8+
[Issue 2837]: https://github.com/libp2p/rust-libp2p/issues/2837
9+
[PR 2846]: https://github.com/libp2p/rust-libp2p/pull/2846
10+
511
# 0.39.0
612

713
- Update prost requirement from 0.10 to 0.11 which no longer installs the protoc Protobuf compiler.

protocols/kad/src/behaviour.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,28 @@ impl Default for KademliaConfig {
214214
}
215215

216216
impl KademliaConfig {
217+
/// Sets custom protocol names.
218+
///
219+
/// Kademlia nodes only communicate with other nodes using the same protocol
220+
/// name. Using custom name(s) therefore allows to segregate the DHT from
221+
/// others, if that is desired.
222+
///
223+
/// More than one protocol name can be supplied. In this case the node will
224+
/// be able to talk to other nodes supporting any of the provided names.
225+
/// Multiple names must be used with caution to avoid network partitioning.
226+
pub fn set_protocol_names(&mut self, names: Vec<Cow<'static, [u8]>>) -> &mut Self {
227+
self.protocol_config.set_protocol_names(names);
228+
self
229+
}
230+
217231
/// Sets a custom protocol name.
218232
///
219233
/// Kademlia nodes only communicate with other nodes using the same protocol
220234
/// name. Using a custom name therefore allows to segregate the DHT from
221235
/// others, if that is desired.
236+
#[deprecated(since = "0.40.0", note = "use `set_protocol_names()` instead")]
222237
pub fn set_protocol_name(&mut self, name: impl Into<Cow<'static, [u8]>>) -> &mut Self {
223-
self.protocol_config.set_protocol_name(name);
224-
self
238+
self.set_protocol_names(std::iter::once(name.into()).collect())
225239
}
226240

227241
/// Sets the timeout for a single query.
@@ -403,8 +417,8 @@ where
403417
}
404418

405419
/// Get the protocol name of this kademlia instance.
406-
pub fn protocol_name(&self) -> &[u8] {
407-
self.protocol_config.protocol_name()
420+
pub fn protocol_names(&self) -> &[Cow<'static, [u8]>] {
421+
self.protocol_config.protocol_names()
408422
}
409423

410424
/// Creates a new `Kademlia` network behaviour with the given configuration.

protocols/kad/src/protocol.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,28 @@ impl From<KadPeer> for proto::message::Peer {
142142
// `OutboundUpgrade` to be just a single message
143143
#[derive(Debug, Clone)]
144144
pub struct KademliaProtocolConfig {
145-
protocol_name: Cow<'static, [u8]>,
145+
protocol_names: Vec<Cow<'static, [u8]>>,
146146
/// Maximum allowed size of a packet.
147147
max_packet_size: usize,
148148
}
149149

150150
impl KademliaProtocolConfig {
151151
/// Returns the configured protocol name.
152-
pub fn protocol_name(&self) -> &[u8] {
153-
&self.protocol_name
152+
pub fn protocol_names(&self) -> &[Cow<'static, [u8]>] {
153+
&self.protocol_names
154154
}
155155

156-
/// Modifies the protocol name used on the wire. Can be used to create incompatibilities
156+
/// Modifies the protocol names used on the wire. Can be used to create incompatibilities
157157
/// between networks on purpose.
158+
pub fn set_protocol_names(&mut self, names: Vec<Cow<'static, [u8]>>) {
159+
self.protocol_names = names;
160+
}
161+
162+
/// Sets single protocol name used on the wire. Can be used to create incompatibilities
163+
/// between networks on purpose.
164+
#[deprecated(since = "0.40.0", note = "use `set_protocol_names()` instead")]
158165
pub fn set_protocol_name(&mut self, name: impl Into<Cow<'static, [u8]>>) {
159-
self.protocol_name = name.into();
166+
self.set_protocol_names(std::iter::once(name.into()).collect());
160167
}
161168

162169
/// Modifies the maximum allowed size of a single Kademlia packet.
@@ -168,18 +175,18 @@ impl KademliaProtocolConfig {
168175
impl Default for KademliaProtocolConfig {
169176
fn default() -> Self {
170177
KademliaProtocolConfig {
171-
protocol_name: Cow::Borrowed(DEFAULT_PROTO_NAME),
178+
protocol_names: iter::once(Cow::Borrowed(DEFAULT_PROTO_NAME)).collect(),
172179
max_packet_size: DEFAULT_MAX_PACKET_SIZE,
173180
}
174181
}
175182
}
176183

177184
impl UpgradeInfo for KademliaProtocolConfig {
178185
type Info = Cow<'static, [u8]>;
179-
type InfoIter = iter::Once<Self::Info>;
186+
type InfoIter = std::vec::IntoIter<Self::Info>;
180187

181188
fn protocol_info(&self) -> Self::InfoIter {
182-
iter::once(self.protocol_name.clone())
189+
self.protocol_names.clone().into_iter()
183190
}
184191
}
185192

0 commit comments

Comments
 (0)