Skip to content

Commit dc56d44

Browse files
authored
Refine boxing during transport construction. (#1794)
* Rework boxing during transport construction. * Cleanup * Fix chat-tokio example. * Update changelogs and versions.
1 parent 2dbf834 commit dc56d44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+357
-319
lines changed

Cargo.toml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,22 @@ atomic = "0.5.0"
6262
bytes = "0.5"
6363
futures = "0.3.1"
6464
lazy_static = "1.2"
65-
libp2p-core = { version = "0.22.2", path = "core" }
65+
libp2p-core = { version = "0.23.0", path = "core" }
6666
libp2p-core-derive = { version = "0.20.2", path = "misc/core-derive" }
6767
libp2p-floodsub = { version = "0.23.0", path = "protocols/floodsub", optional = true }
68-
libp2p-gossipsub = { version = "0.22.1", path = "./protocols/gossipsub", optional = true }
69-
libp2p-identify = { version = "0.22.0", path = "protocols/identify", optional = true }
70-
libp2p-kad = { version = "0.23.1", path = "protocols/kad", optional = true }
68+
libp2p-gossipsub = { version = "0.23.0", path = "./protocols/gossipsub", optional = true }
69+
libp2p-identify = { version = "0.23.0", path = "protocols/identify", optional = true }
70+
libp2p-kad = { version = "0.24.0", path = "protocols/kad", optional = true }
7171
libp2p-mplex = { version = "0.23.0", path = "muxers/mplex", optional = true }
72-
libp2p-noise = { version = "0.24.1", path = "protocols/noise", optional = true }
73-
libp2p-ping = { version = "0.22.0", path = "protocols/ping", optional = true }
72+
libp2p-noise = { version = "0.25.0", path = "protocols/noise", optional = true }
73+
libp2p-ping = { version = "0.23.0", path = "protocols/ping", optional = true }
7474
libp2p-plaintext = { version = "0.23.0", path = "protocols/plaintext", optional = true }
7575
libp2p-pnet = { version = "0.19.2", path = "protocols/pnet", optional = true }
7676
libp2p-request-response = { version = "0.4.0", path = "protocols/request-response", optional = true }
77-
libp2p-swarm = { version = "0.22.1", path = "swarm" }
78-
libp2p-uds = { version = "0.22.0", path = "transports/uds", optional = true }
79-
libp2p-wasm-ext = { version = "0.22.0", path = "transports/wasm-ext", optional = true }
80-
libp2p-yamux = { version = "0.25.0", path = "muxers/yamux", optional = true }
77+
libp2p-swarm = { version = "0.23.0", path = "swarm" }
78+
libp2p-uds = { version = "0.23.0", path = "transports/uds", optional = true }
79+
libp2p-wasm-ext = { version = "0.23.0", path = "transports/wasm-ext", optional = true }
80+
libp2p-yamux = { version = "0.26.0", path = "muxers/yamux", optional = true }
8181
multiaddr = { package = "parity-multiaddr", version = "0.9.3", path = "misc/multiaddr" }
8282
multihash = "0.11.0"
8383
parking_lot = "0.11.0"
@@ -86,11 +86,11 @@ smallvec = "1.0"
8686
wasm-timer = "0.2.4"
8787

8888
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
89-
libp2p-deflate = { version = "0.22.0", path = "protocols/deflate", optional = true }
90-
libp2p-dns = { version = "0.22.0", path = "transports/dns", optional = true }
91-
libp2p-mdns = { version = "0.22.1", path = "protocols/mdns", optional = true }
92-
libp2p-tcp = { version = "0.22.1", path = "transports/tcp", optional = true }
93-
libp2p-websocket = { version = "0.23.1", path = "transports/websocket", optional = true }
89+
libp2p-deflate = { version = "0.23.0", path = "protocols/deflate", optional = true }
90+
libp2p-dns = { version = "0.23.0", path = "transports/dns", optional = true }
91+
libp2p-mdns = { version = "0.23.0", path = "protocols/mdns", optional = true }
92+
libp2p-tcp = { version = "0.23.0", path = "transports/tcp", optional = true }
93+
libp2p-websocket = { version = "0.24.0", path = "transports/websocket", optional = true }
9494

9595
[dev-dependencies]
9696
async-std = "1.6.2"

core/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# 0.22.2 [unreleased]
1+
# 0.23.0 [unreleased]
2+
3+
- Rework transport boxing and move timeout configuration
4+
to the transport builder.
5+
[PR 1794](https://github.com/libp2p/rust-libp2p/pull/1794).
26

37
- Update dependencies.
48

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "libp2p-core"
33
edition = "2018"
44
description = "Core traits and structs of libp2p"
5-
version = "0.22.2"
5+
version = "0.23.0"
66
authors = ["Parity Technologies <[email protected]>"]
77
license = "MIT"
88
repository = "https://github.com/libp2p/rust-libp2p"

core/src/transport.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
//! any desired protocols. The rest of the module defines combinators for
2626
//! modifying a transport through composition with other transports or protocol upgrades.
2727
28-
use crate::{ConnectedPoint, ConnectionInfo, muxing::{StreamMuxer, StreamMuxerBox}};
28+
use crate::ConnectedPoint;
2929
use futures::prelude::*;
3030
use multiaddr::Multiaddr;
3131
use std::{error::Error, fmt};
32-
use std::time::Duration;
3332

3433
pub mod and_then;
3534
pub mod choice;
@@ -129,24 +128,16 @@ pub trait Transport {
129128
where
130129
Self: Sized;
131130

132-
/// Boxes an authenticated, multiplexed transport, including the
133-
/// `StreamMuxer` and transport errors.
134-
fn boxed<I, M>(self) -> boxed::Boxed<(I, StreamMuxerBox), std::io::Error>
131+
/// Boxes the transport, including custom transport errors.
132+
fn boxed(self) -> boxed::Boxed<Self::Output>
135133
where
136-
Self: Transport<Output = (I, M)> + Sized + Clone + Send + Sync + 'static,
134+
Self: Transport + Sized + Clone + Send + Sync + 'static,
137135
Self::Dial: Send + 'static,
138136
Self::Listener: Send + 'static,
139137
Self::ListenerUpgrade: Send + 'static,
140138
Self::Error: Send + Sync,
141-
I: ConnectionInfo,
142-
M: StreamMuxer + Send + Sync + 'static,
143-
M::Substream: Send + 'static,
144-
M::OutboundSubstream: Send + 'static
145-
146139
{
147-
boxed::boxed(
148-
self.map(|(i, m), _| (i, StreamMuxerBox::new(m)))
149-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)))
140+
boxed::boxed(self)
150141
}
151142

152143
/// Applies a function on the connections created by the transport.
@@ -198,33 +189,6 @@ pub trait Transport {
198189
and_then::AndThen::new(self, f)
199190
}
200191

201-
/// Adds a timeout to the connection setup (including upgrades) for all
202-
/// inbound and outbound connections established through the transport.
203-
fn timeout(self, timeout: Duration) -> timeout::TransportTimeout<Self>
204-
where
205-
Self: Sized
206-
{
207-
timeout::TransportTimeout::new(self, timeout)
208-
}
209-
210-
/// Adds a timeout to the connection setup (including upgrades) for all outbound
211-
/// connections established through the transport.
212-
fn outbound_timeout(self, timeout: Duration) -> timeout::TransportTimeout<Self>
213-
where
214-
Self: Sized
215-
{
216-
timeout::TransportTimeout::with_outgoing_timeout(self, timeout)
217-
}
218-
219-
/// Adds a timeout to the connection setup (including upgrades) for all inbound
220-
/// connections established through the transport.
221-
fn inbound_timeout(self, timeout: Duration) -> timeout::TransportTimeout<Self>
222-
where
223-
Self: Sized
224-
{
225-
timeout::TransportTimeout::with_ingoing_timeout(self, timeout)
226-
}
227-
228192
/// Begins a series of protocol upgrades via an
229193
/// [`upgrade::Builder`](upgrade::Builder).
230194
fn upgrade(self, version: upgrade::Version) -> upgrade::Builder<Self>

core/src/transport/boxed.rs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
use crate::transport::{ListenerEvent, Transport, TransportError};
2222
use futures::prelude::*;
2323
use multiaddr::Multiaddr;
24-
use std::{error, fmt, pin::Pin, sync::Arc};
24+
use std::{error::Error, fmt, io, pin::Pin, sync::Arc};
2525

26-
/// See the `Transport::boxed` method.
27-
pub fn boxed<T>(transport: T) -> Boxed<T::Output, T::Error>
26+
/// Creates a new [`Boxed`] transport from the given transport.
27+
pub fn boxed<T>(transport: T) -> Boxed<T::Output>
2828
where
2929
T: Transport + Clone + Send + Sync + 'static,
30+
T::Error: Send + Sync,
3031
T::Dial: Send + 'static,
3132
T::Listener: Send + 'static,
3233
T::ListenerUpgrade: Send + 'static,
@@ -36,64 +37,69 @@ where
3637
}
3738
}
3839

39-
/// See the `Transport::boxed` method.
40-
pub struct Boxed<O, E> {
41-
inner: Arc<dyn Abstract<O, E> + Send + Sync>,
40+
/// A `Boxed` transport is a `Transport` whose `Dial`, `Listener`
41+
/// and `ListenerUpgrade` futures are `Box`ed and only the `Output`
42+
/// and `Error` types are captured in type variables.
43+
pub struct Boxed<O> {
44+
inner: Arc<dyn Abstract<O> + Send + Sync>,
4245
}
4346

44-
type Dial<O, E> = Pin<Box<dyn Future<Output = Result<O, E>> + Send>>;
45-
type Listener<O, E> = Pin<Box<dyn Stream<Item = Result<ListenerEvent<ListenerUpgrade<O, E>, E>, E>> + Send>>;
46-
type ListenerUpgrade<O, E> = Pin<Box<dyn Future<Output = Result<O, E>> + Send>>;
47+
type Dial<O> = Pin<Box<dyn Future<Output = io::Result<O>> + Send>>;
48+
type Listener<O> = Pin<Box<dyn Stream<Item = io::Result<ListenerEvent<ListenerUpgrade<O>, io::Error>>> + Send>>;
49+
type ListenerUpgrade<O> = Pin<Box<dyn Future<Output = io::Result<O>> + Send>>;
4750

48-
trait Abstract<O, E> {
49-
fn listen_on(&self, addr: Multiaddr) -> Result<Listener<O, E>, TransportError<E>>;
50-
fn dial(&self, addr: Multiaddr) -> Result<Dial<O, E>, TransportError<E>>;
51+
trait Abstract<O> {
52+
fn listen_on(&self, addr: Multiaddr) -> Result<Listener<O>, TransportError<io::Error>>;
53+
fn dial(&self, addr: Multiaddr) -> Result<Dial<O>, TransportError<io::Error>>;
5154
}
5255

53-
impl<T, O, E> Abstract<O, E> for T
56+
impl<T, O> Abstract<O> for T
5457
where
55-
T: Transport<Output = O, Error = E> + Clone + 'static,
56-
E: error::Error,
58+
T: Transport<Output = O> + Clone + 'static,
59+
T::Error: Send + Sync,
5760
T::Dial: Send + 'static,
5861
T::Listener: Send + 'static,
5962
T::ListenerUpgrade: Send + 'static,
6063
{
61-
fn listen_on(&self, addr: Multiaddr) -> Result<Listener<O, E>, TransportError<E>> {
62-
let listener = Transport::listen_on(self.clone(), addr)?;
63-
let fut = listener.map_ok(|event| event.map(|upgrade| {
64-
Box::pin(upgrade) as ListenerUpgrade<O, E>
65-
}));
64+
fn listen_on(&self, addr: Multiaddr) -> Result<Listener<O>, TransportError<io::Error>> {
65+
let listener = Transport::listen_on(self.clone(), addr).map_err(|e| e.map(box_err))?;
66+
let fut = listener.map_ok(|event|
67+
event.map(|upgrade| {
68+
let up = upgrade.map_err(box_err);
69+
Box::pin(up) as ListenerUpgrade<O>
70+
}).map_err(box_err)
71+
).map_err(box_err);
6672
Ok(Box::pin(fut))
6773
}
6874

69-
fn dial(&self, addr: Multiaddr) -> Result<Dial<O, E>, TransportError<E>> {
70-
let fut = Transport::dial(self.clone(), addr)?;
71-
Ok(Box::pin(fut) as Dial<_, _>)
75+
fn dial(&self, addr: Multiaddr) -> Result<Dial<O>, TransportError<io::Error>> {
76+
let fut = Transport::dial(self.clone(), addr)
77+
.map(|r| r.map_err(box_err))
78+
.map_err(|e| e.map(box_err))?;
79+
Ok(Box::pin(fut) as Dial<_>)
7280
}
7381
}
7482

75-
impl<O, E> fmt::Debug for Boxed<O, E> {
83+
impl<O> fmt::Debug for Boxed<O> {
7684
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7785
write!(f, "BoxedTransport")
7886
}
7987
}
8088

81-
impl<O, E> Clone for Boxed<O, E> {
89+
impl<O> Clone for Boxed<O> {
8290
fn clone(&self) -> Self {
8391
Boxed {
8492
inner: self.inner.clone(),
8593
}
8694
}
8795
}
8896

89-
impl<O, E> Transport for Boxed<O, E>
90-
where E: error::Error,
91-
{
97+
impl<O> Transport for Boxed<O> {
9298
type Output = O;
93-
type Error = E;
94-
type Listener = Listener<O, E>;
95-
type ListenerUpgrade = ListenerUpgrade<O, E>;
96-
type Dial = Dial<O, E>;
99+
type Error = io::Error;
100+
type Listener = Listener<O>;
101+
type ListenerUpgrade = ListenerUpgrade<O>;
102+
type Dial = Dial<O>;
97103

98104
fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> {
99105
self.inner.listen_on(addr)
@@ -103,3 +109,7 @@ where E: error::Error,
103109
self.inner.dial(addr)
104110
}
105111
}
112+
113+
fn box_err<E: Error + Send + Sync + 'static>(e: E) -> io::Error {
114+
io::Error::new(io::ErrorKind::Other, e)
115+
}

0 commit comments

Comments
 (0)