2023-04-19 13:06:00 +00:00
|
|
|
import pkg/chronos
|
2024-10-02 22:00:40 +00:00
|
|
|
import std/strformat
|
2024-12-14 05:07:55 +00:00
|
|
|
import std/times
|
2023-04-19 13:06:00 +00:00
|
|
|
|
|
|
|
import codex/validation
|
2023-05-01 15:22:13 +00:00
|
|
|
import codex/periods
|
2024-12-14 05:07:55 +00:00
|
|
|
import codex/clock
|
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
|
|
|
|
2024-12-14 05:07:55 +00:00
|
|
|
logScope:
|
|
|
|
topics = "testValidation"
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "validation":
|
2023-04-19 13:06:00 +00:00
|
|
|
let period = 10
|
|
|
|
let timeout = 5
|
2024-10-02 22:00:40 +00:00
|
|
|
let maxSlots = MaxSlots(100)
|
|
|
|
let validationGroups = ValidationGroups(8).some
|
2023-04-19 13:06:00 +00:00
|
|
|
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 market: MockMarket
|
|
|
|
var clock: MockClock
|
2024-10-02 22:00:40 +00:00
|
|
|
var groupIndex: uint16
|
2024-12-14 05:07:55 +00:00
|
|
|
var validation: Validation
|
2024-10-02 22:00:40 +00:00
|
|
|
|
|
|
|
proc initValidationConfig(maxSlots: MaxSlots,
|
|
|
|
validationGroups: ?ValidationGroups,
|
|
|
|
groupIndex: uint16 = 0): ValidationConfig =
|
|
|
|
without validationConfig =? ValidationConfig.init(
|
|
|
|
maxSlots, groups=validationGroups, groupIndex), error:
|
|
|
|
raiseAssert fmt"Creating ValidationConfig failed! Error msg: {error.msg}"
|
|
|
|
validationConfig
|
2024-12-14 05:07:55 +00:00
|
|
|
|
|
|
|
proc newValidation(clock: Clock,
|
|
|
|
market: Market,
|
|
|
|
maxSlots: MaxSlots,
|
|
|
|
validationGroups: ?ValidationGroups,
|
|
|
|
groupIndex: uint16 = 0): Validation =
|
|
|
|
let validationConfig = initValidationConfig(
|
|
|
|
maxSlots, validationGroups, groupIndex)
|
|
|
|
Validation.new(clock, market, validationConfig)
|
2023-04-19 13:06:00 +00:00
|
|
|
|
|
|
|
setup:
|
2024-10-02 22:00:40 +00:00
|
|
|
groupIndex = groupIndexForSlotId(slot.id, !validationGroups)
|
2023-04-19 13:06:00 +00:00
|
|
|
clock = MockClock.new()
|
2024-12-14 05:07:55 +00:00
|
|
|
market = MockMarket.new(clock = Clock(clock).some)
|
2023-04-19 13:06:00 +00:00
|
|
|
market.config.proofs.period = period.u256
|
|
|
|
market.config.proofs.timeout = timeout.u256
|
2024-12-14 05:07:55 +00:00
|
|
|
validation = newValidation(
|
|
|
|
clock, market, maxSlots, validationGroups, groupIndex)
|
2023-04-19 13:06:00 +00:00
|
|
|
|
|
|
|
teardown:
|
2024-12-14 05:07:55 +00:00
|
|
|
# calling stop on validation that did not start is harmless
|
2023-04-19 13:06:00 +00:00
|
|
|
await validation.stop()
|
|
|
|
|
2024-02-16 22:12:16 +00:00
|
|
|
proc advanceToNextPeriod =
|
2023-05-01 15:22:13 +00:00
|
|
|
let periodicity = Periodicity(seconds: period.u256)
|
2024-02-16 22:12:16 +00:00
|
|
|
let period = periodicity.periodOf(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
|
|
|
|
|
2024-10-02 22:00:40 +00:00
|
|
|
for (validationGroups, groupIndex) in [(100, 100'u16), (100, 101'u16)]:
|
|
|
|
test "initializing ValidationConfig fails when groupIndex is " &
|
|
|
|
"greater than or equal to validationGroups " &
|
|
|
|
fmt"(testing for {groupIndex = }, {validationGroups = })":
|
|
|
|
let groups = ValidationGroups(validationGroups).some
|
|
|
|
let validationConfig = ValidationConfig.init(
|
|
|
|
maxSlots, groups = groups, groupIndex = groupIndex)
|
|
|
|
check validationConfig.isFailure == true
|
|
|
|
check validationConfig.error.msg == "The value of the group index " &
|
|
|
|
"must be less than validation groups! " &
|
|
|
|
fmt"(got: {groupIndex = }, groups = {!groups})"
|
2024-11-25 11:23:04 +00:00
|
|
|
|
2024-10-02 22:00:40 +00:00
|
|
|
test "initializing ValidationConfig fails when maxSlots is negative":
|
|
|
|
let maxSlots = -1
|
|
|
|
let validationConfig = ValidationConfig.init(
|
|
|
|
maxSlots = maxSlots, groups = ValidationGroups.none)
|
|
|
|
check validationConfig.isFailure == true
|
|
|
|
check validationConfig.error.msg == "The value of maxSlots must " &
|
|
|
|
fmt"be greater than or equal to 0! (got: {maxSlots})"
|
2024-11-25 11:23:04 +00:00
|
|
|
|
2024-10-02 22:00:40 +00:00
|
|
|
test "initializing ValidationConfig fails when maxSlots is negative " &
|
|
|
|
"(validationGroups set)":
|
|
|
|
let maxSlots = -1
|
2024-12-14 05:07:55 +00:00
|
|
|
let groupIndex = 0'u16
|
2024-10-02 22:00:40 +00:00
|
|
|
let validationConfig = ValidationConfig.init(
|
|
|
|
maxSlots = maxSlots, groups = validationGroups, groupIndex)
|
|
|
|
check validationConfig.isFailure == true
|
|
|
|
check validationConfig.error.msg == "The value of maxSlots must " &
|
|
|
|
fmt"be greater than or equal to 0! (got: {maxSlots})"
|
|
|
|
|
|
|
|
test "slot is not observed if it is not in the validation group":
|
2024-12-14 05:07:55 +00:00
|
|
|
validation = newValidation(clock, market, maxSlots, validationGroups,
|
|
|
|
(groupIndex + 1) mod uint16(!validationGroups))
|
2024-10-02 22:00:40 +00:00
|
|
|
await validation.start()
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
check validation.slots.len == 0
|
|
|
|
|
2023-04-19 13:06:00 +00:00
|
|
|
test "when a slot is filled on chain, it is added to the list":
|
2024-12-14 05:07:55 +00:00
|
|
|
await validation.start()
|
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]
|
2024-11-25 11:23:04 +00:00
|
|
|
|
2024-10-02 22:00:40 +00:00
|
|
|
test "slot should be observed if maxSlots is set to 0":
|
2024-12-14 05:07:55 +00:00
|
|
|
validation = newValidation(clock, market, maxSlots = 0, ValidationGroups.none)
|
2024-10-02 22:00:40 +00:00
|
|
|
await validation.start()
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
check validation.slots == @[slot.id]
|
|
|
|
|
|
|
|
test "slot should be observed if validation group is not set (and " &
|
|
|
|
"maxSlots is not 0)":
|
2024-12-14 05:07:55 +00:00
|
|
|
validation = newValidation(clock, market, maxSlots, ValidationGroups.none)
|
2024-10-02 22:00:40 +00:00
|
|
|
await validation.start()
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
check validation.slots == @[slot.id]
|
2023-04-19 13:06:00 +00:00
|
|
|
|
|
|
|
for state in [SlotState.Finished, SlotState.Failed]:
|
2024-10-02 22:00:40 +00:00
|
|
|
test fmt"when slot state changes to {state}, it is removed from the list":
|
2024-12-14 05:07:55 +00:00
|
|
|
validation = newValidation(clock, market, maxSlots, validationGroups)
|
|
|
|
await validation.start()
|
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-12-14 05:07:55 +00:00
|
|
|
await validation.start()
|
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()
|
2024-11-25 11:23:04 +00:00
|
|
|
await sleepAsync(100.millis) # allow validation loop to run
|
2023-04-19 13:06:00 +00:00
|
|
|
check market.markedAsMissingProofs.contains(slot.id)
|
|
|
|
|
|
|
|
test "when a proof can not be marked as missing, it will not be marked":
|
2024-12-14 05:07:55 +00:00
|
|
|
await validation.start()
|
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()
|
2024-11-25 11:23:04 +00:00
|
|
|
await sleepAsync(100.millis) # allow validation loop to run
|
2023-04-19 13:06:00 +00:00
|
|
|
check market.markedAsMissingProofs.len == 0
|
|
|
|
|
|
|
|
test "it does not monitor more than the maximum number of slots":
|
2024-12-14 05:07:55 +00:00
|
|
|
validation = newValidation(clock, market, maxSlots, ValidationGroups.none)
|
2024-10-02 22:00:40 +00:00
|
|
|
await validation.start()
|
2023-04-19 13:06:00 +00:00
|
|
|
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
|
2024-12-14 05:07:55 +00:00
|
|
|
|
|
|
|
suite "restoring historical state":
|
|
|
|
test "it retrieves the historical state " &
|
|
|
|
"for max 30 days in the past":
|
|
|
|
let earlySlot = Slot.example
|
|
|
|
await market.fillSlot(earlySlot.request.id, earlySlot.slotIndex, proof, collateral)
|
|
|
|
let fromTime = clock.now()
|
|
|
|
clock.set(fromTime + 1)
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
|
|
|
|
let duration: times.Duration = initDuration(days = 30)
|
|
|
|
clock.set(fromTime + duration.inSeconds + 1)
|
|
|
|
|
|
|
|
validation = newValidation(clock, market, maxSlots = 0,
|
|
|
|
ValidationGroups.none)
|
|
|
|
await validation.start()
|
|
|
|
|
|
|
|
check validation.slots == @[slot.id]
|
|
|
|
|
|
|
|
for state in [SlotState.Finished, SlotState.Failed]:
|
|
|
|
test "when restoring historical state, " &
|
|
|
|
fmt"it excludes slots in {state} state":
|
|
|
|
let slot1 = Slot.example
|
|
|
|
let slot2 = Slot.example
|
|
|
|
await market.fillSlot(slot1.request.id, slot1.slotIndex,
|
|
|
|
proof, collateral)
|
|
|
|
await market.fillSlot(slot2.request.id, slot2.slotIndex,
|
|
|
|
proof, collateral)
|
|
|
|
|
|
|
|
market.slotState[slot1.id] = state
|
|
|
|
|
|
|
|
validation = newValidation(clock, market, maxSlots = 0,
|
|
|
|
ValidationGroups.none)
|
|
|
|
await validation.start()
|
|
|
|
|
|
|
|
check validation.slots == @[slot2.id]
|
|
|
|
|
|
|
|
test "it does not monitor more than the maximum number of slots ":
|
|
|
|
for _ in 0..<maxSlots + 1:
|
|
|
|
let slot = Slot.example
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
validation = newValidation(clock, market, maxSlots, ValidationGroups.none)
|
|
|
|
await validation.start()
|
|
|
|
check validation.slots.len == maxSlots
|
|
|
|
|
|
|
|
test "slot is not observed if it is not in the validation group":
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
validation = newValidation(clock, market, maxSlots, validationGroups,
|
|
|
|
(groupIndex + 1) mod uint16(!validationGroups))
|
|
|
|
await validation.start()
|
|
|
|
check validation.slots.len == 0
|
|
|
|
|
|
|
|
test "slot should be observed if maxSlots is set to 0":
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
validation = newValidation(clock, market, maxSlots = 0, ValidationGroups.none)
|
|
|
|
await validation.start()
|
|
|
|
check validation.slots == @[slot.id]
|
|
|
|
|
|
|
|
test "slot should be observed if validation " &
|
|
|
|
"group is not set (and maxSlots is not 0)":
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
validation = newValidation(clock, market, maxSlots, ValidationGroups.none)
|
|
|
|
await validation.start()
|
|
|
|
check validation.slots == @[slot.id]
|