Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit 03009c3

Browse files
Merge #1720
1720: Simplify transport creation r=thomaseizinger a=thomaseizinger Co-authored-by: Thomas Eizinger <[email protected]>
2 parents d9ff24d + 49cc1cd commit 03009c3

File tree

3 files changed

+24
-70
lines changed

3 files changed

+24
-70
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cnd/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ hyper = "0.12"
3636
lazy_static = "1"
3737
libp2p = { version = "0.13" }
3838
libp2p-comit = { path = "../libp2p-comit" }
39-
libp2p-core = "0.13"
4039
libsqlite3-sys = { version = ">=0.8.0, <0.13.0", features = ["bundled"] }
4140
log = { version = "0.4", features = ["serde"] }
4241
maplit = "1"

cnd/src/network/transport.rs

Lines changed: 24 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
use libp2p::{
2-
dns, identity, mplex, secio, tcp, yamux, Multiaddr, PeerId, Transport, TransportError,
2+
core::{
3+
muxing::{StreamMuxer, StreamMuxerBox},
4+
upgrade::{SelectUpgrade, Version},
5+
},
6+
dns::DnsConfig,
7+
identity,
8+
mplex::MplexConfig,
9+
secio::SecioConfig,
10+
tcp::TcpConfig,
11+
yamux, PeerId, Transport,
312
};
4-
use libp2p_core::{muxing, upgrade};
513
use std::{error, io, time::Duration};
614

7-
/// Builds a `Transport` that supports the most commonly-used protocols that
8-
/// libp2p supports.
15+
/// Builds a libp2p transport with the following features:
16+
/// - TcpConnection
17+
/// - DNS name resolution
18+
/// - authentication via secio
19+
/// - multiplexing via yamux or mplex
920
pub fn build_comit_transport(
1021
keypair: identity::Keypair,
1122
) -> impl Transport<
1223
Output = (
1324
PeerId,
14-
impl muxing::StreamMuxer<
25+
impl StreamMuxer<
1526
OutboundSubstream = impl Send,
1627
Substream = impl Send,
1728
Error = impl Into<io::Error>,
@@ -23,71 +34,16 @@ pub fn build_comit_transport(
2334
Dial = impl Send,
2435
ListenerUpgrade = impl Send,
2536
> + Clone {
26-
build_tcp_ws_secio_mplex_yamux(keypair)
27-
}
37+
let transport = TcpConfig::new().nodelay(true);
38+
let transport = DnsConfig::new(transport);
2839

29-
/// Builds an implementation of `Transport` that is suitable for usage with the
30-
/// `Swarm`.
31-
///
32-
/// The implementation supports TCP/IP, secio as the encryption layer, and mplex
33-
/// or yamux as the multiplexing layer.
34-
pub fn build_tcp_ws_secio_mplex_yamux(
35-
keypair: identity::Keypair,
36-
) -> impl Transport<
37-
Output = (
38-
PeerId,
39-
impl muxing::StreamMuxer<
40-
OutboundSubstream = impl Send,
41-
Substream = impl Send,
42-
Error = impl Into<io::Error>,
43-
> + Send
44-
+ Sync,
45-
),
46-
Error = impl error::Error + Send,
47-
Listener = impl Send,
48-
Dial = impl Send,
49-
ListenerUpgrade = impl Send,
50-
> + Clone {
51-
ComitTransport::new()
52-
.upgrade(upgrade::Version::V1)
53-
.authenticate(secio::SecioConfig::new(keypair))
54-
.multiplex(upgrade::SelectUpgrade::new(
40+
transport
41+
.upgrade(Version::V1)
42+
.authenticate(SecioConfig::new(keypair))
43+
.multiplex(SelectUpgrade::new(
5544
yamux::Config::default(),
56-
mplex::MplexConfig::new(),
45+
MplexConfig::new(),
5746
))
58-
.map(|(peer, muxer), _| (peer, muxing::StreamMuxerBox::new(muxer)))
47+
.map(|(peer, muxer), _| (peer, StreamMuxerBox::new(muxer)))
5948
.timeout(Duration::from_secs(20))
6049
}
61-
62-
#[derive(Debug, Clone)]
63-
struct ComitTransport {
64-
inner: InnerImplementation,
65-
}
66-
67-
type InnerImplementation = dns::DnsConfig<tcp::TcpConfig>;
68-
69-
impl ComitTransport {
70-
/// Initializes the `ComitTransport`.
71-
pub fn new() -> ComitTransport {
72-
let tcp = tcp::TcpConfig::new().nodelay(true);
73-
let transport = dns::DnsConfig::new(tcp);
74-
75-
ComitTransport { inner: transport }
76-
}
77-
}
78-
79-
impl Transport for ComitTransport {
80-
type Output = <InnerImplementation as Transport>::Output;
81-
type Error = <InnerImplementation as Transport>::Error;
82-
type Listener = <InnerImplementation as Transport>::Listener;
83-
type ListenerUpgrade = <InnerImplementation as Transport>::ListenerUpgrade;
84-
type Dial = <InnerImplementation as Transport>::Dial;
85-
86-
fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> {
87-
self.inner.listen_on(addr)
88-
}
89-
90-
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
91-
self.inner.dial(addr)
92-
}
93-
}

0 commit comments

Comments
 (0)