2022-04-13 16:41:48 +00:00
|
|
|
import pkg/ethers
|
2022-09-20 02:10:42 +00:00
|
|
|
import pkg/ethers/testing
|
2022-05-24 05:24:15 +00:00
|
|
|
import ../storageproofs/timing/proofs
|
2022-04-13 16:41:48 +00:00
|
|
|
import ./storage
|
|
|
|
|
|
|
|
export proofs
|
|
|
|
|
|
|
|
type
|
|
|
|
OnChainProofs* = ref object of Proofs
|
|
|
|
storage: Storage
|
|
|
|
pollInterval*: Duration
|
|
|
|
ProofsSubscription = proofs.Subscription
|
|
|
|
EventSubscription = ethers.Subscription
|
|
|
|
OnChainProofsSubscription = ref object of ProofsSubscription
|
|
|
|
eventSubscription: EventSubscription
|
|
|
|
|
|
|
|
const DefaultPollInterval = 3.seconds
|
|
|
|
|
|
|
|
proc new*(_: type OnChainProofs, storage: Storage): OnChainProofs =
|
|
|
|
OnChainProofs(storage: storage, pollInterval: DefaultPollInterval)
|
|
|
|
|
|
|
|
method periodicity*(proofs: OnChainProofs): Future[Periodicity] {.async.} =
|
|
|
|
let period = await proofs.storage.proofPeriod()
|
|
|
|
return Periodicity(seconds: period)
|
|
|
|
|
|
|
|
method isProofRequired*(proofs: OnChainProofs,
|
2022-08-17 02:29:44 +00:00
|
|
|
id: SlotId): Future[bool] {.async.} =
|
2022-09-13 06:33:04 +00:00
|
|
|
try:
|
|
|
|
return await proofs.storage.isProofRequired(id)
|
2022-09-19 06:38:23 +00:00
|
|
|
except JsonRpcProviderError as e:
|
|
|
|
if e.revertReason == "Slot empty":
|
|
|
|
return false
|
|
|
|
raise e
|
2022-04-13 16:41:48 +00:00
|
|
|
|
|
|
|
method willProofBeRequired*(proofs: OnChainProofs,
|
2022-08-17 02:29:44 +00:00
|
|
|
id: SlotId): Future[bool] {.async.} =
|
2022-09-13 06:33:04 +00:00
|
|
|
try:
|
|
|
|
return await proofs.storage.willProofBeRequired(id)
|
2022-09-20 02:10:42 +00:00
|
|
|
except JsonRpcProviderError as e:
|
2022-09-19 06:38:23 +00:00
|
|
|
if e.revertReason == "Slot empty":
|
|
|
|
return false
|
|
|
|
raise e
|
2022-04-13 16:41:48 +00:00
|
|
|
|
|
|
|
method getProofEnd*(proofs: OnChainProofs,
|
2022-08-17 02:29:44 +00:00
|
|
|
id: SlotId): Future[UInt256] {.async.} =
|
2022-09-13 06:33:04 +00:00
|
|
|
try:
|
|
|
|
return await proofs.storage.proofEnd(id)
|
|
|
|
except ValueError:
|
|
|
|
return 0.u256
|
2022-04-13 16:41:48 +00:00
|
|
|
|
|
|
|
method submitProof*(proofs: OnChainProofs,
|
2022-08-17 02:29:44 +00:00
|
|
|
id: SlotId,
|
2022-04-13 16:41:48 +00:00
|
|
|
proof: seq[byte]) {.async.} =
|
|
|
|
await proofs.storage.submitProof(id, proof)
|
|
|
|
|
|
|
|
method subscribeProofSubmission*(proofs: OnChainProofs,
|
|
|
|
callback: OnProofSubmitted):
|
|
|
|
Future[ProofsSubscription] {.async.} =
|
|
|
|
proc onEvent(event: ProofSubmitted) {.upraises: [].} =
|
|
|
|
callback(event.id, event.proof)
|
|
|
|
let subscription = await proofs.storage.subscribe(ProofSubmitted, onEvent)
|
|
|
|
return OnChainProofsSubscription(eventSubscription: subscription)
|
|
|
|
|
|
|
|
method unsubscribe*(subscription: OnChainProofsSubscription) {.async, upraises:[].} =
|
|
|
|
await subscription.eventSubscription.unsubscribe()
|