2023-06-22 10:32:18 +00:00
|
|
|
import std/sequtils
|
2022-04-08 21:58:16 +00:00
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/proving
|
2023-04-19 13:06:00 +00:00
|
|
|
import ./helpers/mockmarket
|
2022-05-17 14:42:03 +00:00
|
|
|
import ./helpers/mockclock
|
2022-11-08 07:10:17 +00:00
|
|
|
import ./helpers/eventually
|
2022-04-08 21:58:16 +00:00
|
|
|
import ./examples
|
2023-06-22 18:01:21 +00:00
|
|
|
import ./helpers
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Proving":
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
var proving: Proving
|
2023-04-19 13:06:00 +00:00
|
|
|
var market: MockMarket
|
2022-05-17 14:42:03 +00:00
|
|
|
var clock: MockClock
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
setup:
|
2023-04-19 13:06:00 +00:00
|
|
|
market = MockMarket.new()
|
2022-05-17 14:42:03 +00:00
|
|
|
clock = MockClock.new()
|
2023-04-19 13:06:00 +00:00
|
|
|
proving = Proving.new(market, clock)
|
2022-05-17 14:40:21 +00:00
|
|
|
await proving.start()
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
teardown:
|
2022-05-17 14:40:21 +00:00
|
|
|
await proving.stop()
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2023-04-19 13:06:00 +00:00
|
|
|
proc advanceToNextPeriod(market: MockMarket) {.async.} =
|
|
|
|
let periodicity = await market.periodicity()
|
2022-05-18 09:18:25 +00:00
|
|
|
clock.advance(periodicity.seconds.truncate(int64))
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
test "maintains a list of slots to watch":
|
|
|
|
let slot1, slot2 = Slot.example
|
2022-08-17 02:29:44 +00:00
|
|
|
check proving.slots.len == 0
|
2023-03-27 13:47:25 +00:00
|
|
|
proving.add(slot1)
|
|
|
|
check proving.slots.contains(slot1)
|
|
|
|
proving.add(slot2)
|
|
|
|
check proving.slots.contains(slot1)
|
|
|
|
check proving.slots.contains(slot2)
|
|
|
|
|
|
|
|
test "removes duplicate slots":
|
|
|
|
let slot = Slot.example
|
|
|
|
proving.add(slot)
|
|
|
|
proving.add(slot)
|
2022-08-17 02:29:44 +00:00
|
|
|
check proving.slots.len == 1
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
test "invokes callback when proof is required":
|
2023-03-27 13:47:25 +00:00
|
|
|
let slot = Slot.example
|
|
|
|
proving.add(slot)
|
2022-04-08 21:58:16 +00:00
|
|
|
var called: bool
|
2023-03-27 13:47:25 +00:00
|
|
|
proc onProve(slot: Slot): Future[seq[byte]] {.async.} =
|
2022-04-08 21:58:16 +00:00
|
|
|
called = true
|
2023-03-27 13:47:25 +00:00
|
|
|
proving.onProve = onProve
|
2023-04-19 13:06:00 +00:00
|
|
|
market.slotState[slot.id] = SlotState.Filled
|
|
|
|
market.setProofRequired(slot.id, true)
|
|
|
|
await market.advanceToNextPeriod()
|
2022-11-08 07:10:17 +00:00
|
|
|
check eventually called
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
test "callback receives slot for which proof is required":
|
|
|
|
let slot1, slot2 = Slot.example
|
|
|
|
proving.add(slot1)
|
|
|
|
proving.add(slot2)
|
|
|
|
var callbackSlots: seq[Slot]
|
|
|
|
proc onProve(slot: Slot): Future[seq[byte]] {.async.} =
|
|
|
|
callbackSlots.add(slot)
|
|
|
|
proving.onProve = onProve
|
2023-04-19 13:06:00 +00:00
|
|
|
market.slotState[slot1.id] = SlotState.Filled
|
|
|
|
market.slotState[slot2.id] = SlotState.Filled
|
|
|
|
market.setProofRequired(slot1.id, true)
|
|
|
|
await market.advanceToNextPeriod()
|
2023-03-27 13:47:25 +00:00
|
|
|
check eventually callbackSlots == @[slot1]
|
2023-04-19 13:06:00 +00:00
|
|
|
market.setProofRequired(slot1.id, false)
|
|
|
|
market.setProofRequired(slot2.id, true)
|
|
|
|
await market.advanceToNextPeriod()
|
2023-03-27 13:47:25 +00:00
|
|
|
check eventually callbackSlots == @[slot1, slot2]
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
test "invokes callback when proof is about to be required":
|
2023-03-27 13:47:25 +00:00
|
|
|
let slot = Slot.example
|
|
|
|
proving.add(slot)
|
2022-04-08 21:58:16 +00:00
|
|
|
var called: bool
|
2023-03-27 13:47:25 +00:00
|
|
|
proc onProve(slot: Slot): Future[seq[byte]] {.async.} =
|
2022-04-08 21:58:16 +00:00
|
|
|
called = true
|
2023-03-27 13:47:25 +00:00
|
|
|
proving.onProve = onProve
|
2023-04-19 13:06:00 +00:00
|
|
|
market.setProofRequired(slot.id, false)
|
|
|
|
market.setProofToBeRequired(slot.id, true)
|
|
|
|
market.slotState[slot.id] = SlotState.Filled
|
|
|
|
await market.advanceToNextPeriod()
|
2022-11-08 07:10:17 +00:00
|
|
|
check eventually called
|
2022-04-08 21:58:16 +00:00
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
test "stops watching when slot is finished":
|
|
|
|
let slot = Slot.example
|
|
|
|
proving.add(slot)
|
2023-04-19 13:06:00 +00:00
|
|
|
market.setProofEnd(slot.id, clock.now().u256)
|
|
|
|
await market.advanceToNextPeriod()
|
2022-04-08 21:58:16 +00:00
|
|
|
var called: bool
|
2023-03-27 13:47:25 +00:00
|
|
|
proc onProve(slot: Slot): Future[seq[byte]] {.async.} =
|
2022-04-08 21:58:16 +00:00
|
|
|
called = true
|
2023-03-27 13:47:25 +00:00
|
|
|
proving.onProve = onProve
|
2023-04-19 13:06:00 +00:00
|
|
|
market.setProofRequired(slot.id, true)
|
|
|
|
await market.advanceToNextPeriod()
|
|
|
|
market.slotState[slot.id] = SlotState.Finished
|
2023-03-27 13:47:25 +00:00
|
|
|
check eventually (not proving.slots.contains(slot))
|
2022-08-17 04:02:53 +00:00
|
|
|
check not called
|
|
|
|
|
2022-04-13 16:41:48 +00:00
|
|
|
test "submits proofs":
|
2023-03-27 13:47:25 +00:00
|
|
|
let slot = Slot.example
|
2022-11-08 07:10:17 +00:00
|
|
|
let proof = exampleProof()
|
2022-04-13 16:41:48 +00:00
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
proving.onProve = proc (slot: Slot): Future[seq[byte]] {.async.} =
|
|
|
|
return proof
|
2022-04-13 16:41:48 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
var receivedIds: seq[SlotId]
|
2022-04-13 16:41:48 +00:00
|
|
|
var receivedProofs: seq[seq[byte]]
|
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
proc onProofSubmission(id: SlotId, proof: seq[byte]) =
|
2022-04-13 16:41:48 +00:00
|
|
|
receivedIds.add(id)
|
|
|
|
receivedProofs.add(proof)
|
|
|
|
|
|
|
|
let subscription = await proving.subscribeProofSubmission(onProofSubmission)
|
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
proving.add(slot)
|
2023-04-19 13:06:00 +00:00
|
|
|
market.slotState[slot.id] = SlotState.Filled
|
|
|
|
market.setProofRequired(slot.id, true)
|
|
|
|
await market.advanceToNextPeriod()
|
2022-04-13 16:41:48 +00:00
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
check eventually receivedIds == @[slot.id] and receivedProofs == @[proof]
|
2022-04-13 16:41:48 +00:00
|
|
|
|
|
|
|
await subscription.unsubscribe()
|
2023-06-22 10:32:18 +00:00
|
|
|
|
|
|
|
suite "Simulated proving":
|
|
|
|
|
|
|
|
var proving: SimulatedProving
|
|
|
|
var subscription: Subscription
|
|
|
|
var market: MockMarket
|
|
|
|
var clock: MockClock
|
|
|
|
var submitted: seq[seq[byte]]
|
|
|
|
var proof: seq[byte]
|
|
|
|
let slot = Slot.example
|
|
|
|
var proofSubmitted: Future[void]
|
|
|
|
|
|
|
|
setup:
|
|
|
|
proof = exampleProof()
|
|
|
|
submitted = @[]
|
|
|
|
market = MockMarket.new()
|
|
|
|
clock = MockClock.new()
|
|
|
|
proofSubmitted = newFuture[void]("proofSubmitted")
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
await proving.stop()
|
|
|
|
|
|
|
|
proc newSimulatedProving(failEveryNProofs: uint) {.async.} =
|
|
|
|
proc onProofSubmission(id: SlotId, proof: seq[byte]) =
|
|
|
|
submitted.add(proof)
|
|
|
|
proofSubmitted.complete()
|
|
|
|
proofSubmitted = newFuture[void]("proofSubmitted")
|
|
|
|
|
|
|
|
proving = SimulatedProving.new(market, clock, failEveryNProofs)
|
|
|
|
proving.onProve = proc (slot: Slot): Future[seq[byte]] {.async.} =
|
|
|
|
return proof
|
|
|
|
subscription = await proving.subscribeProofSubmission(onProofSubmission)
|
|
|
|
proving.add(slot)
|
|
|
|
market.slotState[slot.id] = SlotState.Filled
|
|
|
|
market.setProofRequired(slot.id, true)
|
|
|
|
await proving.start()
|
|
|
|
|
|
|
|
proc advanceToNextPeriod(market: Market) {.async.} =
|
|
|
|
let periodicity = await market.periodicity()
|
|
|
|
clock.advance(periodicity.seconds.truncate(int64))
|
|
|
|
|
|
|
|
proc waitForProvingRounds(market: Market, rounds: uint) {.async.} =
|
|
|
|
var rnds = rounds - 1 # proof round runs prior to advancing
|
|
|
|
while rnds > 0:
|
|
|
|
await market.advanceToNextPeriod()
|
|
|
|
await proofSubmitted
|
|
|
|
rnds -= 1
|
|
|
|
|
|
|
|
test "submits invalid proof every 3 proofs":
|
|
|
|
let failEveryNProofs = 3'u
|
|
|
|
let totalProofs = 6'u
|
|
|
|
await newSimulatedProving(failEveryNProofs)
|
|
|
|
await market.waitForProvingRounds(totalProofs)
|
|
|
|
check submitted == @[proof, proof, @[], proof, proof, @[]]
|
|
|
|
|
|
|
|
test "does not submit invalid proofs when failEveryNProofs is 0":
|
|
|
|
let failEveryNProofs = 0'u
|
|
|
|
let totalProofs = 6'u
|
|
|
|
await newSimulatedProving(failEveryNProofs)
|
|
|
|
await market.waitForProvingRounds(totalProofs)
|
|
|
|
check submitted == proof.repeat(totalProofs)
|