[chore] clean up `array[32, byte]` types
- rename `ContractId` to `SlotId` - add `RequestId`, `PurchaseId`, `Nonce` types as aliases of `array[32, byte]` - rename `Proving.contracts` to `Proving.slots` - change signatures of `isSlotCancelled` and `isCancelled` to use `SlotId` and `RequestId` types, respectively. - change all references to `RequestId`, `SlotId`, and `PurchaseId`
This commit is contained in:
parent
4258734a81
commit
6df5a7cf54
|
@ -35,7 +35,7 @@ method requestStorage(market: OnChainMarket,
|
||||||
return request
|
return request
|
||||||
|
|
||||||
method getRequest(market: OnChainMarket,
|
method getRequest(market: OnChainMarket,
|
||||||
id: array[32, byte]): Future[?StorageRequest] {.async.} =
|
id: RequestId): Future[?StorageRequest] {.async.} =
|
||||||
let request = await market.contract.getRequest(id)
|
let request = await market.contract.getRequest(id)
|
||||||
if request != StorageRequest.default:
|
if request != StorageRequest.default:
|
||||||
return some request
|
return some request
|
||||||
|
@ -43,7 +43,7 @@ method getRequest(market: OnChainMarket,
|
||||||
return none StorageRequest
|
return none StorageRequest
|
||||||
|
|
||||||
method getHost(market: OnChainMarket,
|
method getHost(market: OnChainMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256): Future[?Address] {.async.} =
|
slotIndex: UInt256): Future[?Address] {.async.} =
|
||||||
let slotId = slotId(requestId, slotIndex)
|
let slotId = slotId(requestId, slotIndex)
|
||||||
let address = await market.contract.getHost(slotId)
|
let address = await market.contract.getHost(slotId)
|
||||||
|
@ -53,7 +53,7 @@ method getHost(market: OnChainMarket,
|
||||||
return none Address
|
return none Address
|
||||||
|
|
||||||
method fillSlot(market: OnChainMarket,
|
method fillSlot(market: OnChainMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256,
|
slotIndex: UInt256,
|
||||||
proof: seq[byte]) {.async.} =
|
proof: seq[byte]) {.async.} =
|
||||||
await market.contract.fillSlot(requestId, slotIndex, proof)
|
await market.contract.fillSlot(requestId, slotIndex, proof)
|
||||||
|
@ -67,7 +67,7 @@ method subscribeRequests(market: OnChainMarket,
|
||||||
return OnChainMarketSubscription(eventSubscription: subscription)
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
||||||
|
|
||||||
method subscribeSlotFilled*(market: OnChainMarket,
|
method subscribeSlotFilled*(market: OnChainMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256,
|
slotIndex: UInt256,
|
||||||
callback: OnSlotFilled):
|
callback: OnSlotFilled):
|
||||||
Future[MarketSubscription] {.async.} =
|
Future[MarketSubscription] {.async.} =
|
||||||
|
@ -78,7 +78,7 @@ method subscribeSlotFilled*(market: OnChainMarket,
|
||||||
return OnChainMarketSubscription(eventSubscription: subscription)
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
||||||
|
|
||||||
method subscribeFulfillment(market: OnChainMarket,
|
method subscribeFulfillment(market: OnChainMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
callback: OnFulfillment):
|
callback: OnFulfillment):
|
||||||
Future[MarketSubscription] {.async.} =
|
Future[MarketSubscription] {.async.} =
|
||||||
proc onEvent(event: RequestFulfilled) {.upraises:[].} =
|
proc onEvent(event: RequestFulfilled) {.upraises:[].} =
|
||||||
|
|
|
@ -23,19 +23,19 @@ method periodicity*(proofs: OnChainProofs): Future[Periodicity] {.async.} =
|
||||||
return Periodicity(seconds: period)
|
return Periodicity(seconds: period)
|
||||||
|
|
||||||
method isProofRequired*(proofs: OnChainProofs,
|
method isProofRequired*(proofs: OnChainProofs,
|
||||||
id: ContractId): Future[bool] {.async.} =
|
id: SlotId): Future[bool] {.async.} =
|
||||||
return await proofs.storage.isProofRequired(id)
|
return await proofs.storage.isProofRequired(id)
|
||||||
|
|
||||||
method willProofBeRequired*(proofs: OnChainProofs,
|
method willProofBeRequired*(proofs: OnChainProofs,
|
||||||
id: ContractId): Future[bool] {.async.} =
|
id: SlotId): Future[bool] {.async.} =
|
||||||
return await proofs.storage.willProofBeRequired(id)
|
return await proofs.storage.willProofBeRequired(id)
|
||||||
|
|
||||||
method getProofEnd*(proofs: OnChainProofs,
|
method getProofEnd*(proofs: OnChainProofs,
|
||||||
id: ContractId): Future[UInt256] {.async.} =
|
id: SlotId): Future[UInt256] {.async.} =
|
||||||
return await proofs.storage.proofEnd(id)
|
return await proofs.storage.proofEnd(id)
|
||||||
|
|
||||||
method submitProof*(proofs: OnChainProofs,
|
method submitProof*(proofs: OnChainProofs,
|
||||||
id: ContractId,
|
id: SlotId,
|
||||||
proof: seq[byte]) {.async.} =
|
proof: seq[byte]) {.async.} =
|
||||||
await proofs.storage.submitProof(id, proof)
|
await proofs.storage.submitProof(id, proof)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ type
|
||||||
ask*: StorageAsk
|
ask*: StorageAsk
|
||||||
content*: StorageContent
|
content*: StorageContent
|
||||||
expiry*: UInt256
|
expiry*: UInt256
|
||||||
nonce*: array[32, byte]
|
nonce*: Nonce
|
||||||
StorageAsk* = object
|
StorageAsk* = object
|
||||||
slots*: uint64
|
slots*: uint64
|
||||||
slotSize*: UInt256
|
slotSize*: UInt256
|
||||||
|
@ -28,6 +28,9 @@ type
|
||||||
u*: seq[byte]
|
u*: seq[byte]
|
||||||
publicKey*: seq[byte]
|
publicKey*: seq[byte]
|
||||||
name*: seq[byte]
|
name*: seq[byte]
|
||||||
|
SlotId* = array[32, byte]
|
||||||
|
RequestId* = array[32, byte]
|
||||||
|
Nonce* = array[32, byte]
|
||||||
|
|
||||||
func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest =
|
func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest =
|
||||||
StorageRequest(
|
StorageRequest(
|
||||||
|
@ -116,15 +119,15 @@ func decode*(decoder: var AbiDecoder, T: type StorageRequest): ?!T =
|
||||||
let tupl = ?decoder.read(StorageRequest.fieldTypes)
|
let tupl = ?decoder.read(StorageRequest.fieldTypes)
|
||||||
success StorageRequest.fromTuple(tupl)
|
success StorageRequest.fromTuple(tupl)
|
||||||
|
|
||||||
func id*(request: StorageRequest): array[32, byte] =
|
func id*(request: StorageRequest): RequestId =
|
||||||
let encoding = AbiEncoder.encode((request, ))
|
let encoding = AbiEncoder.encode((request, ))
|
||||||
keccak256.digest(encoding).data
|
keccak256.digest(encoding).data
|
||||||
|
|
||||||
func slotId*(requestId: array[32, byte], slot: UInt256): array[32, byte] =
|
func slotId*(requestId: RequestId, slot: UInt256): SlotId =
|
||||||
let encoding = AbiEncoder.encode((requestId, slot))
|
let encoding = AbiEncoder.encode((requestId, slot))
|
||||||
keccak256.digest(encoding).data
|
keccak256.digest(encoding).data
|
||||||
|
|
||||||
func slotId*(request: StorageRequest, slot: UInt256): array[32, byte] =
|
func slotId*(request: StorageRequest, slot: UInt256): SlotId =
|
||||||
slotId(request.id, slot)
|
slotId(request.id, slot)
|
||||||
|
|
||||||
func pricePerSlot*(ask: StorageAsk): UInt256 =
|
func pricePerSlot*(ask: StorageAsk): UInt256 =
|
||||||
|
|
|
@ -9,18 +9,18 @@ export ethers
|
||||||
|
|
||||||
type
|
type
|
||||||
Storage* = ref object of Contract
|
Storage* = ref object of Contract
|
||||||
Id = array[32, byte]
|
|
||||||
StorageRequested* = object of Event
|
StorageRequested* = object of Event
|
||||||
requestId*: Id
|
requestId*: RequestId
|
||||||
ask*: StorageAsk
|
ask*: StorageAsk
|
||||||
SlotFilled* = object of Event
|
SlotFilled* = object of Event
|
||||||
requestId* {.indexed.}: Id
|
requestId* {.indexed.}: RequestId
|
||||||
slotIndex* {.indexed.}: UInt256
|
slotIndex* {.indexed.}: UInt256
|
||||||
slotId* {.indexed.}: Id
|
slotId* {.indexed.}: SlotId
|
||||||
RequestFulfilled* = object of Event
|
RequestFulfilled* = object of Event
|
||||||
requestId* {.indexed.}: Id
|
requestId* {.indexed.}: RequestId
|
||||||
|
|
||||||
ProofSubmitted* = object of Event
|
ProofSubmitted* = object of Event
|
||||||
id*: Id
|
id*: SlotId
|
||||||
proof*: seq[byte]
|
proof*: seq[byte]
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,22 +33,20 @@ proc withdraw*(storage: Storage) {.contract.}
|
||||||
proc balanceOf*(storage: Storage, account: Address): UInt256 {.contract, view.}
|
proc balanceOf*(storage: Storage, account: Address): UInt256 {.contract, view.}
|
||||||
|
|
||||||
proc requestStorage*(storage: Storage, request: StorageRequest) {.contract.}
|
proc requestStorage*(storage: Storage, request: StorageRequest) {.contract.}
|
||||||
proc fillSlot*(storage: Storage, requestId: Id, slotIndex: UInt256, proof: seq[byte]) {.contract.}
|
proc fillSlot*(storage: Storage, requestId: RequestId, slotIndex: UInt256, proof: seq[byte]) {.contract.}
|
||||||
proc payoutSlot*(storage: Storage, requestId: Id, slotIndex: UInt256) {.contract.}
|
proc payoutSlot*(storage: Storage, requestId: RequestId, slotIndex: UInt256) {.contract.}
|
||||||
proc getRequest*(storage: Storage, id: Id): StorageRequest {.contract, view.}
|
proc getRequest*(storage: Storage, id: RequestId): StorageRequest {.contract, view.}
|
||||||
proc getHost*(storage: Storage, id: Id): Address {.contract, view.}
|
proc getHost*(storage: Storage, id: SlotId): Address {.contract, view.}
|
||||||
|
|
||||||
proc finishContract*(storage: Storage, id: Id) {.contract.}
|
|
||||||
|
|
||||||
proc proofPeriod*(storage: Storage): UInt256 {.contract, view.}
|
proc proofPeriod*(storage: Storage): UInt256 {.contract, view.}
|
||||||
proc proofTimeout*(storage: Storage): UInt256 {.contract, view.}
|
proc proofTimeout*(storage: Storage): UInt256 {.contract, view.}
|
||||||
|
|
||||||
proc proofEnd*(storage: Storage, id: Id): UInt256 {.contract, view.}
|
proc proofEnd*(storage: Storage, id: SlotId): UInt256 {.contract, view.}
|
||||||
proc missingProofs*(storage: Storage, id: Id): UInt256 {.contract, view.}
|
proc missingProofs*(storage: Storage, id: SlotId): UInt256 {.contract, view.}
|
||||||
proc isProofRequired*(storage: Storage, id: Id): bool {.contract, view.}
|
proc isProofRequired*(storage: Storage, id: SlotId): bool {.contract, view.}
|
||||||
proc willProofBeRequired*(storage: Storage, id: Id): bool {.contract, view.}
|
proc willProofBeRequired*(storage: Storage, id: SlotId): bool {.contract, view.}
|
||||||
proc getChallenge*(storage: Storage, id: Id): array[32, byte] {.contract, view.}
|
proc getChallenge*(storage: Storage, id: SlotId): array[32, byte] {.contract, view.}
|
||||||
proc getPointer*(storage: Storage, id: Id): uint8 {.contract, view.}
|
proc getPointer*(storage: Storage, id: SlotId): uint8 {.contract, view.}
|
||||||
|
|
||||||
proc submitProof*(storage: Storage, id: Id, proof: seq[byte]) {.contract.}
|
proc submitProof*(storage: Storage, id: SlotId, proof: seq[byte]) {.contract.}
|
||||||
proc markProofAsMissing*(storage: Storage, id: Id, period: UInt256) {.contract.}
|
proc markProofAsMissing*(storage: Storage, id: SlotId, period: UInt256) {.contract.}
|
||||||
|
|
|
@ -10,9 +10,9 @@ export requests
|
||||||
type
|
type
|
||||||
Market* = ref object of RootObj
|
Market* = ref object of RootObj
|
||||||
Subscription* = ref object of RootObj
|
Subscription* = ref object of RootObj
|
||||||
OnRequest* = proc(id: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].}
|
OnRequest* = proc(id: RequestId, ask: StorageAsk) {.gcsafe, upraises:[].}
|
||||||
OnFulfillment* = proc(requestId: array[32, byte]) {.gcsafe, upraises: [].}
|
OnFulfillment* = proc(requestId: RequestId) {.gcsafe, upraises: [].}
|
||||||
OnSlotFilled* = proc(requestId: array[32, byte], slotIndex: UInt256) {.gcsafe, upraises:[].}
|
OnSlotFilled* = proc(requestId: RequestId, slotIndex: UInt256) {.gcsafe, upraises:[].}
|
||||||
|
|
||||||
method getSigner*(market: Market): Future[Address] {.base, async.} =
|
method getSigner*(market: Market): Future[Address] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
@ -23,17 +23,17 @@ method requestStorage*(market: Market,
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method getRequest*(market: Market,
|
method getRequest*(market: Market,
|
||||||
id: array[32, byte]):
|
id: RequestId):
|
||||||
Future[?StorageRequest] {.base, async.} =
|
Future[?StorageRequest] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method getHost*(market: Market,
|
method getHost*(market: Market,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256): Future[?Address] {.base, async.} =
|
slotIndex: UInt256): Future[?Address] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method fillSlot*(market: Market,
|
method fillSlot*(market: Market,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256,
|
slotIndex: UInt256,
|
||||||
proof: seq[byte]) {.base, async.} =
|
proof: seq[byte]) {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
@ -44,13 +44,13 @@ method subscribeRequests*(market: Market,
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method subscribeFulfillment*(market: Market,
|
method subscribeFulfillment*(market: Market,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
callback: OnFulfillment):
|
callback: OnFulfillment):
|
||||||
Future[Subscription] {.base, async.} =
|
Future[Subscription] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method subscribeSlotFilled*(market: Market,
|
method subscribeSlotFilled*(market: Market,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256,
|
slotIndex: UInt256,
|
||||||
callback: OnSlotFilled):
|
callback: OnSlotFilled):
|
||||||
Future[Subscription] {.base, async.} =
|
Future[Subscription] {.base, async.} =
|
||||||
|
|
|
@ -228,7 +228,7 @@ proc requestStorage*(self: CodexNodeRef,
|
||||||
nodes: uint,
|
nodes: uint,
|
||||||
tolerance: uint,
|
tolerance: uint,
|
||||||
reward: UInt256,
|
reward: UInt256,
|
||||||
expiry = UInt256.none): Future[?!array[32, byte]] {.async.} =
|
expiry = UInt256.none): Future[?!PurchaseId] {.async.} =
|
||||||
## Initiate a request for storage sequence, this might
|
## Initiate a request for storage sequence, this might
|
||||||
## be a multistep procedure.
|
## be a multistep procedure.
|
||||||
##
|
##
|
||||||
|
|
|
@ -13,9 +13,9 @@ type
|
||||||
proofs: Proofs
|
proofs: Proofs
|
||||||
clock: Clock
|
clock: Clock
|
||||||
loop: ?Future[void]
|
loop: ?Future[void]
|
||||||
contracts*: HashSet[ContractId]
|
slots*: HashSet[SlotId]
|
||||||
onProofRequired: ?OnProofRequired
|
onProofRequired: ?OnProofRequired
|
||||||
OnProofRequired* = proc (id: ContractId) {.gcsafe, upraises:[].}
|
OnProofRequired* = proc (id: SlotId) {.gcsafe, upraises:[].}
|
||||||
|
|
||||||
func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving =
|
func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving =
|
||||||
Proving(proofs: proofs, clock: clock)
|
Proving(proofs: proofs, clock: clock)
|
||||||
|
@ -23,8 +23,8 @@ func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving =
|
||||||
proc `onProofRequired=`*(proving: Proving, callback: OnProofRequired) =
|
proc `onProofRequired=`*(proving: Proving, callback: OnProofRequired) =
|
||||||
proving.onProofRequired = some callback
|
proving.onProofRequired = some callback
|
||||||
|
|
||||||
func add*(proving: Proving, id: ContractId) =
|
func add*(proving: Proving, id: SlotId) =
|
||||||
proving.contracts.incl(id)
|
proving.slots.incl(id)
|
||||||
|
|
||||||
proc getCurrentPeriod(proving: Proving): Future[Period] {.async.} =
|
proc getCurrentPeriod(proving: Proving): Future[Period] {.async.} =
|
||||||
let periodicity = await proving.proofs.periodicity()
|
let periodicity = await proving.proofs.periodicity()
|
||||||
|
@ -36,18 +36,18 @@ proc waitUntilPeriod(proving: Proving, period: Period) {.async.} =
|
||||||
|
|
||||||
proc removeEndedContracts(proving: Proving) {.async.} =
|
proc removeEndedContracts(proving: Proving) {.async.} =
|
||||||
let now = proving.clock.now().u256
|
let now = proving.clock.now().u256
|
||||||
var ended: HashSet[ContractId]
|
var ended: HashSet[SlotId]
|
||||||
for id in proving.contracts:
|
for id in proving.slots:
|
||||||
if now >= (await proving.proofs.getProofEnd(id)):
|
if now >= (await proving.proofs.getProofEnd(id)):
|
||||||
ended.incl(id)
|
ended.incl(id)
|
||||||
proving.contracts.excl(ended)
|
proving.slots.excl(ended)
|
||||||
|
|
||||||
proc run(proving: Proving) {.async.} =
|
proc run(proving: Proving) {.async.} =
|
||||||
try:
|
try:
|
||||||
while true:
|
while true:
|
||||||
let currentPeriod = await proving.getCurrentPeriod()
|
let currentPeriod = await proving.getCurrentPeriod()
|
||||||
await proving.removeEndedContracts()
|
await proving.removeEndedContracts()
|
||||||
for id in proving.contracts:
|
for id in proving.slots:
|
||||||
if (await proving.proofs.isProofRequired(id)) or
|
if (await proving.proofs.isProofRequired(id)) or
|
||||||
(await proving.proofs.willProofBeRequired(id)):
|
(await proving.proofs.willProofBeRequired(id)):
|
||||||
if callback =? proving.onProofRequired:
|
if callback =? proving.onProofRequired:
|
||||||
|
@ -70,7 +70,7 @@ proc stop*(proving: Proving) {.async.} =
|
||||||
if not loop.finished:
|
if not loop.finished:
|
||||||
await loop.cancelAndWait()
|
await loop.cancelAndWait()
|
||||||
|
|
||||||
proc submitProof*(proving: Proving, id: ContractId, proof: seq[byte]) {.async.} =
|
proc submitProof*(proving: Proving, id: SlotId, proof: seq[byte]) {.async.} =
|
||||||
await proving.proofs.submitProof(id, proof)
|
await proving.proofs.submitProof(id, proof)
|
||||||
|
|
||||||
proc subscribeProofSubmission*(proving: Proving,
|
proc subscribeProofSubmission*(proving: Proving,
|
||||||
|
|
|
@ -13,7 +13,7 @@ type
|
||||||
Purchasing* = ref object
|
Purchasing* = ref object
|
||||||
market: Market
|
market: Market
|
||||||
clock: Clock
|
clock: Clock
|
||||||
purchases: Table[array[32, byte], Purchase]
|
purchases: Table[PurchaseId, Purchase]
|
||||||
proofProbability*: UInt256
|
proofProbability*: UInt256
|
||||||
requestExpiryInterval*: UInt256
|
requestExpiryInterval*: UInt256
|
||||||
Purchase* = ref object
|
Purchase* = ref object
|
||||||
|
@ -22,12 +22,13 @@ type
|
||||||
clock: Clock
|
clock: Clock
|
||||||
request*: StorageRequest
|
request*: StorageRequest
|
||||||
PurchaseTimeout* = Timeout
|
PurchaseTimeout* = Timeout
|
||||||
|
PurchaseId* = array[32, byte]
|
||||||
|
|
||||||
const DefaultProofProbability = 100.u256
|
const DefaultProofProbability = 100.u256
|
||||||
const DefaultRequestExpiryInterval = (10 * 60).u256
|
const DefaultRequestExpiryInterval = (10 * 60).u256
|
||||||
|
|
||||||
proc start(purchase: Purchase) {.gcsafe.}
|
proc start(purchase: Purchase) {.gcsafe.}
|
||||||
func id*(purchase: Purchase): array[32, byte]
|
func id*(purchase: Purchase): PurchaseId
|
||||||
|
|
||||||
proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
|
proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
|
||||||
Purchasing(
|
Purchasing(
|
||||||
|
@ -43,7 +44,7 @@ proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest
|
||||||
result.ask.proofProbability = purchasing.proofProbability
|
result.ask.proofProbability = purchasing.proofProbability
|
||||||
if result.expiry == 0.u256:
|
if result.expiry == 0.u256:
|
||||||
result.expiry = (purchasing.clock.now().u256 + purchasing.requestExpiryInterval)
|
result.expiry = (purchasing.clock.now().u256 + purchasing.requestExpiryInterval)
|
||||||
if result.nonce == array[32, byte].default:
|
if result.nonce == Nonce.default:
|
||||||
doAssert randomBytes(result.nonce) == 32
|
doAssert randomBytes(result.nonce) == 32
|
||||||
|
|
||||||
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
||||||
|
@ -57,7 +58,7 @@ proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
||||||
purchasing.purchases[purchase.id] = purchase
|
purchasing.purchases[purchase.id] = purchase
|
||||||
purchase
|
purchase
|
||||||
|
|
||||||
func getPurchase*(purchasing: Purchasing, id: array[32, byte]): ?Purchase =
|
func getPurchase*(purchasing: Purchasing, id: PurchaseId): ?Purchase =
|
||||||
if purchasing.purchases.hasKey(id):
|
if purchasing.purchases.hasKey(id):
|
||||||
some purchasing.purchases[id]
|
some purchasing.purchases[id]
|
||||||
else:
|
else:
|
||||||
|
@ -72,7 +73,7 @@ proc run(purchase: Purchase) {.async.} =
|
||||||
|
|
||||||
proc waitUntilFulfilled {.async.} =
|
proc waitUntilFulfilled {.async.} =
|
||||||
let done = newFuture[void]()
|
let done = newFuture[void]()
|
||||||
proc callback(_: array[32, byte]) =
|
proc callback(_: RequestId) =
|
||||||
done.complete()
|
done.complete()
|
||||||
let request = purchase.request
|
let request = purchase.request
|
||||||
let subscription = await market.subscribeFulfillment(request.id, callback)
|
let subscription = await market.subscribeFulfillment(request.id, callback)
|
||||||
|
@ -92,7 +93,7 @@ proc start(purchase: Purchase) =
|
||||||
proc wait*(purchase: Purchase) {.async.} =
|
proc wait*(purchase: Purchase) {.async.} =
|
||||||
await purchase.future
|
await purchase.future
|
||||||
|
|
||||||
func id*(purchase: Purchase): array[32, byte] =
|
func id*(purchase: Purchase): PurchaseId =
|
||||||
purchase.request.id
|
purchase.request.id
|
||||||
|
|
||||||
func finished*(purchase: Purchase): bool =
|
func finished*(purchase: Purchase): bool =
|
||||||
|
|
|
@ -305,7 +305,7 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
|
||||||
router.api(
|
router.api(
|
||||||
MethodGet,
|
MethodGet,
|
||||||
"/api/codex/v1/storage/purchases/{id}") do (
|
"/api/codex/v1/storage/purchases/{id}") do (
|
||||||
id: array[32, byte]) -> RestApiResponse:
|
id: PurchaseId) -> RestApiResponse:
|
||||||
|
|
||||||
without contracts =? node.contracts:
|
without contracts =? node.contracts:
|
||||||
return RestApiResponse.error(Http503, "Purchasing unavailable")
|
return RestApiResponse.error(Http503, "Purchasing unavailable")
|
||||||
|
|
|
@ -45,7 +45,7 @@ type
|
||||||
minPrice*: UInt256
|
minPrice*: UInt256
|
||||||
SalesAgent = ref object
|
SalesAgent = ref object
|
||||||
sales: Sales
|
sales: Sales
|
||||||
requestId: array[32, byte]
|
requestId: RequestId
|
||||||
ask: StorageAsk
|
ask: StorageAsk
|
||||||
availability: Availability
|
availability: Availability
|
||||||
request: ?StorageRequest
|
request: ?StorageRequest
|
||||||
|
@ -134,7 +134,7 @@ proc selectSlot(agent: SalesAgent) =
|
||||||
agent.slotIndex = some slotIndex.u256
|
agent.slotIndex = some slotIndex.u256
|
||||||
|
|
||||||
proc onSlotFilled(agent: SalesAgent,
|
proc onSlotFilled(agent: SalesAgent,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256) {.async.} =
|
slotIndex: UInt256) {.async.} =
|
||||||
try:
|
try:
|
||||||
let market = agent.sales.market
|
let market = agent.sales.market
|
||||||
|
@ -145,7 +145,7 @@ proc onSlotFilled(agent: SalesAgent,
|
||||||
agent.finish(success = false)
|
agent.finish(success = false)
|
||||||
|
|
||||||
proc subscribeSlotFilled(agent: SalesAgent, slotIndex: UInt256) {.async.} =
|
proc subscribeSlotFilled(agent: SalesAgent, slotIndex: UInt256) {.async.} =
|
||||||
proc onSlotFilled(requestId: array[32, byte],
|
proc onSlotFilled(requestId: RequestId,
|
||||||
slotIndex: UInt256) {.gcsafe, upraises:[].} =
|
slotIndex: UInt256) {.gcsafe, upraises:[].} =
|
||||||
asyncSpawn agent.onSlotFilled(requestId, slotIndex)
|
asyncSpawn agent.onSlotFilled(requestId, slotIndex)
|
||||||
let market = agent.sales.market
|
let market = agent.sales.market
|
||||||
|
@ -196,7 +196,7 @@ proc start(agent: SalesAgent) {.async.} =
|
||||||
error "SalesAgent failed", msg = e.msg
|
error "SalesAgent failed", msg = e.msg
|
||||||
agent.finish(success = false)
|
agent.finish(success = false)
|
||||||
|
|
||||||
proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
|
proc handleRequest(sales: Sales, requestId: RequestId, ask: StorageAsk) =
|
||||||
without availability =? sales.findAvailability(ask):
|
without availability =? sales.findAvailability(ask):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -212,7 +212,7 @@ proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
|
||||||
proc start*(sales: Sales) {.async.} =
|
proc start*(sales: Sales) {.async.} =
|
||||||
doAssert sales.subscription.isNone, "Sales already started"
|
doAssert sales.subscription.isNone, "Sales already started"
|
||||||
|
|
||||||
proc onRequest(requestId: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} =
|
proc onRequest(requestId: RequestId, ask: StorageAsk) {.gcsafe, upraises:[].} =
|
||||||
sales.handleRequest(requestId, ask)
|
sales.handleRequest(requestId, ask)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -2,35 +2,36 @@ import pkg/chronos
|
||||||
import pkg/stint
|
import pkg/stint
|
||||||
import pkg/upraises
|
import pkg/upraises
|
||||||
import ./periods
|
import ./periods
|
||||||
|
from ../../contracts/requests import SlotId, RequestId
|
||||||
|
|
||||||
export chronos
|
export chronos
|
||||||
export stint
|
export stint
|
||||||
export periods
|
export periods
|
||||||
|
export SlotId, RequestId
|
||||||
|
|
||||||
type
|
type
|
||||||
Proofs* = ref object of RootObj
|
Proofs* = ref object of RootObj
|
||||||
Subscription* = ref object of RootObj
|
Subscription* = ref object of RootObj
|
||||||
OnProofSubmitted* = proc(id: ContractId, proof: seq[byte]) {.gcsafe, upraises:[].}
|
OnProofSubmitted* = proc(id: SlotId, proof: seq[byte]) {.gcsafe, upraises:[].}
|
||||||
ContractId* = array[32, byte]
|
|
||||||
|
|
||||||
method periodicity*(proofs: Proofs):
|
method periodicity*(proofs: Proofs):
|
||||||
Future[Periodicity] {.base, async.} =
|
Future[Periodicity] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method isProofRequired*(proofs: Proofs,
|
method isProofRequired*(proofs: Proofs,
|
||||||
id: ContractId): Future[bool] {.base, async.} =
|
id: SlotId): Future[bool] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method willProofBeRequired*(proofs: Proofs,
|
method willProofBeRequired*(proofs: Proofs,
|
||||||
id: ContractId): Future[bool] {.base, async.} =
|
id: SlotId): Future[bool] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method getProofEnd*(proofs: Proofs,
|
method getProofEnd*(proofs: Proofs,
|
||||||
id: ContractId): Future[UInt256] {.base, async.} =
|
id: SlotId): Future[UInt256] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method submitProof*(proofs: Proofs,
|
method submitProof*(proofs: Proofs,
|
||||||
id: ContractId,
|
id: SlotId,
|
||||||
proof: seq[byte]) {.base, async.} =
|
proof: seq[byte]) {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
|
|
|
@ -11,11 +11,11 @@ type
|
||||||
signer: Address
|
signer: Address
|
||||||
subscriptions: Subscriptions
|
subscriptions: Subscriptions
|
||||||
Fulfillment* = object
|
Fulfillment* = object
|
||||||
requestId*: array[32, byte]
|
requestId*: RequestId
|
||||||
proof*: seq[byte]
|
proof*: seq[byte]
|
||||||
host*: Address
|
host*: Address
|
||||||
Slot* = object
|
Slot* = object
|
||||||
requestId*: array[32, byte]
|
requestId*: RequestId
|
||||||
slotIndex*: UInt256
|
slotIndex*: UInt256
|
||||||
proof*: seq[byte]
|
proof*: seq[byte]
|
||||||
host*: Address
|
host*: Address
|
||||||
|
@ -28,11 +28,11 @@ type
|
||||||
callback: OnRequest
|
callback: OnRequest
|
||||||
FulfillmentSubscription* = ref object of Subscription
|
FulfillmentSubscription* = ref object of Subscription
|
||||||
market: MockMarket
|
market: MockMarket
|
||||||
requestId: array[32, byte]
|
requestId: RequestId
|
||||||
callback: OnFulfillment
|
callback: OnFulfillment
|
||||||
SlotFilledSubscription* = ref object of Subscription
|
SlotFilledSubscription* = ref object of Subscription
|
||||||
market: MockMarket
|
market: MockMarket
|
||||||
requestId: array[32, byte]
|
requestId: RequestId
|
||||||
slotIndex: UInt256
|
slotIndex: UInt256
|
||||||
callback: OnSlotFilled
|
callback: OnSlotFilled
|
||||||
|
|
||||||
|
@ -52,14 +52,14 @@ method requestStorage*(market: MockMarket,
|
||||||
return request
|
return request
|
||||||
|
|
||||||
method getRequest(market: MockMarket,
|
method getRequest(market: MockMarket,
|
||||||
id: array[32, byte]): Future[?StorageRequest] {.async.} =
|
id: RequestId): Future[?StorageRequest] {.async.} =
|
||||||
for request in market.requested:
|
for request in market.requested:
|
||||||
if request.id == id:
|
if request.id == id:
|
||||||
return some request
|
return some request
|
||||||
return none StorageRequest
|
return none StorageRequest
|
||||||
|
|
||||||
method getHost(market: MockMarket,
|
method getHost(market: MockMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256): Future[?Address] {.async.} =
|
slotIndex: UInt256): Future[?Address] {.async.} =
|
||||||
for slot in market.filled:
|
for slot in market.filled:
|
||||||
if slot.requestId == requestId and slot.slotIndex == slotIndex:
|
if slot.requestId == requestId and slot.slotIndex == slotIndex:
|
||||||
|
@ -67,7 +67,7 @@ method getHost(market: MockMarket,
|
||||||
return none Address
|
return none Address
|
||||||
|
|
||||||
proc emitSlotFilled*(market: MockMarket,
|
proc emitSlotFilled*(market: MockMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256) =
|
slotIndex: UInt256) =
|
||||||
var subscriptions = market.subscriptions.onSlotFilled
|
var subscriptions = market.subscriptions.onSlotFilled
|
||||||
for subscription in subscriptions:
|
for subscription in subscriptions:
|
||||||
|
@ -75,14 +75,14 @@ proc emitSlotFilled*(market: MockMarket,
|
||||||
subscription.slotIndex == slotIndex:
|
subscription.slotIndex == slotIndex:
|
||||||
subscription.callback(requestId, slotIndex)
|
subscription.callback(requestId, slotIndex)
|
||||||
|
|
||||||
proc emitRequestFulfilled*(market: MockMarket, requestId: array[32, byte]) =
|
proc emitRequestFulfilled*(market: MockMarket, requestId: RequestId) =
|
||||||
var subscriptions = market.subscriptions.onFulfillment
|
var subscriptions = market.subscriptions.onFulfillment
|
||||||
for subscription in subscriptions:
|
for subscription in subscriptions:
|
||||||
if subscription.requestId == requestId:
|
if subscription.requestId == requestId:
|
||||||
subscription.callback(requestId)
|
subscription.callback(requestId)
|
||||||
|
|
||||||
proc fillSlot*(market: MockMarket,
|
proc fillSlot*(market: MockMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256,
|
slotIndex: UInt256,
|
||||||
proof: seq[byte],
|
proof: seq[byte],
|
||||||
host: Address) =
|
host: Address) =
|
||||||
|
@ -96,7 +96,7 @@ proc fillSlot*(market: MockMarket,
|
||||||
market.emitSlotFilled(requestId, slotIndex)
|
market.emitSlotFilled(requestId, slotIndex)
|
||||||
|
|
||||||
method fillSlot*(market: MockMarket,
|
method fillSlot*(market: MockMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256,
|
slotIndex: UInt256,
|
||||||
proof: seq[byte]) {.async.} =
|
proof: seq[byte]) {.async.} =
|
||||||
market.fillSlot(requestId, slotIndex, proof, market.signer)
|
market.fillSlot(requestId, slotIndex, proof, market.signer)
|
||||||
|
@ -112,7 +112,7 @@ method subscribeRequests*(market: MockMarket,
|
||||||
return subscription
|
return subscription
|
||||||
|
|
||||||
method subscribeFulfillment*(market: MockMarket,
|
method subscribeFulfillment*(market: MockMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
callback: OnFulfillment):
|
callback: OnFulfillment):
|
||||||
Future[Subscription] {.async.} =
|
Future[Subscription] {.async.} =
|
||||||
let subscription = FulfillmentSubscription(
|
let subscription = FulfillmentSubscription(
|
||||||
|
@ -124,7 +124,7 @@ method subscribeFulfillment*(market: MockMarket,
|
||||||
return subscription
|
return subscription
|
||||||
|
|
||||||
method subscribeSlotFilled*(market: MockMarket,
|
method subscribeSlotFilled*(market: MockMarket,
|
||||||
requestId: array[32, byte],
|
requestId: RequestId,
|
||||||
slotIndex: UInt256,
|
slotIndex: UInt256,
|
||||||
callback: OnSlotFilled):
|
callback: OnSlotFilled):
|
||||||
Future[Subscription] {.async.} =
|
Future[Subscription] {.async.} =
|
||||||
|
|
|
@ -7,9 +7,9 @@ import pkg/codex/storageproofs
|
||||||
type
|
type
|
||||||
MockProofs* = ref object of Proofs
|
MockProofs* = ref object of Proofs
|
||||||
periodicity: Periodicity
|
periodicity: Periodicity
|
||||||
proofsRequired: HashSet[ContractId]
|
proofsRequired: HashSet[SlotId]
|
||||||
proofsToBeRequired: HashSet[ContractId]
|
proofsToBeRequired: HashSet[SlotId]
|
||||||
proofEnds: Table[ContractId, UInt256]
|
proofEnds: Table[SlotId, UInt256]
|
||||||
subscriptions: seq[MockSubscription]
|
subscriptions: seq[MockSubscription]
|
||||||
MockSubscription* = ref object of Subscription
|
MockSubscription* = ref object of Subscription
|
||||||
proofs: MockProofs
|
proofs: MockProofs
|
||||||
|
@ -26,38 +26,38 @@ func setPeriodicity*(mock: MockProofs, periodicity: Periodicity) =
|
||||||
method periodicity*(mock: MockProofs): Future[Periodicity] {.async.} =
|
method periodicity*(mock: MockProofs): Future[Periodicity] {.async.} =
|
||||||
return mock.periodicity
|
return mock.periodicity
|
||||||
|
|
||||||
proc setProofRequired*(mock: MockProofs, id: ContractId, required: bool) =
|
proc setProofRequired*(mock: MockProofs, id: SlotId, required: bool) =
|
||||||
if required:
|
if required:
|
||||||
mock.proofsRequired.incl(id)
|
mock.proofsRequired.incl(id)
|
||||||
else:
|
else:
|
||||||
mock.proofsRequired.excl(id)
|
mock.proofsRequired.excl(id)
|
||||||
|
|
||||||
method isProofRequired*(mock: MockProofs,
|
method isProofRequired*(mock: MockProofs,
|
||||||
id: ContractId): Future[bool] {.async.} =
|
id: SlotId): Future[bool] {.async.} =
|
||||||
return mock.proofsRequired.contains(id)
|
return mock.proofsRequired.contains(id)
|
||||||
|
|
||||||
proc setProofToBeRequired*(mock: MockProofs, id: ContractId, required: bool) =
|
proc setProofToBeRequired*(mock: MockProofs, id: SlotId, required: bool) =
|
||||||
if required:
|
if required:
|
||||||
mock.proofsToBeRequired.incl(id)
|
mock.proofsToBeRequired.incl(id)
|
||||||
else:
|
else:
|
||||||
mock.proofsToBeRequired.excl(id)
|
mock.proofsToBeRequired.excl(id)
|
||||||
|
|
||||||
method willProofBeRequired*(mock: MockProofs,
|
method willProofBeRequired*(mock: MockProofs,
|
||||||
id: ContractId): Future[bool] {.async.} =
|
id: SlotId): Future[bool] {.async.} =
|
||||||
return mock.proofsToBeRequired.contains(id)
|
return mock.proofsToBeRequired.contains(id)
|
||||||
|
|
||||||
proc setProofEnd*(mock: MockProofs, id: ContractId, proofEnd: UInt256) =
|
proc setProofEnd*(mock: MockProofs, id: SlotId, proofEnd: UInt256) =
|
||||||
mock.proofEnds[id] = proofEnd
|
mock.proofEnds[id] = proofEnd
|
||||||
|
|
||||||
method getProofEnd*(mock: MockProofs,
|
method getProofEnd*(mock: MockProofs,
|
||||||
id: ContractId): Future[UInt256] {.async.} =
|
id: SlotId): Future[UInt256] {.async.} =
|
||||||
if mock.proofEnds.hasKey(id):
|
if mock.proofEnds.hasKey(id):
|
||||||
return mock.proofEnds[id]
|
return mock.proofEnds[id]
|
||||||
else:
|
else:
|
||||||
return UInt256.high
|
return UInt256.high
|
||||||
|
|
||||||
method submitProof*(mock: MockProofs,
|
method submitProof*(mock: MockProofs,
|
||||||
id: ContractId,
|
id: SlotId,
|
||||||
proof: seq[byte]) {.async.} =
|
proof: seq[byte]) {.async.} =
|
||||||
for subscription in mock.subscriptions:
|
for subscription in mock.subscriptions:
|
||||||
subscription.callback(id, proof)
|
subscription.callback(id, proof)
|
||||||
|
|
|
@ -26,25 +26,25 @@ suite "Proving":
|
||||||
await sleepAsync(2.seconds)
|
await sleepAsync(2.seconds)
|
||||||
|
|
||||||
test "maintains a list of contract ids to watch":
|
test "maintains a list of contract ids to watch":
|
||||||
let id1, id2 = ContractId.example
|
let id1, id2 = SlotId.example
|
||||||
check proving.contracts.len == 0
|
check proving.slots.len == 0
|
||||||
proving.add(id1)
|
proving.add(id1)
|
||||||
check proving.contracts.contains(id1)
|
check proving.slots.contains(id1)
|
||||||
proving.add(id2)
|
proving.add(id2)
|
||||||
check proving.contracts.contains(id1)
|
check proving.slots.contains(id1)
|
||||||
check proving.contracts.contains(id2)
|
check proving.slots.contains(id2)
|
||||||
|
|
||||||
test "removes duplicate contract ids":
|
test "removes duplicate contract ids":
|
||||||
let id = ContractId.example
|
let id = SlotId.example
|
||||||
proving.add(id)
|
proving.add(id)
|
||||||
proving.add(id)
|
proving.add(id)
|
||||||
check proving.contracts.len == 1
|
check proving.slots.len == 1
|
||||||
|
|
||||||
test "invokes callback when proof is required":
|
test "invokes callback when proof is required":
|
||||||
let id = ContractId.example
|
let id = SlotId.example
|
||||||
proving.add(id)
|
proving.add(id)
|
||||||
var called: bool
|
var called: bool
|
||||||
proc onProofRequired(id: ContractId) =
|
proc onProofRequired(id: SlotId) =
|
||||||
called = true
|
called = true
|
||||||
proving.onProofRequired = onProofRequired
|
proving.onProofRequired = onProofRequired
|
||||||
proofs.setProofRequired(id, true)
|
proofs.setProofRequired(id, true)
|
||||||
|
@ -52,11 +52,11 @@ suite "Proving":
|
||||||
check called
|
check called
|
||||||
|
|
||||||
test "callback receives id of contract for which proof is required":
|
test "callback receives id of contract for which proof is required":
|
||||||
let id1, id2 = ContractId.example
|
let id1, id2 = SlotId.example
|
||||||
proving.add(id1)
|
proving.add(id1)
|
||||||
proving.add(id2)
|
proving.add(id2)
|
||||||
var callbackIds: seq[ContractId]
|
var callbackIds: seq[SlotId]
|
||||||
proc onProofRequired(id: ContractId) =
|
proc onProofRequired(id: SlotId) =
|
||||||
callbackIds.add(id)
|
callbackIds.add(id)
|
||||||
proving.onProofRequired = onProofRequired
|
proving.onProofRequired = onProofRequired
|
||||||
proofs.setProofRequired(id1, true)
|
proofs.setProofRequired(id1, true)
|
||||||
|
@ -68,10 +68,10 @@ suite "Proving":
|
||||||
check callbackIds == @[id1, id2]
|
check callbackIds == @[id1, id2]
|
||||||
|
|
||||||
test "invokes callback when proof is about to be required":
|
test "invokes callback when proof is about to be required":
|
||||||
let id = ContractId.example
|
let id = SlotId.example
|
||||||
proving.add(id)
|
proving.add(id)
|
||||||
var called: bool
|
var called: bool
|
||||||
proc onProofRequired(id: ContractId) =
|
proc onProofRequired(id: SlotId) =
|
||||||
called = true
|
called = true
|
||||||
proving.onProofRequired = onProofRequired
|
proving.onProofRequired = onProofRequired
|
||||||
proofs.setProofRequired(id, false)
|
proofs.setProofRequired(id, false)
|
||||||
|
@ -80,12 +80,12 @@ suite "Proving":
|
||||||
check called
|
check called
|
||||||
|
|
||||||
test "stops watching when contract has ended":
|
test "stops watching when contract has ended":
|
||||||
let id = ContractId.example
|
let id = SlotId.example
|
||||||
proving.add(id)
|
proving.add(id)
|
||||||
proofs.setProofEnd(id, clock.now().u256)
|
proofs.setProofEnd(id, clock.now().u256)
|
||||||
await proofs.advanceToNextPeriod()
|
await proofs.advanceToNextPeriod()
|
||||||
var called: bool
|
var called: bool
|
||||||
proc onProofRequired(id: ContractId) =
|
proc onProofRequired(id: SlotId) =
|
||||||
called = true
|
called = true
|
||||||
proving.onProofRequired = onProofRequired
|
proving.onProofRequired = onProofRequired
|
||||||
proofs.setProofRequired(id, true)
|
proofs.setProofRequired(id, true)
|
||||||
|
@ -93,18 +93,18 @@ suite "Proving":
|
||||||
check not called
|
check not called
|
||||||
|
|
||||||
test "submits proofs":
|
test "submits proofs":
|
||||||
let id = ContractId.example
|
let id = SlotId.example
|
||||||
let proof = seq[byte].example
|
let proof = seq[byte].example
|
||||||
await proving.submitProof(id, proof)
|
await proving.submitProof(id, proof)
|
||||||
|
|
||||||
test "supports proof submission subscriptions":
|
test "supports proof submission subscriptions":
|
||||||
let id = ContractId.example
|
let id = SlotId.example
|
||||||
let proof = seq[byte].example
|
let proof = seq[byte].example
|
||||||
|
|
||||||
var receivedIds: seq[ContractId]
|
var receivedIds: seq[SlotId]
|
||||||
var receivedProofs: seq[seq[byte]]
|
var receivedProofs: seq[seq[byte]]
|
||||||
|
|
||||||
proc onProofSubmission(id: ContractId, proof: seq[byte]) =
|
proc onProofSubmission(id: SlotId, proof: seq[byte]) =
|
||||||
receivedIds.add(id)
|
receivedIds.add(id)
|
||||||
receivedProofs.add(proof)
|
receivedProofs.add(proof)
|
||||||
|
|
||||||
|
|
|
@ -31,5 +31,5 @@ proc example*(_: type StorageRequest): StorageRequest =
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
expiry: (getTime() + initDuration(hours=1)).toUnix.u256,
|
expiry: (getTime() + initDuration(hours=1)).toUnix.u256,
|
||||||
nonce: array[32, byte].example
|
nonce: Nonce.example
|
||||||
)
|
)
|
||||||
|
|
|
@ -16,7 +16,7 @@ ethersuite "Storage contracts":
|
||||||
var collateralAmount: UInt256
|
var collateralAmount: UInt256
|
||||||
var periodicity: Periodicity
|
var periodicity: Periodicity
|
||||||
var request: StorageRequest
|
var request: StorageRequest
|
||||||
var id: array[32, byte]
|
var slotId: SlotId
|
||||||
|
|
||||||
proc switchAccount(account: Signer) =
|
proc switchAccount(account: Signer) =
|
||||||
storage = storage.connect(account)
|
storage = storage.connect(account)
|
||||||
|
@ -46,31 +46,31 @@ ethersuite "Storage contracts":
|
||||||
await token.approve(storage.address, collateralAmount)
|
await token.approve(storage.address, collateralAmount)
|
||||||
await storage.deposit(collateralAmount)
|
await storage.deposit(collateralAmount)
|
||||||
await storage.fillSlot(request.id, 0.u256, proof)
|
await storage.fillSlot(request.id, 0.u256, proof)
|
||||||
id = request.slotId(0.u256)
|
slotId = request.slotId(0.u256)
|
||||||
|
|
||||||
proc waitUntilProofRequired(id: array[32, byte]) {.async.} =
|
proc waitUntilProofRequired(slotId: SlotId) {.async.} =
|
||||||
let currentPeriod = periodicity.periodOf(await provider.currentTime())
|
let currentPeriod = periodicity.periodOf(await provider.currentTime())
|
||||||
await provider.advanceTimeTo(periodicity.periodEnd(currentPeriod))
|
await provider.advanceTimeTo(periodicity.periodEnd(currentPeriod))
|
||||||
while not (
|
while not (
|
||||||
(await storage.isProofRequired(id)) and
|
(await storage.isProofRequired(slotId)) and
|
||||||
(await storage.getPointer(id)) < 250
|
(await storage.getPointer(slotId)) < 250
|
||||||
):
|
):
|
||||||
await provider.advanceTime(periodicity.seconds)
|
await provider.advanceTime(periodicity.seconds)
|
||||||
|
|
||||||
test "accept storage proofs":
|
test "accept storage proofs":
|
||||||
switchAccount(host)
|
switchAccount(host)
|
||||||
await waitUntilProofRequired(id)
|
await waitUntilProofRequired(slotId)
|
||||||
await storage.submitProof(id, proof)
|
await storage.submitProof(slotId, proof)
|
||||||
|
|
||||||
test "can mark missing proofs":
|
test "can mark missing proofs":
|
||||||
switchAccount(host)
|
switchAccount(host)
|
||||||
await waitUntilProofRequired(id)
|
await waitUntilProofRequired(slotId)
|
||||||
let missingPeriod = periodicity.periodOf(await provider.currentTime())
|
let missingPeriod = periodicity.periodOf(await provider.currentTime())
|
||||||
await provider.advanceTime(periodicity.seconds)
|
await provider.advanceTime(periodicity.seconds)
|
||||||
switchAccount(client)
|
switchAccount(client)
|
||||||
await storage.markProofAsMissing(id, missingPeriod)
|
await storage.markProofAsMissing(slotId, missingPeriod)
|
||||||
|
|
||||||
test "can be payed out at the end":
|
test "can be payed out at the end":
|
||||||
switchAccount(host)
|
switchAccount(host)
|
||||||
await provider.advanceTimeTo(await storage.proofEnd(id))
|
await provider.advanceTimeTo(await storage.proofEnd(slotId))
|
||||||
await storage.payoutSlot(request.id, 0.u256)
|
await storage.payoutSlot(request.id, 0.u256)
|
||||||
|
|
|
@ -57,9 +57,9 @@ ethersuite "On-Chain Market":
|
||||||
check (await market.getRequest(request.id)) == some request
|
check (await market.getRequest(request.id)) == some request
|
||||||
|
|
||||||
test "supports request subscriptions":
|
test "supports request subscriptions":
|
||||||
var receivedIds: seq[array[32, byte]]
|
var receivedIds: seq[RequestId]
|
||||||
var receivedAsks: seq[StorageAsk]
|
var receivedAsks: seq[StorageAsk]
|
||||||
proc onRequest(id: array[32, byte], ask: StorageAsk) =
|
proc onRequest(id: RequestId, ask: StorageAsk) =
|
||||||
receivedIds.add(id)
|
receivedIds.add(id)
|
||||||
receivedAsks.add(ask)
|
receivedAsks.add(ask)
|
||||||
let subscription = await market.subscribeRequests(onRequest)
|
let subscription = await market.subscribeRequests(onRequest)
|
||||||
|
@ -84,9 +84,9 @@ ethersuite "On-Chain Market":
|
||||||
test "support slot filled subscriptions":
|
test "support slot filled subscriptions":
|
||||||
await token.approve(storage.address, request.price)
|
await token.approve(storage.address, request.price)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
var receivedIds: seq[array[32, byte]]
|
var receivedIds: seq[RequestId]
|
||||||
var receivedSlotIndices: seq[UInt256]
|
var receivedSlotIndices: seq[UInt256]
|
||||||
proc onSlotFilled(id: array[32, byte], slotIndex: UInt256) =
|
proc onSlotFilled(id: RequestId, slotIndex: UInt256) =
|
||||||
receivedIds.add(id)
|
receivedIds.add(id)
|
||||||
receivedSlotIndices.add(slotIndex)
|
receivedSlotIndices.add(slotIndex)
|
||||||
let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled)
|
let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled)
|
||||||
|
@ -100,7 +100,7 @@ ethersuite "On-Chain Market":
|
||||||
await token.approve(storage.address, request.price)
|
await token.approve(storage.address, request.price)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
var receivedSlotIndices: seq[UInt256]
|
var receivedSlotIndices: seq[UInt256]
|
||||||
proc onSlotFilled(requestId: array[32, byte], slotIndex: UInt256) =
|
proc onSlotFilled(requestId: RequestId, slotIndex: UInt256) =
|
||||||
receivedSlotIndices.add(slotIndex)
|
receivedSlotIndices.add(slotIndex)
|
||||||
let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled)
|
let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled)
|
||||||
await market.fillSlot(request.id, slotIndex - 1, proof)
|
await market.fillSlot(request.id, slotIndex - 1, proof)
|
||||||
|
@ -112,8 +112,8 @@ ethersuite "On-Chain Market":
|
||||||
test "support fulfillment subscriptions":
|
test "support fulfillment subscriptions":
|
||||||
await token.approve(storage.address, request.price)
|
await token.approve(storage.address, request.price)
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
var receivedIds: seq[array[32, byte]]
|
var receivedIds: seq[RequestId]
|
||||||
proc onFulfillment(id: array[32, byte]) =
|
proc onFulfillment(id: RequestId) =
|
||||||
receivedIds.add(id)
|
receivedIds.add(id)
|
||||||
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
||||||
for slotIndex in 0..<request.ask.slots:
|
for slotIndex in 0..<request.ask.slots:
|
||||||
|
@ -130,8 +130,8 @@ ethersuite "On-Chain Market":
|
||||||
await token.approve(storage.address, otherrequest.price)
|
await token.approve(storage.address, otherrequest.price)
|
||||||
discard await market.requestStorage(otherRequest)
|
discard await market.requestStorage(otherRequest)
|
||||||
|
|
||||||
var receivedIds: seq[array[32, byte]]
|
var receivedIds: seq[RequestId]
|
||||||
proc onFulfillment(id: array[32, byte]) =
|
proc onFulfillment(id: RequestId) =
|
||||||
receivedIds.add(id)
|
receivedIds.add(id)
|
||||||
|
|
||||||
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
||||||
|
|
|
@ -5,7 +5,7 @@ import ./time
|
||||||
|
|
||||||
ethersuite "On-Chain Proofs":
|
ethersuite "On-Chain Proofs":
|
||||||
|
|
||||||
let contractId = ContractId.example
|
let contractId = SlotId.example
|
||||||
let proof = seq[byte].example
|
let proof = seq[byte].example
|
||||||
|
|
||||||
var proofs: OnChainProofs
|
var proofs: OnChainProofs
|
||||||
|
@ -34,10 +34,10 @@ ethersuite "On-Chain Proofs":
|
||||||
await proofs.submitProof(contractId, proof)
|
await proofs.submitProof(contractId, proof)
|
||||||
|
|
||||||
test "supports proof submission subscriptions":
|
test "supports proof submission subscriptions":
|
||||||
var receivedIds: seq[ContractId]
|
var receivedIds: seq[SlotId]
|
||||||
var receivedProofs: seq[seq[byte]]
|
var receivedProofs: seq[seq[byte]]
|
||||||
|
|
||||||
proc onProofSubmission(id: ContractId, proof: seq[byte]) =
|
proc onProofSubmission(id: SlotId, proof: seq[byte]) =
|
||||||
receivedIds.add(id)
|
receivedIds.add(id)
|
||||||
receivedProofs.add(proof)
|
receivedProofs.add(proof)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue