[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 ../clock
export clock
type
OnChainClock* = ref object of Clock
provider: Provider

View File

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

View File

@ -1,9 +1,9 @@
import std/sets
import std/times
import pkg/upraises
import pkg/questionable
import pkg/chronicles
import ./por/timing/proofs
import ./clock
export sets
export proofs
@ -11,13 +11,14 @@ export proofs
type
Proving* = ref object
proofs: Proofs
clock: Clock
loop: ?Future[void]
contracts*: HashSet[ContractId]
onProofRequired: ?OnProofRequired
OnProofRequired* = proc (id: ContractId) {.gcsafe, upraises:[].}
func new*(_: type Proving, proofs: Proofs): Proving =
Proving(proofs: proofs)
func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving =
Proving(proofs: proofs, clock: clock)
proc `onProofRequired=`*(proving: Proving, callback: OnProofRequired) =
proving.onProofRequired = some callback
@ -26,7 +27,7 @@ func add*(proving: Proving, id: ContractId) =
proving.contracts.incl(id)
proc removeEndedContracts(proving: Proving) {.async.} =
let now = getTime().toUnix().u256
let now = proving.clock.now().u256
var ended: HashSet[ContractId]
for id in proving.contracts:
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/chronos
import pkg/dagger/proving
import ./helpers/mockproofs
import ./helpers/mockclock
import ./examples
suite "Proving":
var proving: Proving
var proofs: MockProofs
var clock: MockClock
setup:
proofs = MockProofs.new()
proving = Proving.new(proofs)
clock = MockClock.new()
proving = Proving.new(proofs, clock)
await proving.start()
teardown:
@ -80,7 +82,7 @@ suite "Proving":
test "stops watching when contract has ended":
let id = ContractId.example
proving.add(id)
proofs.setProofEnd(id, getTime().toUnix().u256)
proofs.setProofEnd(id, clock.now().u256)
await proofs.advanceToNextPeriod()
var called: bool
proc onProofRequired(id: ContractId) =