Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
client/network: Remove default Kademlia DHT in favor of per protocol …
Browse files Browse the repository at this point in the history
…DHT (#5993)

Substrate was previously running both a chain specific Kademlia DHT as
well as the `/ipfs/kad/1.0.0` Kademlia DHT for backwards compatibility.
(See 710722f for details.)

With the end of the transition period this commit removes the former
only leaving the latter and thus preventing the discovery of
incompatible nodes via Kademlia.
  • Loading branch information
mxinden authored May 12, 2020
1 parent f3315cd commit 1d0b92c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
30 changes: 15 additions & 15 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
//!
//! - mDNS. Discovers nodes on the local network by broadcasting UDP packets.
//!
//! - Kademlia random walk. Once connected, we perform random Kademlia `FIND_NODE` requests in
//! order for nodes to propagate to us their view of the network. This is performed automatically
//! by the `DiscoveryBehaviour`.
//! - Kademlia random walk. Once connected, we perform random Kademlia `FIND_NODE` requests on the
//! configured Kademlia DHTs in order for nodes to propagate to us their view of the network. This
//! is performed automatically by the `DiscoveryBehaviour`.
//!
//! Additionally, the `DiscoveryBehaviour` is also capable of storing and loading value in the
//! network-wide DHT.
//! configured DHTs.
//!
//! ## Usage
//!
Expand Down Expand Up @@ -68,6 +68,9 @@ use std::task::{Context, Poll};
use sp_core::hexdisplay::HexDisplay;

/// `DiscoveryBehaviour` configuration.
///
/// Note: In order to discover nodes or load and store values via Kademlia one has to add at least
/// one protocol via [`DiscoveryConfig::add_protocol`].
pub struct DiscoveryConfig {
local_peer_id: PeerId,
user_defined: Vec<(PeerId, Multiaddr)>,
Expand All @@ -81,23 +84,15 @@ pub struct DiscoveryConfig {
impl DiscoveryConfig {
/// Create a default configuration with the given public key.
pub fn new(local_public_key: PublicKey) -> Self {
let mut this = DiscoveryConfig {
DiscoveryConfig {
local_peer_id: local_public_key.into_peer_id(),
user_defined: Vec::new(),
allow_private_ipv4: true,
allow_non_globals_in_dht: false,
discovery_only_if_under_num: std::u64::MAX,
enable_mdns: false,
kademlias: HashMap::new()
};

// Temporary hack to retain backwards compatibility.
// We should eventually remove the special handling of DEFAULT_PROTO_NAME.
let proto_id = ProtocolId::from(libp2p::kad::protocol::DEFAULT_PROTO_NAME);
let proto_name = Vec::from(proto_id.as_bytes());
this.add_kademlia(proto_id, proto_name);

this
}
}

/// Set the number of active connections at which we pause discovery.
Expand Down Expand Up @@ -706,6 +701,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {

#[cfg(test)]
mod tests {
use crate::config::ProtocolId;
use futures::prelude::*;
use libp2p::identity::Keypair;
use libp2p::Multiaddr;
Expand Down Expand Up @@ -744,11 +740,15 @@ mod tests {
});

let behaviour = {
let protocol_id: &[u8] = b"/test/kad/1.0.0";

let mut config = DiscoveryConfig::new(keypair.public());
config.with_user_defined(user_defined.clone())
.allow_private_ipv4(true)
.allow_non_globals_in_dht(true)
.discovery_limit(50);
.discovery_limit(50)
.add_protocol(ProtocolId::from(protocol_id));

config.finish()
};

Expand Down
10 changes: 6 additions & 4 deletions client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@
//! - mDNS. We perform a UDP broadcast on the local network. Nodes that listen may respond with
//! their identity. More info [here](https://github.com/libp2p/specs/blob/master/discovery/mdns.md).
//! mDNS can be disabled in the network configuration.
//! - Kademlia random walk. Once connected, we perform random Kademlia `FIND_NODE` requests in
//! order for nodes to propagate to us their view of the network. More information about Kademlia
//! can be found [on Wikipedia](https://en.wikipedia.org/wiki/Kademlia).
//! - Kademlia random walk. Once connected, we perform random Kademlia `FIND_NODE` requests on the
//! configured Kademlia DHTs (one per configured chain protocol) in order for nodes to propagate to
//! us their view of the network. More information about Kademlia can be found [on
//! Wikipedia](https://en.wikipedia.org/wiki/Kademlia).
//!
//! ## Connection establishment
//!
Expand All @@ -77,6 +78,7 @@
//! frames. Encryption and multiplexing are additionally negotiated again inside this channel.
//! - DNS for addresses of the form `/dns4/example.com/tcp/5` or `/dns4/example.com/tcp/5/ws`. A
//! node's address can contain a domain name.
//! - (All of the above using IPv6 instead of IPv4.)
//!
//! On top of the base-layer protocol, the [Noise](https://noiseprotocol.org/) protocol is
//! negotiated and applied. The exact handshake protocol is experimental and is subject to change.
Expand Down Expand Up @@ -109,7 +111,7 @@
//! to a disconnection.
//! - **[`/ipfs/id/1.0.0`](https://github.com/libp2p/specs/tree/master/identify)**. We
//! periodically open an ephemeral substream in order to ask information from the remote.
//! - **[`/ipfs/kad/1.0.0`](https://github.com/libp2p/specs/pull/108)**. We periodically open
//! - **[`/<protocol_id>/kad`](https://github.com/libp2p/specs/pull/108)**. We periodically open
//! ephemeral substreams for Kademlia random walk queries. Each Kademlia query is done in a
//! separate substream.
//!
Expand Down

0 comments on commit 1d0b92c

Please sign in to comment.