2018-01-26 11:40:43 +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.
|
|
|
|
|
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
|
|
|
use libp2p_core::{muxing, upgrade, Transport, transport::ListenerEvent};
|
2018-12-18 11:06:37 +01:00
|
|
|
use libp2p_tcp::TcpConfig;
|
2018-12-05 15:01:17 +01:00
|
|
|
use futures::prelude::*;
|
2018-08-31 10:31:34 +02:00
|
|
|
use std::sync::{Arc, mpsc};
|
2018-01-26 11:40:43 +01:00
|
|
|
use std::thread;
|
2018-12-05 15:01:17 +01:00
|
|
|
use tokio::{
|
|
|
|
codec::length_delimited::Builder,
|
|
|
|
runtime::current_thread::Runtime
|
|
|
|
};
|
2018-01-26 11:40:43 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn client_to_server_outbound() {
|
|
|
|
// Simulate a client sending a message to a server through a multiplex upgrade.
|
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
let bg_thread = thread::spawn(move || {
|
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
|
|
|
let mplex = libp2p_mplex::MplexConfig::new();
|
|
|
|
|
2019-10-10 11:31:44 +02:00
|
|
|
let transport = TcpConfig::new().and_then(move |c, e|
|
|
|
|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
|
2018-01-26 11:40:43 +01:00
|
|
|
|
2019-04-10 10:29:21 +02:00
|
|
|
let mut listener = transport
|
2018-03-07 16:20:55 +01:00
|
|
|
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
|
|
|
.unwrap();
|
2019-04-10 10:29:21 +02:00
|
|
|
|
|
|
|
let addr = listener.by_ref().wait()
|
|
|
|
.next()
|
|
|
|
.expect("some event")
|
|
|
|
.expect("no error")
|
|
|
|
.into_new_address()
|
|
|
|
.expect("listen address");
|
|
|
|
|
2018-01-26 11:40:43 +01:00
|
|
|
tx.send(addr).unwrap();
|
|
|
|
|
|
|
|
let future = listener
|
2019-04-10 10:29:21 +02:00
|
|
|
.filter_map(ListenerEvent::into_upgrade)
|
2018-01-26 11:40:43 +01:00
|
|
|
.into_future()
|
2019-01-10 11:27:06 +01:00
|
|
|
.map_err(|(err, _)| panic!("{:?}", err))
|
2018-10-17 10:17:40 +01:00
|
|
|
.and_then(|(client, _)| client.unwrap().0)
|
2019-01-10 11:27:06 +01:00
|
|
|
.map_err(|err| panic!("{:?}", err))
|
2018-10-17 10:17:40 +01:00
|
|
|
.and_then(|client| muxing::outbound_from_ref_and_wrap(Arc::new(client)))
|
2019-03-11 17:19:50 +01:00
|
|
|
.map(|client| Builder::new().new_read(client))
|
2018-01-26 11:40:43 +01:00
|
|
|
.and_then(|client| {
|
2018-03-07 16:20:55 +01:00
|
|
|
client
|
|
|
|
.into_future()
|
2018-01-26 11:40:43 +01:00
|
|
|
.map_err(|(err, _)| err)
|
|
|
|
.map(|(msg, _)| msg)
|
|
|
|
})
|
|
|
|
.and_then(|msg| {
|
|
|
|
let msg = msg.unwrap();
|
|
|
|
assert_eq!(msg, "hello world");
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
let _ = rt.block_on(future).unwrap();
|
2018-01-26 11:40:43 +01:00
|
|
|
});
|
|
|
|
|
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
|
|
|
let mplex = libp2p_mplex::MplexConfig::new();
|
2019-10-10 11:31:44 +02:00
|
|
|
let transport = TcpConfig::new().and_then(move |c, e|
|
|
|
|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
|
2018-01-26 11:40:43 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
let future = transport
|
2019-04-11 22:51:07 +02:00
|
|
|
.dial(rx.recv().unwrap())
|
2018-03-07 16:20:55 +01:00
|
|
|
.unwrap()
|
2019-01-10 11:27:06 +01:00
|
|
|
.map_err(|err| panic!("{:?}", err))
|
2018-10-17 10:17:40 +01:00
|
|
|
.and_then(|client| muxing::inbound_from_ref_and_wrap(Arc::new(client)))
|
2019-03-11 17:19:50 +01:00
|
|
|
.map(|server| Builder::new().new_write(server))
|
2018-01-26 11:40:43 +01:00
|
|
|
.and_then(|server| server.send("hello world".into()))
|
|
|
|
.map(|_| ());
|
|
|
|
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
let _ = rt.block_on(future).unwrap();
|
2018-01-26 11:40:43 +01:00
|
|
|
bg_thread.join().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn client_to_server_inbound() {
|
|
|
|
// Simulate a client sending a message to a server through a multiplex upgrade.
|
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
let bg_thread = thread::spawn(move || {
|
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
|
|
|
let mplex = libp2p_mplex::MplexConfig::new();
|
2019-10-10 11:31:44 +02:00
|
|
|
let transport = TcpConfig::new().and_then(move |c, e|
|
|
|
|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
|
2018-01-26 11:40:43 +01:00
|
|
|
|
2019-04-10 10:29:21 +02:00
|
|
|
let mut listener = transport
|
2018-03-07 16:20:55 +01:00
|
|
|
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
|
|
|
.unwrap();
|
2019-04-10 10:29:21 +02:00
|
|
|
|
|
|
|
let addr = listener.by_ref().wait()
|
|
|
|
.next()
|
|
|
|
.expect("some event")
|
|
|
|
.expect("no error")
|
|
|
|
.into_new_address()
|
|
|
|
.expect("listen address");
|
|
|
|
|
|
|
|
|
2018-01-26 11:40:43 +01:00
|
|
|
tx.send(addr).unwrap();
|
|
|
|
|
|
|
|
let future = listener
|
2019-04-10 10:29:21 +02:00
|
|
|
.filter_map(ListenerEvent::into_upgrade)
|
2018-01-26 11:40:43 +01:00
|
|
|
.into_future()
|
2019-01-10 11:27:06 +01:00
|
|
|
.map_err(|(err, _)| panic!("{:?}", err))
|
2018-10-17 10:17:40 +01:00
|
|
|
.and_then(|(client, _)| client.unwrap().0)
|
2019-01-10 11:27:06 +01:00
|
|
|
.map_err(|err| panic!("{:?}", err))
|
2018-08-31 10:31:34 +02:00
|
|
|
.and_then(|client| muxing::inbound_from_ref_and_wrap(Arc::new(client)))
|
2019-03-11 17:19:50 +01:00
|
|
|
.map(|client| Builder::new().new_read(client))
|
2018-01-26 11:40:43 +01:00
|
|
|
.and_then(|client| {
|
2018-03-07 16:20:55 +01:00
|
|
|
client
|
|
|
|
.into_future()
|
2018-01-26 11:40:43 +01:00
|
|
|
.map_err(|(err, _)| err)
|
|
|
|
.map(|(msg, _)| msg)
|
|
|
|
})
|
|
|
|
.and_then(|msg| {
|
|
|
|
let msg = msg.unwrap();
|
|
|
|
assert_eq!(msg, "hello world");
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
let _ = rt.block_on(future).unwrap();
|
2018-01-26 11:40:43 +01:00
|
|
|
});
|
|
|
|
|
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
|
|
|
let mplex = libp2p_mplex::MplexConfig::new();
|
2019-10-10 11:31:44 +02:00
|
|
|
let transport = TcpConfig::new().and_then(move |c, e|
|
|
|
|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
|
2018-01-26 11:40:43 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
let future = transport
|
2019-04-11 22:51:07 +02:00
|
|
|
.dial(rx.recv().unwrap())
|
2018-03-07 16:20:55 +01:00
|
|
|
.unwrap()
|
2019-01-10 11:27:06 +01:00
|
|
|
.map_err(|err| panic!("{:?}", err))
|
2018-10-17 10:17:40 +01:00
|
|
|
.and_then(|client| muxing::outbound_from_ref_and_wrap(Arc::new(client)))
|
2019-03-11 17:19:50 +01:00
|
|
|
.map(|server| Builder::new().new_write(server))
|
2018-01-26 11:40:43 +01:00
|
|
|
.and_then(|server| server.send("hello world".into()))
|
|
|
|
.map(|_| ());
|
|
|
|
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
let _ = rt.block_on(future).unwrap();
|
2018-01-26 11:40:43 +01:00
|
|
|
bg_thread.join().unwrap();
|
|
|
|
}
|