From 6df5a7cf5456b37c959b616aaa2a269df218a857 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Wed, 17 Aug 2022 12:29:44 +1000 Subject: [PATCH] [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` --- codex/contracts/market.nim | 10 +++---- codex/contracts/proofs.nim | 8 +++--- codex/contracts/requests.nim | 11 +++++--- codex/contracts/storage.nim | 38 ++++++++++++------------- codex/market.nim | 16 +++++------ codex/node.nim | 2 +- codex/proving.nim | 18 ++++++------ codex/purchasing.nim | 13 +++++---- codex/rest/api.nim | 2 +- codex/sales.nim | 10 +++---- codex/storageproofs/timing/proofs.nim | 13 +++++---- tests/codex/helpers/mockmarket.nim | 24 ++++++++-------- tests/codex/helpers/mockproofs.nim | 20 +++++++------- tests/codex/testproving.nim | 40 +++++++++++++-------------- tests/contracts/examples.nim | 2 +- tests/contracts/testContracts.nim | 20 +++++++------- tests/contracts/testMarket.nim | 18 ++++++------ tests/contracts/testProofs.nim | 6 ++-- 18 files changed, 137 insertions(+), 134 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index a3723c81..bbf89f67 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -35,7 +35,7 @@ method requestStorage(market: OnChainMarket, return request method getRequest(market: OnChainMarket, - id: array[32, byte]): Future[?StorageRequest] {.async.} = + id: RequestId): Future[?StorageRequest] {.async.} = let request = await market.contract.getRequest(id) if request != StorageRequest.default: return some request @@ -43,7 +43,7 @@ method getRequest(market: OnChainMarket, return none StorageRequest method getHost(market: OnChainMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256): Future[?Address] {.async.} = let slotId = slotId(requestId, slotIndex) let address = await market.contract.getHost(slotId) @@ -53,7 +53,7 @@ method getHost(market: OnChainMarket, return none Address method fillSlot(market: OnChainMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256, proof: seq[byte]) {.async.} = await market.contract.fillSlot(requestId, slotIndex, proof) @@ -67,7 +67,7 @@ method subscribeRequests(market: OnChainMarket, return OnChainMarketSubscription(eventSubscription: subscription) method subscribeSlotFilled*(market: OnChainMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256, callback: OnSlotFilled): Future[MarketSubscription] {.async.} = @@ -78,7 +78,7 @@ method subscribeSlotFilled*(market: OnChainMarket, return OnChainMarketSubscription(eventSubscription: subscription) method subscribeFulfillment(market: OnChainMarket, - requestId: array[32, byte], + requestId: RequestId, callback: OnFulfillment): Future[MarketSubscription] {.async.} = proc onEvent(event: RequestFulfilled) {.upraises:[].} = diff --git a/codex/contracts/proofs.nim b/codex/contracts/proofs.nim index 4b6a7535..81ee8b68 100644 --- a/codex/contracts/proofs.nim +++ b/codex/contracts/proofs.nim @@ -23,19 +23,19 @@ method periodicity*(proofs: OnChainProofs): Future[Periodicity] {.async.} = return Periodicity(seconds: period) method isProofRequired*(proofs: OnChainProofs, - id: ContractId): Future[bool] {.async.} = + id: SlotId): Future[bool] {.async.} = return await proofs.storage.isProofRequired(id) method willProofBeRequired*(proofs: OnChainProofs, - id: ContractId): Future[bool] {.async.} = + id: SlotId): Future[bool] {.async.} = return await proofs.storage.willProofBeRequired(id) method getProofEnd*(proofs: OnChainProofs, - id: ContractId): Future[UInt256] {.async.} = + id: SlotId): Future[UInt256] {.async.} = return await proofs.storage.proofEnd(id) method submitProof*(proofs: OnChainProofs, - id: ContractId, + id: SlotId, proof: seq[byte]) {.async.} = await proofs.storage.submitProof(id, proof) diff --git a/codex/contracts/requests.nim b/codex/contracts/requests.nim index 421fe87a..f3cf87d1 100644 --- a/codex/contracts/requests.nim +++ b/codex/contracts/requests.nim @@ -11,7 +11,7 @@ type ask*: StorageAsk content*: StorageContent expiry*: UInt256 - nonce*: array[32, byte] + nonce*: Nonce StorageAsk* = object slots*: uint64 slotSize*: UInt256 @@ -28,6 +28,9 @@ type u*: seq[byte] publicKey*: seq[byte] name*: seq[byte] + SlotId* = array[32, byte] + RequestId* = array[32, byte] + Nonce* = array[32, byte] func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest = StorageRequest( @@ -116,15 +119,15 @@ func decode*(decoder: var AbiDecoder, T: type StorageRequest): ?!T = let tupl = ?decoder.read(StorageRequest.fieldTypes) success StorageRequest.fromTuple(tupl) -func id*(request: StorageRequest): array[32, byte] = +func id*(request: StorageRequest): RequestId = let encoding = AbiEncoder.encode((request, )) 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)) keccak256.digest(encoding).data -func slotId*(request: StorageRequest, slot: UInt256): array[32, byte] = +func slotId*(request: StorageRequest, slot: UInt256): SlotId = slotId(request.id, slot) func pricePerSlot*(ask: StorageAsk): UInt256 = diff --git a/codex/contracts/storage.nim b/codex/contracts/storage.nim index 4f03aacf..7d5228d4 100644 --- a/codex/contracts/storage.nim +++ b/codex/contracts/storage.nim @@ -9,18 +9,18 @@ export ethers type Storage* = ref object of Contract - Id = array[32, byte] StorageRequested* = object of Event - requestId*: Id + requestId*: RequestId ask*: StorageAsk SlotFilled* = object of Event - requestId* {.indexed.}: Id + requestId* {.indexed.}: RequestId slotIndex* {.indexed.}: UInt256 - slotId* {.indexed.}: Id + slotId* {.indexed.}: SlotId RequestFulfilled* = object of Event - requestId* {.indexed.}: Id + requestId* {.indexed.}: RequestId + ProofSubmitted* = object of Event - id*: Id + id*: SlotId proof*: seq[byte] @@ -33,22 +33,20 @@ proc withdraw*(storage: Storage) {.contract.} proc balanceOf*(storage: Storage, account: Address): UInt256 {.contract, view.} proc requestStorage*(storage: Storage, request: StorageRequest) {.contract.} -proc fillSlot*(storage: Storage, requestId: Id, slotIndex: UInt256, proof: seq[byte]) {.contract.} -proc payoutSlot*(storage: Storage, requestId: Id, slotIndex: UInt256) {.contract.} -proc getRequest*(storage: Storage, id: Id): StorageRequest {.contract, view.} -proc getHost*(storage: Storage, id: Id): Address {.contract, view.} - -proc finishContract*(storage: Storage, id: Id) {.contract.} +proc fillSlot*(storage: Storage, requestId: RequestId, slotIndex: UInt256, proof: seq[byte]) {.contract.} +proc payoutSlot*(storage: Storage, requestId: RequestId, slotIndex: UInt256) {.contract.} +proc getRequest*(storage: Storage, id: RequestId): StorageRequest {.contract, view.} +proc getHost*(storage: Storage, id: SlotId): Address {.contract, view.} proc proofPeriod*(storage: Storage): UInt256 {.contract, view.} proc proofTimeout*(storage: Storage): UInt256 {.contract, view.} -proc proofEnd*(storage: Storage, id: Id): UInt256 {.contract, view.} -proc missingProofs*(storage: Storage, id: Id): UInt256 {.contract, view.} -proc isProofRequired*(storage: Storage, id: Id): bool {.contract, view.} -proc willProofBeRequired*(storage: Storage, id: Id): bool {.contract, view.} -proc getChallenge*(storage: Storage, id: Id): array[32, byte] {.contract, view.} -proc getPointer*(storage: Storage, id: Id): uint8 {.contract, view.} +proc proofEnd*(storage: Storage, id: SlotId): UInt256 {.contract, view.} +proc missingProofs*(storage: Storage, id: SlotId): UInt256 {.contract, view.} +proc isProofRequired*(storage: Storage, id: SlotId): bool {.contract, view.} +proc willProofBeRequired*(storage: Storage, id: SlotId): bool {.contract, view.} +proc getChallenge*(storage: Storage, id: SlotId): array[32, byte] {.contract, view.} +proc getPointer*(storage: Storage, id: SlotId): uint8 {.contract, view.} -proc submitProof*(storage: Storage, id: Id, proof: seq[byte]) {.contract.} -proc markProofAsMissing*(storage: Storage, id: Id, period: UInt256) {.contract.} +proc submitProof*(storage: Storage, id: SlotId, proof: seq[byte]) {.contract.} +proc markProofAsMissing*(storage: Storage, id: SlotId, period: UInt256) {.contract.} diff --git a/codex/market.nim b/codex/market.nim index 7d44183b..deeabfe8 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -10,9 +10,9 @@ export requests type Market* = ref object of RootObj Subscription* = ref object of RootObj - OnRequest* = proc(id: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} - OnFulfillment* = proc(requestId: array[32, byte]) {.gcsafe, upraises: [].} - OnSlotFilled* = proc(requestId: array[32, byte], slotIndex: UInt256) {.gcsafe, upraises:[].} + OnRequest* = proc(id: RequestId, ask: StorageAsk) {.gcsafe, upraises:[].} + OnFulfillment* = proc(requestId: RequestId) {.gcsafe, upraises: [].} + OnSlotFilled* = proc(requestId: RequestId, slotIndex: UInt256) {.gcsafe, upraises:[].} method getSigner*(market: Market): Future[Address] {.base, async.} = raiseAssert("not implemented") @@ -23,17 +23,17 @@ method requestStorage*(market: Market, raiseAssert("not implemented") method getRequest*(market: Market, - id: array[32, byte]): + id: RequestId): Future[?StorageRequest] {.base, async.} = raiseAssert("not implemented") method getHost*(market: Market, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256): Future[?Address] {.base, async.} = raiseAssert("not implemented") method fillSlot*(market: Market, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256, proof: seq[byte]) {.base, async.} = raiseAssert("not implemented") @@ -44,13 +44,13 @@ method subscribeRequests*(market: Market, raiseAssert("not implemented") method subscribeFulfillment*(market: Market, - requestId: array[32, byte], + requestId: RequestId, callback: OnFulfillment): Future[Subscription] {.base, async.} = raiseAssert("not implemented") method subscribeSlotFilled*(market: Market, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256, callback: OnSlotFilled): Future[Subscription] {.base, async.} = diff --git a/codex/node.nim b/codex/node.nim index 620eb61f..324e4285 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -228,7 +228,7 @@ proc requestStorage*(self: CodexNodeRef, nodes: uint, tolerance: uint, reward: UInt256, - expiry = UInt256.none): Future[?!array[32, byte]] {.async.} = + expiry = UInt256.none): Future[?!PurchaseId] {.async.} = ## Initiate a request for storage sequence, this might ## be a multistep procedure. ## diff --git a/codex/proving.nim b/codex/proving.nim index 6c65b775..05b88de2 100644 --- a/codex/proving.nim +++ b/codex/proving.nim @@ -13,9 +13,9 @@ type proofs: Proofs clock: Clock loop: ?Future[void] - contracts*: HashSet[ContractId] + slots*: HashSet[SlotId] onProofRequired: ?OnProofRequired - OnProofRequired* = proc (id: ContractId) {.gcsafe, upraises:[].} + OnProofRequired* = proc (id: SlotId) {.gcsafe, upraises:[].} func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving = Proving(proofs: proofs, clock: clock) @@ -23,8 +23,8 @@ func new*(_: type Proving, proofs: Proofs, clock: Clock): Proving = proc `onProofRequired=`*(proving: Proving, callback: OnProofRequired) = proving.onProofRequired = some callback -func add*(proving: Proving, id: ContractId) = - proving.contracts.incl(id) +func add*(proving: Proving, id: SlotId) = + proving.slots.incl(id) proc getCurrentPeriod(proving: Proving): Future[Period] {.async.} = let periodicity = await proving.proofs.periodicity() @@ -36,18 +36,18 @@ proc waitUntilPeriod(proving: Proving, period: Period) {.async.} = proc removeEndedContracts(proving: Proving) {.async.} = let now = proving.clock.now().u256 - var ended: HashSet[ContractId] - for id in proving.contracts: + var ended: HashSet[SlotId] + for id in proving.slots: if now >= (await proving.proofs.getProofEnd(id)): ended.incl(id) - proving.contracts.excl(ended) + proving.slots.excl(ended) proc run(proving: Proving) {.async.} = try: while true: let currentPeriod = await proving.getCurrentPeriod() await proving.removeEndedContracts() - for id in proving.contracts: + for id in proving.slots: if (await proving.proofs.isProofRequired(id)) or (await proving.proofs.willProofBeRequired(id)): if callback =? proving.onProofRequired: @@ -70,7 +70,7 @@ proc stop*(proving: Proving) {.async.} = if not loop.finished: 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) proc subscribeProofSubmission*(proving: Proving, diff --git a/codex/purchasing.nim b/codex/purchasing.nim index f5497086..e8d35b3e 100644 --- a/codex/purchasing.nim +++ b/codex/purchasing.nim @@ -13,7 +13,7 @@ type Purchasing* = ref object market: Market clock: Clock - purchases: Table[array[32, byte], Purchase] + purchases: Table[PurchaseId, Purchase] proofProbability*: UInt256 requestExpiryInterval*: UInt256 Purchase* = ref object @@ -22,12 +22,13 @@ type clock: Clock request*: StorageRequest PurchaseTimeout* = Timeout + PurchaseId* = array[32, byte] const DefaultProofProbability = 100.u256 const DefaultRequestExpiryInterval = (10 * 60).u256 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 = Purchasing( @@ -43,7 +44,7 @@ proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest result.ask.proofProbability = purchasing.proofProbability if result.expiry == 0.u256: 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 proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase = @@ -57,7 +58,7 @@ proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase = purchasing.purchases[purchase.id] = purchase purchase -func getPurchase*(purchasing: Purchasing, id: array[32, byte]): ?Purchase = +func getPurchase*(purchasing: Purchasing, id: PurchaseId): ?Purchase = if purchasing.purchases.hasKey(id): some purchasing.purchases[id] else: @@ -72,7 +73,7 @@ proc run(purchase: Purchase) {.async.} = proc waitUntilFulfilled {.async.} = let done = newFuture[void]() - proc callback(_: array[32, byte]) = + proc callback(_: RequestId) = done.complete() let request = purchase.request let subscription = await market.subscribeFulfillment(request.id, callback) @@ -92,7 +93,7 @@ proc start(purchase: Purchase) = proc wait*(purchase: Purchase) {.async.} = await purchase.future -func id*(purchase: Purchase): array[32, byte] = +func id*(purchase: Purchase): PurchaseId = purchase.request.id func finished*(purchase: Purchase): bool = diff --git a/codex/rest/api.nim b/codex/rest/api.nim index d5be7951..2e3d27f1 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -305,7 +305,7 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter = router.api( MethodGet, "/api/codex/v1/storage/purchases/{id}") do ( - id: array[32, byte]) -> RestApiResponse: + id: PurchaseId) -> RestApiResponse: without contracts =? node.contracts: return RestApiResponse.error(Http503, "Purchasing unavailable") diff --git a/codex/sales.nim b/codex/sales.nim index 3664c81a..0e177d56 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -45,7 +45,7 @@ type minPrice*: UInt256 SalesAgent = ref object sales: Sales - requestId: array[32, byte] + requestId: RequestId ask: StorageAsk availability: Availability request: ?StorageRequest @@ -134,7 +134,7 @@ proc selectSlot(agent: SalesAgent) = agent.slotIndex = some slotIndex.u256 proc onSlotFilled(agent: SalesAgent, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256) {.async.} = try: let market = agent.sales.market @@ -145,7 +145,7 @@ proc onSlotFilled(agent: SalesAgent, agent.finish(success = false) proc subscribeSlotFilled(agent: SalesAgent, slotIndex: UInt256) {.async.} = - proc onSlotFilled(requestId: array[32, byte], + proc onSlotFilled(requestId: RequestId, slotIndex: UInt256) {.gcsafe, upraises:[].} = asyncSpawn agent.onSlotFilled(requestId, slotIndex) let market = agent.sales.market @@ -196,7 +196,7 @@ proc start(agent: SalesAgent) {.async.} = error "SalesAgent failed", msg = e.msg 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): return @@ -212,7 +212,7 @@ proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) = proc start*(sales: Sales) {.async.} = 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) try: diff --git a/codex/storageproofs/timing/proofs.nim b/codex/storageproofs/timing/proofs.nim index 39853741..3d261076 100644 --- a/codex/storageproofs/timing/proofs.nim +++ b/codex/storageproofs/timing/proofs.nim @@ -2,35 +2,36 @@ import pkg/chronos import pkg/stint import pkg/upraises import ./periods +from ../../contracts/requests import SlotId, RequestId export chronos export stint export periods +export SlotId, RequestId type Proofs* = ref object of RootObj Subscription* = ref object of RootObj - OnProofSubmitted* = proc(id: ContractId, proof: seq[byte]) {.gcsafe, upraises:[].} - ContractId* = array[32, byte] + OnProofSubmitted* = proc(id: SlotId, proof: seq[byte]) {.gcsafe, upraises:[].} method periodicity*(proofs: Proofs): Future[Periodicity] {.base, async.} = raiseAssert("not implemented") method isProofRequired*(proofs: Proofs, - id: ContractId): Future[bool] {.base, async.} = + id: SlotId): Future[bool] {.base, async.} = raiseAssert("not implemented") method willProofBeRequired*(proofs: Proofs, - id: ContractId): Future[bool] {.base, async.} = + id: SlotId): Future[bool] {.base, async.} = raiseAssert("not implemented") method getProofEnd*(proofs: Proofs, - id: ContractId): Future[UInt256] {.base, async.} = + id: SlotId): Future[UInt256] {.base, async.} = raiseAssert("not implemented") method submitProof*(proofs: Proofs, - id: ContractId, + id: SlotId, proof: seq[byte]) {.base, async.} = raiseAssert("not implemented") diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index 65fe4646..74825046 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -11,11 +11,11 @@ type signer: Address subscriptions: Subscriptions Fulfillment* = object - requestId*: array[32, byte] + requestId*: RequestId proof*: seq[byte] host*: Address Slot* = object - requestId*: array[32, byte] + requestId*: RequestId slotIndex*: UInt256 proof*: seq[byte] host*: Address @@ -28,11 +28,11 @@ type callback: OnRequest FulfillmentSubscription* = ref object of Subscription market: MockMarket - requestId: array[32, byte] + requestId: RequestId callback: OnFulfillment SlotFilledSubscription* = ref object of Subscription market: MockMarket - requestId: array[32, byte] + requestId: RequestId slotIndex: UInt256 callback: OnSlotFilled @@ -52,14 +52,14 @@ method requestStorage*(market: MockMarket, return request method getRequest(market: MockMarket, - id: array[32, byte]): Future[?StorageRequest] {.async.} = + id: RequestId): Future[?StorageRequest] {.async.} = for request in market.requested: if request.id == id: return some request return none StorageRequest method getHost(market: MockMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256): Future[?Address] {.async.} = for slot in market.filled: if slot.requestId == requestId and slot.slotIndex == slotIndex: @@ -67,7 +67,7 @@ method getHost(market: MockMarket, return none Address proc emitSlotFilled*(market: MockMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256) = var subscriptions = market.subscriptions.onSlotFilled for subscription in subscriptions: @@ -75,14 +75,14 @@ proc emitSlotFilled*(market: MockMarket, subscription.slotIndex == slotIndex: subscription.callback(requestId, slotIndex) -proc emitRequestFulfilled*(market: MockMarket, requestId: array[32, byte]) = +proc emitRequestFulfilled*(market: MockMarket, requestId: RequestId) = var subscriptions = market.subscriptions.onFulfillment for subscription in subscriptions: if subscription.requestId == requestId: subscription.callback(requestId) proc fillSlot*(market: MockMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256, proof: seq[byte], host: Address) = @@ -96,7 +96,7 @@ proc fillSlot*(market: MockMarket, market.emitSlotFilled(requestId, slotIndex) method fillSlot*(market: MockMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256, proof: seq[byte]) {.async.} = market.fillSlot(requestId, slotIndex, proof, market.signer) @@ -112,7 +112,7 @@ method subscribeRequests*(market: MockMarket, return subscription method subscribeFulfillment*(market: MockMarket, - requestId: array[32, byte], + requestId: RequestId, callback: OnFulfillment): Future[Subscription] {.async.} = let subscription = FulfillmentSubscription( @@ -124,7 +124,7 @@ method subscribeFulfillment*(market: MockMarket, return subscription method subscribeSlotFilled*(market: MockMarket, - requestId: array[32, byte], + requestId: RequestId, slotIndex: UInt256, callback: OnSlotFilled): Future[Subscription] {.async.} = diff --git a/tests/codex/helpers/mockproofs.nim b/tests/codex/helpers/mockproofs.nim index 605775fb..25bfc6ee 100644 --- a/tests/codex/helpers/mockproofs.nim +++ b/tests/codex/helpers/mockproofs.nim @@ -7,9 +7,9 @@ import pkg/codex/storageproofs type MockProofs* = ref object of Proofs periodicity: Periodicity - proofsRequired: HashSet[ContractId] - proofsToBeRequired: HashSet[ContractId] - proofEnds: Table[ContractId, UInt256] + proofsRequired: HashSet[SlotId] + proofsToBeRequired: HashSet[SlotId] + proofEnds: Table[SlotId, UInt256] subscriptions: seq[MockSubscription] MockSubscription* = ref object of Subscription proofs: MockProofs @@ -26,38 +26,38 @@ func setPeriodicity*(mock: MockProofs, periodicity: Periodicity) = method periodicity*(mock: MockProofs): Future[Periodicity] {.async.} = return mock.periodicity -proc setProofRequired*(mock: MockProofs, id: ContractId, required: bool) = +proc setProofRequired*(mock: MockProofs, id: SlotId, required: bool) = if required: mock.proofsRequired.incl(id) else: mock.proofsRequired.excl(id) method isProofRequired*(mock: MockProofs, - id: ContractId): Future[bool] {.async.} = + id: SlotId): Future[bool] {.async.} = return mock.proofsRequired.contains(id) -proc setProofToBeRequired*(mock: MockProofs, id: ContractId, required: bool) = +proc setProofToBeRequired*(mock: MockProofs, id: SlotId, required: bool) = if required: mock.proofsToBeRequired.incl(id) else: mock.proofsToBeRequired.excl(id) method willProofBeRequired*(mock: MockProofs, - id: ContractId): Future[bool] {.async.} = + id: SlotId): Future[bool] {.async.} = 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 method getProofEnd*(mock: MockProofs, - id: ContractId): Future[UInt256] {.async.} = + id: SlotId): Future[UInt256] {.async.} = if mock.proofEnds.hasKey(id): return mock.proofEnds[id] else: return UInt256.high method submitProof*(mock: MockProofs, - id: ContractId, + id: SlotId, proof: seq[byte]) {.async.} = for subscription in mock.subscriptions: subscription.callback(id, proof) diff --git a/tests/codex/testproving.nim b/tests/codex/testproving.nim index a2779efd..a47c4a2e 100644 --- a/tests/codex/testproving.nim +++ b/tests/codex/testproving.nim @@ -26,25 +26,25 @@ suite "Proving": await sleepAsync(2.seconds) test "maintains a list of contract ids to watch": - let id1, id2 = ContractId.example - check proving.contracts.len == 0 + let id1, id2 = SlotId.example + check proving.slots.len == 0 proving.add(id1) - check proving.contracts.contains(id1) + check proving.slots.contains(id1) proving.add(id2) - check proving.contracts.contains(id1) - check proving.contracts.contains(id2) + check proving.slots.contains(id1) + check proving.slots.contains(id2) test "removes duplicate contract ids": - let id = ContractId.example + let id = SlotId.example proving.add(id) proving.add(id) - check proving.contracts.len == 1 + check proving.slots.len == 1 test "invokes callback when proof is required": - let id = ContractId.example + let id = SlotId.example proving.add(id) var called: bool - proc onProofRequired(id: ContractId) = + proc onProofRequired(id: SlotId) = called = true proving.onProofRequired = onProofRequired proofs.setProofRequired(id, true) @@ -52,11 +52,11 @@ suite "Proving": check called 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(id2) - var callbackIds: seq[ContractId] - proc onProofRequired(id: ContractId) = + var callbackIds: seq[SlotId] + proc onProofRequired(id: SlotId) = callbackIds.add(id) proving.onProofRequired = onProofRequired proofs.setProofRequired(id1, true) @@ -68,10 +68,10 @@ suite "Proving": check callbackIds == @[id1, id2] test "invokes callback when proof is about to be required": - let id = ContractId.example + let id = SlotId.example proving.add(id) var called: bool - proc onProofRequired(id: ContractId) = + proc onProofRequired(id: SlotId) = called = true proving.onProofRequired = onProofRequired proofs.setProofRequired(id, false) @@ -80,12 +80,12 @@ suite "Proving": check called test "stops watching when contract has ended": - let id = ContractId.example + let id = SlotId.example proving.add(id) proofs.setProofEnd(id, clock.now().u256) await proofs.advanceToNextPeriod() var called: bool - proc onProofRequired(id: ContractId) = + proc onProofRequired(id: SlotId) = called = true proving.onProofRequired = onProofRequired proofs.setProofRequired(id, true) @@ -93,18 +93,18 @@ suite "Proving": check not called test "submits proofs": - let id = ContractId.example + let id = SlotId.example let proof = seq[byte].example await proving.submitProof(id, proof) test "supports proof submission subscriptions": - let id = ContractId.example + let id = SlotId.example let proof = seq[byte].example - var receivedIds: seq[ContractId] + var receivedIds: seq[SlotId] var receivedProofs: seq[seq[byte]] - proc onProofSubmission(id: ContractId, proof: seq[byte]) = + proc onProofSubmission(id: SlotId, proof: seq[byte]) = receivedIds.add(id) receivedProofs.add(proof) diff --git a/tests/contracts/examples.nim b/tests/contracts/examples.nim index 072e56eb..96255034 100644 --- a/tests/contracts/examples.nim +++ b/tests/contracts/examples.nim @@ -31,5 +31,5 @@ proc example*(_: type StorageRequest): StorageRequest = ) ), expiry: (getTime() + initDuration(hours=1)).toUnix.u256, - nonce: array[32, byte].example + nonce: Nonce.example ) diff --git a/tests/contracts/testContracts.nim b/tests/contracts/testContracts.nim index 2dd5852b..6e4eff70 100644 --- a/tests/contracts/testContracts.nim +++ b/tests/contracts/testContracts.nim @@ -16,7 +16,7 @@ ethersuite "Storage contracts": var collateralAmount: UInt256 var periodicity: Periodicity var request: StorageRequest - var id: array[32, byte] + var slotId: SlotId proc switchAccount(account: Signer) = storage = storage.connect(account) @@ -46,31 +46,31 @@ ethersuite "Storage contracts": await token.approve(storage.address, collateralAmount) await storage.deposit(collateralAmount) 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()) await provider.advanceTimeTo(periodicity.periodEnd(currentPeriod)) while not ( - (await storage.isProofRequired(id)) and - (await storage.getPointer(id)) < 250 + (await storage.isProofRequired(slotId)) and + (await storage.getPointer(slotId)) < 250 ): await provider.advanceTime(periodicity.seconds) test "accept storage proofs": switchAccount(host) - await waitUntilProofRequired(id) - await storage.submitProof(id, proof) + await waitUntilProofRequired(slotId) + await storage.submitProof(slotId, proof) test "can mark missing proofs": switchAccount(host) - await waitUntilProofRequired(id) + await waitUntilProofRequired(slotId) let missingPeriod = periodicity.periodOf(await provider.currentTime()) await provider.advanceTime(periodicity.seconds) switchAccount(client) - await storage.markProofAsMissing(id, missingPeriod) + await storage.markProofAsMissing(slotId, missingPeriod) test "can be payed out at the end": switchAccount(host) - await provider.advanceTimeTo(await storage.proofEnd(id)) + await provider.advanceTimeTo(await storage.proofEnd(slotId)) await storage.payoutSlot(request.id, 0.u256) diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 9a731cd8..69ad605e 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -57,9 +57,9 @@ ethersuite "On-Chain Market": check (await market.getRequest(request.id)) == some request test "supports request subscriptions": - var receivedIds: seq[array[32, byte]] + var receivedIds: seq[RequestId] var receivedAsks: seq[StorageAsk] - proc onRequest(id: array[32, byte], ask: StorageAsk) = + proc onRequest(id: RequestId, ask: StorageAsk) = receivedIds.add(id) receivedAsks.add(ask) let subscription = await market.subscribeRequests(onRequest) @@ -84,9 +84,9 @@ ethersuite "On-Chain Market": test "support slot filled subscriptions": await token.approve(storage.address, request.price) discard await market.requestStorage(request) - var receivedIds: seq[array[32, byte]] + var receivedIds: seq[RequestId] var receivedSlotIndices: seq[UInt256] - proc onSlotFilled(id: array[32, byte], slotIndex: UInt256) = + proc onSlotFilled(id: RequestId, slotIndex: UInt256) = receivedIds.add(id) receivedSlotIndices.add(slotIndex) let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled) @@ -100,7 +100,7 @@ ethersuite "On-Chain Market": await token.approve(storage.address, request.price) discard await market.requestStorage(request) var receivedSlotIndices: seq[UInt256] - proc onSlotFilled(requestId: array[32, byte], slotIndex: UInt256) = + proc onSlotFilled(requestId: RequestId, slotIndex: UInt256) = receivedSlotIndices.add(slotIndex) let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled) await market.fillSlot(request.id, slotIndex - 1, proof) @@ -112,8 +112,8 @@ ethersuite "On-Chain Market": test "support fulfillment subscriptions": await token.approve(storage.address, request.price) discard await market.requestStorage(request) - var receivedIds: seq[array[32, byte]] - proc onFulfillment(id: array[32, byte]) = + var receivedIds: seq[RequestId] + proc onFulfillment(id: RequestId) = receivedIds.add(id) let subscription = await market.subscribeFulfillment(request.id, onFulfillment) for slotIndex in 0..