[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 <eric.mastro@gmail.com>
This commit is contained in:
Mark Spanbroek 2022-09-29 10:47:20 +02:00 committed by markspanbroek
parent 8d315b38b8
commit 4e6d84850a
1 changed files with 4 additions and 4 deletions

View File

@ -10,7 +10,7 @@ type
OnChainClock* = ref object of Clock OnChainClock* = ref object of Clock
provider: Provider provider: Provider
subscription: Subscription subscription: Subscription
offset: int64 offset: float
started: bool started: bool
proc new*(_: type OnChainClock, provider: Provider): OnChainClock = proc new*(_: type OnChainClock, provider: Provider): OnChainClock =
@ -22,8 +22,8 @@ proc start*(clock: OnChainClock) {.async.} =
clock.started = true clock.started = true
proc onBlock(blck: Block) {.async, upraises:[].} = proc onBlock(blck: Block) {.async, upraises:[].} =
let blockTime = blck.timestamp.truncate(int64) let blockTime = blck.timestamp.truncate(int64).float
let computerTime = getTime().toUnix let computerTime = epochTime()
clock.offset = blockTime - computerTime clock.offset = blockTime - computerTime
if latestBlock =? (await clock.provider.getBlock(BlockTag.latest)): if latestBlock =? (await clock.provider.getBlock(BlockTag.latest)):
@ -40,4 +40,4 @@ proc stop*(clock: OnChainClock) {.async.} =
method now*(clock: OnChainClock): SecondsSince1970 = method now*(clock: OnChainClock): SecondsSince1970 =
doAssert clock.started, "clock should be started before calling now()" doAssert clock.started, "clock should be started before calling now()"
getTime().toUnix + clock.offset int64(epochTime() + clock.offset)