Allow passing an optional transport to network-libp2p (#2680)

* Allow passing an optional transport

* Fix tests

* Fix Cargo.lock

* Fix Cargo.lock again?!?!
This commit is contained in:
Pierre Krieger
2019-05-28 12:54:02 +02:00
committed by GitHub
parent 22d3043917
commit 25b88f1a1f
5 changed files with 43 additions and 18 deletions
+15 -9
View File
@@ -1439,10 +1439,10 @@ dependencies = [
"libp2p-ping 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-plaintext 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-ratelimit 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-secio 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-secio 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-tcp 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-uds 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-wasm-ext 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-wasm-ext 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-websocket 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-yamux 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-multiaddr 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1680,7 +1680,7 @@ dependencies = [
[[package]]
name = "libp2p-secio"
version = "0.8.0"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1693,11 +1693,11 @@ dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-core 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-send-wrapper 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"protobuf 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rw-stream-sink 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"send_wrapper 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1735,13 +1735,13 @@ dependencies = [
[[package]]
name = "libp2p-wasm-ext"
version = "0.1.0"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
"js-sys 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p-core 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"send_wrapper 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-send-wrapper 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen-futures 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2349,6 +2349,11 @@ dependencies = [
"unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parity-send-wrapper"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "parity-wasm"
version = "0.31.3"
@@ -4988,7 +4993,7 @@ name = "twox-hash"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -5625,10 +5630,10 @@ dependencies = [
"checksum libp2p-ping 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3b3bb3328d206ad3061e863f179a211fc978d7bce05f90440ed6b8a6a9d17ced"
"checksum libp2p-plaintext 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4b23a8ece138f448572c5ff781d62323a954f1f681c303e6553368026764b0ae"
"checksum libp2p-ratelimit 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "55918f058f118d72d83f9a976f12e02e54c8616ddfc795c779c4801a5042a44f"
"checksum libp2p-secio 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9bff57806e0f71832cc02b5dea1010e5f1a9d16393fd104a4b64e4aaae40d885"
"checksum libp2p-secio 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ea5c0636053e01575d6269b6112f9239e9d35ca861e3e5c7d6970a07f9e1682a"
"checksum libp2p-tcp 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3629f9a667d9f5acb5876df59cf3b547250e340131c47587f9ace7c517f21327"
"checksum libp2p-uds 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d65c15f89c0607d4a334664d759e54e847e1856a73ea78e7bb6a75e6f4039010"
"checksum libp2p-wasm-ext 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dff6c81d0f46261a6327219349753aefd3a92021a1e6102185fa112cfcddca97"
"checksum libp2p-wasm-ext 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "318d727d5e8e0fe3bb70aacbf99bde57334eae9559deff447edd993422dc0f03"
"checksum libp2p-websocket 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "04451aa166aa2040293f44c1c27144b65500e4a2ebbb723dfe732f39436eccbd"
"checksum libp2p-yamux 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5369165359bea84e7ebe73f37b6240f31f8d924ce6710be3d8e1fa678985c9b8"
"checksum librocksdb-sys 5.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7dfb546562f9b450237bb8df7a31961849ee9fb1186d9e356db1d7a6b7609ff2"
@@ -5676,6 +5681,7 @@ dependencies = [
"checksum parity-codec-derive 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "00a486fd383382ddcb2de928364b1f82571c1e48274fc43b7667a4738ee4056c"
"checksum parity-multiaddr 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18a130a727008cfcd1068a28439fe939897ccad28664422aeca65b384d6de6d0"
"checksum parity-multihash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05d6a68e07ab34a9e87bd8dd4936f6bb5be21e4f6dbcdbaf04d8e854eba0af01"
"checksum parity-send-wrapper 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aa9777aa91b8ad9dd5aaa04a9b6bcb02c7f1deb952fca5a66034d5e63afc5c6f"
"checksum parity-wasm 0.31.3 (registry+https://github.com/rust-lang/crates.io-index)" = "511379a8194230c2395d2f5fa627a5a7e108a9f976656ce723ae68fca4097bfc"
"checksum parity-ws 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2fec5048fba72a2e01baeb0d08089db79aead4b57e2443df172fb1840075a233"
"checksum parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4d05f1349491390b1730afba60bb20d55761bef489a954546b58b4b34e1e2ac"
@@ -17,6 +17,7 @@
//! Libp2p network configuration.
use libp2p::identity::{Keypair, secp256k1, ed25519};
use libp2p::wasm_ext;
use libp2p::{Multiaddr, multiaddr::Protocol};
use std::error::Error;
use std::{io::{self, Write}, iter, fs, net::Ipv4Addr, path::{Path, PathBuf}};
@@ -52,6 +53,13 @@ pub struct NetworkConfiguration {
/// If true, the network will use mDNS to discover other libp2p nodes on the local network
/// and connect to them if they support the same chain.
pub enable_mdns: bool,
/// Optional external implementation of a libp2p transport. Used in WASM contexts where we need
/// some binding between the networking provided by the operating system or environment and
/// libp2p.
///
/// This parameter exists whatever the target platform is, but it is expected to be set to
/// `Some` only when compiling for WASM.
pub wasm_external_transport: Option<wasm_ext::ExtTransport>,
}
impl Default for NetworkConfiguration {
@@ -70,6 +78,7 @@ impl Default for NetworkConfiguration {
client_version: "unknown".into(),
node_name: "unknown".into(),
enable_mdns: false,
wasm_external_transport: None,
}
}
}
@@ -89,7 +89,10 @@ where TMessage: CustomMessage + Send + 'static {
let user_agent = format!("{} ({})", config.client_version, config.node_name);
let proto = CustomProto::new(registered_custom, peerset);
let behaviour = Behaviour::new(proto, user_agent, local_public, known_addresses, config.enable_mdns);
let (transport, bandwidth) = transport::build_transport(local_identity);
let (transport, bandwidth) = transport::build_transport(
local_identity,
config.wasm_external_transport
);
(Swarm::new(transport, behaviour, local_peer_id.clone()), bandwidth)
};
+14 -8
View File
@@ -17,11 +17,11 @@
use futures::prelude::*;
use libp2p::{
InboundUpgradeExt, OutboundUpgradeExt, PeerId, Transport,
mplex, identity, secio, yamux, websocket, bandwidth
mplex, identity, secio, yamux, websocket, bandwidth, wasm_ext
};
#[cfg(not(target_os = "unknown"))]
use libp2p::{tcp, dns};
use libp2p::core::{self, transport::boxed::Boxed, muxing::StreamMuxerBox};
use libp2p::core::{self, transport::boxed::Boxed, transport::OptionalTransport, muxing::StreamMuxerBox};
use std::{io, sync::Arc, time::Duration, usize};
pub use self::bandwidth::BandwidthSinks;
@@ -31,20 +31,26 @@ pub use self::bandwidth::BandwidthSinks;
/// Returns a `BandwidthSinks` object that allows querying the average bandwidth produced by all
/// the connections spawned with this transport.
pub fn build_transport(
keypair: identity::Keypair
keypair: identity::Keypair,
wasm_external_transport: Option<wasm_ext::ExtTransport>
) -> (Boxed<(PeerId, StreamMuxerBox), io::Error>, Arc<bandwidth::BandwidthSinks>) {
let mut mplex_config = mplex::MplexConfig::new();
mplex_config.max_buffer_len_behaviour(mplex::MaxBufferBehaviour::Block);
mplex_config.max_buffer_len(usize::MAX);
let transport = if let Some(t) = wasm_external_transport {
OptionalTransport::some(t)
} else {
OptionalTransport::none()
};
#[cfg(not(target_os = "unknown"))]
let transport = {
let transport = tcp::TcpConfig::new();
let transport = websocket::WsConfig::new(transport.clone()).or_transport(transport);
dns::DnsConfig::new(transport)
let desktop_trans = tcp::TcpConfig::new();
let desktop_trans = websocket::WsConfig::new(desktop_trans.clone())
.or_transport(desktop_trans);
transport.or_transport(dns::DnsConfig::new(desktop_trans))
};
#[cfg(target_os = "unknown")]
let transport = websocket::BrowserWsConfig::new();
let (transport, sinks) = bandwidth::BandwidthLogging::new(transport, Duration::from_secs(5));
+1
View File
@@ -99,6 +99,7 @@ fn node_config<F: ServiceFactory> (
client_version: "network/test/0.1".to_owned(),
node_name: "unknown".to_owned(),
enable_mdns: false,
wasm_external_transport: None,
};
Configuration {