Upgrade to libp2p v0.19 - Changes the default PeerId representation (#6064)

* Upgrade to libp2p v0.19

* Listen on IPv6 by default

* Increase channels sizes

* Use spec-compliant noise protocol

* Show legacy PeerId

* Switch order of Noise protocols

* Switch to crates.io version

* Fix subkey's version

* Fix line width and Wasm build

* I think Wasm is fixed for real this time
This commit is contained in:
Pierre Krieger
2020-05-18 19:57:08 +02:00
committed by GitHub
parent 80accdbf7c
commit 8b9bd9018e
15 changed files with 143 additions and 93 deletions
+18 -17
View File
@@ -52,7 +52,7 @@ use ip_network::IpNetwork;
use libp2p::core::{connection::{ConnectionId, ListenerId}, ConnectedPoint, Multiaddr, PeerId, PublicKey};
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters, ProtocolsHandler};
use libp2p::swarm::protocols_handler::multi::MultiHandler;
use libp2p::kad::{Kademlia, KademliaConfig, KademliaEvent, Quorum, Record};
use libp2p::kad::{Kademlia, KademliaConfig, KademliaEvent, QueryResult, Quorum, Record};
use libp2p::kad::GetClosestPeersError;
use libp2p::kad::handler::KademliaHandler;
use libp2p::kad::QueryId;
@@ -177,7 +177,7 @@ impl DiscoveryConfig {
kademlias: self.kademlias,
next_kad_random_query: Delay::new(Duration::new(0, 0)),
duration_to_next_kad: Duration::from_secs(1),
discoveries: VecDeque::new(),
pending_events: VecDeque::new(),
local_peer_id: self.local_peer_id,
num_connections: 0,
allow_private_ipv4: self.allow_private_ipv4,
@@ -213,8 +213,8 @@ pub struct DiscoveryBehaviour {
next_kad_random_query: Delay,
/// After `next_kad_random_query` triggers, the next one triggers after this duration.
duration_to_next_kad: Duration,
/// Discovered nodes to return.
discoveries: VecDeque<PeerId>,
/// Events to return in priority when polled.
pending_events: VecDeque<DiscoveryOut>,
/// Identity of our local node.
local_peer_id: PeerId,
/// Number of nodes we're currently connected to.
@@ -248,7 +248,7 @@ impl DiscoveryBehaviour {
for k in self.kademlias.values_mut() {
k.add_address(&peer_id, addr.clone())
}
self.discoveries.push_back(peer_id.clone());
self.pending_events.push_back(DiscoveryOut::Discovered(peer_id.clone()));
self.user_defined.push((peer_id, addr));
}
}
@@ -272,7 +272,7 @@ impl DiscoveryBehaviour {
/// A corresponding `ValueFound` or `ValueNotFound` event will later be generated.
pub fn get_value(&mut self, key: &record::Key) {
for k in self.kademlias.values_mut() {
k.get_record(key, Quorum::One)
k.get_record(key, Quorum::One);
}
}
@@ -282,7 +282,10 @@ impl DiscoveryBehaviour {
/// A corresponding `ValuePut` or `ValuePutFailed` event will later be generated.
pub fn put_value(&mut self, key: record::Key, value: Vec<u8>) {
for k in self.kademlias.values_mut() {
k.put_record(Record::new(key.clone(), value.clone()), Quorum::All)
if let Err(e) = k.put_record(Record::new(key.clone(), value.clone()), Quorum::All) {
warn!(target: "sub-libp2p", "Libp2p => Failed to put record: {:?}", e);
self.pending_events.push_back(DiscoveryOut::ValuePutFailed(key.clone()));
}
}
}
@@ -528,8 +531,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
>,
> {
// Immediately process the content of `discovered`.
if let Some(peer_id) = self.discoveries.pop_front() {
let ev = DiscoveryOut::Discovered(peer_id);
if let Some(ev) = self.pending_events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev));
}
@@ -541,7 +543,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
"Libp2p <= Starting random Kademlia request for {:?}",
random_peer_id);
for k in self.kademlias.values_mut() {
k.get_closest_peers(random_peer_id.clone())
k.get_closest_peers(random_peer_id.clone());
}
true
} else {
@@ -578,7 +580,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
let ev = DiscoveryOut::Discovered(peer);
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev));
}
KademliaEvent::GetClosestPeersResult(res) => {
KademliaEvent::QueryResult { result: QueryResult::GetClosestPeers(res), .. } => {
match res {
Err(GetClosestPeersError::Timeout { key, peers }) => {
debug!(target: "sub-libp2p",
@@ -596,7 +598,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
}
}
}
KademliaEvent::GetRecordResult(res) => {
KademliaEvent::QueryResult { result: QueryResult::GetRecord(res), .. } => {
let ev = match res {
Ok(ok) => {
let results = ok.records
@@ -619,7 +621,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
};
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev));
}
KademliaEvent::PutRecordResult(res) => {
KademliaEvent::QueryResult { result: QueryResult::PutRecord(res), .. } => {
let ev = match res {
Ok(ok) => DiscoveryOut::ValuePut(ok.key),
Err(e) => {
@@ -630,7 +632,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
};
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev));
}
KademliaEvent::RepublishRecordResult(res) => {
KademliaEvent::QueryResult { result: QueryResult::RepublishRecord(res), .. } => {
match res {
Ok(ok) => debug!(target: "sub-libp2p",
"Libp2p => Record republished: {:?}",
@@ -675,9 +677,8 @@ impl NetworkBehaviour for DiscoveryBehaviour {
continue;
}
self.discoveries.extend(list.map(|(peer_id, _)| peer_id));
if let Some(peer_id) = self.discoveries.pop_front() {
let ev = DiscoveryOut::Discovered(peer_id);
self.pending_events.extend(list.map(|(peer_id, _)| DiscoveryOut::Discovered(peer_id)));
if let Some(ev) = self.pending_events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev));
}
},
+12 -3
View File
@@ -59,10 +59,11 @@ use sp_runtime::{
};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::{
borrow::Cow,
borrow::{Borrow, Cow},
collections::HashSet,
fs, io,
marker::PhantomData,
num:: NonZeroUsize,
pin::Pin,
str,
sync::{
@@ -185,7 +186,13 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
let local_identity = params.network_config.node_key.clone().into_keypair()?;
let local_public = local_identity.public();
let local_peer_id = local_public.clone().into_peer_id();
info!(target: "sub-libp2p", "🏷 Local node identity is: {}", local_peer_id.to_base58());
let local_peer_id_legacy = bs58::encode(Borrow::<[u8]>::borrow(&local_peer_id)).into_string();
info!(
target: "sub-libp2p",
"🏷 Local node identity is: {} (legacy representation: {})",
local_peer_id.to_base58(),
local_peer_id_legacy
);
// Initialize the metrics.
let metrics = match &params.metrics_registry {
@@ -287,7 +294,9 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
transport::build_transport(local_identity, config_mem, config_wasm, flowctrl)
};
let mut builder = SwarmBuilder::new(transport, behaviour, local_peer_id.clone())
.peer_connection_limit(crate::MAX_CONNECTIONS_PER_PEER);
.peer_connection_limit(crate::MAX_CONNECTIONS_PER_PEER)
.notify_handler_buffer_size(NonZeroUsize::new(16).expect("16 != 0; qed"))
.connection_event_buffer_size(128);
if let Some(spawner) = params.executor {
struct SpawnImpl<F>(F);
impl<F: Fn(Pin<Box<dyn Future<Output = ()> + Send>>)> Executor for SpawnImpl<F> {
+31 -9
View File
@@ -18,11 +18,14 @@
use futures::prelude::*;
use libp2p::{
InboundUpgradeExt, OutboundUpgradeExt, PeerId, Transport,
core::{
self, either::{EitherError, EitherOutput}, muxing::StreamMuxerBox,
transport::{boxed::Boxed, OptionalTransport}, upgrade
},
mplex, identity, bandwidth, wasm_ext, noise
};
#[cfg(not(target_os = "unknown"))]
use libp2p::{tcp, dns, websocket};
use libp2p::core::{self, upgrade, transport::boxed::Boxed, transport::OptionalTransport, muxing::StreamMuxerBox};
use std::{io, sync::Arc, time::Duration, usize};
pub use self::bandwidth::BandwidthSinks;
@@ -42,14 +45,22 @@ pub fn build_transport(
) -> (Boxed<(PeerId, StreamMuxerBox), io::Error>, Arc<bandwidth::BandwidthSinks>) {
// Build configuration objects for encryption mechanisms.
let noise_config = {
let noise_keypair = noise::Keypair::new().into_authentic(&keypair)
// For more information about this panic, see in "On the Importance of Checking
// Cryptographic Protocols for Faults" by Dan Boneh, Richard A. DeMillo,
// and Richard J. Lipton.
// For more information about these two panics, see in "On the Importance of
// Checking Cryptographic Protocols for Faults" by Dan Boneh, Richard A. DeMillo,
// and Richard J. Lipton.
let noise_keypair_legacy = noise::Keypair::<noise::X25519>::new().into_authentic(&keypair)
.expect("can only fail in case of a hardware bug; since this signing is performed only \
once and at initialization, we're taking the bet that the inconvenience of a very \
rare panic here is basically zero");
noise::NoiseConfig::ix(noise_keypair)
let noise_keypair_spec = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&keypair)
.expect("can only fail in case of a hardware bug; since this signing is performed only \
once and at initialization, we're taking the bet that the inconvenience of a very \
rare panic here is basically zero");
core::upgrade::SelectUpgrade::new(
noise::NoiseConfig::xx(noise_keypair_spec),
noise::NoiseConfig::ix(noise_keypair_legacy)
)
};
// Build configuration objects for multiplexing mechanisms.
@@ -97,11 +108,22 @@ pub fn build_transport(
// Encryption
let transport = transport.and_then(move |stream, endpoint| {
core::upgrade::apply(stream, noise_config, endpoint, upgrade::Version::V1)
.and_then(|(remote_id, out)| async move {
let remote_key = match remote_id {
noise::RemoteIdentity::IdentityKey(key) => key,
.map_err(|err|
err.map_err(|err| match err {
EitherError::A(err) => err,
EitherError::B(err) => err,
})
)
.and_then(|result| async move {
let remote_key = match &result {
EitherOutput::First((noise::RemoteIdentity::IdentityKey(key), _)) => key.clone(),
EitherOutput::Second((noise::RemoteIdentity::IdentityKey(key), _)) => key.clone(),
_ => return Err(upgrade::UpgradeError::Apply(noise::NoiseError::InvalidKey))
};
let out = match result {
EitherOutput::First((_, o)) => o,
EitherOutput::Second((_, o)) => o,
};
Ok((out, remote_key.into_peer_id()))
})
});