Skip to content

Commit

Permalink
Use a copy of slots hashset for iteration. (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed May 6, 2024
1 parent c58d4d7 commit 4312e5c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions codex/validation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ proc new*(
proc slots*(validation: Validation): seq[SlotId] =
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 =
return validation.periodicity.periodOf(validation.clock.now().u256)

Expand All @@ -55,11 +61,12 @@ proc subscribeSlotFilled(validation: Validation) {.async.} =

proc removeSlotsThatHaveEnded(validation: Validation) {.async.} =
var ended: HashSet[SlotId]
for slotId in validation.slots:
proc onSlot(slotId: SlotId) {.async.} =
let state = await validation.market.slotState(slotId)
if state != SlotState.Filled:
trace "Removing slot", slotId
ended.incl(slotId)
await validation.iterateSlots(onSlot)
validation.slots.excl(ended)

proc markProofAsMissing(validation: Validation,
Expand All @@ -81,9 +88,10 @@ proc markProofAsMissing(validation: Validation,
error "Marking proof as missing failed", msg = e.msg

proc markProofsAsMissing(validation: Validation) {.async.} =
for slotId in validation.slots:
proc onSlot(slotId: SlotId) {.async.} =
let previousPeriod = validation.getCurrentPeriod() - 1
await validation.markProofAsMissing(slotId, previousPeriod)
await validation.iterateSlots(onSlot)

proc run(validation: Validation) {.async.} =
trace "Validation started"
Expand Down

0 comments on commit 4312e5c

Please sign in to comment.