mirror of
https://github.com/status-im/nim-dagger.git
synced 2025-02-05 03:15:10 +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.
89 lines
2.8 KiB
Nim
89 lines
2.8 KiB
Nim
import std/sets
|
|
import std/tables
|
|
import std/sequtils
|
|
import pkg/upraises
|
|
import pkg/codex/storageproofs
|
|
|
|
type
|
|
MockProofs* = ref object of Proofs
|
|
periodicity: Periodicity
|
|
cancelledRequests: HashSet[ContractId]
|
|
proofsRequired: HashSet[SlotId]
|
|
proofsToBeRequired: HashSet[SlotId]
|
|
proofEnds: Table[SlotId, UInt256]
|
|
subscriptions: seq[MockSubscription]
|
|
MockSubscription* = ref object of Subscription
|
|
proofs: MockProofs
|
|
callback: OnProofSubmitted
|
|
|
|
const DefaultPeriodLength = 10.u256
|
|
|
|
func new*(_: type MockProofs): MockProofs =
|
|
MockProofs(periodicity: Periodicity(seconds: DefaultPeriodLength))
|
|
|
|
func setPeriodicity*(mock: MockProofs, periodicity: Periodicity) =
|
|
mock.periodicity = periodicity
|
|
|
|
method periodicity*(mock: MockProofs): Future[Periodicity] {.async.} =
|
|
return mock.periodicity
|
|
|
|
proc setProofRequired*(mock: MockProofs, id: SlotId, required: bool) =
|
|
if required:
|
|
mock.proofsRequired.incl(id)
|
|
else:
|
|
mock.proofsRequired.excl(id)
|
|
|
|
proc setCancelled*(mock: MockProofs, id: ContractId, required: bool) =
|
|
if required:
|
|
mock.cancelledRequests.incl(id)
|
|
else:
|
|
mock.cancelledRequests.excl(id)
|
|
|
|
method isCancelled*(mock: MockProofs,
|
|
id: array[32, byte]): Future[bool] {.async.} =
|
|
return mock.cancelledRequests.contains(id)
|
|
|
|
method isSlotCancelled*(mock: MockProofs,
|
|
id: ContractId): Future[bool] {.async.} =
|
|
return mock.cancelledRequests.contains(id)
|
|
|
|
method isProofRequired*(mock: MockProofs,
|
|
id: SlotId): Future[bool] {.async.} =
|
|
return mock.proofsRequired.contains(id)
|
|
|
|
proc setProofToBeRequired*(mock: MockProofs, id: SlotId, required: bool) =
|
|
if required:
|
|
mock.proofsToBeRequired.incl(id)
|
|
else:
|
|
mock.proofsToBeRequired.excl(id)
|
|
|
|
method willProofBeRequired*(mock: MockProofs,
|
|
id: SlotId): Future[bool] {.async.} =
|
|
return mock.proofsToBeRequired.contains(id)
|
|
|
|
proc setProofEnd*(mock: MockProofs, id: SlotId, proofEnd: UInt256) =
|
|
mock.proofEnds[id] = proofEnd
|
|
|
|
method getProofEnd*(mock: MockProofs,
|
|
id: SlotId): Future[UInt256] {.async.} =
|
|
if mock.proofEnds.hasKey(id):
|
|
return mock.proofEnds[id]
|
|
else:
|
|
return UInt256.high
|
|
|
|
method submitProof*(mock: MockProofs,
|
|
id: SlotId,
|
|
proof: seq[byte]) {.async.} =
|
|
for subscription in mock.subscriptions:
|
|
subscription.callback(id, proof)
|
|
|
|
method subscribeProofSubmission*(mock: MockProofs,
|
|
callback: OnProofSubmitted):
|
|
Future[Subscription] {.async.} =
|
|
let subscription = MockSubscription(proofs: mock, callback: callback)
|
|
mock.subscriptions.add(subscription)
|
|
return subscription
|
|
|
|
method unsubscribe*(subscription: MockSubscription) {.async, upraises:[].} =
|
|
subscription.proofs.subscriptions.keepItIf(it != subscription)
|