Use a copy of slots hashset for iteration. (#801)

This commit is contained in:
Ben Bierens 2024-05-06 10:12:49 +02:00 committed by GitHub
parent c58d4d7dbe
commit 4312e5ca36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 2 deletions

View File

@ -34,6 +34,12 @@ proc new*(
proc slots*(validation: Validation): seq[SlotId] = proc slots*(validation: Validation): seq[SlotId] =
validation.slots.toSeq validation.slots.toSeq
proc iterateSlots(validation: Validation, action: proc(s: SlotId): Future[void] {.async.}) {.async.} =
# Copy of hashSet, for iteration.
let slots = validation.slots
for slotId in slots:
await action(slotId)
proc getCurrentPeriod(validation: Validation): UInt256 = proc getCurrentPeriod(validation: Validation): UInt256 =
return validation.periodicity.periodOf(validation.clock.now().u256) return validation.periodicity.periodOf(validation.clock.now().u256)
@ -55,11 +61,12 @@ proc subscribeSlotFilled(validation: Validation) {.async.} =
proc removeSlotsThatHaveEnded(validation: Validation) {.async.} = proc removeSlotsThatHaveEnded(validation: Validation) {.async.} =
var ended: HashSet[SlotId] var ended: HashSet[SlotId]
for slotId in validation.slots: proc onSlot(slotId: SlotId) {.async.} =
let state = await validation.market.slotState(slotId) let state = await validation.market.slotState(slotId)
if state != SlotState.Filled: if state != SlotState.Filled:
trace "Removing slot", slotId trace "Removing slot", slotId
ended.incl(slotId) ended.incl(slotId)
await validation.iterateSlots(onSlot)
validation.slots.excl(ended) validation.slots.excl(ended)
proc markProofAsMissing(validation: Validation, proc markProofAsMissing(validation: Validation,
@ -81,9 +88,10 @@ proc markProofAsMissing(validation: Validation,
error "Marking proof as missing failed", msg = e.msg error "Marking proof as missing failed", msg = e.msg
proc markProofsAsMissing(validation: Validation) {.async.} = proc markProofsAsMissing(validation: Validation) {.async.} =
for slotId in validation.slots: proc onSlot(slotId: SlotId) {.async.} =
let previousPeriod = validation.getCurrentPeriod() - 1 let previousPeriod = validation.getCurrentPeriod() - 1
await validation.markProofAsMissing(slotId, previousPeriod) await validation.markProofAsMissing(slotId, previousPeriod)
await validation.iterateSlots(onSlot)
proc run(validation: Validation) {.async.} = proc run(validation: Validation) {.async.} =
trace "Validation started" trace "Validation started"