Skip to content

Commit dca3918

Browse files
committed
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. Signed-off-by: Onur Özkan <[email protected]>
1 parent 2a7e1fe commit dca3918

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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)