Skip to content

Commit 285cd64

Browse files
authored
fix(gossipsub): avoid direct casting from u128 to u64
Replaces the lossy cast with a safer/checked version when initializing sequence numbers from the unix timestamp in nanoseconds. Pull-Request: #6211.
1 parent 2a7e1fe commit 285cd64

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

protocols/gossipsub/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
- Refactor gossipsub with in-place negative-score peer removal.
3333
See [PR 6209](https://github.com/libp2p/rust-libp2p/pull/6209).
3434

35+
- Avoid direct casting from u128 to u64.
36+
See [PR 6211](https://github.com/libp2p/rust-libp2p/pull/6211).
37+
3538
## 0.49.2
3639

3740
- Relax `Behaviour::with_metrics` requirements, do not require DataTransform and TopicSubscriptionFilter to also impl Default

protocols/gossipsub/src/behaviour.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ enum PublishConfig {
183183

184184
/// A strictly linearly increasing sequence number.
185185
///
186-
/// We start from the current time as unix timestamp in milliseconds.
186+
/// We start from the current time as unix timestamp in nanoseconds.
187187
#[derive(Debug)]
188188
struct SequenceNumber(u64);
189189

@@ -194,7 +194,10 @@ impl SequenceNumber {
194194
.expect("time to be linear")
195195
.as_nanos();
196196

197-
Self(unix_timestamp as u64)
197+
Self(
198+
u64::try_from(unix_timestamp)
199+
.expect("timestamp in nanos since UNIX_EPOCH should fit in u64"),
200+
)
198201
}
199202

200203
fn next(&mut self) -> u64 {

0 commit comments

Comments
 (0)