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.
This commit is contained in:
Onur
2023-05-25 09:19:47 +03:00
committed by GitHub
parent fc386a51a7
commit 801023f468
18 changed files with 69 additions and 94 deletions

View File

@ -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 }

View File

@ -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"

View File

@ -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);

View File

@ -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;
@ -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};
@ -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;
@ -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.
@ -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>,
@ -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(),
@ -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(())
@ -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) {
@ -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();
}

View File

@ -21,10 +21,10 @@
use crate::peer_score::RejectReason;
use crate::MessageId;
use crate::ValidationError;
use instant::Instant;
use libp2p_identity::PeerId;
use log::debug;
use std::collections::HashMap;
use wasm_timer::Instant;
/// Tracks recently sent `IWANT` messages and checks if peers respond to them.
#[derive(Default)]

View File

@ -24,12 +24,12 @@
use crate::metrics::{Metrics, Penalty};
use crate::time_cache::TimeCache;
use crate::{MessageId, TopicHash};
use instant::Instant;
use libp2p_identity::PeerId;
use log::{debug, trace, warn};
use std::collections::{hash_map, HashMap, HashSet};
use std::net::IpAddr;
use std::time::Duration;
use wasm_timer::Instant;
mod params;
use crate::ValidationError;

View File

@ -21,13 +21,13 @@
//! This implements a time-based LRU cache for checking gossipsub message duplicates.
use fnv::FnvHashMap;
use instant::Instant;
use std::collections::hash_map::{
self,
Entry::{Occupied, Vacant},
};
use std::collections::VecDeque;
use std::time::Duration;
use wasm_timer::Instant;
struct ExpiringElement<Element> {
/// The element that expires

View File

@ -29,7 +29,7 @@ uint = "0.9"
unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] }
void = "1.0"
futures-timer = "3.0.2"
instant = "0.1.11"
instant = "0.1.12"
serde = { version = "1.0", optional = true, features = ["derive"] }
thiserror = "1"

View File

@ -16,7 +16,7 @@ async-std = { version = "1.9.0", features = ["attributes"] }
clap = { version = "4.3.0", features = ["derive"] }
env_logger = "0.10.0"
futures = "0.3.28"
instant = "0.1.11"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-dns = { workspace = true, features = ["async-std"] }
libp2p-identity = { workspace = true }

View File

@ -14,7 +14,7 @@ categories = ["network-programming", "asynchronous"]
either = "1.8.0"
futures = "0.3.28"
futures-timer = "3.0.2"
instant = "0.1.11"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true }

View File

@ -16,7 +16,7 @@ bytes = "1"
either = "1.6.0"
futures = "0.3.28"
futures-timer = "3"
instant = "0.1.11"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true, features = ["async-std"] }
libp2p-identity = { workspace = true }

View File

@ -15,7 +15,7 @@ asynchronous-codec = "0.6"
bimap = "0.6.3"
futures = { version = "0.3", default-features = false, features = ["std"] }
futures-timer = "3.0.2"
instant = "0.1.11"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true }

View File

@ -13,7 +13,7 @@ categories = ["network-programming", "asynchronous"]
[dependencies]
async-trait = "0.1"
futures = "0.3.28"
instant = "0.1.11"
instant = "0.1.12"
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true }