mirror of
https://github.com/status-im/nim-codex.git
synced 2025-01-11 03:16:16 +00:00
372f827982
Add or remove proof requirements when a request contract’s state changes. When a request sale has completed (for a slot), the host who purchased that slot now must provide regular proofs for the data they are contracted to hold. This is now enforced by adding the slotId to the HashSet of Ids for which to require proofs. When a request has been cancelled (not all slots were filled before the request expired), proofs no longer need to be provided and the slotId is removed from teh HashSet. Add `isCancelled` and `isSlotCancelled` checks to query the contract state without relying the on the state context variable in the contract. Because contract state can only be updated in a transaction, and the client withdrawing funds is responsible for changing the contract state to “Cancelled”, the `isCancelled` and `isSlotCancelled` functions were introduced to check the state regardless of whether or not the client had already withdrawn their funds.
133 lines
3.7 KiB
Nim
133 lines
3.7 KiB
Nim
import pkg/asynctest
|
|
import pkg/chronos
|
|
import pkg/codex/proving
|
|
import ./helpers/mockproofs
|
|
import ./helpers/mockclock
|
|
import ./examples
|
|
|
|
suite "Proving":
|
|
|
|
var proving: Proving
|
|
var proofs: MockProofs
|
|
var clock: MockClock
|
|
|
|
setup:
|
|
proofs = MockProofs.new()
|
|
clock = MockClock.new()
|
|
proving = Proving.new(proofs, clock)
|
|
await proving.start()
|
|
|
|
teardown:
|
|
await proving.stop()
|
|
|
|
proc advanceToNextPeriod(proofs: MockProofs) {.async.} =
|
|
let periodicity = await proofs.periodicity()
|
|
clock.advance(periodicity.seconds.truncate(int64))
|
|
await sleepAsync(2.seconds)
|
|
|
|
test "maintains a list of contract ids to watch":
|
|
let id1, id2 = SlotId.example
|
|
check proving.slots.len == 0
|
|
proving.add(id1)
|
|
check proving.slots.contains(id1)
|
|
proving.add(id2)
|
|
check proving.slots.contains(id1)
|
|
check proving.slots.contains(id2)
|
|
|
|
test "removes duplicate contract ids":
|
|
let id = SlotId.example
|
|
proving.add(id)
|
|
proving.add(id)
|
|
check proving.slots.len == 1
|
|
|
|
test "invokes callback when proof is required":
|
|
let id = SlotId.example
|
|
proving.add(id)
|
|
var called: bool
|
|
proc onProofRequired(id: SlotId) =
|
|
called = true
|
|
proving.onProofRequired = onProofRequired
|
|
proofs.setProofRequired(id, true)
|
|
await proofs.advanceToNextPeriod()
|
|
check called
|
|
|
|
test "callback receives id of contract for which proof is required":
|
|
let id1, id2 = SlotId.example
|
|
proving.add(id1)
|
|
proving.add(id2)
|
|
var callbackIds: seq[SlotId]
|
|
proc onProofRequired(id: SlotId) =
|
|
callbackIds.add(id)
|
|
proving.onProofRequired = onProofRequired
|
|
proofs.setProofRequired(id1, true)
|
|
await proofs.advanceToNextPeriod()
|
|
check callbackIds == @[id1]
|
|
proofs.setProofRequired(id1, false)
|
|
proofs.setProofRequired(id2, true)
|
|
await proofs.advanceToNextPeriod()
|
|
check callbackIds == @[id1, id2]
|
|
|
|
test "invokes callback when proof is about to be required":
|
|
let id = SlotId.example
|
|
proving.add(id)
|
|
var called: bool
|
|
proc onProofRequired(id: SlotId) =
|
|
called = true
|
|
proving.onProofRequired = onProofRequired
|
|
proofs.setProofRequired(id, false)
|
|
proofs.setProofToBeRequired(id, true)
|
|
await proofs.advanceToNextPeriod()
|
|
check called
|
|
|
|
test "stops watching when contract has ended":
|
|
let id = SlotId.example
|
|
proving.add(id)
|
|
proofs.setProofEnd(id, clock.now().u256)
|
|
await proofs.advanceToNextPeriod()
|
|
var called: bool
|
|
proc onProofRequired(id: SlotId) =
|
|
called = true
|
|
proving.onProofRequired = onProofRequired
|
|
proofs.setProofRequired(id, true)
|
|
await proofs.advanceToNextPeriod()
|
|
check not called
|
|
|
|
test "stops watching when contract is cancelled":
|
|
let id = ContractId.example
|
|
proving.add(id)
|
|
var called: bool
|
|
proc onProofRequired(id: ContractId) =
|
|
called = true
|
|
proofs.setProofRequired(id, true)
|
|
await proofs.advanceToNextPeriod()
|
|
proving.onProofRequired = onProofRequired
|
|
proofs.setCancelled(id, true)
|
|
await proofs.advanceToNextPeriod()
|
|
check not proving.contracts.contains(id)
|
|
check not called
|
|
|
|
test "submits proofs":
|
|
let id = SlotId.example
|
|
let proof = seq[byte].example
|
|
await proving.submitProof(id, proof)
|
|
|
|
test "supports proof submission subscriptions":
|
|
let id = SlotId.example
|
|
let proof = seq[byte].example
|
|
|
|
var receivedIds: seq[SlotId]
|
|
var receivedProofs: seq[seq[byte]]
|
|
|
|
proc onProofSubmission(id: SlotId, proof: seq[byte]) =
|
|
receivedIds.add(id)
|
|
receivedProofs.add(proof)
|
|
|
|
let subscription = await proving.subscribeProofSubmission(onProofSubmission)
|
|
|
|
await proving.submitProof(id, proof)
|
|
|
|
check receivedIds == @[id]
|
|
check receivedProofs == @[proof]
|
|
|
|
await subscription.unsubscribe()
|