mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-04-25 11:02:12 +00:00
* 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.
99 lines
3.3 KiB
Rust
99 lines
3.3 KiB
Rust
// 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.
|
|
|
|
use futures::prelude::*;
|
|
use libp2p_core::transport::{ListenerEvent, Transport};
|
|
use libp2p_core::upgrade::Negotiated;
|
|
use libp2p_deflate::{DeflateConfig, DeflateOutput};
|
|
use libp2p_tcp::{TcpConfig, TcpTransStream};
|
|
use log::info;
|
|
use quickcheck::QuickCheck;
|
|
use tokio::{self, io};
|
|
|
|
#[test]
|
|
fn deflate() {
|
|
let _ = env_logger::try_init();
|
|
|
|
fn prop(message: Vec<u8>) -> bool {
|
|
let server_transport = TcpConfig::new().with_upgrade(DeflateConfig {});
|
|
let client_transport = TcpConfig::new().with_upgrade(DeflateConfig {});
|
|
run(server_transport, client_transport, message);
|
|
true
|
|
}
|
|
|
|
QuickCheck::new()
|
|
.max_tests(30)
|
|
.quickcheck(prop as fn(Vec<u8>) -> bool)
|
|
}
|
|
|
|
type Output = DeflateOutput<Negotiated<TcpTransStream>>;
|
|
|
|
fn run<T>(server_transport: T, client_transport: T, message1: Vec<u8>)
|
|
where
|
|
T: Transport<Output = Output>,
|
|
T::Dial: Send + 'static,
|
|
T::Listener: Send + 'static,
|
|
T::ListenerUpgrade: Send + 'static,
|
|
{
|
|
let message2 = message1.clone();
|
|
|
|
let mut server = server_transport
|
|
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
|
.unwrap();
|
|
let server_address = server
|
|
.by_ref()
|
|
.wait()
|
|
.next()
|
|
.expect("some event")
|
|
.expect("no error")
|
|
.into_new_address()
|
|
.expect("listen address");
|
|
let server = server
|
|
.take(1)
|
|
.filter_map(ListenerEvent::into_upgrade)
|
|
.and_then(|(client, _)| client)
|
|
.map_err(|e| panic!("server error: {}", e))
|
|
.and_then(|client| {
|
|
info!("server: reading message");
|
|
io::read_to_end(client, Vec::new())
|
|
})
|
|
.for_each(move |(_, msg)| {
|
|
info!("server: read message: {:?}", msg);
|
|
assert_eq!(msg, message1);
|
|
Ok(())
|
|
});
|
|
|
|
let client = client_transport
|
|
.dial(server_address.clone())
|
|
.unwrap()
|
|
.map_err(|e| panic!("client error: {}", e))
|
|
.and_then(move |server| {
|
|
io::write_all(server, message2).and_then(|(client, _)| io::shutdown(client))
|
|
})
|
|
.map(|_| ());
|
|
|
|
let future = client
|
|
.join(server)
|
|
.map_err(|e| panic!("{:?}", e))
|
|
.map(|_| ());
|
|
|
|
tokio::run(future)
|
|
}
|