2018-12-07 10:23:38 +01:00
|
|
|
// Copyright 2018 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-09-02 11:16:52 +02:00
|
|
|
use crate::handler::{IdentifyHandler, IdentifyHandlerEvent};
|
2019-09-16 11:08:44 +02:00
|
|
|
use crate::protocol::{IdentifyInfo, ReplySubstream};
|
2018-12-07 10:23:38 +01:00
|
|
|
use futures::prelude::*;
|
2019-07-04 14:47:59 +02:00
|
|
|
use libp2p_core::{
|
|
|
|
ConnectedPoint,
|
|
|
|
Multiaddr,
|
|
|
|
PeerId,
|
|
|
|
PublicKey,
|
2019-09-16 11:08:44 +02:00
|
|
|
upgrade::{Negotiated, ReadOneError, UpgradeError}
|
2019-07-04 14:47:59 +02:00
|
|
|
};
|
|
|
|
use libp2p_swarm::{
|
|
|
|
NetworkBehaviour,
|
|
|
|
NetworkBehaviourAction,
|
|
|
|
PollParameters,
|
|
|
|
ProtocolsHandler,
|
|
|
|
ProtocolsHandlerUpgrErr
|
|
|
|
};
|
2019-09-16 11:08:44 +02:00
|
|
|
use std::{collections::HashMap, collections::VecDeque, io, pin::Pin, task::Context, task::Poll};
|
2018-12-07 10:23:38 +01:00
|
|
|
use void::Void;
|
|
|
|
|
|
|
|
/// Network behaviour that automatically identifies nodes periodically, returns information
|
|
|
|
/// about them, and answers identify queries from other nodes.
|
|
|
|
pub struct Identify<TSubstream> {
|
|
|
|
/// Protocol version to send back to remotes.
|
|
|
|
protocol_version: String,
|
|
|
|
/// Agent version to send back to remotes.
|
|
|
|
agent_version: String,
|
2019-01-26 23:57:53 +01:00
|
|
|
/// The public key of the local node. To report on the wire.
|
|
|
|
local_public_key: PublicKey,
|
2018-12-07 10:23:38 +01:00
|
|
|
/// For each peer we're connected to, the observed address to send back to it.
|
|
|
|
observed_addresses: HashMap<PeerId, Multiaddr>,
|
2019-09-02 11:16:52 +02:00
|
|
|
/// Pending replies to send.
|
|
|
|
pending_replies: VecDeque<Reply<TSubstream>>,
|
|
|
|
/// Pending events to be emitted when polled.
|
|
|
|
events: VecDeque<NetworkBehaviourAction<Void, IdentifyEvent>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A pending reply to an inbound identification request.
|
|
|
|
enum Reply<TSubstream> {
|
|
|
|
/// The reply is queued for sending.
|
|
|
|
Queued {
|
|
|
|
peer: PeerId,
|
|
|
|
io: ReplySubstream<Negotiated<TSubstream>>,
|
|
|
|
observed: Multiaddr
|
|
|
|
},
|
|
|
|
/// The reply is being sent.
|
|
|
|
Sending {
|
|
|
|
peer: PeerId,
|
2019-09-16 11:08:44 +02:00
|
|
|
io: Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>>,
|
2019-09-02 11:16:52 +02:00
|
|
|
}
|
2018-12-07 10:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<TSubstream> Identify<TSubstream> {
|
2019-09-02 11:16:52 +02:00
|
|
|
/// Creates a new `Identify` network behaviour.
|
2019-01-26 23:57:53 +01:00
|
|
|
pub fn new(protocol_version: String, agent_version: String, local_public_key: PublicKey) -> Self {
|
2018-12-07 10:23:38 +01:00
|
|
|
Identify {
|
|
|
|
protocol_version,
|
|
|
|
agent_version,
|
2019-01-26 23:57:53 +01:00
|
|
|
local_public_key,
|
2018-12-07 10:23:38 +01:00
|
|
|
observed_addresses: HashMap::new(),
|
2019-09-02 11:16:52 +02:00
|
|
|
pending_replies: VecDeque::new(),
|
2018-12-07 10:23:38 +01:00
|
|
|
events: VecDeque::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:57:53 +01:00
|
|
|
impl<TSubstream> NetworkBehaviour for Identify<TSubstream>
|
2018-12-07 10:23:38 +01:00
|
|
|
where
|
2019-09-16 11:08:44 +02:00
|
|
|
TSubstream: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
2018-12-07 10:23:38 +01:00
|
|
|
{
|
2019-09-02 11:16:52 +02:00
|
|
|
type ProtocolsHandler = IdentifyHandler<TSubstream>;
|
2018-12-07 10:23:38 +01:00
|
|
|
type OutEvent = IdentifyEvent;
|
|
|
|
|
|
|
|
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
2019-09-02 11:16:52 +02:00
|
|
|
IdentifyHandler::new()
|
2018-12-07 10:23:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-30 14:55:39 +01:00
|
|
|
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
2019-01-26 23:57:53 +01:00
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
2018-12-07 10:23:38 +01:00
|
|
|
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint) {
|
|
|
|
let observed = match endpoint {
|
|
|
|
ConnectedPoint::Dialer { address } => address,
|
|
|
|
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.observed_addresses.insert(peer_id, observed);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_disconnected(&mut self, peer_id: &PeerId, _: ConnectedPoint) {
|
|
|
|
self.observed_addresses.remove(peer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_node_event(
|
|
|
|
&mut self,
|
|
|
|
peer_id: PeerId,
|
|
|
|
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
|
|
|
) {
|
|
|
|
match event {
|
2019-09-02 11:16:52 +02:00
|
|
|
IdentifyHandlerEvent::Identified(remote) => {
|
|
|
|
self.events.push_back(
|
|
|
|
NetworkBehaviourAction::GenerateEvent(
|
|
|
|
IdentifyEvent::Received {
|
|
|
|
peer_id,
|
|
|
|
info: remote.info,
|
|
|
|
observed_addr: remote.observed_addr.clone(),
|
|
|
|
}));
|
|
|
|
self.events.push_back(
|
|
|
|
NetworkBehaviourAction::ReportObservedAddr {
|
2018-12-07 10:23:38 +01:00
|
|
|
address: remote.observed_addr,
|
|
|
|
});
|
|
|
|
}
|
2019-09-02 11:16:52 +02:00
|
|
|
IdentifyHandlerEvent::Identify(sender) => {
|
2018-12-07 10:23:38 +01:00
|
|
|
let observed = self.observed_addresses.get(&peer_id)
|
2018-12-19 22:22:39 +00:00
|
|
|
.expect("We only receive events from nodes we're connected to. We insert \
|
2018-12-07 10:23:38 +01:00
|
|
|
into the hashmap when we connect to a node and remove only when we \
|
|
|
|
disconnect; QED");
|
2019-09-02 11:16:52 +02:00
|
|
|
self.pending_replies.push_back(
|
|
|
|
Reply::Queued {
|
|
|
|
peer: peer_id,
|
|
|
|
io: sender,
|
|
|
|
observed: observed.clone()
|
|
|
|
});
|
2018-12-07 10:23:38 +01:00
|
|
|
}
|
2019-09-02 11:16:52 +02:00
|
|
|
IdentifyHandlerEvent::IdentificationError(error) => {
|
|
|
|
self.events.push_back(
|
|
|
|
NetworkBehaviourAction::GenerateEvent(
|
|
|
|
IdentifyEvent::Error { peer_id, error }));
|
2018-12-07 10:23:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
&mut self,
|
2019-09-16 11:08:44 +02:00
|
|
|
cx: &mut Context,
|
2019-06-18 10:23:26 +02:00
|
|
|
params: &mut impl PollParameters,
|
2019-09-16 11:08:44 +02:00
|
|
|
) -> Poll<
|
2018-12-07 10:23:38 +01:00
|
|
|
NetworkBehaviourAction<
|
|
|
|
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
|
|
|
|
Self::OutEvent,
|
|
|
|
>,
|
|
|
|
> {
|
|
|
|
if let Some(event) = self.events.pop_front() {
|
2019-09-16 11:08:44 +02:00
|
|
|
return Poll::Ready(event);
|
2018-12-07 10:23:38 +01:00
|
|
|
}
|
|
|
|
|
2019-09-02 11:16:52 +02:00
|
|
|
if let Some(r) = self.pending_replies.pop_front() {
|
2018-12-07 10:23:38 +01:00
|
|
|
// The protocol names can be bytes, but the identify protocol except UTF-8 strings.
|
|
|
|
// There's not much we can do to solve this conflict except strip non-UTF-8 characters.
|
2019-09-02 11:16:52 +02:00
|
|
|
let protocols: Vec<_> = params
|
2018-12-07 10:23:38 +01:00
|
|
|
.supported_protocols()
|
2019-06-18 10:23:26 +02:00
|
|
|
.map(|p| String::from_utf8_lossy(&p).to_string())
|
2018-12-07 10:23:38 +01:00
|
|
|
.collect();
|
|
|
|
|
2019-06-18 10:23:26 +02:00
|
|
|
let mut listen_addrs: Vec<_> = params.external_addresses().collect();
|
|
|
|
listen_addrs.extend(params.listened_addresses());
|
2019-02-07 11:04:04 +01:00
|
|
|
|
2019-09-02 11:16:52 +02:00
|
|
|
let mut sending = 0;
|
|
|
|
let to_send = self.pending_replies.len() + 1;
|
|
|
|
let mut reply = Some(r);
|
|
|
|
loop {
|
|
|
|
match reply {
|
|
|
|
Some(Reply::Queued { peer, io, observed }) => {
|
|
|
|
let info = IdentifyInfo {
|
|
|
|
public_key: self.local_public_key.clone(),
|
|
|
|
protocol_version: self.protocol_version.clone(),
|
|
|
|
agent_version: self.agent_version.clone(),
|
|
|
|
listen_addrs: listen_addrs.clone(),
|
|
|
|
protocols: protocols.clone(),
|
|
|
|
};
|
2019-09-16 11:08:44 +02:00
|
|
|
let io = Box::pin(io.send(info, &observed));
|
2019-09-02 11:16:52 +02:00
|
|
|
reply = Some(Reply::Sending { peer, io });
|
|
|
|
}
|
|
|
|
Some(Reply::Sending { peer, mut io }) => {
|
|
|
|
sending += 1;
|
2019-09-16 11:08:44 +02:00
|
|
|
match Future::poll(Pin::new(&mut io), cx) {
|
|
|
|
Poll::Ready(Ok(())) => {
|
2019-09-02 11:16:52 +02:00
|
|
|
let event = IdentifyEvent::Sent { peer_id: peer };
|
2019-09-16 11:08:44 +02:00
|
|
|
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
2019-09-02 11:16:52 +02:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending => {
|
2019-09-02 11:16:52 +02:00
|
|
|
self.pending_replies.push_back(Reply::Sending { peer, io });
|
|
|
|
if sending == to_send {
|
|
|
|
// All remaining futures are NotReady
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
reply = self.pending_replies.pop_front();
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(Err(err)) => {
|
2019-09-02 11:16:52 +02:00
|
|
|
let event = IdentifyEvent::Error {
|
|
|
|
peer_id: peer,
|
2019-09-16 11:08:44 +02:00
|
|
|
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err.into()))
|
2019-09-02 11:16:52 +02:00
|
|
|
};
|
2019-09-16 11:08:44 +02:00
|
|
|
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
2019-09-02 11:16:52 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => unreachable!()
|
|
|
|
}
|
2018-12-07 10:23:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending
|
2018-12-07 10:23:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 11:16:52 +02:00
|
|
|
/// Event emitted by the `Identify` behaviour.
|
2018-12-07 10:23:38 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum IdentifyEvent {
|
2019-09-02 11:16:52 +02:00
|
|
|
/// Identifying information has been received from a peer.
|
|
|
|
Received {
|
|
|
|
/// The peer that has been identified.
|
2018-12-07 10:23:38 +01:00
|
|
|
peer_id: PeerId,
|
2019-09-02 11:16:52 +02:00
|
|
|
/// The information provided by the peer.
|
2018-12-07 10:23:38 +01:00
|
|
|
info: IdentifyInfo,
|
2019-09-02 11:16:52 +02:00
|
|
|
/// The address observed by the peer for the local node.
|
2018-12-07 10:23:38 +01:00
|
|
|
observed_addr: Multiaddr,
|
|
|
|
},
|
2019-09-02 11:16:52 +02:00
|
|
|
/// Identifying information of the local node has been sent to a peer.
|
|
|
|
Sent {
|
|
|
|
/// The peer that the information has been sent to.
|
|
|
|
peer_id: PeerId,
|
|
|
|
},
|
2018-12-07 10:23:38 +01:00
|
|
|
/// Error while attempting to identify the remote.
|
|
|
|
Error {
|
2019-09-02 11:16:52 +02:00
|
|
|
/// The peer with whom the error originated.
|
2018-12-07 10:23:38 +01:00
|
|
|
peer_id: PeerId,
|
2019-09-02 11:16:52 +02:00
|
|
|
/// The error that occurred.
|
2019-09-16 11:08:44 +02:00
|
|
|
error: ProtocolsHandlerUpgrErr<ReadOneError>,
|
2018-12-07 10:23:38 +01:00
|
|
|
},
|
|
|
|
}
|
2019-02-18 13:59:12 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::{Identify, IdentifyEvent};
|
2019-04-20 16:00:21 +02:00
|
|
|
use futures::{future, prelude::*};
|
2019-04-10 10:29:21 +02:00
|
|
|
use libp2p_core::{
|
2019-04-20 16:00:21 +02:00
|
|
|
identity,
|
|
|
|
PeerId,
|
|
|
|
muxing::StreamMuxer,
|
2019-04-10 10:29:21 +02:00
|
|
|
Multiaddr,
|
Rework the transport upgrade API. (#1240)
* Rework the transport upgrade API.
ALthough transport upgrades must follow a specific pattern
in order fot the resulting transport to be usable with a
`Network` or `Swarm`, that pattern is currently not well
reflected in the transport upgrade API. Rather, transport
upgrades are rather laborious and involve non-trivial code
duplication.
This commit introduces a `transport::upgrade::Builder` that is
obtained from `Transport::upgrade`. The `Builder` encodes the
previously implicit rules for transport upgrades:
1. Authentication upgrades must happen first.
2. Any number of upgrades may follow.
3. A multiplexer upgrade must happen last.
Since multiplexing is the last (regular) transport upgrade (because
that upgrade yields a `StreamMuxer` which is no longer a `AsyncRead`
/ `AsyncWrite` resource, which the upgrade process is based on),
the upgrade starts with `Transport::upgrade` and ends with
`Builder::multiplex`, which drops back down to the `Transport`,
providing a fluent API.
Authentication and multiplexer upgrades must furthermore adhere
to a minimal contract w.r.t their outputs:
1. An authentication upgrade is given an (async) I/O resource `C`
and must produce a pair `(I, D)` where `I: ConnectionInfo` and
`D` is a new (async) I/O resource `D`.
2. A multiplexer upgrade is given an (async) I/O resource `C`
and must produce a `M: StreamMuxer`.
To that end, two changes to the `secio` and `noise` protocols have been
made:
1. The `secio` upgrade now outputs a pair of `(PeerId, SecioOutput)`.
The former implements `ConnectionInfo` and the latter `AsyncRead` /
`AsyncWrite`, fulfilling the `Builder` contract.
2. A new `NoiseAuthenticated` upgrade has been added that wraps around
any noise upgrade (i.e. `NoiseConfig`) and has an output of
`(PeerId, NoiseOutput)`, i.e. it checks if the `RemoteIdentity` from
the handshake output is an `IdentityKey`, failing if that is not the
case. This is the standard upgrade procedure one wants for integrating
noise with libp2p-core/swarm.
* Cleanup
* Add a new integration test.
* Add missing license.
2019-09-10 15:42:45 +02:00
|
|
|
Transport,
|
2019-10-10 11:31:44 +02:00
|
|
|
upgrade
|
2019-04-10 10:29:21 +02:00
|
|
|
};
|
2019-04-20 16:00:21 +02:00
|
|
|
use libp2p_tcp::TcpConfig;
|
|
|
|
use libp2p_secio::SecioConfig;
|
2019-07-04 14:47:59 +02:00
|
|
|
use libp2p_swarm::Swarm;
|
2019-04-20 16:00:21 +02:00
|
|
|
use libp2p_mplex::MplexConfig;
|
2019-09-02 11:16:52 +02:00
|
|
|
use rand::{Rng, thread_rng};
|
2019-04-20 16:00:21 +02:00
|
|
|
use std::{fmt, io};
|
|
|
|
use tokio::runtime::current_thread;
|
|
|
|
|
|
|
|
fn transport() -> (identity::PublicKey, impl Transport<
|
2019-04-28 14:42:18 +03:00
|
|
|
Output = (PeerId, impl StreamMuxer<Substream = impl Send, OutboundSubstream = impl Send, Error = impl Into<io::Error>>),
|
2019-04-20 16:00:21 +02:00
|
|
|
Listener = impl Send,
|
|
|
|
ListenerUpgrade = impl Send,
|
|
|
|
Dial = impl Send,
|
|
|
|
Error = impl fmt::Debug
|
|
|
|
> + Clone) {
|
|
|
|
let id_keys = identity::Keypair::generate_ed25519();
|
|
|
|
let pubkey = id_keys.public();
|
|
|
|
let transport = TcpConfig::new()
|
|
|
|
.nodelay(true)
|
2019-10-10 11:31:44 +02:00
|
|
|
.upgrade(upgrade::Version::V1)
|
Rework the transport upgrade API. (#1240)
* Rework the transport upgrade API.
ALthough transport upgrades must follow a specific pattern
in order fot the resulting transport to be usable with a
`Network` or `Swarm`, that pattern is currently not well
reflected in the transport upgrade API. Rather, transport
upgrades are rather laborious and involve non-trivial code
duplication.
This commit introduces a `transport::upgrade::Builder` that is
obtained from `Transport::upgrade`. The `Builder` encodes the
previously implicit rules for transport upgrades:
1. Authentication upgrades must happen first.
2. Any number of upgrades may follow.
3. A multiplexer upgrade must happen last.
Since multiplexing is the last (regular) transport upgrade (because
that upgrade yields a `StreamMuxer` which is no longer a `AsyncRead`
/ `AsyncWrite` resource, which the upgrade process is based on),
the upgrade starts with `Transport::upgrade` and ends with
`Builder::multiplex`, which drops back down to the `Transport`,
providing a fluent API.
Authentication and multiplexer upgrades must furthermore adhere
to a minimal contract w.r.t their outputs:
1. An authentication upgrade is given an (async) I/O resource `C`
and must produce a pair `(I, D)` where `I: ConnectionInfo` and
`D` is a new (async) I/O resource `D`.
2. A multiplexer upgrade is given an (async) I/O resource `C`
and must produce a `M: StreamMuxer`.
To that end, two changes to the `secio` and `noise` protocols have been
made:
1. The `secio` upgrade now outputs a pair of `(PeerId, SecioOutput)`.
The former implements `ConnectionInfo` and the latter `AsyncRead` /
`AsyncWrite`, fulfilling the `Builder` contract.
2. A new `NoiseAuthenticated` upgrade has been added that wraps around
any noise upgrade (i.e. `NoiseConfig`) and has an output of
`(PeerId, NoiseOutput)`, i.e. it checks if the `RemoteIdentity` from
the handshake output is an `IdentityKey`, failing if that is not the
case. This is the standard upgrade procedure one wants for integrating
noise with libp2p-core/swarm.
* Cleanup
* Add a new integration test.
* Add missing license.
2019-09-10 15:42:45 +02:00
|
|
|
.authenticate(SecioConfig::new(id_keys))
|
|
|
|
.multiplex(MplexConfig::new());
|
2019-04-20 16:00:21 +02:00
|
|
|
(pubkey, transport)
|
|
|
|
}
|
2019-02-18 13:59:12 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn periodic_id_works() {
|
2019-04-20 16:00:21 +02:00
|
|
|
let (mut swarm1, pubkey1) = {
|
|
|
|
let (pubkey, transport) = transport();
|
|
|
|
let protocol = Identify::new("a".to_string(), "b".to_string(), pubkey.clone());
|
|
|
|
let swarm = Swarm::new(transport, protocol, pubkey.clone().into_peer_id());
|
|
|
|
(swarm, pubkey)
|
2019-02-18 13:59:12 +01:00
|
|
|
};
|
|
|
|
|
2019-04-20 16:00:21 +02:00
|
|
|
let (mut swarm2, pubkey2) = {
|
|
|
|
let (pubkey, transport) = transport();
|
|
|
|
let protocol = Identify::new("c".to_string(), "d".to_string(), pubkey.clone());
|
|
|
|
let swarm = Swarm::new(transport, protocol, pubkey.clone().into_peer_id());
|
|
|
|
(swarm, pubkey)
|
2019-02-18 13:59:12 +01:00
|
|
|
};
|
|
|
|
|
2019-04-10 10:29:21 +02:00
|
|
|
let addr: Multiaddr = {
|
2019-09-02 11:16:52 +02:00
|
|
|
let port = thread_rng().gen_range(49152, std::u16::MAX);
|
2019-04-10 10:29:21 +02:00
|
|
|
format!("/ip4/127.0.0.1/tcp/{}", port).parse().unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
Swarm::listen_on(&mut swarm1, addr.clone()).unwrap();
|
2019-04-20 16:00:21 +02:00
|
|
|
Swarm::dial_addr(&mut swarm2, addr.clone()).unwrap();
|
2019-02-18 13:59:12 +01:00
|
|
|
|
2019-04-20 16:00:21 +02:00
|
|
|
// nb. Either swarm may receive the `Identified` event first, upon which
|
|
|
|
// it will permit the connection to be closed, as defined by
|
2019-09-02 11:16:52 +02:00
|
|
|
// `IdentifyHandler::connection_keep_alive`. Hence the test succeeds if
|
2019-04-20 16:00:21 +02:00
|
|
|
// either `Identified` event arrives correctly.
|
|
|
|
current_thread::Runtime::new().unwrap().block_on(
|
|
|
|
future::poll_fn(move || -> Result<_, io::Error> {
|
2019-02-18 13:59:12 +01:00
|
|
|
loop {
|
|
|
|
match swarm1.poll().unwrap() {
|
2019-09-02 11:16:52 +02:00
|
|
|
Async::Ready(Some(IdentifyEvent::Received { info, .. })) => {
|
2019-04-20 16:00:21 +02:00
|
|
|
assert_eq!(info.public_key, pubkey2);
|
2019-02-18 13:59:12 +01:00
|
|
|
assert_eq!(info.protocol_version, "c");
|
|
|
|
assert_eq!(info.agent_version, "d");
|
|
|
|
assert!(!info.protocols.is_empty());
|
|
|
|
assert!(info.listen_addrs.is_empty());
|
2019-09-16 11:08:44 +02:00
|
|
|
return Ok(Poll::Ready(()))
|
2019-02-18 13:59:12 +01:00
|
|
|
},
|
2019-09-02 11:16:52 +02:00
|
|
|
Async::Ready(Some(IdentifyEvent::Sent { .. })) => (),
|
2019-04-20 16:00:21 +02:00
|
|
|
Async::Ready(e) => panic!("{:?}", e),
|
|
|
|
Async::NotReady => {}
|
2019-02-18 13:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
match swarm2.poll().unwrap() {
|
2019-09-02 11:16:52 +02:00
|
|
|
Async::Ready(Some(IdentifyEvent::Received { info, .. })) => {
|
2019-04-20 16:00:21 +02:00
|
|
|
assert_eq!(info.public_key, pubkey1);
|
2019-02-18 13:59:12 +01:00
|
|
|
assert_eq!(info.protocol_version, "a");
|
|
|
|
assert_eq!(info.agent_version, "b");
|
|
|
|
assert!(!info.protocols.is_empty());
|
|
|
|
assert_eq!(info.listen_addrs.len(), 1);
|
2019-09-16 11:08:44 +02:00
|
|
|
return Ok(Poll::Ready(()))
|
2019-02-18 13:59:12 +01:00
|
|
|
},
|
2019-09-02 11:16:52 +02:00
|
|
|
Async::Ready(Some(IdentifyEvent::Sent { .. })) => (),
|
2019-04-20 16:00:21 +02:00
|
|
|
Async::Ready(e) => panic!("{:?}", e),
|
|
|
|
Async::NotReady => break
|
2019-02-18 13:59:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
Ok(Poll::Pending)
|
2019-02-18 13:59:12 +01:00
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|