From 0b6490854dafb2e4f24a55368a52e9d7aef097ab Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 29 Apr 2019 16:07:40 +0200 Subject: [PATCH] Make libp2p compile for wasm32-unkown-unknown (#2412) * Make libp2p compile for wasm32-unkown-unknown * Fix tests * Add some crates --- substrate/.gitlab-ci.yml | 13 +++++++++++++ substrate/core/network-libp2p/Cargo.toml | 3 +-- substrate/core/network-libp2p/src/behaviour.rs | 10 ++++++++++ substrate/core/network-libp2p/src/transport.rs | 16 ++++++++++++---- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/substrate/.gitlab-ci.yml b/substrate/.gitlab-ci.yml index a8141ee79e..b7834bb366 100644 --- a/substrate/.gitlab-ci.yml +++ b/substrate/.gitlab-ci.yml @@ -78,6 +78,19 @@ test-linux-stable: &test - time cargo test --all --release --verbose --locked - sccache -s +check-web-wasm: + stage: test + image: tomaka/cargo-web:latest + allow_failure: true + only: + - master + script: + # WASM support is in progress. As more and more crates support WASM, we + # should add entries here. + - time cargo web build -p substrate-network-libp2p + - time cargo web build -p substrate-panic-handler + - time cargo web build -p substrate-peerset + .build-only: &build-only only: - master diff --git a/substrate/core/network-libp2p/Cargo.toml b/substrate/core/network-libp2p/Cargo.toml index b6be5f1841..5610b67ead 100644 --- a/substrate/core/network-libp2p/Cargo.toml +++ b/substrate/core/network-libp2p/Cargo.toml @@ -22,7 +22,6 @@ serde = { version = "1.0.70", features = ["derive"] } serde_json = "1.0.24" smallvec = "0.6" substrate-peerset = { path = "../peerset" } -tokio = "0.1" tokio-io = "0.1" tokio-timer = "0.2" unsigned-varint = { version = "0.2.1", features = ["codec"] } @@ -34,4 +33,4 @@ erased-serde = "0.3.9" [dev-dependencies] tempdir = "0.3" - +tokio = "0.1" diff --git a/substrate/core/network-libp2p/src/behaviour.rs b/substrate/core/network-libp2p/src/behaviour.rs index cf9fc2c1c2..30dc0f52e4 100644 --- a/substrate/core/network-libp2p/src/behaviour.rs +++ b/substrate/core/network-libp2p/src/behaviour.rs @@ -20,9 +20,11 @@ use libp2p::NetworkBehaviour; use libp2p::core::{Multiaddr, PeerId, ProtocolsHandler, PublicKey}; use libp2p::core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction}; use libp2p::core::swarm::{NetworkBehaviourEventProcess, PollParameters}; +#[cfg(not(target_os = "unknown"))] use libp2p::core::swarm::toggle::Toggle; use libp2p::identify::{Identify, IdentifyEvent, protocol::IdentifyInfo}; use libp2p::kad::{Kademlia, KademliaOut}; +#[cfg(not(target_os = "unknown"))] use libp2p::mdns::{Mdns, MdnsEvent}; use libp2p::multiaddr::Protocol; use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess}; @@ -45,6 +47,7 @@ pub struct Behaviour { /// Periodically identifies the remote and responds to incoming requests. identify: Identify, /// Discovers nodes on the local network. + #[cfg(not(target_os = "unknown"))] mdns: Toggle>, /// Queue of events to produce for the outside. @@ -74,6 +77,11 @@ impl Behaviour { kademlia.add_connected_address(peer_id, addr.clone()); } + if enable_mdns { + #[cfg(target_os = "unknown")] + warn!(target: "sub-libp2p", "mDNS is not available on this platform"); + } + let clock = Clock::new(); Behaviour { ping: Ping::new(PingConfig::new()), @@ -87,6 +95,7 @@ impl Behaviour { local_peer_id: local_public_key.into_peer_id(), }, identify, + #[cfg(not(target_os = "unknown"))] mdns: if enable_mdns { match Mdns::new() { Ok(mdns) => Some(mdns).into(), @@ -304,6 +313,7 @@ impl NetworkBehaviourEventProcess for Behaviour } } +#[cfg(not(target_os = "unknown"))] impl NetworkBehaviourEventProcess for Behaviour { fn inject_event(&mut self, event: MdnsEvent) { match event { diff --git a/substrate/core/network-libp2p/src/transport.rs b/substrate/core/network-libp2p/src/transport.rs index 404fdb6bda..1e8b280f3b 100644 --- a/substrate/core/network-libp2p/src/transport.rs +++ b/substrate/core/network-libp2p/src/transport.rs @@ -17,8 +17,10 @@ use futures::prelude::*; use libp2p::{ InboundUpgradeExt, OutboundUpgradeExt, PeerId, Transport, - mplex, identity, secio, yamux, tcp, dns, websocket, bandwidth + mplex, identity, secio, yamux, websocket, bandwidth }; +#[cfg(not(target_os = "unknown"))] +use libp2p::{tcp, dns}; use libp2p::core::{self, transport::boxed::Boxed, muxing::StreamMuxerBox}; use std::{io, sync::Arc, time::Duration, usize}; @@ -35,9 +37,15 @@ pub fn build_transport( mplex_config.max_buffer_len_behaviour(mplex::MaxBufferBehaviour::Block); mplex_config.max_buffer_len(usize::MAX); - let transport = tcp::TcpConfig::new(); - let transport = websocket::WsConfig::new(transport.clone()).or_transport(transport); - let transport = dns::DnsConfig::new(transport); + #[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) + }; + #[cfg(target_os = "unknown")] + let transport = websocket::BrowserWsConfig::new(); + let (transport, sinks) = bandwidth::BandwidthLogging::new(transport, Duration::from_secs(5)); // TODO: rework the transport creation (https://github.com/libp2p/rust-libp2p/issues/783)