2019-04-04 12:25:42 -03:00
|
|
|
// Copyright 2019 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2019-07-04 14:47:59 +02:00
|
|
|
//! High level manager of the network.
|
|
|
|
//!
|
|
|
|
//! A [`Swarm`] contains the state of the network as a whole. The entire
|
|
|
|
//! behaviour of a libp2p network can be controlled through the `Swarm`.
|
|
|
|
//! The `Swarm` struct contains all active and pending connections to
|
|
|
|
//! remotes and manages the state of all the substreams that have been
|
|
|
|
//! opened, and all the upgrades that were built upon these substreams.
|
|
|
|
//!
|
|
|
|
//! # Initializing a Swarm
|
|
|
|
//!
|
|
|
|
//! Creating a `Swarm` requires three things:
|
|
|
|
//!
|
|
|
|
//! 1. A network identity of the local node in form of a [`PeerId`].
|
|
|
|
//! 2. An implementation of the [`Transport`] trait. This is the type that
|
|
|
|
//! will be used in order to reach nodes on the network based on their
|
|
|
|
//! address. See the `transport` module for more information.
|
|
|
|
//! 3. An implementation of the [`NetworkBehaviour`] trait. This is a state
|
|
|
|
//! machine that defines how the swarm should behave once it is connected
|
|
|
|
//! to a node.
|
|
|
|
//!
|
|
|
|
//! # Network Behaviour
|
|
|
|
//!
|
|
|
|
//! The [`NetworkBehaviour`] trait is implemented on types that indicate to
|
|
|
|
//! the swarm how it should behave. This includes which protocols are supported
|
|
|
|
//! and which nodes to try to connect to. It is the `NetworkBehaviour` that
|
|
|
|
//! controls what happens on the network. Multiple types that implement
|
|
|
|
//! `NetworkBehaviour` can be composed into a single behaviour.
|
|
|
|
//!
|
|
|
|
//! # Protocols Handler
|
|
|
|
//!
|
|
|
|
//! The [`ProtocolsHandler`] trait defines how each active connection to a
|
|
|
|
//! remote should behave: how to handle incoming substreams, which protocols
|
|
|
|
//! are supported, when to open a new outbound substream, etc.
|
|
|
|
//!
|
|
|
|
|
|
|
|
mod behaviour;
|
|
|
|
mod registry;
|
|
|
|
|
|
|
|
pub mod protocols_handler;
|
|
|
|
pub mod toggle;
|
|
|
|
|
|
|
|
pub use behaviour::{
|
|
|
|
NetworkBehaviour,
|
|
|
|
NetworkBehaviourAction,
|
|
|
|
NetworkBehaviourEventProcess,
|
|
|
|
PollParameters
|
|
|
|
};
|
|
|
|
pub use protocols_handler::{
|
|
|
|
IntoProtocolsHandler,
|
|
|
|
IntoProtocolsHandlerSelect,
|
|
|
|
KeepAlive,
|
|
|
|
ProtocolsHandler,
|
|
|
|
ProtocolsHandlerEvent,
|
|
|
|
ProtocolsHandlerSelect,
|
|
|
|
ProtocolsHandlerUpgrErr,
|
|
|
|
OneShotHandler,
|
|
|
|
SubstreamProtocol
|
|
|
|
};
|
|
|
|
|
|
|
|
use protocols_handler::{NodeHandlerWrapperBuilder, NodeHandlerWrapper, NodeHandlerWrapperError};
|
|
|
|
use futures::prelude::*;
|
|
|
|
use libp2p_core::{
|
2019-04-04 12:25:42 -03:00
|
|
|
Transport, Multiaddr, PeerId, InboundUpgrade, OutboundUpgrade, UpgradeInfo, ProtocolName,
|
|
|
|
muxing::StreamMuxer,
|
|
|
|
nodes::{
|
2019-08-13 15:41:12 +02:00
|
|
|
ListenerId,
|
2019-04-05 13:37:12 -03:00
|
|
|
collection::ConnectionInfo,
|
2019-04-04 12:25:42 -03:00
|
|
|
handled_node::NodeHandler,
|
|
|
|
node::Substream,
|
2019-07-10 10:27:21 +02:00
|
|
|
network::{self, Network, NetworkEvent}
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-07-04 14:47:59 +02:00
|
|
|
transport::TransportError
|
2019-04-04 12:25:42 -03:00
|
|
|
};
|
2019-07-04 14:47:59 +02:00
|
|
|
use registry::{Addresses, AddressIntoIter};
|
2019-04-04 12:25:42 -03:00
|
|
|
use smallvec::SmallVec;
|
2019-09-16 11:08:44 +02:00
|
|
|
use std::{error, fmt, io, ops::{Deref, DerefMut}, pin::Pin, task::{Context, Poll}};
|
2019-04-18 19:17:14 +03:00
|
|
|
use std::collections::HashSet;
|
2019-04-04 12:25:42 -03:00
|
|
|
|
|
|
|
/// Contains the state of the network, plus the way it should behave.
|
2019-06-12 16:21:39 +02:00
|
|
|
pub type Swarm<TTransport, TBehaviour, TConnInfo = PeerId> = ExpandedSwarm<
|
2019-04-07 18:34:14 -03:00
|
|
|
TTransport,
|
|
|
|
TBehaviour,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
|
|
|
|
<TBehaviour as NetworkBehaviour>::ProtocolsHandler,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error,
|
2019-06-12 16:21:39 +02:00
|
|
|
TConnInfo,
|
2019-04-07 18:34:14 -03:00
|
|
|
>;
|
|
|
|
|
|
|
|
/// Contains the state of the network, plus the way it should behave.
|
2019-06-12 16:21:39 +02:00
|
|
|
pub struct ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo = PeerId>
|
2019-04-07 18:34:14 -03:00
|
|
|
where
|
|
|
|
TTransport: Transport,
|
2019-04-04 12:25:42 -03:00
|
|
|
{
|
2019-07-10 10:27:21 +02:00
|
|
|
network: Network<
|
2019-04-04 12:25:42 -03:00
|
|
|
TTransport,
|
2019-04-07 18:34:14 -03:00
|
|
|
TInEvent,
|
|
|
|
TOutEvent,
|
|
|
|
NodeHandlerWrapperBuilder<THandler>,
|
|
|
|
NodeHandlerWrapperError<THandlerErr>,
|
2019-06-12 16:21:39 +02:00
|
|
|
TConnInfo,
|
2019-04-05 13:37:12 -03:00
|
|
|
PeerId,
|
2019-04-04 12:25:42 -03:00
|
|
|
>,
|
|
|
|
|
|
|
|
/// Handles which nodes to connect to and how to handle the events sent back by the protocol
|
|
|
|
/// handlers.
|
|
|
|
behaviour: TBehaviour,
|
|
|
|
|
|
|
|
/// List of protocols that the behaviour says it supports.
|
|
|
|
supported_protocols: SmallVec<[Vec<u8>; 16]>,
|
|
|
|
|
|
|
|
/// List of multiaddresses we're listening on.
|
|
|
|
listened_addrs: SmallVec<[Multiaddr; 8]>,
|
|
|
|
|
|
|
|
/// List of multiaddresses we're listening on, after account for external IP addresses and
|
|
|
|
/// similar mechanisms.
|
2019-05-02 19:46:27 +02:00
|
|
|
external_addrs: Addresses,
|
2019-04-18 19:17:14 +03:00
|
|
|
|
|
|
|
/// List of nodes for which we deny any incoming connection.
|
|
|
|
banned_peers: HashSet<PeerId>,
|
2019-07-09 16:47:24 +02:00
|
|
|
|
|
|
|
/// Pending event message to be delivered.
|
2019-09-16 11:08:44 +02:00
|
|
|
send_event_to_complete: Option<(PeerId, TInEvent)>
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:21:39 +02:00
|
|
|
impl<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo> Deref for
|
|
|
|
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
2019-04-07 18:34:14 -03:00
|
|
|
where
|
|
|
|
TTransport: Transport,
|
2019-04-04 12:25:42 -03:00
|
|
|
{
|
|
|
|
type Target = TBehaviour;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.behaviour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:21:39 +02:00
|
|
|
impl<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo> DerefMut for
|
|
|
|
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
2019-04-07 18:34:14 -03:00
|
|
|
where
|
|
|
|
TTransport: Transport,
|
2019-04-04 12:25:42 -03:00
|
|
|
{
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.behaviour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
impl<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo> Unpin for
|
|
|
|
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
|
|
|
where
|
|
|
|
TTransport: Transport,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:21:39 +02:00
|
|
|
impl<TTransport, TBehaviour, TMuxer, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
|
|
|
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
2019-04-07 18:34:14 -03:00
|
|
|
where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
|
2019-04-04 12:25:42 -03:00
|
|
|
TMuxer: StreamMuxer + Send + Sync + 'static,
|
|
|
|
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
|
|
|
|
<TMuxer as StreamMuxer>::Substream: Send + 'static,
|
2019-06-12 16:21:39 +02:00
|
|
|
TTransport: Transport<Output = (TConnInfo, TMuxer)> + Clone,
|
2019-04-04 12:25:42 -03:00
|
|
|
TTransport::Error: Send + 'static,
|
2019-09-16 11:08:44 +02:00
|
|
|
TTransport::Listener: Unpin + Send + 'static,
|
|
|
|
TTransport::ListenerUpgrade: Unpin + Send + 'static,
|
|
|
|
TTransport::Dial: Unpin + Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
THandlerErr: error::Error,
|
|
|
|
THandler: IntoProtocolsHandler + Send + 'static,
|
|
|
|
<THandler as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEvent, OutEvent = TOutEvent, Substream = Substream<TMuxer>, Error = THandlerErr> + Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
|
|
|
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
2019-06-04 13:08:37 +02:00
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
|
|
|
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
2019-06-04 13:08:37 +02:00
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
<NodeHandlerWrapper<<THandler as IntoProtocolsHandler>::Handler> as NodeHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
2019-06-12 16:21:39 +02:00
|
|
|
TConnInfo: ConnectionInfo<PeerId = PeerId> + fmt::Debug + Clone + Send + 'static,
|
2019-04-04 12:25:42 -03:00
|
|
|
{
|
|
|
|
/// Builds a new `Swarm`.
|
|
|
|
pub fn new(transport: TTransport, behaviour: TBehaviour, local_peer_id: PeerId) -> Self {
|
|
|
|
SwarmBuilder::new(transport, behaviour, local_peer_id)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the transport passed when building this object.
|
|
|
|
pub fn transport(me: &Self) -> &TTransport {
|
2019-07-10 10:27:21 +02:00
|
|
|
me.network.transport()
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts listening on the given address.
|
|
|
|
///
|
|
|
|
/// Returns an error if the address is not supported.
|
2019-08-13 15:41:12 +02:00
|
|
|
pub fn listen_on(me: &mut Self, addr: Multiaddr) -> Result<ListenerId, TransportError<TTransport::Error>> {
|
2019-07-10 10:27:21 +02:00
|
|
|
me.network.listen_on(addr)
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:41:12 +02:00
|
|
|
/// Remove some listener.
|
|
|
|
pub fn remove_listener(me: &mut Self, id: ListenerId) -> Option<TTransport::Listener> {
|
|
|
|
me.network.remove_listener(id)
|
|
|
|
}
|
|
|
|
|
2019-04-04 12:25:42 -03:00
|
|
|
/// Tries to dial the given address.
|
|
|
|
///
|
|
|
|
/// Returns an error if the address is not supported.
|
|
|
|
pub fn dial_addr(me: &mut Self, addr: Multiaddr) -> Result<(), TransportError<TTransport::Error>> {
|
|
|
|
let handler = me.behaviour.new_handler();
|
2019-07-10 10:27:21 +02:00
|
|
|
me.network.dial(addr, handler.into_node_handler_builder())
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to reach the given peer using the elements in the topology.
|
|
|
|
///
|
|
|
|
/// Has no effect if we are already connected to that peer, or if no address is known for the
|
|
|
|
/// peer.
|
|
|
|
pub fn dial(me: &mut Self, peer_id: PeerId) {
|
|
|
|
let addrs = me.behaviour.addresses_of_peer(&peer_id);
|
2019-07-10 10:27:21 +02:00
|
|
|
match me.network.peer(peer_id.clone()) {
|
|
|
|
network::Peer::NotConnected(peer) => {
|
2019-04-04 12:25:42 -03:00
|
|
|
let handler = me.behaviour.new_handler().into_node_handler_builder();
|
|
|
|
if peer.connect_iter(addrs, handler).is_err() {
|
|
|
|
me.behaviour.inject_dial_failure(&peer_id);
|
|
|
|
}
|
|
|
|
},
|
2019-07-10 10:27:21 +02:00
|
|
|
network::Peer::PendingConnect(mut peer) => {
|
2019-04-04 12:25:42 -03:00
|
|
|
peer.append_multiaddr_attempts(addrs)
|
|
|
|
},
|
2019-07-10 10:27:21 +02:00
|
|
|
network::Peer::Connected(_) | network::Peer::LocalNode => {}
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that produces the list of addresses we're listening on.
|
|
|
|
pub fn listeners(me: &Self) -> impl Iterator<Item = &Multiaddr> {
|
2019-07-10 10:27:21 +02:00
|
|
|
me.network.listen_addrs()
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that produces the list of addresses that other nodes can use to reach
|
|
|
|
/// us.
|
|
|
|
pub fn external_addresses(me: &Self) -> impl Iterator<Item = &Multiaddr> {
|
|
|
|
me.external_addrs.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the peer ID of the swarm passed as parameter.
|
|
|
|
pub fn local_peer_id(me: &Self) -> &PeerId {
|
2019-07-10 10:27:21 +02:00
|
|
|
&me.network.local_peer_id()
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds an external address.
|
|
|
|
///
|
|
|
|
/// An external address is an address we are listening on but that accounts for things such as
|
|
|
|
/// NAT traversal.
|
|
|
|
pub fn add_external_address(me: &mut Self, addr: Multiaddr) {
|
2019-05-02 19:46:27 +02:00
|
|
|
me.external_addrs.add(addr)
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
2019-04-18 19:17:14 +03:00
|
|
|
|
2019-06-12 16:21:39 +02:00
|
|
|
/// Returns the connection info of a node, or `None` if we're not connected to it.
|
2019-07-10 10:27:21 +02:00
|
|
|
// TODO: should take &self instead of &mut self, but the API in network requires &mut
|
2019-06-12 16:21:39 +02:00
|
|
|
pub fn connection_info(me: &mut Self, peer_id: &PeerId) -> Option<TConnInfo> {
|
2019-07-10 10:27:21 +02:00
|
|
|
if let Some(mut n) = me.network.peer(peer_id.clone()).into_connected() {
|
2019-06-12 16:21:39 +02:00
|
|
|
Some(n.connection_info().clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 19:17:14 +03:00
|
|
|
/// Bans a peer by its peer ID.
|
|
|
|
///
|
|
|
|
/// Any incoming connection and any dialing attempt will immediately be rejected.
|
|
|
|
/// This function has no effect is the peer is already banned.
|
2019-04-23 10:54:25 +02:00
|
|
|
pub fn ban_peer_id(me: &mut Self, peer_id: PeerId) {
|
|
|
|
me.banned_peers.insert(peer_id.clone());
|
2019-07-10 10:27:21 +02:00
|
|
|
if let Some(c) = me.network.peer(peer_id).into_connected() {
|
2019-04-23 10:54:25 +02:00
|
|
|
c.close();
|
|
|
|
}
|
2019-04-18 19:17:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unbans a peer.
|
2019-04-23 10:54:25 +02:00
|
|
|
pub fn unban_peer_id(me: &mut Self, peer_id: PeerId) {
|
|
|
|
me.banned_peers.remove(&peer_id);
|
2019-04-18 19:17:14 +03:00
|
|
|
}
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:21:39 +02:00
|
|
|
impl<TTransport, TBehaviour, TMuxer, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo> Stream for
|
|
|
|
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
2019-04-07 18:34:14 -03:00
|
|
|
where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
|
2019-04-04 12:25:42 -03:00
|
|
|
TMuxer: StreamMuxer + Send + Sync + 'static,
|
|
|
|
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
|
|
|
|
<TMuxer as StreamMuxer>::Substream: Send + 'static,
|
2019-06-12 16:21:39 +02:00
|
|
|
TTransport: Transport<Output = (TConnInfo, TMuxer)> + Clone,
|
2019-04-04 12:25:42 -03:00
|
|
|
TTransport::Error: Send + 'static,
|
2019-09-16 11:08:44 +02:00
|
|
|
TTransport::Listener: Unpin + Send + 'static,
|
|
|
|
TTransport::ListenerUpgrade: Unpin + Send + 'static,
|
|
|
|
TTransport::Dial: Unpin + Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
THandlerErr: error::Error,
|
|
|
|
THandler: IntoProtocolsHandler + Send + 'static,
|
|
|
|
<THandler as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEvent, OutEvent = TOutEvent, Substream = Substream<TMuxer>, Error = THandlerErr> + Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
2019-06-04 13:08:37 +02:00
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
|
|
|
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
|
|
|
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
2019-06-04 13:08:37 +02:00
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
|
|
|
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
|
|
|
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
|
|
|
<NodeHandlerWrapper<<THandler as IntoProtocolsHandler>::Handler> as NodeHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
2019-06-12 16:21:39 +02:00
|
|
|
TConnInfo: ConnectionInfo<PeerId = PeerId> + fmt::Debug + Clone + Send + 'static,
|
2019-04-04 12:25:42 -03:00
|
|
|
{
|
2019-09-16 11:08:44 +02:00
|
|
|
type Item = Result<TBehaviour::OutEvent, io::Error>;
|
|
|
|
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
2019-09-20 10:46:13 +02:00
|
|
|
// We use a `this` variable because the compiler can't mutably borrow multiple times
|
|
|
|
// across a `Deref`.
|
2019-09-16 11:08:44 +02:00
|
|
|
let this = &mut *self;
|
2019-04-04 12:25:42 -03:00
|
|
|
|
|
|
|
loop {
|
2019-07-10 10:27:21 +02:00
|
|
|
let mut network_not_ready = false;
|
2019-04-04 12:25:42 -03:00
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
match this.network.poll(cx) {
|
|
|
|
Poll::Pending => network_not_ready = true,
|
|
|
|
Poll::Ready(NetworkEvent::NodeEvent { conn_info, event }) => {
|
|
|
|
this.behaviour.inject_node_event(conn_info.peer_id().clone(), event);
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::Connected { conn_info, endpoint }) => {
|
|
|
|
if this.banned_peers.contains(conn_info.peer_id()) {
|
|
|
|
this.network.peer(conn_info.peer_id().clone())
|
2019-04-18 19:17:14 +03:00
|
|
|
.into_connected()
|
2019-07-10 10:27:21 +02:00
|
|
|
.expect("the Network just notified us that we were connected; QED")
|
2019-04-18 19:17:14 +03:00
|
|
|
.close();
|
|
|
|
} else {
|
2019-09-16 11:08:44 +02:00
|
|
|
this.behaviour.inject_connected(conn_info.peer_id().clone(), endpoint);
|
2019-04-18 19:17:14 +03:00
|
|
|
}
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::NodeClosed { conn_info, endpoint, .. }) => {
|
|
|
|
this.behaviour.inject_disconnected(conn_info.peer_id(), endpoint);
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::Replaced { new_info, closed_endpoint, endpoint, .. }) => {
|
|
|
|
this.behaviour.inject_replaced(new_info.peer_id().clone(), closed_endpoint, endpoint);
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::IncomingConnection(incoming)) => {
|
|
|
|
let handler = this.behaviour.new_handler();
|
2019-04-04 12:25:42 -03:00
|
|
|
incoming.accept(handler.into_node_handler_builder());
|
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::NewListenerAddress { listen_addr, .. }) => {
|
|
|
|
if !this.listened_addrs.contains(&listen_addr) {
|
|
|
|
this.listened_addrs.push(listen_addr.clone())
|
2019-04-10 10:29:21 +02:00
|
|
|
}
|
2019-09-16 11:08:44 +02:00
|
|
|
this.behaviour.inject_new_listen_addr(&listen_addr);
|
2019-04-10 10:29:21 +02:00
|
|
|
}
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::ExpiredListenerAddress { listen_addr, .. }) => {
|
|
|
|
this.listened_addrs.retain(|a| a != &listen_addr);
|
|
|
|
this.behaviour.inject_expired_listen_addr(&listen_addr);
|
2019-04-10 10:29:21 +02:00
|
|
|
}
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::ListenerClosed { listener_id, .. }) =>
|
|
|
|
this.behaviour.inject_listener_closed(listener_id),
|
|
|
|
Poll::Ready(NetworkEvent::ListenerError { listener_id, error }) =>
|
|
|
|
this.behaviour.inject_listener_error(listener_id, &error),
|
|
|
|
Poll::Ready(NetworkEvent::IncomingConnectionError { .. }) => {},
|
|
|
|
Poll::Ready(NetworkEvent::DialError { peer_id, multiaddr, error, new_state }) => {
|
|
|
|
this.behaviour.inject_addr_reach_failure(Some(&peer_id), &multiaddr, &error);
|
2019-07-10 10:27:21 +02:00
|
|
|
if let network::PeerState::NotConnected = new_state {
|
2019-09-16 11:08:44 +02:00
|
|
|
this.behaviour.inject_dial_failure(&peer_id);
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkEvent::UnknownPeerDialError { multiaddr, error, .. }) => {
|
|
|
|
this.behaviour.inject_addr_reach_failure(None, &multiaddr, &error);
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:47:24 +02:00
|
|
|
// Try to deliver pending event.
|
2019-09-16 11:08:44 +02:00
|
|
|
if let Some((id, pending)) = this.send_event_to_complete.take() {
|
|
|
|
if let Some(mut peer) = this.network.peer(id.clone()).into_connected() {
|
|
|
|
match peer.poll_ready_event(cx) {
|
|
|
|
Poll::Ready(()) => peer.start_send_event(pending),
|
|
|
|
Poll::Pending => {
|
|
|
|
this.send_event_to_complete = Some((id, pending));
|
|
|
|
return Poll::Pending
|
|
|
|
},
|
2019-07-09 16:47:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 12:25:42 -03:00
|
|
|
let behaviour_poll = {
|
2019-06-18 10:23:26 +02:00
|
|
|
let mut parameters = SwarmPollParameters {
|
2019-09-16 11:08:44 +02:00
|
|
|
local_peer_id: &mut this.network.local_peer_id(),
|
|
|
|
supported_protocols: &this.supported_protocols,
|
|
|
|
listened_addrs: &this.listened_addrs,
|
|
|
|
external_addrs: &this.external_addrs
|
2019-04-04 12:25:42 -03:00
|
|
|
};
|
2019-09-16 11:08:44 +02:00
|
|
|
this.behaviour.poll(cx, &mut parameters)
|
2019-04-04 12:25:42 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
match behaviour_poll {
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending if network_not_ready => return Poll::Pending,
|
|
|
|
Poll::Pending => (),
|
|
|
|
Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
|
|
|
|
return Poll::Ready(Some(Ok(event)))
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkBehaviourAction::DialAddress { address }) => {
|
|
|
|
let _ = ExpandedSwarm::dial_addr(&mut *this, address);
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }) => {
|
|
|
|
if this.banned_peers.contains(&peer_id) {
|
|
|
|
this.behaviour.inject_dial_failure(&peer_id);
|
2019-04-18 19:17:14 +03:00
|
|
|
} else {
|
2019-09-16 11:08:44 +02:00
|
|
|
ExpandedSwarm::dial(&mut *this, peer_id);
|
2019-04-18 19:17:14 +03:00
|
|
|
}
|
2019-04-04 12:25:42 -03:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }) => {
|
|
|
|
if let Some(mut peer) = this.network.peer(peer_id.clone()).into_connected() {
|
|
|
|
if let Poll::Ready(()) = peer.poll_ready_event(cx) {
|
|
|
|
peer.start_send_event(event);
|
|
|
|
} else {
|
|
|
|
debug_assert!(this.send_event_to_complete.is_none());
|
|
|
|
this.send_event_to_complete = Some((peer_id, event));
|
|
|
|
return Poll::Pending;
|
2019-07-09 16:47:24 +02:00
|
|
|
}
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) => {
|
|
|
|
for addr in this.network.address_translation(&address) {
|
|
|
|
if this.external_addrs.iter().all(|a| *a != addr) {
|
|
|
|
this.behaviour.inject_new_external_addr(&addr);
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
2019-09-16 11:08:44 +02:00
|
|
|
this.external_addrs.add(addr)
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.
|
|
|
|
// TODO: #[derive(Debug)]
|
2019-06-18 10:23:26 +02:00
|
|
|
pub struct SwarmPollParameters<'a> {
|
2019-04-04 12:25:42 -03:00
|
|
|
local_peer_id: &'a PeerId,
|
|
|
|
supported_protocols: &'a [Vec<u8>],
|
|
|
|
listened_addrs: &'a [Multiaddr],
|
2019-06-18 10:23:26 +02:00
|
|
|
external_addrs: &'a Addresses,
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:23:26 +02:00
|
|
|
impl<'a> PollParameters for SwarmPollParameters<'a> {
|
|
|
|
type SupportedProtocolsIter = std::vec::IntoIter<Vec<u8>>;
|
|
|
|
type ListenedAddressesIter = std::vec::IntoIter<Multiaddr>;
|
|
|
|
type ExternalAddressesIter = AddressIntoIter;
|
|
|
|
|
|
|
|
fn supported_protocols(&self) -> Self::SupportedProtocolsIter {
|
|
|
|
self.supported_protocols.to_vec().into_iter()
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:23:26 +02:00
|
|
|
fn listened_addresses(&self) -> Self::ListenedAddressesIter {
|
|
|
|
self.listened_addrs.to_vec().into_iter()
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:23:26 +02:00
|
|
|
fn external_addresses(&self) -> Self::ExternalAddressesIter {
|
|
|
|
self.external_addrs.clone().into_iter()
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:23:26 +02:00
|
|
|
fn local_peer_id(&self) -> &PeerId {
|
2019-04-04 12:25:42 -03:00
|
|
|
self.local_peer_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SwarmBuilder<TTransport, TBehaviour> {
|
|
|
|
incoming_limit: Option<u32>,
|
|
|
|
local_peer_id: PeerId,
|
|
|
|
transport: TTransport,
|
|
|
|
behaviour: TBehaviour,
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:21:39 +02:00
|
|
|
impl<TTransport, TBehaviour, TMuxer, TConnInfo> SwarmBuilder<TTransport, TBehaviour>
|
2019-04-04 12:25:42 -03:00
|
|
|
where TBehaviour: NetworkBehaviour,
|
|
|
|
TMuxer: StreamMuxer + Send + Sync + 'static,
|
|
|
|
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
|
|
|
|
<TMuxer as StreamMuxer>::Substream: Send + 'static,
|
2019-06-12 16:21:39 +02:00
|
|
|
TTransport: Transport<Output = (TConnInfo, TMuxer)> + Clone,
|
2019-04-04 12:25:42 -03:00
|
|
|
TTransport::Error: Send + 'static,
|
2019-09-16 11:08:44 +02:00
|
|
|
TTransport::Listener: Unpin + Send + 'static,
|
|
|
|
TTransport::ListenerUpgrade: Unpin + Send + 'static,
|
|
|
|
TTransport::Dial: Unpin + Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
<TBehaviour as NetworkBehaviour>::ProtocolsHandler: Send + 'static,
|
|
|
|
<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler: ProtocolsHandler<Substream = Substream<TMuxer>> + Send + 'static,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent: Send + 'static,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Send + 'static,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error: Send + 'static,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
|
|
|
<<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
2019-06-04 13:08:37 +02:00
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
|
|
|
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
|
|
|
<<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
2019-06-04 13:08:37 +02:00
|
|
|
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
2019-04-07 18:34:14 -03:00
|
|
|
<NodeHandlerWrapper<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler> as NodeHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
2019-06-12 16:21:39 +02:00
|
|
|
TConnInfo: ConnectionInfo<PeerId = PeerId> + fmt::Debug + Clone + Send + 'static,
|
2019-04-04 12:25:42 -03:00
|
|
|
{
|
|
|
|
pub fn new(transport: TTransport, behaviour: TBehaviour, local_peer_id: PeerId) -> Self {
|
|
|
|
SwarmBuilder {
|
|
|
|
incoming_limit: None,
|
|
|
|
local_peer_id,
|
|
|
|
transport,
|
|
|
|
behaviour,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn incoming_limit(mut self, incoming_limit: Option<u32>) -> Self {
|
|
|
|
self.incoming_limit = incoming_limit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:21:39 +02:00
|
|
|
pub fn build(mut self) -> Swarm<TTransport, TBehaviour, TConnInfo> {
|
2019-04-04 12:25:42 -03:00
|
|
|
let supported_protocols = self.behaviour
|
|
|
|
.new_handler()
|
2019-05-08 20:23:28 +02:00
|
|
|
.inbound_protocol()
|
2019-04-04 12:25:42 -03:00
|
|
|
.protocol_info()
|
|
|
|
.into_iter()
|
|
|
|
.map(|info| info.protocol_name().to_vec())
|
|
|
|
.collect();
|
|
|
|
|
2019-07-10 10:27:21 +02:00
|
|
|
let network = Network::new_with_incoming_limit(self.transport, self.local_peer_id, self.incoming_limit);
|
2019-04-04 12:25:42 -03:00
|
|
|
|
2019-04-07 18:34:14 -03:00
|
|
|
ExpandedSwarm {
|
2019-07-10 10:27:21 +02:00
|
|
|
network,
|
2019-04-04 12:25:42 -03:00
|
|
|
behaviour: self.behaviour,
|
|
|
|
supported_protocols,
|
|
|
|
listened_addrs: SmallVec::new(),
|
2019-05-02 19:46:27 +02:00
|
|
|
external_addrs: Addresses::default(),
|
2019-04-18 19:17:14 +03:00
|
|
|
banned_peers: HashSet::new(),
|
2019-07-09 16:47:24 +02:00
|
|
|
send_event_to_complete: None
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::protocols_handler::{DummyProtocolsHandler, ProtocolsHandler};
|
2019-07-04 14:47:59 +02:00
|
|
|
use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters, SwarmBuilder};
|
|
|
|
use libp2p_core::{
|
|
|
|
ConnectedPoint,
|
|
|
|
identity,
|
|
|
|
Multiaddr,
|
|
|
|
PeerId,
|
|
|
|
PublicKey,
|
|
|
|
transport::dummy::{DummyStream, DummyTransport}
|
|
|
|
};
|
|
|
|
use libp2p_mplex::Multiplex;
|
2019-04-04 12:25:42 -03:00
|
|
|
use futures::prelude::*;
|
2019-09-16 11:08:44 +02:00
|
|
|
use std::{marker::PhantomData, task::Context, task::Poll};
|
2019-04-04 12:25:42 -03:00
|
|
|
use void::Void;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct DummyBehaviour<TSubstream> {
|
|
|
|
marker: PhantomData<TSubstream>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TSubstream> NetworkBehaviour
|
|
|
|
for DummyBehaviour<TSubstream>
|
2019-09-16 11:08:44 +02:00
|
|
|
where TSubstream: AsyncRead + AsyncWrite + Unpin
|
2019-04-04 12:25:42 -03:00
|
|
|
{
|
|
|
|
type ProtocolsHandler = DummyProtocolsHandler<TSubstream>;
|
|
|
|
type OutEvent = Void;
|
|
|
|
|
|
|
|
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
|
|
|
DummyProtocolsHandler::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_connected(&mut self, _: PeerId, _: ConnectedPoint) {}
|
|
|
|
|
|
|
|
fn inject_disconnected(&mut self, _: &PeerId, _: ConnectedPoint) {}
|
|
|
|
|
|
|
|
fn inject_node_event(&mut self, _: PeerId,
|
|
|
|
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent) {}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
fn poll(&mut self, _: &mut Context, _: &mut impl PollParameters) ->
|
|
|
|
Poll<NetworkBehaviourAction<<Self::ProtocolsHandler as
|
2019-04-04 12:25:42 -03:00
|
|
|
ProtocolsHandler>::InEvent, Self::OutEvent>>
|
|
|
|
{
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_random_id() -> PublicKey {
|
|
|
|
identity::Keypair::generate_ed25519().public()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_build_swarm() {
|
|
|
|
let id = get_random_id();
|
2019-07-04 14:47:59 +02:00
|
|
|
let transport = DummyTransport::<(PeerId, Multiplex<DummyStream>)>::new();
|
2019-04-04 12:25:42 -03:00
|
|
|
let behaviour = DummyBehaviour{marker: PhantomData};
|
|
|
|
let swarm = SwarmBuilder::new(transport, behaviour, id.into())
|
|
|
|
.incoming_limit(Some(4)).build();
|
2019-07-10 10:27:21 +02:00
|
|
|
assert_eq!(swarm.network.incoming_limit(), Some(4));
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_build_swarm_with_max_listeners_none() {
|
|
|
|
let id = get_random_id();
|
2019-07-04 14:47:59 +02:00
|
|
|
let transport = DummyTransport::<(PeerId, Multiplex<DummyStream>)>::new();
|
2019-04-04 12:25:42 -03:00
|
|
|
let behaviour = DummyBehaviour{marker: PhantomData};
|
|
|
|
let swarm = SwarmBuilder::new(transport, behaviour, id.into()).build();
|
2019-07-10 10:27:21 +02:00
|
|
|
assert!(swarm.network.incoming_limit().is_none())
|
2019-04-04 12:25:42 -03:00
|
|
|
}
|
|
|
|
}
|