[proving] Ensure that proving is completely stopped

This commit is contained in:
Mark Spanbroek 2022-05-17 16:40:21 +02:00 committed by markspanbroek
parent ea72d99798
commit 3f627790f8
3 changed files with 16 additions and 10 deletions

View File

@ -66,8 +66,8 @@ proc new*(_: type ContractInteractions): ?ContractInteractions =
proc start*(interactions: ContractInteractions) {.async.} =
interactions.sales.start()
interactions.proving.start()
await interactions.proving.start()
proc stop*(interactions: ContractInteractions) {.async.} =
interactions.sales.stop()
interactions.proving.stop()
await interactions.proving.stop()

View File

@ -11,7 +11,7 @@ export proofs
type
Proving* = ref object
proofs: Proofs
stopped: bool
loop: ?Future[void]
contracts*: HashSet[ContractId]
onProofRequired: ?OnProofRequired
OnProofRequired* = proc (id: ContractId) {.gcsafe, upraises:[].}
@ -35,7 +35,7 @@ proc removeEndedContracts(proving: Proving) {.async.} =
proc run(proving: Proving) {.async.} =
try:
while not proving.stopped:
while true:
let currentPeriod = await proving.proofs.getCurrentPeriod()
await proving.removeEndedContracts()
for id in proving.contracts:
@ -47,11 +47,17 @@ proc run(proving: Proving) {.async.} =
except CatchableError as e:
error "Proving failed", msg = e.msg
proc start*(proving: Proving) =
asyncSpawn proving.run()
proc start*(proving: Proving) {.async.} =
if proving.loop.isSome:
return
proc stop*(proving: Proving) =
proving.stopped = true
proving.loop = some proving.run()
proc stop*(proving: Proving) {.async.} =
if loop =? proving.loop:
proving.loop = Future[void].none
if not loop.finished:
await loop.cancelAndWait()
proc submitProof*(proving: Proving, id: ContractId, proof: seq[byte]) {.async.} =
await proving.proofs.submitProof(id, proof)

View File

@ -13,10 +13,10 @@ suite "Proving":
setup:
proofs = MockProofs.new()
proving = Proving.new(proofs)
proving.start()
await proving.start()
teardown:
proving.stop()
await proving.stop()
proc advanceToNextPeriod(proofs: MockProofs) {.async.} =
let current = await proofs.getCurrentPeriod()