2019-04-16 15:57:29 +02: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.
|
|
|
|
|
|
|
|
//! Integration tests for the `Ping` network behaviour.
|
|
|
|
|
|
|
|
use libp2p_core::{
|
|
|
|
Multiaddr,
|
|
|
|
PeerId,
|
|
|
|
identity,
|
2019-11-25 17:33:59 +01:00
|
|
|
muxing::StreamMuxerBox,
|
2020-10-07 11:10:54 +02:00
|
|
|
transport::{self, Transport},
|
2020-02-07 16:29:30 +01:00
|
|
|
upgrade
|
2019-04-16 15:57:29 +02:00
|
|
|
};
|
2020-09-28 10:57:02 +02:00
|
|
|
use libp2p_mplex as mplex;
|
2020-09-07 12:13:10 +02:00
|
|
|
use libp2p_noise as noise;
|
2019-04-16 15:57:29 +02:00
|
|
|
use libp2p_ping::*;
|
2020-08-10 12:54:55 +02:00
|
|
|
use libp2p_swarm::{Swarm, SwarmEvent};
|
2019-11-25 17:33:59 +01:00
|
|
|
use libp2p_tcp::TcpConfig;
|
2020-09-28 10:57:02 +02:00
|
|
|
use libp2p_yamux as yamux;
|
2019-11-25 17:33:59 +01:00
|
|
|
use futures::{prelude::*, channel::mpsc};
|
2020-08-10 12:54:55 +02:00
|
|
|
use quickcheck::*;
|
2020-09-28 10:57:02 +02:00
|
|
|
use rand::prelude::*;
|
2020-10-16 16:53:02 +02:00
|
|
|
use std::{num::NonZeroU8, time::Duration};
|
2019-04-16 15:57:29 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-08-10 12:54:55 +02:00
|
|
|
fn ping_pong() {
|
2020-09-28 10:57:02 +02:00
|
|
|
fn prop(count: NonZeroU8, muxer: MuxerChoice) {
|
2020-08-10 12:54:55 +02:00
|
|
|
let cfg = PingConfig::new()
|
|
|
|
.with_keep_alive(true)
|
|
|
|
.with_interval(Duration::from_millis(10));
|
2019-04-25 10:33:57 +02:00
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer1_id, trans) = mk_transport(muxer);
|
2020-08-10 12:54:55 +02:00
|
|
|
let mut swarm1 = Swarm::new(trans, Ping::new(cfg.clone()), peer1_id.clone());
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer2_id, trans) = mk_transport(muxer);
|
2020-08-10 12:54:55 +02:00
|
|
|
let mut swarm2 = Swarm::new(trans, Ping::new(cfg), peer2_id.clone());
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let pid1 = peer1_id.clone();
|
|
|
|
let addr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
|
|
|
|
Swarm::listen_on(&mut swarm1, addr).unwrap();
|
2019-11-25 17:33:59 +01:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let mut count1 = count.get();
|
|
|
|
let mut count2 = count.get();
|
2019-11-25 17:33:59 +01:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let peer1 = async move {
|
|
|
|
loop {
|
2021-01-12 13:35:11 +01:00
|
|
|
match swarm1.next_event().await {
|
|
|
|
SwarmEvent::NewListenAddr(listener) => tx.send(listener).await.unwrap(),
|
|
|
|
SwarmEvent::Behaviour(PingEvent { peer, result: Ok(PingSuccess::Ping { rtt }) }) => {
|
2020-08-10 12:54:55 +02:00
|
|
|
count1 -= 1;
|
|
|
|
if count1 == 0 {
|
|
|
|
return (pid1.clone(), peer, rtt)
|
|
|
|
}
|
|
|
|
},
|
2021-01-12 13:35:11 +01:00
|
|
|
SwarmEvent::Behaviour(PingEvent { result: Err(e), .. }) => panic!("Ping failure: {:?}", e),
|
2020-08-10 12:54:55 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let pid2 = peer2_id.clone();
|
|
|
|
let peer2 = async move {
|
|
|
|
Swarm::dial_addr(&mut swarm2, rx.next().await.unwrap()).unwrap();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match swarm2.next().await {
|
|
|
|
PingEvent { peer, result: Ok(PingSuccess::Ping { rtt }) } => {
|
|
|
|
count2 -= 1;
|
|
|
|
if count2 == 0 {
|
|
|
|
return (pid2.clone(), peer, rtt)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PingEvent { result: Err(e), .. } => panic!("Ping failure: {:?}", e),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = future::select(Box::pin(peer1), Box::pin(peer2));
|
|
|
|
let ((p1, p2, rtt), _) = async_std::task::block_on(result).factor_first();
|
|
|
|
assert!(p1 == peer1_id && p2 == peer2_id || p1 == peer2_id && p2 == peer1_id);
|
|
|
|
assert!(rtt < Duration::from_millis(50));
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
QuickCheck::new().tests(10).quickcheck(prop as fn(_,_))
|
2020-08-10 12:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests that the connection is closed upon a configurable
|
|
|
|
/// number of consecutive ping failures.
|
|
|
|
#[test]
|
|
|
|
fn max_failures() {
|
2020-09-28 10:57:02 +02:00
|
|
|
fn prop(max_failures: NonZeroU8, muxer: MuxerChoice) {
|
2020-08-10 12:54:55 +02:00
|
|
|
let cfg = PingConfig::new()
|
|
|
|
.with_keep_alive(true)
|
|
|
|
.with_interval(Duration::from_millis(10))
|
|
|
|
.with_timeout(Duration::from_millis(0))
|
|
|
|
.with_max_failures(max_failures.into());
|
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer1_id, trans) = mk_transport(muxer);
|
2020-08-10 12:54:55 +02:00
|
|
|
let mut swarm1 = Swarm::new(trans, Ping::new(cfg.clone()), peer1_id.clone());
|
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
let (peer2_id, trans) = mk_transport(muxer);
|
2020-08-10 12:54:55 +02:00
|
|
|
let mut swarm2 = Swarm::new(trans, Ping::new(cfg), peer2_id.clone());
|
|
|
|
|
|
|
|
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
|
|
|
|
|
|
|
let addr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
|
|
|
|
Swarm::listen_on(&mut swarm1, addr).unwrap();
|
|
|
|
|
|
|
|
let peer1 = async move {
|
|
|
|
let mut count1: u8 = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match swarm1.next_event().await {
|
2021-01-12 13:35:11 +01:00
|
|
|
SwarmEvent::NewListenAddr(listener) => tx.send(listener).await.unwrap(),
|
2020-08-10 12:54:55 +02:00
|
|
|
SwarmEvent::Behaviour(PingEvent {
|
|
|
|
result: Ok(PingSuccess::Ping { .. }), ..
|
|
|
|
}) => {
|
|
|
|
count1 = 0; // there may be an occasional success
|
|
|
|
}
|
|
|
|
SwarmEvent::Behaviour(PingEvent {
|
|
|
|
result: Err(_), ..
|
|
|
|
}) => {
|
|
|
|
count1 += 1;
|
|
|
|
}
|
|
|
|
SwarmEvent::ConnectionClosed { .. } => {
|
|
|
|
return count1
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-04-16 15:57:29 +02:00
|
|
|
}
|
2020-08-10 12:54:55 +02:00
|
|
|
};
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
let peer2 = async move {
|
|
|
|
Swarm::dial_addr(&mut swarm2, rx.next().await.unwrap()).unwrap();
|
|
|
|
|
|
|
|
let mut count2: u8 = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match swarm2.next_event().await {
|
|
|
|
SwarmEvent::Behaviour(PingEvent {
|
|
|
|
result: Ok(PingSuccess::Ping { .. }), ..
|
|
|
|
}) => {
|
|
|
|
count2 = 0; // there may be an occasional success
|
|
|
|
}
|
|
|
|
SwarmEvent::Behaviour(PingEvent {
|
|
|
|
result: Err(_), ..
|
|
|
|
}) => {
|
|
|
|
count2 += 1;
|
|
|
|
}
|
|
|
|
SwarmEvent::ConnectionClosed { .. } => {
|
|
|
|
return count2
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let future = future::join(peer1, peer2);
|
|
|
|
let (count1, count2) = async_std::task::block_on(future);
|
|
|
|
assert_eq!(u8::max(count1, count2), max_failures.get() - 1);
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
QuickCheck::new().tests(10).quickcheck(prop as fn(_,_))
|
2019-04-16 15:57:29 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:54:55 +02:00
|
|
|
|
2020-09-28 10:57:02 +02:00
|
|
|
fn mk_transport(muxer: MuxerChoice) -> (
|
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
|
|
|
PeerId,
|
2020-10-16 16:53:02 +02:00
|
|
|
transport::Boxed<(PeerId, StreamMuxerBox)>
|
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
|
|
|
) {
|
2019-04-16 15:57:29 +02:00
|
|
|
let id_keys = identity::Keypair::generate_ed25519();
|
|
|
|
let peer_id = id_keys.public().into_peer_id();
|
2020-09-07 12:13:10 +02:00
|
|
|
let noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&id_keys).unwrap();
|
2020-10-07 11:10:54 +02:00
|
|
|
(peer_id, TcpConfig::new()
|
2019-04-16 15:57:29 +02:00
|
|
|
.nodelay(true)
|
2019-10-10 11:31:44 +02:00
|
|
|
.upgrade(upgrade::Version::V1)
|
2020-09-07 12:13:10 +02:00
|
|
|
.authenticate(noise::NoiseConfig::xx(noise_keys).into_authenticated())
|
2020-09-28 10:57:02 +02:00
|
|
|
.multiplex(match muxer {
|
|
|
|
MuxerChoice::Yamux =>
|
2020-11-06 09:46:22 +01:00
|
|
|
upgrade::EitherUpgrade::A(yamux::YamuxConfig::default()),
|
2020-09-28 10:57:02 +02:00
|
|
|
MuxerChoice::Mplex =>
|
|
|
|
upgrade::EitherUpgrade::B(mplex::MplexConfig::default()),
|
|
|
|
})
|
2020-10-07 11:10:54 +02:00
|
|
|
.boxed())
|
2019-04-16 15:57:29 +02:00
|
|
|
}
|
2020-09-28 10:57:02 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
enum MuxerChoice {
|
|
|
|
Mplex,
|
|
|
|
Yamux,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Arbitrary for MuxerChoice {
|
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> MuxerChoice {
|
|
|
|
*[MuxerChoice::Mplex, MuxerChoice::Yamux].choose(g).unwrap()
|
|
|
|
}
|
|
|
|
}
|