2024-10-14 04:32:53 +00:00
|
|
|
from std/times import inMilliseconds, initDuration, inSeconds, fromUnix
|
2024-10-14 22:27:34 +00:00
|
|
|
import std/sets
|
2024-10-13 23:50:56 +00:00
|
|
|
import pkg/codex/logutils
|
|
|
|
import ../contracts/time
|
|
|
|
import ../contracts/deployment
|
|
|
|
import ../codex/helpers
|
|
|
|
import ../examples
|
|
|
|
import ./marketplacesuite
|
|
|
|
import ./nodeconfigs
|
|
|
|
|
|
|
|
export logutils
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "integration test validation"
|
|
|
|
|
2024-10-15 01:11:46 +00:00
|
|
|
marketplacesuite "Validation":
|
2024-10-14 22:27:34 +00:00
|
|
|
let nodes = 3
|
|
|
|
let tolerance = 1
|
|
|
|
var slotsFilled: seq[SlotId]
|
|
|
|
var slotsFreed: seq[SlotId]
|
|
|
|
|
|
|
|
proc trackSlotsFilled(marketplace: Marketplace):
|
|
|
|
Future[provider.Subscription] {.async.} =
|
|
|
|
slotsFilled = newSeq[SlotId]()
|
|
|
|
proc onSlotFilled(event: SlotFilled) =
|
|
|
|
let slotId = slotId(event.requestId, event.slotIndex)
|
|
|
|
slotsFilled.add(slotId)
|
|
|
|
debug "SlotFilled", requestId = event.requestId, slotIndex = event.slotIndex,
|
|
|
|
slotId = slotId
|
2024-10-13 23:50:56 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
let subscription = await marketplace.subscribe(SlotFilled, onSlotFilled)
|
|
|
|
subscription
|
|
|
|
|
|
|
|
proc trackSlotsFreed(requestId: RequestId, marketplace: Marketplace):
|
|
|
|
Future[provider.Subscription] {.async.} =
|
|
|
|
slotsFreed = newSeq[SlotId]()
|
|
|
|
proc onSlotFreed(event: SlotFreed) =
|
|
|
|
if event.requestId == requestId:
|
|
|
|
let slotId = slotId(event.requestId, event.slotIndex)
|
|
|
|
slotsFreed.add(slotId)
|
|
|
|
debug "onSlotFreed", requestId = requestId, slotIndex = event.slotIndex,
|
|
|
|
slotId = slotId, slotsFreed = slotsFreed.len
|
|
|
|
|
|
|
|
let subscription = await marketplace.subscribe(SlotFreed, onSlotFreed)
|
|
|
|
subscription
|
|
|
|
|
|
|
|
proc checkSlotsFailed(slotsFilled: seq[SlotId],
|
|
|
|
slotsFreed: seq[SlotId], marketplace: Marketplace) {.async.} =
|
|
|
|
let slotsNotFreed = slotsFilled.filter(
|
|
|
|
slotId => not slotsFreed.contains(slotId)
|
|
|
|
).toHashSet
|
|
|
|
var slotsFailed = initHashSet[SlotId]()
|
|
|
|
for slotId in slotsFilled:
|
|
|
|
let state = await marketplace.slotState(slotId)
|
|
|
|
if state == SlotState.Failed:
|
|
|
|
slotsFailed.incl(slotId)
|
|
|
|
|
|
|
|
debug "slots failed", slotsFailed = slotsFailed, slotsNotFreed = slotsNotFreed
|
|
|
|
check slotsNotFreed == slotsFailed
|
|
|
|
|
|
|
|
test "validator marks proofs as missing when using validation groups", NodeConfigs(
|
2024-10-14 00:36:00 +00:00
|
|
|
# Uncomment to start Hardhat automatically, typically so logs can be inspected locally
|
|
|
|
hardhat:
|
|
|
|
HardhatConfig.none,
|
|
|
|
|
|
|
|
clients:
|
|
|
|
CodexConfigs.init(nodes=1)
|
|
|
|
# .debug() # uncomment to enable console log output
|
|
|
|
# .withLogFile() # uncomment to output log file to tests/integration/logs/<start_datetime> <suite_name>/<test_name>/<node_role>_<node_idx>.log
|
|
|
|
# .withLogTopics("node", "marketplace", "clock")
|
|
|
|
.some,
|
|
|
|
|
|
|
|
providers:
|
|
|
|
CodexConfigs.init(nodes=1)
|
|
|
|
.withSimulateProofFailures(idx=0, failEveryNProofs=1)
|
|
|
|
# .debug() # uncomment to enable console log output
|
|
|
|
# .withLogFile() # uncomment to output log file to tests/integration/logs/<start_datetime> <suite_name>/<test_name>/<node_role>_<node_idx>.log
|
|
|
|
# .withLogTopics("marketplace", "sales", "reservations", "node", "clock", "slotsbuilder")
|
2024-10-14 22:27:34 +00:00
|
|
|
.some,
|
|
|
|
|
|
|
|
validators:
|
|
|
|
CodexConfigs.init(nodes=2)
|
|
|
|
.withValidationGroups(groups = 2)
|
|
|
|
.withValidationGroupIndex(idx = 0, groupIndex = 0)
|
|
|
|
.withValidationGroupIndex(idx = 1, groupIndex = 1)
|
|
|
|
# .debug() # uncomment to enable console log output
|
|
|
|
# .withLogFile() # uncomment to output log file to tests/integration/logs/<start_datetime> <suite_name>/<test_name>/<node_role>_<node_idx>.log
|
|
|
|
# .withLogTopics("validator")
|
|
|
|
# .withLogTopics("validator", "integration", "ethers", "clock")
|
2024-10-14 00:36:00 +00:00
|
|
|
.some
|
|
|
|
):
|
|
|
|
let client0 = clients()[0].client
|
|
|
|
let expiry = 5.periods
|
|
|
|
let duration = expiry + 10.periods
|
|
|
|
|
|
|
|
let data = await RandomChunker.example(blocks=8)
|
2024-10-14 22:27:34 +00:00
|
|
|
|
|
|
|
# TODO: better value for data.len below. This TODO is also present in
|
|
|
|
# testproofs.nim - we may want to address it or remove the comment.
|
|
|
|
createAvailabilities(data.len * 2, duration)
|
|
|
|
|
|
|
|
let slotFilledSubscription = await trackSlotsFilled(marketplace)
|
2024-10-14 00:36:00 +00:00
|
|
|
|
|
|
|
let cid = client0.upload(data).get
|
|
|
|
|
|
|
|
let purchaseId = await client0.requestStorage(
|
|
|
|
cid,
|
|
|
|
expiry=expiry,
|
|
|
|
duration=duration,
|
2024-10-14 22:27:34 +00:00
|
|
|
nodes=nodes,
|
|
|
|
tolerance=tolerance,
|
2024-10-14 00:36:00 +00:00
|
|
|
proofProbability=1
|
|
|
|
)
|
|
|
|
let requestId = client0.requestId(purchaseId).get
|
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
check eventually(client0.purchaseStateIs(purchaseId, "started"),
|
2024-10-14 00:36:00 +00:00
|
|
|
timeout = expiry.int * 1000)
|
2024-10-14 22:27:34 +00:00
|
|
|
|
|
|
|
let slotFreedSubscription =
|
|
|
|
await trackSlotsFreed(requestId, marketplace)
|
2024-10-14 00:36:00 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
let expectedSlotsFreed = nodes - tolerance
|
|
|
|
check eventually((slotsFreed.len == expectedSlotsFreed),
|
|
|
|
timeout=(duration - expiry).int * 1000)
|
2024-10-14 00:36:00 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
# Because of erasure coding, if e.g. 2 out of 3 nodes are freed, the last
|
|
|
|
# node will not be freed but marked as "Failed" because the whole request
|
|
|
|
# will fail. For this reason we need an extra check:
|
|
|
|
await checkSlotsFailed(slotsFilled, slotsFreed, marketplace)
|
2024-10-14 00:36:00 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
await slotFilledSubscription.unsubscribe()
|
|
|
|
await slotFreedSubscription.unsubscribe()
|
2024-10-14 04:32:53 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
test "validator uses historical state to mark missing proofs", NodeConfigs(
|
2024-10-14 04:32:53 +00:00
|
|
|
# Uncomment to start Hardhat automatically, typically so logs can be inspected locally
|
|
|
|
hardhat:
|
|
|
|
HardhatConfig.none,
|
|
|
|
|
|
|
|
clients:
|
|
|
|
CodexConfigs.init(nodes=1)
|
|
|
|
# .debug() # uncomment to enable console log output
|
|
|
|
# .withLogFile() # uncomment to output log file to tests/integration/logs/<start_datetime> <suite_name>/<test_name>/<node_role>_<node_idx>.log
|
|
|
|
# .withLogTopics("node", "marketplace", "clock")
|
|
|
|
.some,
|
|
|
|
|
|
|
|
providers:
|
|
|
|
CodexConfigs.init(nodes=1)
|
|
|
|
.withSimulateProofFailures(idx=0, failEveryNProofs=1)
|
|
|
|
# .debug() # uncomment to enable console log output
|
|
|
|
# .withLogFile() # uncomment to output log file to tests/integration/logs/<start_datetime> <suite_name>/<test_name>/<node_role>_<node_idx>.log
|
|
|
|
# .withLogTopics("marketplace", "sales", "reservations", "node", "clock", "slotsbuilder")
|
|
|
|
.some
|
|
|
|
):
|
|
|
|
let client0 = clients()[0].client
|
|
|
|
let expiry = 5.periods
|
2024-10-14 22:27:34 +00:00
|
|
|
let duration = expiry + 10.periods
|
2024-10-14 04:32:53 +00:00
|
|
|
|
|
|
|
let data = await RandomChunker.example(blocks=8)
|
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
# TODO: better value for data.len below. This TODO is also present in
|
|
|
|
# testproofs.nim - we may want to address it or remove the comment.
|
|
|
|
createAvailabilities(data.len * 2, duration)
|
2024-10-14 04:32:53 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
let slotFilledSubscription = await trackSlotsFilled(marketplace)
|
2024-10-14 04:32:53 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
let cid = client0.upload(data).get
|
2024-10-14 04:32:53 +00:00
|
|
|
|
|
|
|
let purchaseId = await client0.requestStorage(
|
|
|
|
cid,
|
|
|
|
expiry=expiry,
|
|
|
|
duration=duration,
|
2024-10-14 22:27:34 +00:00
|
|
|
nodes=nodes,
|
|
|
|
tolerance=tolerance,
|
2024-10-14 04:32:53 +00:00
|
|
|
proofProbability=1
|
|
|
|
)
|
|
|
|
let requestId = client0.requestId(purchaseId).get
|
|
|
|
|
|
|
|
check eventually(client0.purchaseStateIs(purchaseId, "started"),
|
|
|
|
timeout = expiry.int * 1000)
|
2024-10-14 22:27:34 +00:00
|
|
|
|
|
|
|
# just to make sure we have a mined block that separates us
|
|
|
|
# from the block containing the last SlotFilled event
|
2024-10-14 04:32:53 +00:00
|
|
|
discard await ethProvider.send("evm_mine")
|
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
var validators = CodexConfigs.init(nodes=2)
|
|
|
|
.withValidationGroups(groups = 2)
|
|
|
|
.withValidationGroupIndex(idx = 0, groupIndex = 0)
|
|
|
|
.withValidationGroupIndex(idx = 1, groupIndex = 1)
|
|
|
|
# .debug() # uncomment to enable console log output
|
|
|
|
# .withLogFile() # uncomment to output log file to:
|
|
|
|
# tests/integration/logs/<start_datetime> <suite_name>/<test_name>/<node_role>_<node_idx>.log
|
|
|
|
# .withLogTopics("validator") # each topic as a separate string argument
|
2024-10-14 04:32:53 +00:00
|
|
|
|
|
|
|
failAndTeardownOnError "failed to start validator nodes":
|
|
|
|
for config in validators.configs.mitems:
|
|
|
|
let node = await startValidatorNode(config)
|
|
|
|
running.add RunningNode(
|
|
|
|
role: Role.Validator,
|
|
|
|
node: node
|
|
|
|
)
|
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
let slotFreedSubscription =
|
|
|
|
await trackSlotsFreed(requestId, marketplace)
|
2024-10-14 04:32:53 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
let expectedSlotsFreed = nodes - tolerance
|
|
|
|
check eventually((slotsFreed.len == expectedSlotsFreed),
|
|
|
|
timeout=(duration - expiry).int * 1000)
|
2024-10-14 04:32:53 +00:00
|
|
|
|
2024-10-14 22:27:34 +00:00
|
|
|
# Because of erasure coding, if e.g. 2 out of 3 nodes are freed, the last
|
|
|
|
# node will not be freed but marked as "Failed" because the whole request
|
|
|
|
# will fail. For this reason we need an extra check:
|
|
|
|
await checkSlotsFailed(slotsFilled, slotsFreed, marketplace)
|
|
|
|
|
|
|
|
await slotFilledSubscription.unsubscribe()
|
|
|
|
await slotFreedSubscription.unsubscribe()
|