2023-04-19 13:06:00 +00:00
|
|
|
import pkg/chronos
|
2024-10-02 22:00:40 +00:00
|
|
|
import std/strformat
|
|
|
|
import std/random
|
2023-04-19 13:06:00 +00:00
|
|
|
|
|
|
|
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
|
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 validation: Validation
|
|
|
|
var market: MockMarket
|
|
|
|
var clock: MockClock
|
2024-10-02 22:00:40 +00:00
|
|
|
var groupIndex: uint16
|
|
|
|
|
|
|
|
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
|
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
|
|
|
market = MockMarket.new()
|
|
|
|
clock = MockClock.new()
|
2024-10-02 22:00:40 +00:00
|
|
|
let validationConfig = initValidationConfig(
|
|
|
|
maxSlots, validationGroups, groupIndex)
|
|
|
|
validation = Validation.new(clock, market, validationConfig)
|
2023-04-19 13:06:00 +00:00
|
|
|
market.config.proofs.period = period.u256
|
|
|
|
market.config.proofs.timeout = timeout.u256
|
|
|
|
await validation.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
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
|
|
|
|
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":
|
|
|
|
let validationConfig = initValidationConfig(maxSlots, validationGroups,
|
|
|
|
(groupIndex + 1) mod uint16(!validationGroups))
|
|
|
|
let validation = Validation.new(clock, market, validationConfig)
|
|
|
|
await validation.start()
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
await validation.stop()
|
|
|
|
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-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":
|
|
|
|
let validationConfig = initValidationConfig(
|
|
|
|
maxSlots = 0, ValidationGroups.none)
|
|
|
|
let validation = Validation.new(clock, market, validationConfig)
|
|
|
|
await validation.start()
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
await validation.stop()
|
|
|
|
check validation.slots == @[slot.id]
|
|
|
|
|
|
|
|
test "slot should be observed if validation group is not set (and " &
|
|
|
|
"maxSlots is not 0)":
|
|
|
|
let validationConfig = initValidationConfig(
|
|
|
|
maxSlots, ValidationGroups.none)
|
|
|
|
let validation = Validation.new(clock, market, validationConfig)
|
|
|
|
await validation.start()
|
|
|
|
await market.fillSlot(slot.request.id, slot.slotIndex, proof, collateral)
|
|
|
|
await validation.stop()
|
|
|
|
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-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()
|
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-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-10-02 22:00:40 +00:00
|
|
|
let validationGroups = ValidationGroups.none
|
|
|
|
let validationConfig = initValidationConfig(
|
|
|
|
maxSlots, validationGroups)
|
|
|
|
let validation = Validation.new(clock, market, validationConfig)
|
|
|
|
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)
|
2024-10-02 22:00:40 +00:00
|
|
|
await validation.stop()
|
2023-04-19 13:06:00 +00:00
|
|
|
check validation.slots.len == maxSlots
|