2022-04-08 21:58:16 +00:00
|
|
|
import std/sets
|
|
|
|
import pkg/upraises
|
|
|
|
import pkg/questionable
|
2022-04-20 10:39:40 +00:00
|
|
|
import pkg/chronicles
|
2022-05-24 05:24:15 +00:00
|
|
|
import ./storageproofs
|
2022-05-17 14:42:03 +00:00
|
|
|
import ./clock
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
export sets
|
2022-05-24 05:24:15 +00:00
|
|
|
export storageproofs
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Proving* = ref object
|
2022-04-13 16:41:48 +00:00
|
|
|
proofs: Proofs
|
2022-05-17 14:42:03 +00:00
|
|
|
clock: Clock
|
2022-05-17 14:40:21 +00:00
|
|
|
loop: ?Future[void]
|
2022-08-17 02:29:44 +00:00
|
|
|
slots*: HashSet[SlotId]
|
2022-04-08 21:58:16 +00:00
|
|
|
onProofRequired: ?OnProofRequired
|
2022-08-17 02:29:44 +00:00
|
|
|
OnProofRequired* = proc (id: SlotId) {.gcsafe, upraises:[].}
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2022-05-17 14:42:03 +00:00
|
|
|
func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving =
|
|
|
|
Proving(proofs: proofs, clock: clock)
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
proc `onProofRequired=`*(proving: Proving, callback: OnProofRequired) =
|
|
|
|
proving.onProofRequired = some callback
|
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
func add*(proving: Proving, id: SlotId) =
|
|
|
|
proving.slots.incl(id)
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2022-05-18 09:18:25 +00:00
|
|
|
proc getCurrentPeriod(proving: Proving): Future[Period] {.async.} =
|
|
|
|
let periodicity = await proving.proofs.periodicity()
|
|
|
|
return periodicity.periodOf(proving.clock.now().u256)
|
|
|
|
|
|
|
|
proc waitUntilPeriod(proving: Proving, period: Period) {.async.} =
|
|
|
|
let periodicity = await proving.proofs.periodicity()
|
|
|
|
await proving.clock.waitUntil(periodicity.periodStart(period).truncate(int64))
|
|
|
|
|
2022-04-08 21:58:16 +00:00
|
|
|
proc removeEndedContracts(proving: Proving) {.async.} =
|
2022-05-17 14:42:03 +00:00
|
|
|
let now = proving.clock.now().u256
|
2022-08-17 02:29:44 +00:00
|
|
|
var ended: HashSet[SlotId]
|
|
|
|
for id in proving.slots:
|
2022-04-13 16:41:48 +00:00
|
|
|
if now >= (await proving.proofs.getProofEnd(id)):
|
2022-04-08 21:58:16 +00:00
|
|
|
ended.incl(id)
|
2022-08-17 02:29:44 +00:00
|
|
|
proving.slots.excl(ended)
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
proc run(proving: Proving) {.async.} =
|
2022-04-20 10:39:40 +00:00
|
|
|
try:
|
2022-05-17 14:40:21 +00:00
|
|
|
while true:
|
2022-05-18 09:18:25 +00:00
|
|
|
let currentPeriod = await proving.getCurrentPeriod()
|
2022-04-20 10:39:40 +00:00
|
|
|
await proving.removeEndedContracts()
|
2022-08-17 02:29:44 +00:00
|
|
|
for id in proving.slots:
|
2022-04-20 10:39:40 +00:00
|
|
|
if (await proving.proofs.isProofRequired(id)) or
|
|
|
|
(await proving.proofs.willProofBeRequired(id)):
|
|
|
|
if callback =? proving.onProofRequired:
|
|
|
|
callback(id)
|
2022-05-18 09:18:25 +00:00
|
|
|
await proving.waitUntilPeriod(currentPeriod + 1)
|
2022-05-18 10:59:23 +00:00
|
|
|
except CancelledError:
|
|
|
|
discard
|
2022-04-20 10:39:40 +00:00
|
|
|
except CatchableError as e:
|
|
|
|
error "Proving failed", msg = e.msg
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2022-05-17 14:40:21 +00:00
|
|
|
proc start*(proving: Proving) {.async.} =
|
|
|
|
if proving.loop.isSome:
|
|
|
|
return
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2022-05-17 14:40:21 +00:00
|
|
|
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()
|
2022-04-13 16:41:48 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
proc submitProof*(proving: Proving, id: SlotId, proof: seq[byte]) {.async.} =
|
2022-04-13 16:41:48 +00:00
|
|
|
await proving.proofs.submitProof(id, proof)
|
|
|
|
|
|
|
|
proc subscribeProofSubmission*(proving: Proving,
|
|
|
|
callback: OnProofSubmitted):
|
|
|
|
Future[Subscription] =
|
|
|
|
proving.proofs.subscribeProofSubmission(callback)
|