mirror of
https://github.com/status-im/nim-codex.git
synced 2025-02-11 02:06:35 +00:00
* Add Proving object, which maintains contract id's to watch * [proving] invoke callback when proof is required # Conflicts: # dagger/por/timing/periods.nim # dagger/por/timing/prooftiming.nim * [proving] check proof requirements for all our contracts # Conflicts: # tests/dagger/helpers/mockprooftiming.nim * Update vendor/dagger-contracts * [proving] call onProofRequired() when proof is required soon * [proving] stop checking contracts that have ended * [proving] Remove duplicated funcs * [proving] Implement ProofTiming on top of smart contract * [proving] Fix race condition in waitUntilNextPeriod() Sometimes waitUntilNextPeriod would take a while to determine the current period, leading to unexpected results. Splits waitUntilNextPeriod() into getCurrentPeriod() and waitUntilPeriod(), to ensure that we're really waiting for the period that we think we're waiting for.
92 lines
2.6 KiB
Nim
92 lines
2.6 KiB
Nim
from std/times import getTime, toUnix
|
|
import pkg/asynctest
|
|
import pkg/chronos
|
|
import pkg/dagger/proving
|
|
import ./helpers/mockprooftiming
|
|
import ./examples
|
|
|
|
suite "Proving":
|
|
|
|
var proving: Proving
|
|
var timing: MockProofTiming
|
|
|
|
setup:
|
|
timing = MockProofTiming.new()
|
|
proving = Proving.new(timing)
|
|
proving.start()
|
|
|
|
teardown:
|
|
proving.stop()
|
|
|
|
proc advanceToNextPeriod(timing: MockProofTiming) {.async.} =
|
|
let current = await timing.getCurrentPeriod()
|
|
timing.advanceToPeriod(current + 1)
|
|
await sleepAsync(1.milliseconds)
|
|
|
|
test "maintains a list of contract ids to watch":
|
|
let id1, id2 = ContractId.example
|
|
check proving.contracts.len == 0
|
|
proving.add(id1)
|
|
check proving.contracts.contains(id1)
|
|
proving.add(id2)
|
|
check proving.contracts.contains(id1)
|
|
check proving.contracts.contains(id2)
|
|
|
|
test "removes duplicate contract ids":
|
|
let id = ContractId.example
|
|
proving.add(id)
|
|
proving.add(id)
|
|
check proving.contracts.len == 1
|
|
|
|
test "invokes callback when proof is required":
|
|
let id = ContractId.example
|
|
proving.add(id)
|
|
var called: bool
|
|
proc onProofRequired(id: ContractId) =
|
|
called = true
|
|
proving.onProofRequired = onProofRequired
|
|
timing.setProofRequired(id, true)
|
|
await timing.advanceToNextPeriod()
|
|
check called
|
|
|
|
test "callback receives id of contract for which proof is required":
|
|
let id1, id2 = ContractId.example
|
|
proving.add(id1)
|
|
proving.add(id2)
|
|
var callbackIds: seq[ContractId]
|
|
proc onProofRequired(id: ContractId) =
|
|
callbackIds.add(id)
|
|
proving.onProofRequired = onProofRequired
|
|
timing.setProofRequired(id1, true)
|
|
await timing.advanceToNextPeriod()
|
|
check callbackIds == @[id1]
|
|
timing.setProofRequired(id1, false)
|
|
timing.setProofRequired(id2, true)
|
|
await timing.advanceToNextPeriod()
|
|
check callbackIds == @[id1, id2]
|
|
|
|
test "invokes callback when proof is about to be required":
|
|
let id = ContractId.example
|
|
proving.add(id)
|
|
var called: bool
|
|
proc onProofRequired(id: ContractId) =
|
|
called = true
|
|
proving.onProofRequired = onProofRequired
|
|
timing.setProofRequired(id, false)
|
|
timing.setProofToBeRequired(id, true)
|
|
await timing.advanceToNextPeriod()
|
|
check called
|
|
|
|
test "stops watching when contract has ended":
|
|
let id = ContractId.example
|
|
proving.add(id)
|
|
timing.setProofEnd(id, getTime().toUnix().u256)
|
|
await timing.advanceToNextPeriod()
|
|
var called: bool
|
|
proc onProofRequired(id: ContractId) =
|
|
called = true
|
|
proving.onProofRequired = onProofRequired
|
|
timing.setProofRequired(id, true)
|
|
await timing.advanceToNextPeriod()
|
|
check not called
|