Skip to content

Commit 801023f

Browse files
authored
feat(gossipsub): allow compiling for WASM
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.
1 parent fc386a5 commit 801023f

18 files changed

Lines changed: 69 additions & 94 deletions

File tree

Cargo.lock

Lines changed: 23 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ either = "1.5"
1515
fnv = "1.0"
1616
futures = { version = "0.3.28", features = ["executor", "thread-pool"] }
1717
futures-timer = "3"
18-
instant = "0.1.11"
18+
instant = "0.1.12"
1919
libp2p-identity = { workspace = true, features = ["peerid", "ed25519"] }
2020
log = "0.4"
2121
multiaddr = { version = "0.17.1" }

libp2p/Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ tcp = ["dep:libp2p-tcp"]
8080
tls = ["dep:libp2p-tls"]
8181
tokio = ["libp2p-swarm/tokio", "libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio", "libp2p-quic?/tokio", "libp2p-webrtc?/tokio"]
8282
uds = ["dep:libp2p-uds"]
83-
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen"]
83+
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen", "libp2p-gossipsub?/wasm-bindgen"]
8484
wasm-ext = ["dep:libp2p-wasm-ext"]
8585
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
8686
webrtc = ["dep:libp2p-webrtc", "libp2p-webrtc?/pem"]
@@ -92,14 +92,15 @@ bytes = "1"
9292
futures = "0.3.26"
9393
futures-timer = "3.0.2" # Explicit dependency to be used in `wasm-bindgen` feature
9494
getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature
95-
instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature
95+
instant = "0.1.12" # Explicit dependency to be used in `wasm-bindgen` feature
9696

9797
libp2p-allow-block-list = { workspace = true }
9898
libp2p-autonat = { workspace = true, optional = true }
9999
libp2p-connection-limits = { workspace = true }
100100
libp2p-core = { workspace = true }
101101
libp2p-dcutr = { workspace = true, optional = true }
102102
libp2p-floodsub = { workspace = true, optional = true }
103+
libp2p-gossipsub = { workspace = true, optional = true }
103104
libp2p-identify = { workspace = true, optional = true }
104105
libp2p-identity = { workspace = true }
105106
libp2p-kad = { workspace = true, optional = true }
@@ -130,9 +131,6 @@ libp2p-uds = { workspace = true, optional = true }
130131
libp2p-webrtc = { workspace = true, optional = true }
131132
libp2p-websocket = { workspace = true, optional = true }
132133

133-
[target.'cfg(not(target_os = "unknown"))'.dependencies]
134-
libp2p-gossipsub = { workspace = true, optional = true }
135-
136134
[dev-dependencies]
137135
async-std = { version = "1.6.2", features = ["attributes"] }
138136
async-trait = "0.1"

misc/metrics/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ relay = ["libp2p-relay"]
1919
dcutr = ["libp2p-dcutr"]
2020

2121
[dependencies]
22-
instant = "0.1.11"
22+
instant = "0.1.12"
2323
libp2p-core = { workspace = true }
2424
libp2p-dcutr = { workspace = true, optional = true }
2525
libp2p-identify = { workspace = true, optional = true }

protocols/dcutr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ asynchronous-codec = "0.6"
1515
either = "1.6.0"
1616
futures = "0.3.28"
1717
futures-timer = "3.0"
18-
instant = "0.1.11"
18+
instant = "0.1.12"
1919
libp2p-core = { workspace = true }
2020
libp2p-swarm = { workspace = true }
2121
libp2p-identity = { workspace = true }

protocols/gossipsub/Cargo.toml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,35 @@ repository = "https://github.com/libp2p/rust-libp2p"
1010
keywords = ["peer-to-peer", "libp2p", "networking"]
1111
categories = ["network-programming", "asynchronous"]
1212

13+
[features]
14+
wasm-bindgen = ["getrandom/js", "instant/wasm-bindgen"]
15+
1316
[dependencies]
14-
either = "1.5"
15-
libp2p-swarm = { workspace = true }
16-
libp2p-core = { workspace = true }
17-
libp2p-identity = { workspace = true }
18-
bytes = "1.4"
17+
asynchronous-codec = "0.6"
18+
base64 = "0.21.0"
1919
byteorder = "1.3.4"
20+
bytes = "1.4"
21+
either = "1.5"
2022
fnv = "1.0.7"
2123
futures = "0.3.28"
22-
rand = "0.8"
23-
asynchronous-codec = "0.6"
24-
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
24+
futures-ticker = "0.0.3"
25+
getrandom = "0.2.9"
26+
hex_fmt = "0.3.0"
27+
instant = "0.1.12"
28+
libp2p-core = { workspace = true }
29+
libp2p-identity = { workspace = true }
30+
libp2p-swarm = { workspace = true }
2531
log = "0.4.11"
26-
sha2 = "0.10.0"
27-
base64 = "0.21.0"
28-
smallvec = "1.6.1"
2932
quick-protobuf = "0.8"
3033
quick-protobuf-codec = { workspace = true }
31-
hex_fmt = "0.3.0"
34+
rand = "0.8"
3235
regex = "1.8.1"
3336
serde = { version = "1", optional = true, features = ["derive"] }
34-
wasm-timer = "0.2.5"
35-
instant = "0.1.11"
37+
sha2 = "0.10.0"
38+
smallvec = "1.6.1"
39+
unsigned-varint = { version = "0.7.0", features = ["asynchronous_codec"] }
3640
void = "1.0.2"
41+
3742
# Metrics dependencies
3843
prometheus-client = "0.21.0"
3944

protocols/gossipsub/src/backoff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
//! Data structure for efficiently storing known back-off's when pruning peers.
2222
use crate::topic::TopicHash;
23+
use instant::Instant;
2324
use libp2p_identity::PeerId;
2425
use std::collections::{
2526
hash_map::{Entry, HashMap},
2627
HashSet,
2728
};
2829
use std::time::Duration;
29-
use wasm_timer::Instant;
3030

3131
#[derive(Copy, Clone)]
3232
struct HeartbeatIndex(usize);

protocols/gossipsub/src/behaviour.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ use std::{
3030
};
3131

3232
use futures::StreamExt;
33+
use futures_ticker::Ticker;
3334
use log::{debug, error, trace, warn};
3435
use prometheus_client::registry::Registry;
3536
use rand::{seq::SliceRandom, thread_rng};
3637

38+
use instant::Instant;
3739
use libp2p_core::{multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, Endpoint, Multiaddr};
3840
use libp2p_identity::Keypair;
3941
use libp2p_identity::PeerId;
@@ -43,7 +45,6 @@ use libp2p_swarm::{
4345
ConnectionDenied, ConnectionId, NetworkBehaviour, NotifyHandler, PollParameters, THandler,
4446
THandlerInEvent, THandlerOutEvent, ToSwarm,
4547
};
46-
use wasm_timer::Instant;
4748

4849
use crate::backoff::BackoffStorage;
4950
use crate::config::{Config, ValidationMode};
@@ -67,7 +68,6 @@ use crate::{PublishError, SubscriptionError, ValidationError};
6768
use instant::SystemTime;
6869
use quick_protobuf::{MessageWrite, Writer};
6970
use std::{cmp::Ordering::Equal, fmt::Debug};
70-
use wasm_timer::Interval;
7171

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

291291
/// Heartbeat interval stream.
292-
heartbeat: Interval,
292+
heartbeat: Ticker,
293293

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

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

312312
/// Counts the number of `IHAVE` received from each peer since the last heartbeat.
313313
count_received_ihave: HashMap<PeerId, usize>,
@@ -460,9 +460,9 @@ where
460460
config.backoff_slack(),
461461
),
462462
mcache: MessageCache::new(config.history_gossip(), config.history_length()),
463-
heartbeat: Interval::new_at(
464-
Instant::now() + config.heartbeat_initial_delay(),
463+
heartbeat: Ticker::new_with_next(
465464
config.heartbeat_interval(),
465+
config.heartbeat_initial_delay(),
466466
),
467467
heartbeat_ticks: 0,
468468
px_peers: HashSet::new(),
@@ -908,7 +908,7 @@ where
908908
return Err("Peer score set twice".into());
909909
}
910910

911-
let interval = Interval::new(params.decay_interval);
911+
let interval = Ticker::new(params.decay_interval);
912912
let peer_score = PeerScore::new_with_message_delivery_time_callback(params, callback);
913913
self.peer_score = Some((peer_score, threshold, interval, GossipPromises::default()));
914914
Ok(())
@@ -1175,7 +1175,7 @@ where
11751175
}
11761176

11771177
fn score_below_threshold_from_scores(
1178-
peer_score: &Option<(PeerScore, PeerScoreThresholds, Interval, GossipPromises)>,
1178+
peer_score: &Option<(PeerScore, PeerScoreThresholds, Ticker, GossipPromises)>,
11791179
peer_id: &PeerId,
11801180
threshold: impl Fn(&PeerScoreThresholds) -> f64,
11811181
) -> (bool, f64) {
@@ -3472,12 +3472,12 @@ where
34723472

34733473
// update scores
34743474
if let Some((peer_score, _, interval, _)) = &mut self.peer_score {
3475-
while let Poll::Ready(Some(())) = interval.poll_next_unpin(cx) {
3475+
while let Poll::Ready(Some(_)) = interval.poll_next_unpin(cx) {
34763476
peer_score.refresh_scores();
34773477
}
34783478
}
34793479

3480-
while let Poll::Ready(Some(())) = self.heartbeat.poll_next_unpin(cx) {
3480+
while let Poll::Ready(Some(_)) = self.heartbeat.poll_next_unpin(cx) {
34813481
self.heartbeat();
34823482
}
34833483

0 commit comments

Comments
 (0)