From 60b6996eb0c4c2b85e49c65c7464d83222d7cdf2 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 26 Mar 2025 09:06:37 +0100 Subject: [PATCH] chore(marketplace): define raises for async pragma (#1165) * Define raises for async pragma * Update nim ethers * Replace CatchableError by MarketError --- codex/contracts/market.nim | 30 ++++++++++++++++++-------- codex/market.nim | 28 +++++++++++++++++------- tests/codex/helpers/mockmarket.nim | 34 ++++++++++++++++++++---------- vendor/nim-ethers | 2 +- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 0b846099..74694285 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -76,7 +76,9 @@ proc config( return resolvedConfig -proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} = +proc approveFunds( + market: OnChainMarket, amount: UInt256 +) {.async: (raises: [CancelledError, MarketError]).} = debug "Approving tokens", amount convertEthersError("Failed to approve funds"): let tokenAddress = await market.contract.token() @@ -105,7 +107,9 @@ method getZkeyHash*( let config = await market.config() return some config.proofs.zkeyHash -method getSigner*(market: OnChainMarket): Future[Address] {.async.} = +method getSigner*( + market: OnChainMarket +): Future[Address] {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to get signer address"): return await market.signer.getAddress() @@ -159,7 +163,9 @@ method mySlots*(market: OnChainMarket): Future[seq[SlotId]] {.async.} = return slots -method requestStorage(market: OnChainMarket, request: StorageRequest) {.async.} = +method requestStorage( + market: OnChainMarket, request: StorageRequest +) {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to request storage"): debug "Requesting storage" await market.approveFunds(request.totalPrice()) @@ -243,7 +249,7 @@ method fillSlot( slotIndex: uint64, proof: Groth16Proof, collateral: UInt256, -) {.async.} = +) {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to fill slot"): logScope: requestId @@ -260,7 +266,9 @@ method fillSlot( parent, ) -method freeSlot*(market: OnChainMarket, slotId: SlotId) {.async.} = +method freeSlot*( + market: OnChainMarket, slotId: SlotId +) {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to free slot"): var freeSlot: Future[Confirmable] if rewardRecipient =? market.rewardRecipient: @@ -279,7 +287,9 @@ method freeSlot*(market: OnChainMarket, slotId: SlotId) {.async.} = discard await freeSlot.confirm(1) -method withdrawFunds(market: OnChainMarket, requestId: RequestId) {.async.} = +method withdrawFunds( + market: OnChainMarket, requestId: RequestId +) {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to withdraw funds"): discard await market.contract.withdrawFunds(requestId).confirm(1) @@ -306,13 +316,15 @@ method getChallenge*( let overrides = CallOverrides(blockTag: some BlockTag.pending) return await market.contract.getChallenge(id, overrides) -method submitProof*(market: OnChainMarket, id: SlotId, proof: Groth16Proof) {.async.} = +method submitProof*( + market: OnChainMarket, id: SlotId, proof: Groth16Proof +) {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to submit proof"): discard await market.contract.submitProof(id, proof).confirm(1) method markProofAsMissing*( market: OnChainMarket, id: SlotId, period: Period -) {.async.} = +) {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to mark proof as missing"): discard await market.contract.markProofAsMissing(id, period).confirm(1) @@ -331,7 +343,7 @@ method canProofBeMarkedAsMissing*( method reserveSlot*( market: OnChainMarket, requestId: RequestId, slotIndex: uint64 -) {.async.} = +) {.async: (raises: [CancelledError, MarketError]).} = convertEthersError("Failed to reserve slot"): try: discard await market.contract diff --git a/codex/market.nim b/codex/market.nim index dd8e14ba..71cad9a9 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -74,7 +74,9 @@ method getZkeyHash*( ): Future[?string] {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") -method getSigner*(market: Market): Future[Address] {.base, async.} = +method getSigner*( + market: Market +): Future[Address] {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") method periodicity*( @@ -108,7 +110,9 @@ proc inDowntime*(market: Market, slotId: SlotId): Future[bool] {.async.} = let pntr = await market.getPointer(slotId) return pntr < downtime -method requestStorage*(market: Market, request: StorageRequest) {.base, async.} = +method requestStorage*( + market: Market, request: StorageRequest +) {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") method myRequests*(market: Market): Future[seq[RequestId]] {.base, async.} = @@ -161,13 +165,17 @@ method fillSlot*( slotIndex: uint64, proof: Groth16Proof, collateral: UInt256, -) {.base, async.} = +) {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") -method freeSlot*(market: Market, slotId: SlotId) {.base, async.} = +method freeSlot*( + market: Market, slotId: SlotId +) {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") -method withdrawFunds*(market: Market, requestId: RequestId) {.base, async.} = +method withdrawFunds*( + market: Market, requestId: RequestId +) {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") method subscribeRequests*( @@ -186,10 +194,14 @@ method getChallenge*( ): Future[ProofChallenge] {.base, async.} = raiseAssert("not implemented") -method submitProof*(market: Market, id: SlotId, proof: Groth16Proof) {.base, async.} = +method submitProof*( + market: Market, id: SlotId, proof: Groth16Proof +) {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") -method markProofAsMissing*(market: Market, id: SlotId, period: Period) {.base, async.} = +method markProofAsMissing*( + market: Market, id: SlotId, period: Period +) {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") method canProofBeMarkedAsMissing*( @@ -199,7 +211,7 @@ method canProofBeMarkedAsMissing*( method reserveSlot*( market: Market, requestId: RequestId, slotIndex: uint64 -) {.base, async.} = +) {.base, async: (raises: [CancelledError, MarketError]).} = raiseAssert("not implemented") method canReserveSlot*( diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index edf8a62d..03e76762 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -47,7 +47,7 @@ type config*: MarketplaceConfig canReserveSlot*: bool errorOnReserveSlot*: ?(ref MarketError) - errorOnFillSlot*: ?(ref CatchableError) + errorOnFillSlot*: ?(ref MarketError) clock: ?Clock Fulfillment* = object @@ -144,7 +144,9 @@ method loadConfig*( ): Future[?!void] {.async: (raises: [CancelledError]).} = discard -method getSigner*(market: MockMarket): Future[Address] {.async.} = +method getSigner*( + market: MockMarket +): Future[Address] {.async: (raises: [CancelledError, MarketError]).} = return market.signer method periodicity*( @@ -173,7 +175,9 @@ method repairRewardPercentage*( method getPointer*(market: MockMarket, slotId: SlotId): Future[uint8] {.async.} = return market.proofPointer -method requestStorage*(market: MockMarket, request: StorageRequest) {.async.} = +method requestStorage*( + market: MockMarket, request: StorageRequest +) {.async: (raises: [CancelledError, MarketError]).} = market.requested.add(request) var subscriptions = market.subscriptions.onRequest for subscription in subscriptions: @@ -311,10 +315,12 @@ method fillSlot*( slotIndex: uint64, proof: Groth16Proof, collateral: UInt256, -) {.async.} = +) {.async: (raises: [CancelledError, MarketError]).} = market.fillSlot(requestId, slotIndex, proof, market.signer, collateral) -method freeSlot*(market: MockMarket, slotId: SlotId) {.async.} = +method freeSlot*( + market: MockMarket, slotId: SlotId +) {.async: (raises: [CancelledError, MarketError]).} = market.freed.add(slotId) for s in market.filled: if slotId(s.requestId, s.slotIndex) == slotId: @@ -322,7 +328,9 @@ method freeSlot*(market: MockMarket, slotId: SlotId) {.async.} = break market.slotState[slotId] = SlotState.Free -method withdrawFunds*(market: MockMarket, requestId: RequestId) {.async.} = +method withdrawFunds*( + market: MockMarket, requestId: RequestId +) {.async: (raises: [CancelledError, MarketError]).} = market.withdrawn.add(requestId) if state =? market.requestState .? [requestId] and state == RequestState.Cancelled: @@ -352,12 +360,16 @@ method getChallenge*(mock: MockMarket, id: SlotId): Future[ProofChallenge] {.asy proc setProofEnd*(mock: MockMarket, id: SlotId, proofEnd: UInt256) = mock.proofEnds[id] = proofEnd -method submitProof*(mock: MockMarket, id: SlotId, proof: Groth16Proof) {.async.} = +method submitProof*( + mock: MockMarket, id: SlotId, proof: Groth16Proof +) {.async: (raises: [CancelledError, MarketError]).} = mock.submitted.add(proof) for subscription in mock.subscriptions.onProofSubmitted: subscription.callback(id) -method markProofAsMissing*(market: MockMarket, id: SlotId, period: Period) {.async.} = +method markProofAsMissing*( + market: MockMarket, id: SlotId, period: Period +) {.async: (raises: [CancelledError, MarketError]).} = market.markedAsMissingProofs.add(id) proc setCanProofBeMarkedAsMissing*(mock: MockMarket, id: SlotId, required: bool) = @@ -373,7 +385,7 @@ method canProofBeMarkedAsMissing*( method reserveSlot*( market: MockMarket, requestId: RequestId, slotIndex: uint64 -) {.async.} = +) {.async: (raises: [CancelledError, MarketError]).} = if error =? market.errorOnReserveSlot: raise error @@ -392,10 +404,10 @@ func setErrorOnReserveSlot*(market: MockMarket, error: ref MarketError) = else: some error -func setErrorOnFillSlot*(market: MockMarket, error: ref CatchableError) = +func setErrorOnFillSlot*(market: MockMarket, error: ref MarketError) = market.errorOnFillSlot = if error.isNil: - none (ref CatchableError) + none (ref MarketError) else: some error diff --git a/vendor/nim-ethers b/vendor/nim-ethers index b505ef1a..5d07b5db 160000 --- a/vendor/nim-ethers +++ b/vendor/nim-ethers @@ -1 +1 @@ -Subproject commit b505ef1ab889be8161bb1efb4908e3dfde5bc1c9 +Subproject commit 5d07b5dbcf584b020c732e84cc8b7229ab3e1083