fix: allow higher resolution timestamps (#1570)

* fix: allow higher resolution timestamps

* fix: higher precision requires coarser test
This commit is contained in:
Hanno Cornelius 2023-02-21 14:52:30 +02:00 committed by GitHub
parent f7584dfc49
commit 59203c7453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 12 deletions

View File

@ -0,0 +1,35 @@
{.used.}
import
stew/results,
testutils/unittests
import
../../waku/v2/utils/time
suite "Utils - Time":
test "Test timestamp conversion":
## Given
let
nanoseconds = 1676562429123456789.int64
secondsPart = nanoseconds div 1_000_000_000
nanosecondsPart = nanoseconds mod 1_000_000_000
secondsFloat = secondsPart.float64 + (nanosecondsPart.float64 / 1_000_000_000.float64)
lowResTimestamp = Timestamp(secondsPart.int64 * 1_000_000_000.int64) # 1676562429000000000
highResTimestamp = Timestamp(secondsFloat * 1_000_000_000.float64) # 1676562429123456789
require highResTimestamp > lowResTimestamp # Sanity check
## When
let
timeInSecondsInt = secondsPart.int
timeInSecondsInt64 = secondsPart.int64
timeInSecondsFloat = float(secondsFloat)
timeInSecondsFloat64 = float64(secondsFloat)
## Then
check:
getNanosecondTime(timeInSecondsInt) == lowResTimestamp
getNanosecondTime(timeInSecondsInt64) == lowResTimestamp
getNanosecondTime(timeInSecondsFloat) == highResTimestamp
getNanosecondTime(timeInSecondsFloat64) == highResTimestamp

View File

@ -100,7 +100,7 @@ suite "Waku Archive - message handling":
## Given
let
now = now()
invalidSenderTime = now + MaxMessageTimestampVariance + 1
invalidSenderTime = now + MaxMessageTimestampVariance + 1_000_000_000 # 1 second over the max variance
let message = fakeWakuMessage(ts=invalidSenderTime)

View File

@ -8,20 +8,12 @@ import
std/times,
metrics
type Timestamp* = int64
type Timestamp* = int64 # A nanosecond precision timestamp
proc getNanosecondTime*[T](timeInSeconds: T): Timestamp =
var ns = Timestamp(timeInSeconds.int64 * 1000_000_000.int64)
proc getNanosecondTime*[T: SomeNumber](timeInSeconds: T): Timestamp =
var ns = Timestamp(timeInSeconds * 1_000_000_000.T)
return ns
proc getMicrosecondTime*[T](timeInSeconds: T): Timestamp =
var us = Timestamp(timeInSeconds.int64 * 1000_000.int64)
return us
proc getMillisecondTime*[T](timeInSeconds: T): Timestamp =
var ms = Timestamp(timeInSeconds.int64 * 1000.int64)
return ms
proc nowInUnixFloat(): float =
return getTime().toUnixFloat()