mirror of https://github.com/waku-org/nwaku.git
fix: allow higher resolution timestamps (#1570)
* fix: allow higher resolution timestamps * fix: higher precision requires coarser test
This commit is contained in:
parent
f7584dfc49
commit
59203c7453
|
@ -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
|
|
@ -100,7 +100,7 @@ suite "Waku Archive - message handling":
|
||||||
## Given
|
## Given
|
||||||
let
|
let
|
||||||
now = now()
|
now = now()
|
||||||
invalidSenderTime = now + MaxMessageTimestampVariance + 1
|
invalidSenderTime = now + MaxMessageTimestampVariance + 1_000_000_000 # 1 second over the max variance
|
||||||
|
|
||||||
let message = fakeWakuMessage(ts=invalidSenderTime)
|
let message = fakeWakuMessage(ts=invalidSenderTime)
|
||||||
|
|
||||||
|
|
|
@ -8,20 +8,12 @@ import
|
||||||
std/times,
|
std/times,
|
||||||
metrics
|
metrics
|
||||||
|
|
||||||
type Timestamp* = int64
|
type Timestamp* = int64 # A nanosecond precision timestamp
|
||||||
|
|
||||||
proc getNanosecondTime*[T](timeInSeconds: T): Timestamp =
|
proc getNanosecondTime*[T: SomeNumber](timeInSeconds: T): Timestamp =
|
||||||
var ns = Timestamp(timeInSeconds.int64 * 1000_000_000.int64)
|
var ns = Timestamp(timeInSeconds * 1_000_000_000.T)
|
||||||
return ns
|
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 =
|
proc nowInUnixFloat(): float =
|
||||||
return getTime().toUnixFloat()
|
return getTime().toUnixFloat()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue