Update libp2p to 0.16 (#4928)

* Update libp2p to 0.16

* Actually update to libp2p 0.16 🤦

* Fix missed updates

* Fix peerset tests
This commit is contained in:
Pierre Krieger
2020-02-17 11:32:37 +01:00
committed by GitHub
parent c8fa6518bf
commit 13cba8dad1
23 changed files with 226 additions and 206 deletions
@@ -38,7 +38,14 @@ use libp2p::{
upgrade::{InboundUpgrade, ReadOneError, UpgradeInfo, Negotiated},
upgrade::{DeniedUpgrade, read_one, write_one}
},
swarm::{NetworkBehaviour, NetworkBehaviourAction, OneShotHandler, PollParameters, SubstreamProtocol}
swarm::{
NegotiatedSubstream,
NetworkBehaviour,
NetworkBehaviourAction,
OneShotHandler,
PollParameters,
SubstreamProtocol
}
};
use prost::Message;
use sp_runtime::{generic::BlockId, traits::{Block, Header, One, Zero}};
@@ -111,20 +118,17 @@ impl Config {
}
/// The block request handling behaviour.
pub struct BlockRequests<T, B: Block> {
pub struct BlockRequests<B: Block> {
/// This behaviour's configuration.
config: Config,
/// Blockchain client.
chain: Arc<dyn Client<B>>,
/// Futures sending back the block request response.
outgoing: FuturesUnordered<BoxFuture<'static, ()>>,
/// Type witness term.
_marker: std::marker::PhantomData<T>
}
impl<T, B> BlockRequests<T, B>
impl<B> BlockRequests<B>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Block,
{
pub fn new(cfg: Config, chain: Arc<dyn Client<B>>) -> Self {
@@ -132,7 +136,6 @@ where
config: cfg,
chain,
outgoing: FuturesUnordered::new(),
_marker: std::marker::PhantomData
}
}
@@ -243,12 +246,11 @@ where
}
}
impl<T, B> NetworkBehaviour for BlockRequests<T, B>
impl<B> NetworkBehaviour for BlockRequests<B>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Block
{
type ProtocolsHandler = OneShotHandler<T, Protocol, DeniedUpgrade, Request<Negotiated<T>>>;
type ProtocolsHandler = OneShotHandler<Protocol, DeniedUpgrade, Request<NegotiatedSubstream>>;
type OutEvent = Void;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
@@ -269,7 +271,7 @@ where
fn inject_disconnected(&mut self, _peer: &PeerId, _info: ConnectedPoint) {
}
fn inject_node_event(&mut self, peer: PeerId, Request(request, mut stream): Request<Negotiated<T>>) {
fn inject_node_event(&mut self, peer: PeerId, Request(request, mut stream): Request<NegotiatedSubstream>) {
match self.on_block_request(&peer, &request) {
Ok(res) => {
log::trace!("enqueueing block response {} for peer {} with {} blocks", res.id, peer, res.blocks.len());
@@ -25,7 +25,7 @@ use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use log::{debug, error, trace, warn};
use rand::distributions::{Distribution as _, Uniform};
use smallvec::SmallVec;
use std::{borrow::Cow, collections::hash_map::Entry, cmp, error, marker::PhantomData, mem, pin::Pin};
use std::{borrow::Cow, collections::hash_map::Entry, cmp, error, mem, pin::Pin};
use std::time::Duration;
use wasm_timer::Instant;
use std::task::{Context, Poll};
@@ -60,7 +60,7 @@ use std::task::{Context, Poll};
/// Note that this "banning" system is not an actual ban. If a "banned" node tries to connect to
/// us, we accept the connection. The "banning" system is only about delaying dialing attempts.
///
pub struct LegacyProto< TSubstream> {
pub struct LegacyProto {
/// List of protocols to open with peers. Never modified.
protocol: RegisteredProtocol,
@@ -80,9 +80,6 @@ pub struct LegacyProto< TSubstream> {
/// Events to produce from `poll()`.
events: SmallVec<[NetworkBehaviourAction<CustomProtoHandlerIn, LegacyProtoOut>; 4]>,
/// Marker to pin the generics.
marker: PhantomData<TSubstream>,
}
/// State of a peer we're connected to.
@@ -225,7 +222,7 @@ pub enum LegacyProtoOut {
},
}
impl<TSubstream> LegacyProto<TSubstream> {
impl LegacyProto {
/// Creates a `CustomProtos`.
pub fn new(
protocol: impl Into<ProtocolId>,
@@ -241,7 +238,6 @@ impl<TSubstream> LegacyProto<TSubstream> {
incoming: SmallVec::new(),
next_incoming_index: sc_peerset::IncomingIndex(0),
events: SmallVec::new(),
marker: PhantomData,
}
}
@@ -605,7 +601,7 @@ impl<TSubstream> LegacyProto<TSubstream> {
}
}
impl<TSubstream> DiscoveryNetBehaviour for LegacyProto<TSubstream> {
impl DiscoveryNetBehaviour for LegacyProto {
fn add_discovered_nodes(&mut self, peer_ids: impl Iterator<Item = PeerId>) {
self.peerset.discovered(peer_ids.into_iter().map(|peer_id| {
debug!(target: "sub-libp2p", "PSM <= Discovered({:?})", peer_id);
@@ -614,11 +610,8 @@ impl<TSubstream> DiscoveryNetBehaviour for LegacyProto<TSubstream> {
}
}
impl<TSubstream> NetworkBehaviour for LegacyProto<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin,
{
type ProtocolsHandler = CustomProtoHandlerProto<TSubstream>;
impl NetworkBehaviour for LegacyProto {
type ProtocolsHandler = CustomProtoHandlerProto;
type OutEvent = LegacyProtoOut;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
@@ -18,7 +18,7 @@ use super::upgrade::{RegisteredProtocol, RegisteredProtocolEvent, RegisteredProt
use bytes::BytesMut;
use futures::prelude::*;
use futures_timer::Delay;
use libp2p::core::{ConnectedPoint, Negotiated, PeerId, Endpoint};
use libp2p::core::{ConnectedPoint, PeerId, Endpoint};
use libp2p::core::upgrade::{InboundUpgrade, OutboundUpgrade};
use libp2p::swarm::{
ProtocolsHandler, ProtocolsHandlerEvent,
@@ -26,10 +26,11 @@ use libp2p::swarm::{
KeepAlive,
ProtocolsHandlerUpgrErr,
SubstreamProtocol,
NegotiatedSubstream,
};
use log::{debug, error};
use smallvec::{smallvec, SmallVec};
use std::{borrow::Cow, error, fmt, io, marker::PhantomData, mem, time::Duration};
use std::{borrow::Cow, error, fmt, io, mem, time::Duration};
use std::{pin::Pin, task::{Context, Poll}};
/// Implements the `IntoProtocolsHandler` trait of libp2p.
@@ -86,32 +87,22 @@ use std::{pin::Pin, task::{Context, Poll}};
/// We consider that we are now "closed" if the remote closes all the existing substreams.
/// Re-opening it can then be performed by closing all active substream and re-opening one.
///
pub struct CustomProtoHandlerProto<TSubstream> {
pub struct CustomProtoHandlerProto {
/// Configuration for the protocol upgrade to negotiate.
protocol: RegisteredProtocol,
/// Marker to pin the generic type.
marker: PhantomData<TSubstream>,
}
impl<TSubstream> CustomProtoHandlerProto<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin,
{
impl CustomProtoHandlerProto {
/// Builds a new `CustomProtoHandlerProto`.
pub fn new(protocol: RegisteredProtocol) -> Self {
CustomProtoHandlerProto {
protocol,
marker: PhantomData,
}
}
}
impl<TSubstream> IntoProtocolsHandler for CustomProtoHandlerProto<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin,
{
type Handler = CustomProtoHandler<TSubstream>;
impl IntoProtocolsHandler for CustomProtoHandlerProto {
type Handler = CustomProtoHandler;
fn inbound_protocol(&self) -> RegisteredProtocol {
self.protocol.clone()
@@ -132,12 +123,12 @@ where
}
/// The actual handler once the connection has been established.
pub struct CustomProtoHandler<TSubstream> {
pub struct CustomProtoHandler {
/// Configuration for the protocol upgrade to negotiate.
protocol: RegisteredProtocol,
/// State of the communications with the remote.
state: ProtocolState<TSubstream>,
state: ProtocolState,
/// Identifier of the node we're talking to. Used only for logging purposes and shouldn't have
/// any influence on the behaviour.
@@ -155,11 +146,11 @@ pub struct CustomProtoHandler<TSubstream> {
}
/// State of the handler.
enum ProtocolState<TSubstream> {
enum ProtocolState {
/// 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<Negotiated<TSubstream>>; 6]>,
substreams: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 6]>,
/// Deadline after which the initialization is abnormally long.
init_deadline: Delay,
},
@@ -175,9 +166,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<Negotiated<TSubstream>>; 4]>,
substreams: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 4]>,
/// Contains substreams which are being shut down.
shutdown: SmallVec<[RegisteredProtocolSubstream<Negotiated<TSubstream>>; 4]>,
shutdown: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 4]>,
},
/// We are disabled. Contains substreams that are being closed.
@@ -185,7 +176,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<Negotiated<TSubstream>>; 6]>,
shutdown: SmallVec<[RegisteredProtocolSubstream<NegotiatedSubstream>; 6]>,
/// If true, we should reactivate the handler after all the substreams in `shutdown` have
/// been closed.
@@ -257,10 +248,7 @@ pub enum CustomProtoHandlerOut {
},
}
impl<TSubstream> CustomProtoHandler<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin,
{
impl CustomProtoHandler {
/// Enables the handler.
fn enable(&mut self) {
self.state = match mem::replace(&mut self.state, ProtocolState::Poisoned) {
@@ -459,7 +447,7 @@ where
/// Called by `inject_fully_negotiated_inbound` and `inject_fully_negotiated_outbound`.
fn inject_fully_negotiated(
&mut self,
mut substream: RegisteredProtocolSubstream<Negotiated<TSubstream>>
mut substream: RegisteredProtocolSubstream<NegotiatedSubstream>
) {
self.state = match mem::replace(&mut self.state, ProtocolState::Poisoned) {
ProtocolState::Poisoned => {
@@ -515,11 +503,9 @@ where
}
}
impl<TSubstream> ProtocolsHandler for CustomProtoHandler<TSubstream>
where TSubstream: AsyncRead + AsyncWrite + Unpin {
impl ProtocolsHandler for CustomProtoHandler {
type InEvent = CustomProtoHandlerIn;
type OutEvent = CustomProtoHandlerOut;
type Substream = TSubstream;
type Error = ConnectionKillError;
type InboundProtocol = RegisteredProtocol;
type OutboundProtocol = RegisteredProtocol;
@@ -531,14 +517,14 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin {
fn inject_fully_negotiated_inbound(
&mut self,
proto: <Self::InboundProtocol as InboundUpgrade<Negotiated<TSubstream>>>::Output
proto: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output
) {
self.inject_fully_negotiated(proto);
}
fn inject_fully_negotiated_outbound(
&mut self,
proto: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<TSubstream>>>::Output,
proto: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
_: Self::OutboundOpenInfo
) {
self.inject_fully_negotiated(proto);
@@ -601,10 +587,7 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin {
}
}
impl<TSubstream> fmt::Debug for CustomProtoHandler<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin,
{
impl fmt::Debug for CustomProtoHandler {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.debug_struct("CustomProtoHandler")
.finish()
@@ -613,10 +596,10 @@ 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<Negotiated<TSubstream>>>>,
fn shutdown_list
(list: &mut SmallVec<impl smallvec::Array<Item = RegisteredProtocolSubstream<NegotiatedSubstream>>>,
cx: &mut Context)
where TSubstream: AsyncRead + AsyncWrite + Unpin {
{
'outer: for n in (0..list.len()).rev() {
let mut substream = list.swap_remove(n);
loop {
@@ -18,8 +18,8 @@
use futures::{prelude::*, ready};
use codec::{Encode, Decode};
use libp2p::core::nodes::{Substream, listeners::ListenerId};
use libp2p::core::{ConnectedPoint, transport::boxed::Boxed, muxing::StreamMuxerBox};
use libp2p::core::nodes::listeners::ListenerId;
use libp2p::core::ConnectedPoint;
use libp2p::swarm::{Swarm, ProtocolsHandler, IntoProtocolsHandler};
use libp2p::swarm::{PollParameters, NetworkBehaviour, NetworkBehaviourAction};
use libp2p::{PeerId, Multiaddr, Transport};
@@ -31,11 +31,7 @@ use sp_test_primitives::Block;
/// Builds two nodes that have each other as bootstrap nodes.
/// This is to be used only for testing, and a panic will happen if something goes wrong.
fn build_nodes()
-> (
Swarm<Boxed<(PeerId, StreamMuxerBox), io::Error>, CustomProtoWithAddr>,
Swarm<Boxed<(PeerId, StreamMuxerBox), io::Error>, CustomProtoWithAddr>
) {
fn build_nodes() -> (Swarm<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {
let mut out = Vec::with_capacity(2);
let keypairs: Vec<_> = (0..2).map(|_| libp2p::identity::Keypair::generate_ed25519()).collect();
@@ -115,12 +111,12 @@ fn build_nodes()
/// Wraps around the `CustomBehaviour` network behaviour, and adds hardcoded node addresses to it.
struct CustomProtoWithAddr {
inner: LegacyProto<Substream<StreamMuxerBox>>,
inner: LegacyProto,
addrs: Vec<(PeerId, Multiaddr)>,
}
impl std::ops::Deref for CustomProtoWithAddr {
type Target = LegacyProto<Substream<StreamMuxerBox>>;
type Target = LegacyProto;
fn deref(&self) -> &Self::Target {
&self.inner
@@ -134,9 +130,8 @@ impl std::ops::DerefMut for CustomProtoWithAddr {
}
impl NetworkBehaviour for CustomProtoWithAddr {
type ProtocolsHandler =
<LegacyProto<Substream<StreamMuxerBox>> as NetworkBehaviour>::ProtocolsHandler;
type OutEvent = <LegacyProto<Substream<StreamMuxerBox>> as NetworkBehaviour>::OutEvent;
type ProtocolsHandler = <LegacyProto as NetworkBehaviour>::ProtocolsHandler;
type OutEvent = <LegacyProto as NetworkBehaviour>::OutEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
self.inner.new_handler()
@@ -40,7 +40,14 @@ use libp2p::{
upgrade::{InboundUpgrade, ReadOneError, UpgradeInfo, Negotiated},
upgrade::{OutboundUpgrade, read_one, write_one}
},
swarm::{NetworkBehaviour, NetworkBehaviourAction, OneShotHandler, PollParameters, SubstreamProtocol}
swarm::{
NegotiatedSubstream,
NetworkBehaviour,
NetworkBehaviourAction,
OneShotHandler,
PollParameters,
SubstreamProtocol
}
};
use nohash_hasher::IntMap;
use prost::Message;
@@ -220,7 +227,7 @@ enum PeerStatus {
}
/// The light client handler behaviour.
pub struct LightClientHandler<T, B: Block> {
pub struct LightClientHandler<B: Block> {
/// This behaviour's configuration.
config: Config,
/// Blockchain client.
@@ -239,13 +246,10 @@ pub struct LightClientHandler<T, B: Block> {
next_request_id: u64,
/// Handle to use for reporting misbehaviour of peers.
peerset: sc_peerset::PeersetHandle,
/// Type witness term.
_marker: std::marker::PhantomData<T>
}
impl<T, B> LightClientHandler<T, B>
impl<B> LightClientHandler<B>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Block,
{
/// Construct a new light client handler.
@@ -266,7 +270,6 @@ where
outstanding: IntMap::default(),
next_request_id: 1,
peerset,
_marker: std::marker::PhantomData
}
}
@@ -646,12 +649,11 @@ where
}
}
impl<T, B> NetworkBehaviour for LightClientHandler<T, B>
impl<B> NetworkBehaviour for LightClientHandler<B>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Block
{
type ProtocolsHandler = OneShotHandler<T, InboundProtocol, OutboundProtocol, Event<Negotiated<T>>>;
type ProtocolsHandler = OneShotHandler<InboundProtocol, OutboundProtocol, Event<NegotiatedSubstream>>;
type OutEvent = Void;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
@@ -690,7 +692,7 @@ where
self.remove_peer(peer)
}
fn inject_node_event(&mut self, peer: PeerId, event: Event<Negotiated<T>>) {
fn inject_node_event(&mut self, peer: PeerId, event: Event<NegotiatedSubstream>) {
match event {
// An incoming request from remote has been received.
Event::Request(request, mut stream) => {
@@ -1144,8 +1146,8 @@ mod tests {
const CHILD_INFO: ChildInfo<'static> = ChildInfo::new_default(b"foobarbaz");
type Block = sp_runtime::generic::Block<Header<u64, BlakeTwo256>, substrate_test_runtime::Extrinsic>;
type Handler = LightClientHandler<SubstreamRef<Arc<StreamMuxerBox>>, Block>;
type Swarm = libp2p::swarm::Swarm<Boxed<(PeerId, StreamMuxerBox), io::Error>, Handler>;
type Handler = LightClientHandler<Block>;
type Swarm = libp2p::swarm::Swarm<Handler>;
fn empty_proof() -> Vec<u8> {
StorageProof::empty().encode()
@@ -1210,7 +1212,7 @@ mod tests {
( ok: bool
, ps: sc_peerset::PeersetHandle
, cf: super::Config
) -> LightClientHandler<futures::io::Cursor<Vec<u8>>, Block>
) -> LightClientHandler<Block>
{
let client = Arc::new(substrate_test_runtime_client::new());
let checker = Arc::new(DummyFetchChecker::new(ok));
@@ -1221,10 +1223,7 @@ mod tests {
ConnectedPoint::Dialer { address: Multiaddr::empty() }
}
fn poll<T>(mut b: &mut LightClientHandler<T, Block>) -> Poll<NetworkBehaviourAction<OutboundProtocol, Void>>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static
{
fn poll(mut b: &mut LightClientHandler<Block>) -> Poll<NetworkBehaviourAction<OutboundProtocol, Void>> {
let mut p = EmptyPollParams(PeerId::random());
match future::poll_fn(|cx| Pin::new(&mut b).poll(cx, &mut p)).now_or_never() {
Some(a) => Poll::Ready(a),
@@ -504,7 +504,7 @@ impl<B: BlockT> LightDispatch<B> where
}
pub fn is_light_response(&self, peer: &PeerId, request_id: message::RequestId) -> bool {
self.active_peers.get(&peer).map_or(false, |r| r.id == request_id)
self.active_peers.get(peer).map_or(false, |r| r.id == request_id)
}
fn remove(&mut self, peer: PeerId, id: u64) -> Option<Request<B>> {