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 10 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ jobs:
target: wasm32-wasi
override: true

- name: Install Rust wasm32-unknown-unknown
uses: actions-rs/[email protected]
with:
toolchain: stable
target: wasm32-unknown-unknown
override: true
wngr marked this conversation as resolved.
Show resolved Hide resolved

- name: Install a recent version of clang
run: |
apt-get update
Expand All @@ -80,6 +87,11 @@ jobs:
# TODO: ideally we would build `--workspace`, but not all crates compile for WASM
run: cargo build --target=wasm32-wasi

- name: Build on wasm32-wasi
wngr marked this conversation as resolved.
Show resolved Hide resolved
# 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=wasm32-unknown-unknown --features wasm-bindgen

check-rustdoc-links:
name: Check rustdoc intra-doc links
runs-on: ubuntu-latest
Expand Down
27 changes: 15 additions & 12 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 @@ -65,9 +65,12 @@ all-features = true

[dependencies]
atomic = "0.5.0"
bytes = "1"
futures = "0.3.1"
lazy_static = "1.2"
bytes = "1.1.0"
futures = "0.3.17"
futures-timer = "3.0.2"
getrandom = "0.2.3"
wngr marked this conversation as resolved.
Show resolved Hide resolved
instant = "0.1.11"
lazy_static = "1.4.0"
libp2p-core = { version = "0.30.0", path = "core", default-features = false }
libp2p-floodsub = { version = "0.31.0", path = "protocols/floodsub", optional = true }
libp2p-gossipsub = { version = "0.33.0", path = "./protocols/gossipsub", optional = true }
Expand All @@ -87,11 +90,11 @@ libp2p-swarm-derive = { version = "0.25.0", path = "swarm-derive" }
libp2p-uds = { version = "0.30.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.30.0", path = "transports/wasm-ext", default-features = false, optional = true }
libp2p-yamux = { version = "0.34.0", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.13.0" }
parking_lot = "0.11.0"
pin-project = "1.0.0"
multiaddr = "0.13.0"
parking_lot = "0.11.2"
pin-project = "1.0.8"
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", path = "transports/deflate", optional = true }
Expand All @@ -101,11 +104,11 @@ libp2p-tcp = { version = "0.30.0", path = "transports/tcp", default-features = f
libp2p-websocket = { version = "0.31.0", path = "transports/websocket", optional = true }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
async-trait = "0.1"
async-std = { version = "1.10.0", features = ["attributes"] }
async-trait = "0.1.51"
env_logger = "0.9.0"
structopt = "0.3.21"
tokio = { version = "1.0.1", features = ["io-util", "io-std", "macros", "rt", "rt-multi-thread"] }
structopt = "0.3.23"
wngr marked this conversation as resolved.
Show resolved Hide resolved
tokio = { version = "1.12.0", features = ["io-util", "io-std", "macros", "rt", "rt-multi-thread"] }

[workspace]
resolver = "2"
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.10"
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.8"
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub mod upgrade;

pub use connection::{Connected, ConnectedPoint, Endpoint};
pub use identity::PublicKey;
pub use instant::{Duration, Instant};
wngr marked this conversation as resolved.
Show resolved Hide resolved
pub use multiaddr::Multiaddr;
pub use multihash;
pub use muxing::StreamMuxer;
Expand Down
23 changes: 12 additions & 11 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
libp2p-swarm = { version = "0.31.0", path = "../../swarm" }
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false }
bytes = "1.0"
asynchronous-codec = "0.6"
base64 = "0.13.0"
byteorder = "1.3.4"
bytes = "1.0"
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"] }
futures-timer = "3.0.2"
hex_fmt = "0.3.0"
libp2p-core = { version = "0.30.0", path = "../../core", default-features = false }
libp2p-swarm = { version = "0.31.0", path = "../../swarm" }
log = "0.4.11"
sha2 = "0.9.1"
base64 = "0.13.0"
smallvec = "1.6.1"
pin-project = "1.0.8"
prost = "0.8"
hex_fmt = "0.3.0"
rand = "0.7.3"
regex = "1.4.0"
sha2 = "0.9.1"
smallvec = "1.6.1"
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }

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

//! Data structure for efficiently storing known back-off's when pruning peers.
use crate::topic::TopicHash;
use libp2p_core::PeerId;
use libp2p_core::{Instant, 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
9 changes: 4 additions & 5 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ use futures::StreamExt;
use log::{debug, error, trace, warn};
use prost::Message;
use rand::{seq::SliceRandom, thread_rng};
use wasm_timer::{Instant, Interval};

use libp2p_core::{
connection::ConnectionId, identity::Keypair, multiaddr::Protocol::Ip4,
multiaddr::Protocol::Ip6, ConnectedPoint, Multiaddr, PeerId,
multiaddr::Protocol::Ip6, ConnectedPoint, Instant, Multiaddr, PeerId,
};
use libp2p_swarm::{
DialPeerCondition, IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
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 +60,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 +405,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
3 changes: 1 addition & 2 deletions protocols/gossipsub/src/gossip_promises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
use crate::error::ValidationError;
use crate::peer_score::RejectReason;
use crate::MessageId;
use libp2p_core::PeerId;
use libp2p_core::{Instant, 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
6 changes: 4 additions & 2 deletions protocols/gossipsub/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use crate::types::{GossipsubRpc, PeerKind, RawGossipsubMessage};
use asynchronous_codec::Framed;
use futures::prelude::*;
use futures::StreamExt;
use libp2p_core::upgrade::{InboundUpgrade, NegotiationError, OutboundUpgrade, UpgradeError};
use libp2p_core::{
upgrade::{InboundUpgrade, NegotiationError, OutboundUpgrade, UpgradeError},
Instant,
};
use libp2p_swarm::protocols_handler::{
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
};
Expand All @@ -39,7 +42,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
Loading