Skip to content

Commit

Permalink
feat(gossipsub): allow compiling for WASM
Browse files Browse the repository at this point in the history
This modification removes deprecated dependency `wasm_timer` and enables wasm compatibility on the gossibsup protocol by simply substituting the `wasm_timer::Instant` with `instant::Instant`(which supports `fn checked_add`) and `wasm_timer::Interval` with `futures_ticker::Ticker`.

Pull-Request: #3973.
  • Loading branch information
onur-ozkan committed May 25, 2023
1 parent fc386a5 commit 801023f
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 94 deletions.
74 changes: 23 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ either = "1.5"
fnv = "1.0"
futures = { version = "0.3.28", features = ["executor", "thread-pool"] }
futures-timer = "3"
instant = "0.1.11"
instant = "0.1.12"
libp2p-identity = { workspace = true, features = ["peerid", "ed25519"] }
log = "0.4"
multiaddr = { version = "0.17.1" }
Expand Down
8 changes: 3 additions & 5 deletions libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ tcp = ["dep:libp2p-tcp"]
tls = ["dep:libp2p-tls"]
tokio = ["libp2p-swarm/tokio", "libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio", "libp2p-quic?/tokio", "libp2p-webrtc?/tokio"]
uds = ["dep:libp2p-uds"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen", "libp2p-gossipsub?/wasm-bindgen"]
wasm-ext = ["dep:libp2p-wasm-ext"]
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
webrtc = ["dep:libp2p-webrtc", "libp2p-webrtc?/pem"]
Expand All @@ -92,14 +92,15 @@ bytes = "1"
futures = "0.3.26"
futures-timer = "3.0.2" # Explicit dependency to be used in `wasm-bindgen` feature
getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature
instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature
instant = "0.1.12" # Explicit dependency to be used in `wasm-bindgen` feature

libp2p-allow-block-list = { workspace = true }
libp2p-autonat = { workspace = true, optional = true }
libp2p-connection-limits = { workspace = true }
libp2p-core = { workspace = true }
libp2p-dcutr = { workspace = true, optional = true }
libp2p-floodsub = { workspace = true, optional = true }
libp2p-gossipsub = { workspace = true, optional = true }
libp2p-identify = { workspace = true, optional = true }
libp2p-identity = { workspace = true }
libp2p-kad = { workspace = true, optional = true }
Expand Down Expand Up @@ -130,9 +131,6 @@ libp2p-uds = { workspace = true, optional = true }
libp2p-webrtc = { workspace = true, optional = true }
libp2p-websocket = { workspace = true, optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
libp2p-gossipsub = { workspace = true, optional = true }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
async-trait = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion misc/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ relay = ["libp2p-relay"]
dcutr = ["libp2p-dcutr"]

[dependencies]
instant = "0.1.11"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-dcutr = { workspace = true, optional = true }
libp2p-identify = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion protocols/dcutr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ asynchronous-codec = "0.6"
either = "1.6.0"
futures = "0.3.28"
futures-timer = "3.0"
instant = "0.1.11"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true }
Expand Down
33 changes: 19 additions & 14 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,35 @@ repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[features]
wasm-bindgen = ["getrandom/js", "instant/wasm-bindgen"]

[dependencies]
either = "1.5"
libp2p-swarm = { workspace = true }
libp2p-core = { workspace = true }
libp2p-identity = { workspace = true }
bytes = "1.4"
asynchronous-codec = "0.6"
base64 = "0.21.0"
byteorder = "1.3.4"
bytes = "1.4"
either = "1.5"
fnv = "1.0.7"
futures = "0.3.28"
rand = "0.8"
asynchronous-codec = "0.6"
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
futures-ticker = "0.0.3"
getrandom = "0.2.9"
hex_fmt = "0.3.0"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-identity = { workspace = true }
libp2p-swarm = { workspace = true }
log = "0.4.11"
sha2 = "0.10.0"
base64 = "0.21.0"
smallvec = "1.6.1"
quick-protobuf = "0.8"
quick-protobuf-codec = { workspace = true }
hex_fmt = "0.3.0"
rand = "0.8"
regex = "1.8.1"
serde = { version = "1", optional = true, features = ["derive"] }
wasm-timer = "0.2.5"
instant = "0.1.11"
sha2 = "0.10.0"
smallvec = "1.6.1"
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
void = "1.0.2"

# Metrics dependencies
prometheus-client = "0.21.0"

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_identity::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
20 changes: 10 additions & 10 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ use std::{
};

use futures::StreamExt;
use futures_ticker::Ticker;
use log::{debug, error, trace, warn};
use prometheus_client::registry::Registry;
use rand::{seq::SliceRandom, thread_rng};

use instant::Instant;
use libp2p_core::{multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, Endpoint, Multiaddr};
use libp2p_identity::Keypair;
use libp2p_identity::PeerId;
Expand All @@ -43,7 +45,6 @@ use libp2p_swarm::{
ConnectionDenied, ConnectionId, NetworkBehaviour, NotifyHandler, PollParameters, THandler,
THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use wasm_timer::Instant;

use crate::backoff::BackoffStorage;
use crate::config::{Config, ValidationMode};
Expand All @@ -67,7 +68,6 @@ use crate::{PublishError, SubscriptionError, ValidationError};
use instant::SystemTime;
use quick_protobuf::{MessageWrite, Writer};
use std::{cmp::Ordering::Equal, fmt::Debug};
use wasm_timer::Interval;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -289,7 +289,7 @@ pub struct Behaviour<D = IdentityTransform, F = AllowAllSubscriptionFilter> {
mcache: MessageCache,

/// Heartbeat interval stream.
heartbeat: Interval,
heartbeat: Ticker,

/// Number of heartbeats since the beginning of time; this allows us to amortize some resource
/// clean up -- eg backoff clean up.
Expand All @@ -307,7 +307,7 @@ pub struct Behaviour<D = IdentityTransform, F = AllowAllSubscriptionFilter> {

/// Stores optional peer score data together with thresholds, decay interval and gossip
/// promises.
peer_score: Option<(PeerScore, PeerScoreThresholds, Interval, GossipPromises)>,
peer_score: Option<(PeerScore, PeerScoreThresholds, Ticker, GossipPromises)>,

/// Counts the number of `IHAVE` received from each peer since the last heartbeat.
count_received_ihave: HashMap<PeerId, usize>,
Expand Down Expand Up @@ -460,9 +460,9 @@ where
config.backoff_slack(),
),
mcache: MessageCache::new(config.history_gossip(), config.history_length()),
heartbeat: Interval::new_at(
Instant::now() + config.heartbeat_initial_delay(),
heartbeat: Ticker::new_with_next(
config.heartbeat_interval(),
config.heartbeat_initial_delay(),
),
heartbeat_ticks: 0,
px_peers: HashSet::new(),
Expand Down Expand Up @@ -908,7 +908,7 @@ where
return Err("Peer score set twice".into());
}

let interval = Interval::new(params.decay_interval);
let interval = Ticker::new(params.decay_interval);
let peer_score = PeerScore::new_with_message_delivery_time_callback(params, callback);
self.peer_score = Some((peer_score, threshold, interval, GossipPromises::default()));
Ok(())
Expand Down Expand Up @@ -1175,7 +1175,7 @@ where
}

fn score_below_threshold_from_scores(
peer_score: &Option<(PeerScore, PeerScoreThresholds, Interval, GossipPromises)>,
peer_score: &Option<(PeerScore, PeerScoreThresholds, Ticker, GossipPromises)>,
peer_id: &PeerId,
threshold: impl Fn(&PeerScoreThresholds) -> f64,
) -> (bool, f64) {
Expand Down Expand Up @@ -3472,12 +3472,12 @@ where

// update scores
if let Some((peer_score, _, interval, _)) = &mut self.peer_score {
while let Poll::Ready(Some(())) = interval.poll_next_unpin(cx) {
while let Poll::Ready(Some(_)) = interval.poll_next_unpin(cx) {
peer_score.refresh_scores();
}
}

while let Poll::Ready(Some(())) = self.heartbeat.poll_next_unpin(cx) {
while let Poll::Ready(Some(_)) = self.heartbeat.poll_next_unpin(cx) {
self.heartbeat();
}

Expand Down

0 comments on commit 801023f

Please sign in to comment.