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

Commit 2600c63

Browse files
author
Tobin C. Harding
committed
Implement a transport for COMIT
1 parent 11d2938 commit 2600c63

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

Cargo.lock

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

cnd/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use libp2p::{
2424
identity::{self, ed25519},
2525
PeerId, Swarm,
2626
};
27+
use libp2p_comit;
2728
use rand::rngs::OsRng;
2829
use std::{
2930
net::SocketAddr,
@@ -84,7 +85,7 @@ fn main() -> anyhow::Result<()> {
8485
.compat(),
8586
)?;
8687

87-
let transport = libp2p::build_development_transport(local_key_pair);
88+
let transport = libp2p_comit::build_comit_transport(local_key_pair);
8889
let behaviour = network::ComitNode::new(
8990
ledger_events.clone(),
9091
Arc::clone(&state_store),

libp2p-comit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ description = "Implementation of the COMIT messaging protocol"
99
bytes = "0.4"
1010
derivative = "1.0.3"
1111
futures = "0.1"
12+
libp2p = "0.13"
1213
libp2p-core = "0.13"
1314
libp2p-swarm = "0.3"
1415
log = "0.4"

libp2p-comit/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod protocol;
99
mod substream;
1010
#[cfg(test)]
1111
pub mod test_harness;
12+
mod transport;
1213

1314
use serde::{Deserialize, Serialize};
1415
use serde_json::{self, Value as JsonValue};
@@ -17,6 +18,7 @@ pub use self::{
1718
behaviour::{BehaviourOutEvent, Comit},
1819
handler::{ComitHandler, PendingInboundRequest, PendingOutboundRequest},
1920
protocol::{ComitProtocolConfig, Frames},
21+
transport::build_comit_transport,
2022
};
2123
use crate::handler::{ProtocolOutEvent, ProtocolOutboundOpenInfo};
2224
use libp2p_swarm::ProtocolsHandlerEvent;

libp2p-comit/src/transport.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use libp2p::{
2+
dns, identity, mplex, secio, tcp, yamux, Multiaddr, PeerId, Transport, TransportError,
3+
};
4+
use libp2p_core::{muxing, upgrade};
5+
use std::{error, io, time::Duration};
6+
7+
/// Builds a `Transport` that supports the most commonly-used protocols that
8+
/// libp2p supports.
9+
pub fn build_comit_transport(
10+
keypair: identity::Keypair,
11+
) -> impl Transport<
12+
Output = (
13+
PeerId,
14+
impl muxing::StreamMuxer<
15+
OutboundSubstream = impl Send,
16+
Substream = impl Send,
17+
Error = impl Into<io::Error>,
18+
> + Send
19+
+ Sync,
20+
),
21+
Error = impl error::Error + Send,
22+
Listener = impl Send,
23+
Dial = impl Send,
24+
ListenerUpgrade = impl Send,
25+
> + Clone {
26+
build_tcp_ws_secio_mplex_yamux(keypair)
27+
}
28+
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(
55+
yamux::Config::default(),
56+
mplex::MplexConfig::new(),
57+
))
58+
.map(|(peer, muxer), _| (peer, muxing::StreamMuxerBox::new(muxer)))
59+
.timeout(Duration::from_secs(20))
60+
}
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)