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
|
2022-04-13 16:41:48 +00:00
|
|
|
import ./helpers/mockproofs
|
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
|
|
|
|
|
|
|
|
suite "Proving":
|
|
|
|
|
|
|
|
var proving: Proving
|
2022-04-13 16:41:48 +00:00
|
|
|
var proofs: MockProofs
|
2022-05-17 14:42:03 +00:00
|
|
|
var clock: MockClock
|
2022-04-08 21:58:16 +00:00
|
|
|
|
|
|
|
setup:
|
2022-04-13 16:41:48 +00:00
|
|
|
proofs = MockProofs.new()
|
2022-05-17 14:42:03 +00:00
|
|
|
clock = MockClock.new()
|
|
|
|
proving = Proving.new(proofs, 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
|
|
|
|
2022-04-13 16:41:48 +00:00
|
|
|
proc advanceToNextPeriod(proofs: MockProofs) {.async.} =
|
2022-05-18 09:18:25 +00:00
|
|
|
let periodicity = await proofs.periodicity()
|
|
|
|
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
|
|
|
|
proofs.setSlotState(slot.id, SlotState.Filled)
|
|
|
|
proofs.setProofRequired(slot.id, true)
|
2022-04-13 16:41:48 +00:00
|
|
|
await proofs.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
|
|
|
|
proofs.setSlotState(slot1.id, SlotState.Filled)
|
|
|
|
proofs.setSlotState(slot2.id, SlotState.Filled)
|
|
|
|
proofs.setProofRequired(slot1.id, true)
|
2022-04-13 16:41:48 +00:00
|
|
|
await proofs.advanceToNextPeriod()
|
2023-03-27 13:47:25 +00:00
|
|
|
check eventually callbackSlots == @[slot1]
|
|
|
|
proofs.setProofRequired(slot1.id, false)
|
|
|
|
proofs.setProofRequired(slot2.id, true)
|
2022-04-13 16:41:48 +00:00
|
|
|
await proofs.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
|
|
|
|
proofs.setProofRequired(slot.id, false)
|
|
|
|
proofs.setProofToBeRequired(slot.id, true)
|
|
|
|
proofs.setSlotState(slot.id, SlotState.Filled)
|
2022-04-13 16:41:48 +00:00
|
|
|
await proofs.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)
|
|
|
|
proofs.setProofEnd(slot.id, clock.now().u256)
|
2022-04-13 16:41:48 +00:00
|
|
|
await proofs.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
|
|
|
|
proofs.setProofRequired(slot.id, true)
|
2022-04-13 16:41:48 +00:00
|
|
|
await proofs.advanceToNextPeriod()
|
2023-03-27 13:47:25 +00:00
|
|
|
proofs.setSlotState(slot.id, SlotState.Finished)
|
|
|
|
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)
|
|
|
|
proofs.setSlotState(slot.id, SlotState.Filled)
|
|
|
|
proofs.setProofRequired(slot.id, true)
|
|
|
|
await proofs.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()
|