From 0723405ee6fcf3c4be308015e067ca59906a622b Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Fri, 26 Aug 2022 14:08:02 +1000 Subject: [PATCH] [marketplace] support freeing of slots and failed state Support storage contract parameters to free slots after too many proofs missed, as well as fail a contract after too many slots have been freed. --- codex/contracts/market.nim | 12 ++++++++---- codex/contracts/requests.nim | 4 +++- codex/contracts/storage.nim | 3 ++- codex/node.nim | 3 ++- tests/contracts/examples.nim | 3 ++- tests/contracts/testMarket.nim | 2 +- vendor/dagger-contracts | 2 +- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index bbf89f67..a02c88a1 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -36,10 +36,14 @@ method requestStorage(market: OnChainMarket, method getRequest(market: OnChainMarket, id: RequestId): Future[?StorageRequest] {.async.} = - let request = await market.contract.getRequest(id) - if request != StorageRequest.default: - return some request - else: + try: + let request = await market.contract.getRequest(id) + if request != StorageRequest.default: + return some request + else: + return none StorageRequest + except ValueError: + # Unknown request return none StorageRequest method getHost(market: OnChainMarket, diff --git a/codex/contracts/requests.nim b/codex/contracts/requests.nim index f591b785..10612765 100644 --- a/codex/contracts/requests.nim +++ b/codex/contracts/requests.nim @@ -19,6 +19,7 @@ type duration*: UInt256 proofProbability*: UInt256 reward*: UInt256 + maxSlotLoss*: uint64 StorageContent* = object cid*: string erasure*: StorageErasure @@ -56,7 +57,8 @@ func fromTuple(_: type StorageAsk, tupl: tuple): StorageAsk = slotSize: tupl[1], duration: tupl[2], proofProbability: tupl[3], - reward: tupl[4] + reward: tupl[4], + maxSlotLoss: tupl[5] ) func fromTuple(_: type StorageContent, tupl: tuple): StorageContent = diff --git a/codex/contracts/storage.nim b/codex/contracts/storage.nim index 7d5228d4..80ff9c63 100644 --- a/codex/contracts/storage.nim +++ b/codex/contracts/storage.nim @@ -15,7 +15,7 @@ type SlotFilled* = object of Event requestId* {.indexed.}: RequestId slotIndex* {.indexed.}: UInt256 - slotId* {.indexed.}: SlotId + slotId*: SlotId RequestFulfilled* = object of Event requestId* {.indexed.}: RequestId @@ -27,6 +27,7 @@ type proc collateralAmount*(storage: Storage): UInt256 {.contract, view.} proc slashMisses*(storage: Storage): UInt256 {.contract, view.} proc slashPercentage*(storage: Storage): UInt256 {.contract, view.} +proc minCollateralThreshold*(storage: Storage): UInt256 {.contract, view.} proc deposit*(storage: Storage, amount: UInt256) {.contract.} proc withdraw*(storage: Storage) {.contract.} diff --git a/codex/node.nim b/codex/node.nim index 2c07e12e..5a62be28 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -270,7 +270,8 @@ proc requestStorage*(self: CodexNodeRef, slots: nodes + tolerance, slotSize: (encoded.blockSize * encoded.steps).u256, duration: duration, - reward: reward + reward: reward, + maxSlotLoss: tolerance ), content: StorageContent( cid: $encodedBlk.cid, diff --git a/tests/contracts/examples.nim b/tests/contracts/examples.nim index 96255034..55388f5d 100644 --- a/tests/contracts/examples.nim +++ b/tests/contracts/examples.nim @@ -17,7 +17,8 @@ proc example*(_: type StorageRequest): StorageRequest = slotSize: (1 * 1024 * 1024 * 1024).u256, # 1 Gigabyte duration: (10 * 60 * 60).u256, # 10 hours proofProbability: 4.u256, # require a proof roughly once every 4 periods - reward: 84.u256 + reward: 84.u256, + maxSlotLoss: 2 # 2 slots can be freed without data considered to be lost ), content: StorageContent( cid: "zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob", diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 69ad605e..282019ab 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -127,7 +127,7 @@ ethersuite "On-Chain Market": await token.approve(storage.address, request.price) discard await market.requestStorage(request) - await token.approve(storage.address, otherrequest.price) + await token.approve(storage.address, otherRequest.price) discard await market.requestStorage(otherRequest) var receivedIds: seq[RequestId] diff --git a/vendor/dagger-contracts b/vendor/dagger-contracts index 9ab65ae5..9a382234 160000 --- a/vendor/dagger-contracts +++ b/vendor/dagger-contracts @@ -1 +1 @@ -Subproject commit 9ab65ae5a61a09a6849cc4adbd8ef58fb89c037e +Subproject commit 9a382234c5de427e8ef824d579b734c2bc861486