From 4e6d84850afd0c59a486644e04c6d9d8b0e24bdc Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 29 Sep 2022 10:47:20 +0200 Subject: [PATCH] [contracts] Fix intermittently failing clock test Use a sub-second offset to avoid off-by-one errors in the test at the second boundary. Co-authored-by: Eric Mastro --- codex/contracts/clock.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/codex/contracts/clock.nim b/codex/contracts/clock.nim index dd17986f..76b75620 100644 --- a/codex/contracts/clock.nim +++ b/codex/contracts/clock.nim @@ -10,7 +10,7 @@ type OnChainClock* = ref object of Clock provider: Provider subscription: Subscription - offset: int64 + offset: float started: bool proc new*(_: type OnChainClock, provider: Provider): OnChainClock = @@ -22,8 +22,8 @@ proc start*(clock: OnChainClock) {.async.} = clock.started = true proc onBlock(blck: Block) {.async, upraises:[].} = - let blockTime = blck.timestamp.truncate(int64) - let computerTime = getTime().toUnix + let blockTime = blck.timestamp.truncate(int64).float + let computerTime = epochTime() clock.offset = blockTime - computerTime if latestBlock =? (await clock.provider.getBlock(BlockTag.latest)): @@ -40,4 +40,4 @@ proc stop*(clock: OnChainClock) {.async.} = method now*(clock: OnChainClock): SecondsSince1970 = doAssert clock.started, "clock should be started before calling now()" - getTime().toUnix + clock.offset + int64(epochTime() + clock.offset)