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
@@ -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));