Skip to content

Commit

Permalink
adds state restoration to validator
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinczenko committed Sep 26, 2024
1 parent 9b8be1e commit ab7b87e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions codex/validation.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import std/times
import std/sets
import std/sequtils
import pkg/chronos
Expand All @@ -23,6 +24,9 @@ type
proofTimeout: UInt256
config: ValidationConfig

const
MaxStorageRequestDuration: times.Duration = initDuration(days = 30)

logScope:
topics = "codex validator"

Expand Down Expand Up @@ -119,10 +123,26 @@ proc run(validation: Validation) {.async.} =
except CatchableError as e:
error "Validation failed", msg = e.msg

proc epochForDurationBackFromNow(duration: times.Duration): int64 =
let now = getTime().toUnix
return now - duration.inSeconds

proc restoreHistoricalState(validation: Validation) {.async} =
let startTimeEpoch = epochForDurationBackFromNow(MaxStorageRequestDuration)
let slotFilledEvents = await validation.market.queryPastEvents(SlotFilled,
fromTime = startTimeEpoch)
for event in slotFilledEvents:
let slotId = slotId(event.requestId, event.slotIndex)
if validation.shouldValidateSlot(slotId):
trace "Adding slot", slotId
validation.slots.incl(slotId)
await removeSlotsThatHaveEnded(validation)

proc start*(validation: Validation) {.async.} =
validation.periodicity = await validation.market.periodicity()
validation.proofTimeout = await validation.market.proofTimeout()
await validation.subscribeSlotFilled()
await validation.restoreHistoricalState()
validation.running = validation.run()

proc stop*(validation: Validation) {.async.} =
Expand Down

0 comments on commit ab7b87e

Please sign in to comment.