better use of logScopes to reduce duplication

This commit is contained in:
Marcin Czenko 2024-10-16 00:51:57 +02:00
parent ba844eed7b
commit 4e83f099e8
No known key found for this signature in database
GPG Key ID: 33DEA0C8E30937C0
1 changed files with 21 additions and 26 deletions

View File

@ -79,13 +79,15 @@ proc subscribeSlotFilled(validation: Validation) {.async.} =
validation.subscriptions.add(subscription)
proc removeSlotsThatHaveEnded(validation: Validation) {.async.} =
logScope:
groups = validation.config.groups
groupIndex = validation.config.groupIndex
var ended: HashSet[SlotId]
let slots = validation.slots
for slotId in slots:
let state = await validation.market.slotState(slotId)
if state != SlotState.Filled:
trace "Removing slot", slotId, groups = validation.config.groups,
groupIndex = validation.config.groupIndex, slotState = state
trace "Removing slot", slotId, slotState = state
ended.incl(slotId)
validation.slots.excl(ended)
@ -94,18 +96,16 @@ proc markProofAsMissing(validation: Validation,
period: Period) {.async.} =
logScope:
currentPeriod = validation.getCurrentPeriod()
groups = validation.config.groups
groupIndex = validation.config.groupIndex
try:
if await validation.market.canProofBeMarkedAsMissing(slotId, period):
trace "Marking proof as missing", slotId, periodProofMissed = period,
groups = validation.config.groups,
groupIndex = validation.config.groupIndex
trace "Marking proof as missing", slotId, periodProofMissed = period
await validation.market.markProofAsMissing(slotId, period)
else:
let inDowntime {.used.} = await validation.market.inDowntime(slotId)
trace "Proof not missing", checkedPeriod = period, inDowntime,
groups = validation.config.groups,
groupIndex = validation.config.groupIndex
trace "Proof not missing", checkedPeriod = period, inDowntime
except CancelledError:
raise
except CatchableError as e:
@ -118,18 +118,18 @@ proc markProofsAsMissing(validation: Validation) {.async.} =
await validation.markProofAsMissing(slotId, previousPeriod)
proc run(validation: Validation) {.async.} =
trace "Validation started", groups = validation.config.groups,
groupIndex = validation.config.groupIndex,
currentTime = validation.clock.now,
currentTime = validation.clock.now.fromUnix
logScope:
groups = validation.config.groups
groupIndex = validation.config.groupIndex
trace "Validation started", currentTime = validation.clock.now,
currentTime = validation.clock.now.fromUnix
try:
while true:
await validation.waitUntilNextPeriod()
await validation.removeSlotsThatHaveEnded()
await validation.markProofsAsMissing()
except CancelledError:
trace "Validation stopped", groups = validation.config.groups,
groupIndex = validation.config.groupIndex
trace "Validation stopped"
discard
except CatchableError as e:
error "Validation failed", msg = e.msg, groups = validation.config.groups,
@ -140,27 +140,22 @@ proc epochForDurationBackFromNow(validation: Validation,
return validation.clock.now - duration.inSeconds
proc restoreHistoricalState(validation: Validation) {.async} =
trace "Restoring historical state...", groups = validation.config.groups,
logScope:
groups = validation.config.groups
groupIndex = validation.config.groupIndex
trace "Restoring historical state..."
let startTimeEpoch = validation.epochForDurationBackFromNow(MaxStorageRequestDuration)
let slotFilledEvents = await validation.market.queryPastSlotFilledEvents(
fromTime = startTimeEpoch)
trace "Found slot filled events", numberOfSlots = slotFilledEvents.len,
groups = validation.config.groups,
groupIndex = validation.config.groupIndex
trace "Found slot filled events", numberOfSlots = slotFilledEvents.len
for event in slotFilledEvents:
let slotId = slotId(event.requestId, event.slotIndex)
if validation.shouldValidateSlot(slotId):
trace "Adding slot [historical]", slotId,
groups = validation.config.groups,
groupIndex = validation.config.groupIndex
trace "Adding slot [historical]", slotId
validation.slots.incl(slotId)
trace "Removing slots that have ended...", groups = validation.config.groups,
groupIndex = validation.config.groupIndex
trace "Removing slots that have ended..."
await removeSlotsThatHaveEnded(validation)
trace "Historical state restored", numberOfSlots = validation.slots.len,
groups = validation.config.groups,
groupIndex = validation.config.groupIndex
trace "Historical state restored", numberOfSlots = validation.slots.len
proc start*(validation: Validation) {.async.} =
validation.periodicity = await validation.market.periodicity()