[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.} = proc start*(interactions: ContractInteractions) {.async.} =
interactions.sales.start() interactions.sales.start()
interactions.proving.start() await interactions.proving.start()
proc stop*(interactions: ContractInteractions) {.async.} = proc stop*(interactions: ContractInteractions) {.async.} =
interactions.sales.stop() interactions.sales.stop()
interactions.proving.stop() await interactions.proving.stop()

View File

@ -11,7 +11,7 @@ export proofs
type type
Proving* = ref object Proving* = ref object
proofs: Proofs proofs: Proofs
stopped: bool 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:[].}
@ -35,7 +35,7 @@ proc removeEndedContracts(proving: Proving) {.async.} =
proc run(proving: Proving) {.async.} = proc run(proving: Proving) {.async.} =
try: try:
while not proving.stopped: while true:
let currentPeriod = await proving.proofs.getCurrentPeriod() let currentPeriod = await proving.proofs.getCurrentPeriod()
await proving.removeEndedContracts() await proving.removeEndedContracts()
for id in proving.contracts: for id in proving.contracts:
@ -47,11 +47,17 @@ proc run(proving: Proving) {.async.} =
except CatchableError as e: except CatchableError as e:
error "Proving failed", msg = e.msg error "Proving failed", msg = e.msg
proc start*(proving: Proving) = proc start*(proving: Proving) {.async.} =
asyncSpawn proving.run() if proving.loop.isSome:
return
proc stop*(proving: Proving) = proving.loop = some proving.run()
proving.stopped = true
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.} = proc submitProof*(proving: Proving, id: ContractId, proof: seq[byte]) {.async.} =
await proving.proofs.submitProof(id, proof) await proving.proofs.submitProof(id, proof)

View File

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