Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Just works" WASM (browser) experience #2245

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
matrix:
toolchain: [
wasm32-unknown-emscripten,
wasm32-unknown-unknown,
wasm32-wasi
]
container:
Expand Down Expand Up @@ -74,10 +75,17 @@ jobs:
key: ${{ matrix.toolchain }}

- name: Build on ${{ matrix.toolchain }}
if: matrix.toolchain != 'wasm32-unknown-unknown'
# TODO: also run `cargo test`
# TODO: ideally we would build `--workspace`, but not all crates compile for WASM
run: cargo build --target=${{ matrix.toolchain }}

- name: Build on ${{ matrix.toolchain }}
wngr marked this conversation as resolved.
Show resolved Hide resolved
if: matrix.toolchain == 'wasm32-unknown-unknown'
# TODO: also run `cargo test`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please, that would be great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bigger undertaking, which might be more geared towards wasm32-wasi rather than wasm32-unknown-unknown.
I guess for wasm32-unknown-unknown a wasm-bindgen based browser smoke test might be something, but I'll leave that to future-us :-)

# TODO: ideally we would build `--workspace`, but not all crates compile for WASM
run: cargo build --target=${{ matrix.toolchain }} --features wasm-bindgen

check-rustdoc-links:
name: Check rustdoc intra-doc links
runs-on: ubuntu-latest
Expand Down
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ rendezvous = ["libp2p-rendezvous"]
tcp-async-io = ["libp2p-tcp", "libp2p-tcp/async-io"]
tcp-tokio = ["libp2p-tcp", "libp2p-tcp/tokio"]
uds = ["libp2p-uds"]
wasm-bindgen = ["parking_lot/wasm-bindgen"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "parking_lot/wasm-bindgen", "getrandom/js", "rand/wasm-bindgen"]
wasm-ext = ["libp2p-wasm-ext"]
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext/websocket"]
websocket = ["libp2p-websocket"]
Expand All @@ -67,6 +67,12 @@ all-features = true
atomic = "0.5.0"
bytes = "1"
futures = "0.3.1"
# Explicit dependency to be used in `wasm-bindgen` feature
futures-timer = "3.0.2"
wngr marked this conversation as resolved.
Show resolved Hide resolved
# Explicit dependency to be used in `wasm-bindgen` feature
getrandom = "0.2.3"
wngr marked this conversation as resolved.
Show resolved Hide resolved
# Explicit dependency to be used in `wasm-bindgen` feature
instant = "0.1.11"
lazy_static = "1.2"
libp2p-core = { version = "0.30.0-rc.1", path = "core", default-features = false }
libp2p-floodsub = { version = "0.31.0-rc.1", path = "protocols/floodsub", optional = true }
Expand All @@ -88,10 +94,12 @@ libp2p-uds = { version = "0.30.0-rc.1", path = "transports/uds", optional = true
libp2p-wasm-ext = { version = "0.30.0-rc.1", path = "transports/wasm-ext", default-features = false, optional = true }
libp2p-yamux = { version = "0.34.0-rc.1", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.13.0-rc.1" }
parking_lot = "0.11.0"
# Explicit dependency to be used in `wasm-bindgen` feature
parking_lot = "0.11.2"
pin-project = "1.0.0"
# Explicit dependency to be used in `wasm-bindgen` feature
rand = "0.7.3"
smallvec = "1.6.1"
wasm-timer = "0.2.4"

[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.30.0-rc.1", path = "transports/deflate", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ either = "1.5"
fnv = "1.0"
futures = { version = "0.3.1", features = ["executor", "thread-pool"] }
futures-timer = "3"
instant = "0.1.11"
lazy_static = "1.2"
libsecp256k1 = { version = "0.6.0", optional = true }
log = "0.4"
Expand Down Expand Up @@ -47,7 +48,6 @@ libp2p-noise = { path = "../transports/noise" }
libp2p-tcp = { path = "../transports/tcp" }
multihash = { version = "0.14", default-features = false, features = ["arb"] }
quickcheck = "0.9.0"
wasm-timer = "0.2"

[build-dependencies]
prost-build = "0.9"
Expand Down
2 changes: 1 addition & 1 deletion core/src/peer_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::identity::error::SigningError;
use crate::identity::Keypair;
use crate::signed_envelope::SignedEnvelope;
use crate::{peer_record_proto, signed_envelope, Multiaddr, PeerId};
use instant::SystemTime;
use std::convert::TryInto;
use std::fmt;
use std::time::SystemTime;

const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record";
const DOMAIN_SEP: &str = "libp2p-routing-state";
Expand Down
4 changes: 3 additions & 1 deletion protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fnv = "1.0.7"
futures = "0.3.5"
rand = "0.7.3"
asynchronous-codec = "0.6"
wasm-timer = "0.2.4"
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
log = "0.4.11"
sha2 = "0.9.1"
Expand All @@ -27,6 +26,9 @@ smallvec = "1.6.1"
prost = "0.9"
hex_fmt = "0.3.0"
regex = "1.4.0"
futures-timer = "3.0.2"
pin-project = "1.0.8"
instant = "0.1.11"

[dev-dependencies]
async-std = "1.6.3"
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/backoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

//! Data structure for efficiently storing known back-off's when pruning peers.
use crate::topic::TopicHash;
use instant::Instant;
use libp2p_core::PeerId;
use std::collections::{
hash_map::{Entry, HashMap},
HashSet,
};
use std::time::Duration;
use wasm_timer::Instant;

#[derive(Copy, Clone)]
struct HeartbeatIndex(usize);
Expand Down
8 changes: 4 additions & 4 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use futures::StreamExt;
use log::{debug, error, trace, warn};
use prost::Message;
use rand::{seq::SliceRandom, thread_rng};
use wasm_timer::{Instant, Interval};

use instant::Instant;
use libp2p_core::{
connection::ConnectionId, identity::Keypair, multiaddr::Protocol::Ip4,
multiaddr::Protocol::Ip6, ConnectedPoint, Multiaddr, PeerId,
Expand All @@ -45,7 +45,6 @@ use libp2p_swarm::{
NotifyHandler, PollParameters,
};

use crate::backoff::BackoffStorage;
use crate::config::{GossipsubConfig, ValidationMode};
use crate::error::{PublishError, SubscriptionError, ValidationError};
use crate::gossip_promises::GossipPromises;
Expand All @@ -62,6 +61,7 @@ use crate::types::{
GossipsubSubscriptionAction, MessageAcceptance, MessageId, PeerInfo, RawGossipsubMessage,
};
use crate::types::{GossipsubRpc, PeerConnections, PeerKind};
use crate::{backoff::BackoffStorage, interval::Interval};
use crate::{rpc_proto, TopicScoreParams};
use std::{cmp::Ordering::Equal, fmt::Debug};

Expand Down Expand Up @@ -406,8 +406,8 @@ where
config.backoff_slack(),
),
mcache: MessageCache::new(config.history_gossip(), config.history_length()),
heartbeat: Interval::new_at(
Instant::now() + config.heartbeat_initial_delay(),
heartbeat: Interval::new_initial(
config.heartbeat_initial_delay(),
config.heartbeat_interval(),
),
heartbeat_ticks: 0,
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/gossip_promises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use crate::error::ValidationError;
use crate::peer_score::RejectReason;
use crate::MessageId;
use instant::Instant;
use libp2p_core::PeerId;
use log::debug;
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::collections::HashMap;
use wasm_timer::Instant;

/// Tracks recently sent `IWANT` messages and checks if peers respond to them
/// for each `IWANT` message we track one random requested message id.
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::types::{GossipsubRpc, PeerKind, RawGossipsubMessage};
use asynchronous_codec::Framed;
use futures::prelude::*;
use futures::StreamExt;
use instant::Instant;
use libp2p_core::upgrade::{InboundUpgrade, NegotiationError, OutboundUpgrade, UpgradeError};
use libp2p_swarm::protocols_handler::{
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
Expand All @@ -39,7 +40,6 @@ use std::{
task::{Context, Poll},
time::Duration,
};
use wasm_timer::Instant;

/// The initial time (in seconds) we set the keep alive for protocol negotiations to occur.
const INITIAL_KEEP_ALIVE: u64 = 30;
Expand Down