Simplify trait bounds on NetworkBehaviour (#1405)

* Simplify trait bounds requirements

* More work

* Moar

* Finish

* Fix final tests

* More simplification

* Use separate traits for Inbound/Outbound

* Update gossipsub and remove warnings

* Add documentation to swarm

* Remove BoxSubstream

* Fix tests not compiling

* Fix stack overflow

* Address concerns

* For some reason my IDE ignored libp2p-kad
This commit is contained in:
Pierre Krieger
2020-02-07 16:29:30 +01:00
committed by GitHub
parent 69852a580b
commit 1eff4b9823
32 changed files with 580 additions and 652 deletions

View File

@ -23,10 +23,10 @@ use futures::prelude::*;
use libp2p_core::upgrade::{
InboundUpgrade,
OutboundUpgrade,
ReadOneError,
Negotiated
ReadOneError
};
use libp2p_swarm::{
NegotiatedSubstream,
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
@ -34,7 +34,7 @@ use libp2p_swarm::{
ProtocolsHandlerUpgrErr
};
use smallvec::SmallVec;
use std::{marker::PhantomData, pin::Pin, task::Context, task::Poll, time::Duration};
use std::{pin::Pin, task::Context, task::Poll, time::Duration};
use wasm_timer::Delay;
/// Delay between the moment we connect and the first time we identify.
@ -49,35 +49,32 @@ const TRY_AGAIN_ON_ERR: Duration = Duration::from_secs(60 * 60);
/// Outbound requests are sent periodically. The handler performs expects
/// at least one identification request to be answered by the remote before
/// permitting the underlying connection to be closed.
pub struct IdentifyHandler<TSubstream> {
pub struct IdentifyHandler {
/// Configuration for the protocol.
config: IdentifyProtocolConfig,
/// Pending events to yield.
events: SmallVec<[IdentifyHandlerEvent<TSubstream>; 4]>,
events: SmallVec<[IdentifyHandlerEvent; 4]>,
/// Future that fires when we need to identify the node again.
next_id: Delay,
/// Whether the handler should keep the connection alive.
keep_alive: KeepAlive,
/// Marker for strong typing.
marker: PhantomData<TSubstream>,
}
/// Event produced by the `IdentifyHandler`.
#[derive(Debug)]
pub enum IdentifyHandlerEvent<TSubstream> {
pub enum IdentifyHandlerEvent {
/// We obtained identification information from the remote
Identified(RemoteInfo),
/// We received a request for identification.
Identify(ReplySubstream<Negotiated<TSubstream>>),
Identify(ReplySubstream<NegotiatedSubstream>),
/// Failed to identify the remote.
IdentificationError(ProtocolsHandlerUpgrErr<ReadOneError>),
}
impl<TSubstream> IdentifyHandler<TSubstream> {
impl IdentifyHandler {
/// Creates a new `IdentifyHandler`.
pub fn new() -> Self {
IdentifyHandler {
@ -85,19 +82,14 @@ impl<TSubstream> IdentifyHandler<TSubstream> {
events: SmallVec::new(),
next_id: Delay::new(DELAY_TO_FIRST_ID),
keep_alive: KeepAlive::Yes,
marker: PhantomData,
}
}
}
impl<TSubstream> ProtocolsHandler for IdentifyHandler<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
impl ProtocolsHandler for IdentifyHandler {
type InEvent = ();
type OutEvent = IdentifyHandlerEvent<TSubstream>;
type OutEvent = IdentifyHandlerEvent;
type Error = ReadOneError;
type Substream = TSubstream;
type InboundProtocol = IdentifyProtocolConfig;
type OutboundProtocol = IdentifyProtocolConfig;
type OutboundOpenInfo = ();
@ -108,14 +100,14 @@ where
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<Negotiated<TSubstream>>>::Output
protocol: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output
) {
self.events.push(IdentifyHandlerEvent::Identify(protocol))
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<TSubstream>>>::Output,
protocol: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
_info: Self::OutboundOpenInfo,
) {
self.events.push(IdentifyHandlerEvent::Identified(protocol));
@ -128,7 +120,7 @@ where
&mut self,
_info: Self::OutboundOpenInfo,
err: ProtocolsHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error
>
) {
self.events.push(IdentifyHandlerEvent::IdentificationError(err));
@ -144,7 +136,7 @@ where
ProtocolsHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
IdentifyHandlerEvent<TSubstream>,
IdentifyHandlerEvent,
Self::Error,
>,
> {

View File

@ -26,9 +26,10 @@ use libp2p_core::{
Multiaddr,
PeerId,
PublicKey,
upgrade::{Negotiated, ReadOneError, UpgradeError}
upgrade::{ReadOneError, UpgradeError}
};
use libp2p_swarm::{
NegotiatedSubstream,
NetworkBehaviour,
NetworkBehaviourAction,
PollParameters,
@ -39,7 +40,7 @@ use std::{collections::HashMap, collections::VecDeque, io, pin::Pin, task::Conte
/// Network behaviour that automatically identifies nodes periodically, returns information
/// about them, and answers identify queries from other nodes.
pub struct Identify<TSubstream> {
pub struct Identify {
/// Protocol version to send back to remotes.
protocol_version: String,
/// Agent version to send back to remotes.
@ -49,17 +50,17 @@ pub struct Identify<TSubstream> {
/// For each peer we're connected to, the observed address to send back to it.
observed_addresses: HashMap<PeerId, Multiaddr>,
/// Pending replies to send.
pending_replies: VecDeque<Reply<TSubstream>>,
pending_replies: VecDeque<Reply>,
/// Pending events to be emitted when polled.
events: VecDeque<NetworkBehaviourAction<(), IdentifyEvent>>,
}
/// A pending reply to an inbound identification request.
enum Reply<TSubstream> {
enum Reply {
/// The reply is queued for sending.
Queued {
peer: PeerId,
io: ReplySubstream<Negotiated<TSubstream>>,
io: ReplySubstream<NegotiatedSubstream>,
observed: Multiaddr
},
/// The reply is being sent.
@ -69,7 +70,7 @@ enum Reply<TSubstream> {
}
}
impl<TSubstream> Identify<TSubstream> {
impl Identify {
/// Creates a new `Identify` network behaviour.
pub fn new(protocol_version: String, agent_version: String, local_public_key: PublicKey) -> Self {
Identify {
@ -83,11 +84,8 @@ impl<TSubstream> Identify<TSubstream> {
}
}
impl<TSubstream> NetworkBehaviour for Identify<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
type ProtocolsHandler = IdentifyHandler<TSubstream>;
impl NetworkBehaviour for Identify {
type ProtocolsHandler = IdentifyHandler;
type OutEvent = IdentifyEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {