2018-11-15 17:41:11 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
2018-05-02 11:50:48 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
//! Contains everything related to upgrading a connection or a substream to use a protocol.
|
|
|
|
//!
|
|
|
|
//! After a connection with a remote has been successfully established or a substream successfully
|
|
|
|
//! opened, the next step is to *upgrade* this connection or substream to use a protocol.
|
|
|
|
//!
|
|
|
|
//! This is where the `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` traits come into play.
|
|
|
|
//! The `InboundUpgrade` and `OutboundUpgrade` traits are implemented on types that represent a
|
|
|
|
//! collection of one or more possible protocols for respectively an ingoing or outgoing
|
|
|
|
//! connection or substream.
|
|
|
|
//!
|
|
|
|
//! > **Note**: Multiple versions of the same protocol are treated as different protocols.
|
|
|
|
//! > For example, `/foo/1.0.0` and `/foo/1.1.0` are totally unrelated as far as
|
|
|
|
//! > upgrading is concerned.
|
|
|
|
//!
|
|
|
|
//! # Upgrade process
|
|
|
|
//!
|
|
|
|
//! An upgrade is performed in two steps:
|
|
|
|
//!
|
2018-12-11 15:13:10 +01:00
|
|
|
//! - A protocol negotiation step. The `UpgradeInfo::protocol_info` method is called to determine
|
2018-11-16 12:11:44 +01:00
|
|
|
//! which protocols are supported by the trait implementation. The `multistream-select` protocol
|
|
|
|
//! is used in order to agree on which protocol to use amongst the ones supported.
|
|
|
|
//!
|
|
|
|
//! - A handshake. After a successful negotiation, the `InboundUpgrade::upgrade_inbound` or
|
|
|
|
//! `OutboundUpgrade::upgrade_outbound` method is called. This method will return a `Future` that
|
|
|
|
//! performs a handshake. This handshake is considered mandatory, however in practice it is
|
|
|
|
//! possible for the trait implementation to return a dummy `Future` that doesn't perform any
|
|
|
|
//! action and immediately succeeds.
|
|
|
|
//!
|
|
|
|
//! After an upgrade is successful, an object of type `InboundUpgrade::Output` or
|
|
|
|
//! `OutboundUpgrade::Output` is returned. The actual object depends on the implementation and
|
|
|
|
//! there is no constraint on the traits that it should implement, however it is expected that it
|
|
|
|
//! can be used by the user to control the behaviour of the protocol.
|
|
|
|
//!
|
|
|
|
//! > **Note**: You can use the `apply_inbound` or `apply_outbound` methods to try upgrade a
|
|
|
|
//! connection or substream. However if you use the recommended `Swarm` or
|
|
|
|
//! `ProtocolsHandler` APIs, the upgrade is automatically handled for you and you don't
|
|
|
|
//! need to use these methods.
|
|
|
|
//!
|
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
mod apply;
|
|
|
|
mod denied;
|
2018-11-20 15:09:59 +01:00
|
|
|
mod either;
|
2018-11-15 17:41:11 +01:00
|
|
|
mod error;
|
|
|
|
mod map;
|
2019-05-17 15:29:59 +02:00
|
|
|
mod optional;
|
2018-12-07 11:39:18 +01:00
|
|
|
mod select;
|
2019-01-29 16:20:14 +01:00
|
|
|
mod transfer;
|
2018-11-15 17:41:11 +01:00
|
|
|
|
|
|
|
use futures::future::Future;
|
|
|
|
|
[multistream-select] Reduce roundtrips in protocol negotiation. (#1212)
* Remove tokio-codec dependency from multistream-select.
In preparation for the eventual switch from tokio to std futures.
Includes some initial refactoring in preparation for further work
in the context of https://github.com/libp2p/rust-libp2p/issues/659.
* Reduce default buffer sizes.
* Allow more than one frame to be buffered for sending.
* Doc tweaks.
* Remove superfluous (duplicated) Message types.
* Reduce roundtrips in multistream-select negotiation.
1. Enable 0-RTT: If the dialer only supports a single protocol, it can send
protocol data (e.g. the actual application request) together with
the multistream-select header and protocol proposal. Similarly,
if the listener supports a proposed protocol, it can send protocol
data (e.g. the actual application response) together with the
multistream-select header and protocol confirmation.
2. In general, the dialer "settles on" an expected protocol as soon
as it runs out of alternatives. Furthermore, both dialer and listener
do not immediately flush the final protocol confirmation, allowing it
to be sent together with application protocol data. Attempts to read
from the negotiated I/O stream implicitly flushes any pending data.
3. A clean / graceful shutdown of an I/O stream always completes protocol
negotiation.
The publich API of multistream-select changed slightly, requiring both
AsyncRead and AsyncWrite bounds for async reading and writing due to
the implicit buffering and "lazy" negotiation. The error types have
also been changed, but they were not previously fully exported.
Includes some general refactoring with simplifications and some more tests,
e.g. there was an edge case relating to a possible ambiguity when parsing
multistream-select protocol messages.
* Further missing commentary.
* Remove unused test dependency.
* Adjust commentary.
* Cleanup NegotiatedComplete::poll()
* Fix deflate protocol tests.
* Stabilise network_simult test.
The test implicitly relied on "slow" connection establishment
in order to have a sufficient probability of passing.
With the removal of roundtrips in multistream-select, it is now
more likely that within the up to 50ms duration between swarm1
and swarm2 dialing, the connection is already established, causing
the expectation of step == 1 to fail when receiving a Connected event,
since the step may then still be 0.
This commit aims to avoid these spurious errors by detecting runs
during which a connection is established "too quickly", repeating
the test run.
It still seems theoretically possible that, if connections are always
established "too quickly", the test runs forever. However, given that
the delta between swarm1 and swarm2 dialing is 0-50ms and that the
TCP transport is used, that seems probabilistically unlikely.
Nevertheless, the purpose of the artificial dialing delay between
swarm1 and swarm2 should be re-evaluated and possibly at least
the maximum delay further reduced.
* Complete negotiation between upgrades in libp2p-core.
While multistream-select, as a standalone library and providing
an API at the granularity of a single negotiation, supports
lazy negotiation (and in particular 0-RTT negotiation), in the
context of libp2p-core where any number of negotiations are
composed generically within the concept of composable "upgrades",
it is necessary to wait for protocol negotiation between upgrades
to complete.
* Clarify docs. Simplify listener upgrades.
Since reading from a Negotiated I/O stream implicitly flushes any pending
negotiation data, there is no pitfall involved in not waiting for completion.
2019-08-12 12:09:53 +02:00
|
|
|
pub use multistream_select::{Negotiated, NegotiatedComplete, NegotiationError, ProtocolError};
|
2018-11-15 17:41:11 +01:00
|
|
|
pub use self::{
|
2018-11-19 15:19:07 +01:00
|
|
|
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
2018-11-15 17:41:11 +01:00
|
|
|
denied::DeniedUpgrade,
|
2018-11-20 15:09:59 +01:00
|
|
|
either::EitherUpgrade,
|
2018-11-15 17:41:11 +01:00
|
|
|
error::UpgradeError,
|
2018-11-19 16:08:00 +01:00
|
|
|
map::{MapInboundUpgrade, MapOutboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgradeErr},
|
2019-05-17 15:29:59 +02:00
|
|
|
optional::OptionalUpgrade,
|
2019-01-29 16:20:14 +01:00
|
|
|
select::SelectUpgrade,
|
2019-02-14 11:00:46 +01:00
|
|
|
transfer::{write_one, WriteOne, read_one, ReadOne, read_one_then, ReadOneThen, ReadOneError, request_response, RequestResponse, read_respond, ReadRespond},
|
2018-11-15 17:41:11 +01:00
|
|
|
};
|
|
|
|
|
2018-12-11 15:13:10 +01:00
|
|
|
/// Types serving as protocol names.
|
2019-07-10 11:01:48 +02:00
|
|
|
///
|
|
|
|
/// # Context
|
|
|
|
///
|
|
|
|
/// In situations where we provide a list of protocols that we support, the elements of that list are required to
|
|
|
|
/// implement the [`ProtocolName`] trait.
|
|
|
|
///
|
|
|
|
/// Libp2p will call the [`ProtocolName::protocol_name`] trait method on each element of that list, and transmit the
|
|
|
|
/// returned value on the network. If the remote accepts a given protocol, the element serves as the return value of
|
|
|
|
/// the function that performed the negotiation.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use libp2p_core::ProtocolName;
|
|
|
|
///
|
|
|
|
/// enum MyProtocolName {
|
|
|
|
/// Version1,
|
|
|
|
/// Version2,
|
|
|
|
/// Version3,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl ProtocolName for MyProtocolName {
|
|
|
|
/// fn protocol_name(&self) -> &[u8] {
|
|
|
|
/// match *self {
|
|
|
|
/// MyProtocolName::Version1 => b"/myproto/1.0",
|
|
|
|
/// MyProtocolName::Version2 => b"/myproto/2.0",
|
|
|
|
/// MyProtocolName::Version3 => b"/myproto/3.0",
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2018-12-11 15:13:10 +01:00
|
|
|
pub trait ProtocolName {
|
2019-07-10 11:01:48 +02:00
|
|
|
/// The protocol name as bytes. Transmitted on the network.
|
2018-12-11 15:13:10 +01:00
|
|
|
fn protocol_name(&self) -> &[u8];
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<[u8]>> ProtocolName for T {
|
|
|
|
fn protocol_name(&self) -> &[u8] {
|
|
|
|
self.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Common trait for upgrades that can be applied on inbound substreams, outbound substreams,
|
|
|
|
/// or both.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait UpgradeInfo {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Opaque type representing a negotiable protocol.
|
2019-01-09 15:09:35 +01:00
|
|
|
type Info: ProtocolName + Clone;
|
2018-12-11 15:13:10 +01:00
|
|
|
/// Iterator returned by `protocol_info`.
|
|
|
|
type InfoIter: IntoIterator<Item = Self::Info>;
|
2018-11-15 17:41:11 +01:00
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns the list of protocols that are supported. Used during the negotiation process.
|
2018-12-11 15:13:10 +01:00
|
|
|
fn protocol_info(&self) -> Self::InfoIter;
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible upgrade on an inbound connection or substream.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait InboundUpgrade<C>: UpgradeInfo {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Output after the upgrade has been successfully negotiated and the handshake performed.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Output;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible error during the handshake.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Error;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Future that performs the handshake with the remote.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Future: Future<Item = Self::Output, Error = Self::Error>;
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// After we have determined that the remote supports one of the protocols we support, this
|
|
|
|
/// method is called to start the handshake.
|
|
|
|
///
|
2018-12-11 15:13:10 +01:00
|
|
|
/// The `info` is the identifier of the protocol, as produced by `protocol_info`.
|
2019-03-19 17:27:30 +01:00
|
|
|
fn upgrade_inbound(self, socket: Negotiated<C>, info: Self::Info) -> Self::Future;
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Extension trait for `InboundUpgrade`. Automatically implemented on all types that implement
|
|
|
|
/// `InboundUpgrade`.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait InboundUpgradeExt<C>: InboundUpgrade<C> {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Output`.
|
2018-11-19 10:58:45 +01:00
|
|
|
fn map_inbound<F, T>(self, f: F) -> MapInboundUpgrade<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Output) -> T
|
|
|
|
{
|
2018-11-19 10:58:45 +01:00
|
|
|
MapInboundUpgrade::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
2018-11-19 16:08:00 +01:00
|
|
|
fn map_inbound_err<F, T>(self, f: F) -> MapInboundUpgradeErr<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Error) -> T
|
|
|
|
{
|
2018-11-19 16:08:00 +01:00
|
|
|
MapInboundUpgradeErr::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, U: InboundUpgrade<C>> InboundUpgradeExt<C> for U {}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible upgrade on an outbound connection or substream.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait OutboundUpgrade<C>: UpgradeInfo {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Output after the upgrade has been successfully negotiated and the handshake performed.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Output;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible error during the handshake.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Error;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Future that performs the handshake with the remote.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Future: Future<Item = Self::Output, Error = Self::Error>;
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// After we have determined that the remote supports one of the protocols we support, this
|
|
|
|
/// method is called to start the handshake.
|
|
|
|
///
|
2018-12-11 15:13:10 +01:00
|
|
|
/// The `info` is the identifier of the protocol, as produced by `protocol_info`.
|
2019-03-19 17:27:30 +01:00
|
|
|
fn upgrade_outbound(self, socket: Negotiated<C>, info: Self::Info) -> Self::Future;
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Extention trait for `OutboundUpgrade`. Automatically implemented on all types that implement
|
|
|
|
/// `OutboundUpgrade`.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait OutboundUpgradeExt<C>: OutboundUpgrade<C> {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Output`.
|
2018-11-19 10:58:45 +01:00
|
|
|
fn map_outbound<F, T>(self, f: F) -> MapOutboundUpgrade<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Output) -> T
|
|
|
|
{
|
2018-11-19 10:58:45 +01:00
|
|
|
MapOutboundUpgrade::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
2018-11-19 16:08:00 +01:00
|
|
|
fn map_outbound_err<F, T>(self, f: F) -> MapOutboundUpgradeErr<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Error) -> T
|
|
|
|
{
|
2018-11-19 16:08:00 +01:00
|
|
|
MapOutboundUpgradeErr::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, U: OutboundUpgrade<C>> OutboundUpgradeExt<C> for U {}
|
|
|
|
|