2023-04-19 13:06:00 +00:00
|
|
|
import pkg/chronos
|
|
|
|
|
|
|
|
import codex/validation
|
2023-05-01 15:22:13 +00:00
|
|
|
import codex/periods
|
2024-01-29 20:03:51 +00:00
|
|
|
|
|
|
|
import ../asynctest
|
2023-04-19 13:06:00 +00:00
|
|
|
import ./helpers/mockmarket
|
|
|
|
import ./helpers/mockclock
|
|
|
|
import ./examples
|
2023-06-22 18:01:21 +00:00
|
|
|
import ./helpers
|
2023-04-19 13:06:00 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "validation":
|
2023-04-19 13:06:00 +00:00
|
|
|
let period = 10
|
|
|
|
let timeout = 5
|
|
|
|
let maxSlots = 100
|
|
|
|
let slot = Slot.example
|
2024-02-07 06:50:35 +00:00
|
|
|
let proof = Groth16Proof.example
|
2023-04-19 13:06:00 +00:00
|
|
|
let collateral = slot.request.ask.collateral
|
|
|
|
|
|
|
|
var validation: Validation
|
|
|
|
var market: MockMarket
|
|
|
|
var clock: MockClock
|
|
|
|
|
|
|
|
setup:
|
|
|
|
market = MockMarket.new()
|
|
|
|
clock = MockClock.new()
|
|
|
|
validation = Validation.new(clock, market, maxSlots)
|
|
|
|
market.config.proofs.period = period.u256
|
|
|
|
market.config.proofs.timeout = timeout.u256
|
|
|
|
await validation.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await validation.stop()
|
|
|
|
|
2024-02-05 11:36:28 +00:00
|
|
|
proc advanceToNextPeriod {.async.} =
|
2023-05-01 15:22:13 +00:00
|
|
|
let periodicity = Periodicity(seconds: period.u256)
|
2024-02-05 11:36:28 +00:00
|
|
|
let period = periodicity.periodOf((await clock.now()).u256)
|
2023-05-01 15:22:13 +00:00
|
|
|
let periodEnd = periodicity.periodEnd(period)
|
|
|
|
clock.set((periodEnd + 1).truncate(int))
|
|
|
|
|
2023-04-19 13:06:00 +00:00
|
|
|
test "the list of slots that it's monitoring is empty initially":
|
|
|
|
check validation.slots.len == 0
|
|
|
|
|
|
|
|
test "when a slot is filled on chain, it is added to the list":
|
2024-02-07 06:50:35 +00:00
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
2023-04-19 13:06:00 +00:00
|
|
|
check validation.slots == @[slot.id]
|
|
|
|
|
|
|
|
for state in [SlotState.Finished, SlotState.Failed]:
|
|
|
|
test "when slot state changes, it is removed from the list":
|
2024-02-07 06:50:35 +00:00
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
2023-04-19 13:06:00 +00:00
|
|
|
market.slotState[slot.id] = state
|
2023-05-01 15:22:13 +00:00
|
|
|
advanceToNextPeriod()
|
2023-04-19 13:06:00 +00:00
|
|
|
check eventually validation.slots.len == 0
|
|
|
|
|
|
|
|
test "when a proof is missed, it is marked as missing":
|
2024-02-07 06:50:35 +00:00
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
2023-04-19 13:06:00 +00:00
|
|
|
market.setCanProofBeMarkedAsMissing(slot.id, true)
|
2023-05-01 15:22:13 +00:00
|
|
|
advanceToNextPeriod()
|
2023-04-19 13:06:00 +00:00
|
|
|
await sleepAsync(1.millis)
|
|
|
|
check market.markedAsMissingProofs.contains(slot.id)
|
|
|
|
|
|
|
|
test "when a proof can not be marked as missing, it will not be marked":
|
2024-02-07 06:50:35 +00:00
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
2023-04-19 13:06:00 +00:00
|
|
|
market.setCanProofBeMarkedAsMissing(slot.id, false)
|
2023-05-01 15:22:13 +00:00
|
|
|
advanceToNextPeriod()
|
2023-04-19 13:06:00 +00:00
|
|
|
await sleepAsync(1.millis)
|
|
|
|
check market.markedAsMissingProofs.len == 0
|
|
|
|
|
|
|
|
test "it does not monitor more than the maximum number of slots":
|
|
|
|
for _ in 0..<maxSlots + 1:
|
|
|
|
let slot = Slot.example
|
2024-02-07 06:50:35 +00:00
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
2023-04-19 13:06:00 +00:00
|
|
|
check validation.slots.len == maxSlots
|