[proving] Use Clock instead of getTime()

This commit is contained in:
Mark Spanbroek 2022-05-17 16:42:03 +02:00 committed by markspanbroek
parent 8d19476a4c
commit 4210cca6e7
5 changed files with 40 additions and 8 deletions

View File

@ -4,6 +4,8 @@ import pkg/chronos
import pkg/stint import pkg/stint
import ../clock import ../clock
export clock
type type
OnChainClock* = ref object of Clock OnChainClock* = ref object of Clock
provider: Provider provider: Provider

View File

@ -7,6 +7,7 @@ import ./deployment
import ./storage import ./storage
import ./market import ./market
import ./proofs import ./proofs
import ./clock
export purchasing export purchasing
export sales export sales
@ -18,6 +19,7 @@ type
purchasing*: Purchasing purchasing*: Purchasing
sales*: Sales sales*: Sales
proving*: Proving proving*: Proving
clock: OnChainClock
proc new*(_: type ContractInteractions, proc new*(_: type ContractInteractions,
signer: Signer, signer: Signer,
@ -30,10 +32,12 @@ proc new*(_: type ContractInteractions,
let contract = Storage.new(address, signer) let contract = Storage.new(address, signer)
let market = OnChainMarket.new(contract) let market = OnChainMarket.new(contract)
let proofs = OnChainProofs.new(contract) let proofs = OnChainProofs.new(contract)
let clock = OnChainClock.new(signer.provider)
some ContractInteractions( some ContractInteractions(
purchasing: Purchasing.new(market), purchasing: Purchasing.new(market),
sales: Sales.new(market), sales: Sales.new(market),
proving: Proving.new(proofs) proving: Proving.new(proofs, clock),
clock: clock
) )
proc new*(_: type ContractInteractions, proc new*(_: type ContractInteractions,
@ -65,9 +69,11 @@ proc new*(_: type ContractInteractions): ?ContractInteractions =
ContractInteractions.new("ws://localhost:8545") ContractInteractions.new("ws://localhost:8545")
proc start*(interactions: ContractInteractions) {.async.} = proc start*(interactions: ContractInteractions) {.async.} =
await interactions.clock.start()
await interactions.sales.start() await interactions.sales.start()
await interactions.proving.start() await interactions.proving.start()
proc stop*(interactions: ContractInteractions) {.async.} = proc stop*(interactions: ContractInteractions) {.async.} =
await interactions.sales.stop() await interactions.sales.stop()
await interactions.proving.stop() await interactions.proving.stop()
await interactions.clock.stop()

View File

@ -1,9 +1,9 @@
import std/sets import std/sets
import std/times
import pkg/upraises import pkg/upraises
import pkg/questionable import pkg/questionable
import pkg/chronicles import pkg/chronicles
import ./por/timing/proofs import ./por/timing/proofs
import ./clock
export sets export sets
export proofs export proofs
@ -11,13 +11,14 @@ export proofs
type type
Proving* = ref object Proving* = ref object
proofs: Proofs proofs: Proofs
clock: Clock
loop: ?Future[void] loop: ?Future[void]
contracts*: HashSet[ContractId] contracts*: HashSet[ContractId]
onProofRequired: ?OnProofRequired onProofRequired: ?OnProofRequired
OnProofRequired* = proc (id: ContractId) {.gcsafe, upraises:[].} OnProofRequired* = proc (id: ContractId) {.gcsafe, upraises:[].}
func new*(_: type Proving, proofs: Proofs): Proving = func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving =
Proving(proofs: proofs) Proving(proofs: proofs, clock: clock)
proc `onProofRequired=`*(proving: Proving, callback: OnProofRequired) = proc `onProofRequired=`*(proving: Proving, callback: OnProofRequired) =
proving.onProofRequired = some callback proving.onProofRequired = some callback
@ -26,7 +27,7 @@ func add*(proving: Proving, id: ContractId) =
proving.contracts.incl(id) proving.contracts.incl(id)
proc removeEndedContracts(proving: Proving) {.async.} = proc removeEndedContracts(proving: Proving) {.async.} =
let now = getTime().toUnix().u256 let now = proving.clock.now().u256
var ended: HashSet[ContractId] var ended: HashSet[ContractId]
for id in proving.contracts: for id in proving.contracts:
if now >= (await proving.proofs.getProofEnd(id)): if now >= (await proving.proofs.getProofEnd(id)):

View File

@ -0,0 +1,21 @@
import std/times
import dagger/clock
export clock
type
MockClock* = ref object of Clock
time: SecondsSince1970
func new*(_: type MockClock,
time: SecondsSince1970 = getTime().toUnix): MockClock =
MockClock(time: time)
func set*(clock: MockClock, time: SecondsSince1970) =
clock.time = time
func advance*(clock: MockClock, seconds: int64) =
clock.time += seconds
method now*(clock: MockClock): SecondsSince1970 =
clock.time

View File

@ -1,18 +1,20 @@
from std/times import getTime, toUnix
import pkg/asynctest import pkg/asynctest
import pkg/chronos import pkg/chronos
import pkg/dagger/proving import pkg/dagger/proving
import ./helpers/mockproofs import ./helpers/mockproofs
import ./helpers/mockclock
import ./examples import ./examples
suite "Proving": suite "Proving":
var proving: Proving var proving: Proving
var proofs: MockProofs var proofs: MockProofs
var clock: MockClock
setup: setup:
proofs = MockProofs.new() proofs = MockProofs.new()
proving = Proving.new(proofs) clock = MockClock.new()
proving = Proving.new(proofs, clock)
await proving.start() await proving.start()
teardown: teardown:
@ -80,7 +82,7 @@ suite "Proving":
test "stops watching when contract has ended": test "stops watching when contract has ended":
let id = ContractId.example let id = ContractId.example
proving.add(id) proving.add(id)
proofs.setProofEnd(id, getTime().toUnix().u256) proofs.setProofEnd(id, clock.now().u256)
await proofs.advanceToNextPeriod() await proofs.advanceToNextPeriod()
var called: bool var called: bool
proc onProofRequired(id: ContractId) = proc onProofRequired(id: ContractId) =