2022-05-16 13:38:52 +00:00
|
|
|
import std/times
|
|
|
|
import pkg/chronos
|
2022-05-19 19:56:03 +00:00
|
|
|
import codex/contracts/clock
|
2023-12-07 01:16:36 +00:00
|
|
|
import codex/utils/json
|
2022-05-16 13:38:52 +00:00
|
|
|
import ../ethertest
|
|
|
|
|
2022-05-17 09:50:52 +00:00
|
|
|
ethersuite "On-Chain Clock":
|
|
|
|
var clock: OnChainClock
|
2022-05-16 13:38:52 +00:00
|
|
|
|
|
|
|
setup:
|
2023-12-18 09:34:04 +00:00
|
|
|
clock = OnChainClock.new(ethProvider)
|
2022-05-16 13:38:52 +00:00
|
|
|
await clock.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await clock.stop()
|
|
|
|
|
|
|
|
test "returns the current time of the EVM":
|
2023-12-18 09:34:04 +00:00
|
|
|
let latestBlock = (!await ethProvider.getBlock(BlockTag.latest))
|
2022-05-16 13:38:52 +00:00
|
|
|
let timestamp = latestBlock.timestamp.truncate(int64)
|
2024-02-16 22:12:16 +00:00
|
|
|
check clock.now() == timestamp
|
2022-05-16 13:38:52 +00:00
|
|
|
|
|
|
|
test "updates time with timestamp of new blocks":
|
|
|
|
let future = (getTime() + 42.years).toUnix
|
2023-12-18 09:34:04 +00:00
|
|
|
discard await ethProvider.send("evm_setNextBlockTimestamp", @[%future])
|
|
|
|
discard await ethProvider.send("evm_mine")
|
2024-03-11 16:57:20 +00:00
|
|
|
check eventually clock.now() == future
|
2022-05-16 13:38:52 +00:00
|
|
|
|
2023-07-13 09:19:45 +00:00
|
|
|
test "can wait until a certain time is reached by the chain":
|
2024-02-16 22:12:16 +00:00
|
|
|
let future = clock.now() + 42 # seconds
|
2023-07-13 09:19:45 +00:00
|
|
|
let waiting = clock.waitUntil(future)
|
2023-12-18 09:34:04 +00:00
|
|
|
discard await ethProvider.send("evm_setNextBlockTimestamp", @[%future])
|
|
|
|
discard await ethProvider.send("evm_mine")
|
2024-11-07 19:05:19 +00:00
|
|
|
check await waiting.withTimeout(chronos.milliseconds(500))
|
2023-07-13 09:19:45 +00:00
|
|
|
|
|
|
|
test "can wait until a certain time is reached by the wall-clock":
|
2024-02-16 22:12:16 +00:00
|
|
|
let future = clock.now() + 1 # seconds
|
2023-07-13 09:19:45 +00:00
|
|
|
let waiting = clock.waitUntil(future)
|
|
|
|
check await waiting.withTimeout(chronos.seconds(2))
|
|
|
|
|
2022-05-16 13:38:52 +00:00
|
|
|
test "handles starting multiple times":
|
|
|
|
await clock.start()
|
|
|
|
await clock.start()
|
|
|
|
|
|
|
|
test "handles stopping multiple times":
|
|
|
|
await clock.stop()
|
|
|
|
await clock.stop()
|