Upgrade to libp2p 0.15 (#4732)

* Fixes for libp2p 0.15

* More work

* Update libp2p

* Update to libp2p 0.15
This commit is contained in:
Pierre Krieger
2020-01-27 18:27:36 +01:00
committed by Gavin Wood
parent ed3da9f903
commit 5c8743510e
16 changed files with 332 additions and 237 deletions
+5
View File
@@ -32,6 +32,7 @@ use libp2p::identity::{Keypair, ed25519};
use libp2p::wasm_ext;
use libp2p::{PeerId, Multiaddr, multiaddr};
use core::{fmt, iter};
use std::{future::Future, pin::Pin};
use std::{error::Error, fs, io::{self, Write}, net::Ipv4Addr, path::{Path, PathBuf}, sync::Arc};
use zeroize::Zeroize;
@@ -40,6 +41,10 @@ pub struct Params<B: BlockT, S, H: ExHashT> {
/// Assigned roles for our node (full, light, ...).
pub roles: Roles,
/// How to spawn background tasks. If you pass `None`, then a threads pool will be used by
/// default.
pub executor: Option<Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>>,
/// Network layer configuration.
pub network_config: NetworkConfiguration,
+1 -1
View File
@@ -122,7 +122,7 @@ impl<TSubstream> DiscoveryBehaviour<TSubstream> {
allow_private_ipv4,
#[cfg(not(target_os = "unknown"))]
mdns: if enable_mdns {
match Mdns::new().await {
match Mdns::new() {
Ok(mdns) => Some(mdns).into(),
Err(err) => {
warn!(target: "sub-libp2p", "Failed to initialize mDNS: {:?}", err);
@@ -18,7 +18,7 @@ use super::upgrade::{RegisteredProtocol, RegisteredProtocolEvent, RegisteredProt
use bytes::BytesMut;
use futures::prelude::*;
use futures_timer::Delay;
use libp2p::core::{ConnectedPoint, PeerId, Endpoint};
use libp2p::core::{ConnectedPoint, Negotiated, PeerId, Endpoint};
use libp2p::core::upgrade::{InboundUpgrade, OutboundUpgrade};
use libp2p::swarm::{
ProtocolsHandler, ProtocolsHandlerEvent,
@@ -159,7 +159,7 @@ enum ProtocolState<TSubstream> {
/// Waiting for the behaviour to tell the handler whether it is enabled or disabled.
Init {
/// List of substreams opened by the remote but that haven't been processed yet.
substreams: SmallVec<[RegisteredProtocolSubstream<TSubstream>; 6]>,
substreams: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 6]>,
/// Deadline after which the initialization is abnormally long.
init_deadline: Delay,
},
@@ -175,9 +175,9 @@ enum ProtocolState<TSubstream> {
/// If we are in this state, we have sent a `CustomProtocolOpen` message to the outside.
Normal {
/// The substreams where bidirectional communications happen.
substreams: SmallVec<[RegisteredProtocolSubstream<TSubstream>; 4]>,
substreams: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 4]>,
/// Contains substreams which are being shut down.
shutdown: SmallVec<[RegisteredProtocolSubstream<TSubstream>; 4]>,
shutdown: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 4]>,
},
/// We are disabled. Contains substreams that are being closed.
@@ -185,7 +185,7 @@ enum ProtocolState<TSubstream> {
/// outside or we have never sent any `CustomProtocolOpen` in the first place.
Disabled {
/// List of substreams to shut down.
shutdown: SmallVec<[RegisteredProtocolSubstream<TSubstream>; 6]>,
shutdown: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 6]>,
/// If true, we should reactivate the handler after all the substreams in `shutdown` have
/// been closed.
@@ -466,7 +466,7 @@ where
/// Called by `inject_fully_negotiated_inbound` and `inject_fully_negotiated_outbound`.
fn inject_fully_negotiated(
&mut self,
mut substream: RegisteredProtocolSubstream<TSubstream>
mut substream: RegisteredProtocolSubstream<Negotiated<TSubstream>>
) {
self.state = match mem::replace(&mut self.state, ProtocolState::Poisoned) {
ProtocolState::Poisoned => {
@@ -538,14 +538,14 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin {
fn inject_fully_negotiated_inbound(
&mut self,
proto: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output
proto: <Self::InboundProtocol as InboundUpgrade<Negotiated<TSubstream>>>::Output
) {
self.inject_fully_negotiated(proto);
}
fn inject_fully_negotiated_outbound(
&mut self,
proto: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output,
proto: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<TSubstream>>>::Output,
_: Self::OutboundOpenInfo
) {
self.inject_fully_negotiated(proto);
@@ -621,7 +621,7 @@ where
/// Given a list of substreams, tries to shut them down. The substreams that have been successfully
/// shut down are removed from the list.
fn shutdown_list<TSubstream>
(list: &mut SmallVec<impl smallvec::Array<Item = RegisteredProtocolSubstream<TSubstream>>>,
(list: &mut SmallVec<impl smallvec::Array<Item = RegisteredProtocolSubstream<Negotiated<TSubstream>>>>,
cx: &mut Context)
where TSubstream: AsyncRead + AsyncWrite + Unpin {
'outer: for n in (0..list.len()).rev() {
@@ -18,7 +18,7 @@ use crate::config::ProtocolId;
use bytes::BytesMut;
use futures::prelude::*;
use futures_codec::Framed;
use libp2p::core::{Negotiated, Endpoint, UpgradeInfo, InboundUpgrade, OutboundUpgrade, upgrade::ProtocolName};
use libp2p::core::{Endpoint, UpgradeInfo, InboundUpgrade, OutboundUpgrade, upgrade::ProtocolName};
use std::{collections::VecDeque, io, pin::Pin, vec::IntoIter as VecIntoIter};
use std::task::{Context, Poll};
use unsigned_varint::codec::UviBytes;
@@ -82,7 +82,7 @@ pub struct RegisteredProtocolSubstream<TSubstream> {
/// If true, we should call `poll_complete` on the inner sink.
requires_poll_flush: bool,
/// The underlying substream.
inner: stream::Fuse<Framed<Negotiated<TSubstream>, UviBytes<BytesMut>>>,
inner: stream::Fuse<Framed<TSubstream, UviBytes<BytesMut>>>,
/// Version of the protocol that was negotiated.
protocol_version: u8,
/// If true, we have sent a "remote is clogged" event recently and shouldn't send another one
@@ -250,7 +250,7 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin,
fn upgrade_inbound(
self,
socket: Negotiated<TSubstream>,
socket: TSubstream,
info: Self::Info,
) -> Self::Future {
let framed = {
@@ -280,7 +280,7 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin,
fn upgrade_outbound(
self,
socket: Negotiated<TSubstream>,
socket: TSubstream,
info: Self::Info,
) -> Self::Future {
let framed = Framed::new(socket, UviBytes::default());
+7 -3
View File
@@ -36,7 +36,7 @@ use futures::{prelude::*, channel::mpsc};
use log::{warn, error, info, trace};
use libp2p::{PeerId, Multiaddr, kad::record};
use libp2p::core::{transport::boxed::Boxed, muxing::StreamMuxerBox};
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
use parking_lot::Mutex;
use sc_peerset::PeersetHandle;
use sp_runtime::{traits::{Block as BlockT, NumberFor}, ConsensusEngineId};
@@ -213,7 +213,7 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
)?;
// Build the swarm.
let (mut swarm, bandwidth) = {
let (mut swarm, bandwidth): (Swarm::<B, S, H>, _) = {
let user_agent = format!(
"{} ({})",
params.network_config.client_version,
@@ -241,7 +241,11 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
};
transport::build_transport(local_identity, config_mem, config_wasm)
};
(Swarm::<B, S, H>::new(transport, behaviour, local_peer_id.clone()), bandwidth)
let mut builder = SwarmBuilder::new(transport, behaviour, local_peer_id.clone());
if let Some(spawner) = params.executor {
builder = builder.executor_fn(spawner);
}
(builder.build(), bandwidth)
};
// Listen on multiaddresses.