Skip to content

Commit c3a21d1

Browse files
authored
chore(gossispsub): deprecate futures-ticker
to address [RUSTSEC-2024-0384 ](https://rustsec.org/advisories/RUSTSEC-2024-0384.html). Use `futures-timer` and `Delay` instead Pull-Request: #5674.
1 parent 13b9ea2 commit c3a21d1

File tree

5 files changed

+34
-43
lines changed

5 files changed

+34
-43
lines changed

Cargo.lock

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

protocols/gossipsub/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 0.48.0
22

3+
- Deprecate `futures-ticker` and use `futures-timer` instead.
4+
See [PR 5674](https://github.com/libp2p/rust-libp2p/pull/5674).
35
- Apply `max_transmit_size` to the inner message instead of the final payload.
46
See [PR 5642](https://github.com/libp2p/rust-libp2p/pull/5642).
57

protocols/gossipsub/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
1111
categories = ["network-programming", "asynchronous"]
1212

1313
[features]
14-
wasm-bindgen = ["getrandom/js"]
14+
wasm-bindgen = ["getrandom/js", "futures-timer/wasm-bindgen"]
1515

1616
[dependencies]
1717
asynchronous-codec = { workspace = true }
@@ -21,7 +21,6 @@ bytes = "1.6"
2121
either = "1.11"
2222
fnv = "1.0.7"
2323
futures = { workspace = true }
24-
futures-ticker = "0.0.3"
2524
getrandom = "0.2.15"
2625
hex_fmt = "0.3.0"
2726
web-time = { workspace = true }
@@ -39,6 +38,7 @@ tracing = { workspace = true }
3938

4039
# Metrics dependencies
4140
prometheus-client = { workspace = true }
41+
futures-timer = "3.0.3"
4242

4343
[dev-dependencies]
4444
hex = "0.4.2"

protocols/gossipsub/src/behaviour.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use std::{
2929
time::Duration,
3030
};
3131

32-
use futures::StreamExt;
33-
use futures_ticker::Ticker;
32+
use futures::FutureExt;
33+
use futures_timer::Delay;
3434
use prometheus_client::registry::Registry;
3535
use rand::{seq::SliceRandom, thread_rng};
3636

@@ -283,7 +283,7 @@ pub struct Behaviour<D = IdentityTransform, F = AllowAllSubscriptionFilter> {
283283
mcache: MessageCache,
284284

285285
/// Heartbeat interval stream.
286-
heartbeat: Ticker,
286+
heartbeat: Delay,
287287

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

302302
/// Stores optional peer score data together with thresholds, decay interval and gossip
303303
/// promises.
304-
peer_score: Option<(PeerScore, PeerScoreThresholds, Ticker, GossipPromises)>,
304+
peer_score: Option<(PeerScore, PeerScoreThresholds, Delay, GossipPromises)>,
305305

306306
/// Counts the number of `IHAVE` received from each peer since the last heartbeat.
307307
count_received_ihave: HashMap<PeerId, usize>,
@@ -448,10 +448,7 @@ where
448448
config.backoff_slack(),
449449
),
450450
mcache: MessageCache::new(config.history_gossip(), config.history_length()),
451-
heartbeat: Ticker::new_with_next(
452-
config.heartbeat_interval(),
453-
config.heartbeat_initial_delay(),
454-
),
451+
heartbeat: Delay::new(config.heartbeat_interval() + config.heartbeat_initial_delay()),
455452
heartbeat_ticks: 0,
456453
px_peers: HashSet::new(),
457454
outbound_peers: HashSet::new(),
@@ -879,7 +876,7 @@ where
879876
return Err("Peer score set twice".into());
880877
}
881878

882-
let interval = Ticker::new(params.decay_interval);
879+
let interval = Delay::new(params.decay_interval);
883880
let peer_score = PeerScore::new_with_message_delivery_time_callback(params, callback);
884881
self.peer_score = Some((peer_score, threshold, interval, GossipPromises::default()));
885882
Ok(())
@@ -1145,7 +1142,7 @@ where
11451142
}
11461143

11471144
fn score_below_threshold_from_scores(
1148-
peer_score: &Option<(PeerScore, PeerScoreThresholds, Ticker, GossipPromises)>,
1145+
peer_score: &Option<(PeerScore, PeerScoreThresholds, Delay, GossipPromises)>,
11491146
peer_id: &PeerId,
11501147
threshold: impl Fn(&PeerScoreThresholds) -> f64,
11511148
) -> (bool, f64) {
@@ -3105,14 +3102,16 @@ where
31053102
}
31063103

31073104
// update scores
3108-
if let Some((peer_score, _, interval, _)) = &mut self.peer_score {
3109-
while let Poll::Ready(Some(_)) = interval.poll_next_unpin(cx) {
3105+
if let Some((peer_score, _, delay, _)) = &mut self.peer_score {
3106+
if delay.poll_unpin(cx).is_ready() {
31103107
peer_score.refresh_scores();
3108+
delay.reset(peer_score.params.decay_interval);
31113109
}
31123110
}
31133111

3114-
while let Poll::Ready(Some(_)) = self.heartbeat.poll_next_unpin(cx) {
3112+
if self.heartbeat.poll_unpin(cx).is_ready() {
31153113
self.heartbeat();
3114+
self.heartbeat.reset(self.config.heartbeat_interval());
31163115
}
31173116

31183117
Poll::Pending

protocols/gossipsub/src/peer_score.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ mod tests;
4444
const TIME_CACHE_DURATION: u64 = 120;
4545

4646
pub(crate) struct PeerScore {
47-
params: PeerScoreParams,
4847
/// The score parameters.
48+
pub(crate) params: PeerScoreParams,
49+
/// The stats per PeerId.
4950
peer_stats: HashMap<PeerId, PeerStats>,
5051
/// Tracking peers per IP.
5152
peer_ips: HashMap<IpAddr, HashSet<PeerId>>,
5253
/// Message delivery tracking. This is a time-cache of [`DeliveryRecord`]s.
5354
deliveries: TimeCache<MessageId, DeliveryRecord>,
54-
/// callback for monitoring message delivery times
55+
/// Callback for monitoring message delivery times.
5556
message_delivery_time_callback: Option<fn(&PeerId, &TopicHash, f64)>,
5657
}
5758

0 commit comments

Comments
 (0)