refactor: marketplace custom errors handling (#1061)

* refactor: marketplace custom errors handling

* chore: update contracts repo

* chore: update contracts submodule
This commit is contained in:
Adam Uhlíř 2025-01-16 10:34:44 +01:00 committed by GitHub
parent 39e8e6e6fa
commit 68ad804f9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 43 deletions

View File

@ -120,10 +120,8 @@ method getRequest*(market: OnChainMarket,
convertEthersError:
try:
return some await market.contract.getRequest(id)
except ProviderError as e:
if e.msgDetail.contains("Unknown request"):
return none StorageRequest
raise e
except Marketplace_UnknownRequest:
return none StorageRequest
method requestState*(market: OnChainMarket,
requestId: RequestId): Future[?RequestState] {.async.} =
@ -131,10 +129,8 @@ method requestState*(market: OnChainMarket,
try:
let overrides = CallOverrides(blockTag: some BlockTag.pending)
return some await market.contract.requestState(requestId, overrides)
except ProviderError as e:
if e.msgDetail.contains("Unknown request"):
return none RequestState
raise e
except Marketplace_UnknownRequest:
return none RequestState
method slotState*(market: OnChainMarket,
slotId: SlotId): Future[SlotState] {.async.} =
@ -168,10 +164,8 @@ method getActiveSlot*(market: OnChainMarket,
convertEthersError:
try:
return some await market.contract.getActiveSlot(slotId)
except ProviderError as e:
if e.msgDetail.contains("Slot is free"):
return none Slot
raise e
except Marketplace_SlotIsFree:
return none Slot
method fillSlot(market: OnChainMarket,
requestId: RequestId,
@ -219,10 +213,8 @@ method isProofRequired*(market: OnChainMarket,
try:
let overrides = CallOverrides(blockTag: some BlockTag.pending)
return await market.contract.isProofRequired(id, overrides)
except ProviderError as e:
if e.msgDetail.contains("Slot is free"):
return false
raise e
except Marketplace_SlotIsFree:
return false
method willProofBeRequired*(market: OnChainMarket,
id: SlotId): Future[bool] {.async.} =
@ -230,10 +222,8 @@ method willProofBeRequired*(market: OnChainMarket,
try:
let overrides = CallOverrides(blockTag: some BlockTag.pending)
return await market.contract.willProofBeRequired(id, overrides)
except ProviderError as e:
if e.msgDetail.contains("Slot is free"):
return false
raise e
except Marketplace_SlotIsFree:
return false
method getChallenge*(market: OnChainMarket, id: SlotId): Future[ProofChallenge] {.async.} =
convertEthersError:
@ -490,7 +480,7 @@ method queryPastSlotFilledEvents*(
fromTime: SecondsSince1970): Future[seq[SlotFilled]] {.async.} =
convertEthersError:
let fromBlock =
let fromBlock =
await market.contract.provider.blockNumberForEpoch(fromTime)
return await market.queryPastSlotFilledEvents(BlockTag.init(fromBlock))

View File

@ -17,25 +17,54 @@ export requests
type
Marketplace* = ref object of Contract
Marketplace_RepairRewardPercentageTooHigh* = object of SolidityError
Marketplace_SlashPercentageTooHigh* = object of SolidityError
Marketplace_MaximumSlashingTooHigh* = object of SolidityError
Marketplace_InvalidExpiry* = object of SolidityError
Marketplace_InvalidMaxSlotLoss* = object of SolidityError
Marketplace_InsufficientSlots* = object of SolidityError
Marketplace_InvalidClientAddress* = object of SolidityError
Marketplace_RequestAlreadyExists* = object of SolidityError
Marketplace_InvalidSlot* = object of SolidityError
Marketplace_SlotNotFree* = object of SolidityError
Marketplace_InvalidSlotHost* = object of SolidityError
Marketplace_AlreadyPaid* = object of SolidityError
Marketplace_TransferFailed* = object of SolidityError
Marketplace_UnknownRequest* = object of SolidityError
Marketplace_InvalidState* = object of SolidityError
Marketplace_StartNotBeforeExpiry* = object of SolidityError
Marketplace_SlotNotAcceptingProofs* = object of SolidityError
Marketplace_SlotIsFree* = object of SolidityError
Marketplace_ReservationRequired* = object of SolidityError
Marketplace_NothingToWithdraw* = object of SolidityError
Proofs_InsufficientBlockHeight* = object of SolidityError
Proofs_InvalidProof* = object of SolidityError
Proofs_ProofAlreadySubmitted* = object of SolidityError
Proofs_PeriodNotEnded* = object of SolidityError
Proofs_ValidationTimedOut* = object of SolidityError
Proofs_ProofNotMissing* = object of SolidityError
Proofs_ProofNotRequired* = object of SolidityError
Proofs_ProofAlreadyMarkedMissing* = object of SolidityError
proc configuration*(marketplace: Marketplace): MarketplaceConfig {.contract, view.}
proc token*(marketplace: Marketplace): Address {.contract, view.}
proc slashMisses*(marketplace: Marketplace): UInt256 {.contract, view.}
proc slashPercentage*(marketplace: Marketplace): UInt256 {.contract, view.}
proc minCollateralThreshold*(marketplace: Marketplace): UInt256 {.contract, view.}
proc requestStorage*(marketplace: Marketplace, request: StorageRequest): Confirmable {.contract.}
proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: Groth16Proof): Confirmable {.contract.}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId): Confirmable {.contract.}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId, withdrawAddress: Address): Confirmable {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId): Confirmable {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId, rewardRecipient: Address, collateralRecipient: Address): Confirmable {.contract.}
proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view.}
proc requestStorage*(marketplace: Marketplace, request: StorageRequest): Confirmable {.contract, errors:[Marketplace_InvalidClientAddress, Marketplace_RequestAlreadyExists, Marketplace_InvalidExpiry, Marketplace_InsufficientSlots, Marketplace_InvalidMaxSlotLoss].}
proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: Groth16Proof): Confirmable {.contract, errors:[Marketplace_InvalidSlot, Marketplace_ReservationRequired, Marketplace_SlotNotFree, Marketplace_StartNotBeforeExpiry, Marketplace_UnknownRequest].}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId): Confirmable {.contract, errors:[Marketplace_InvalidClientAddress, Marketplace_InvalidState, Marketplace_NothingToWithdraw, Marketplace_UnknownRequest].}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId, withdrawAddress: Address): Confirmable {.contract, errors:[Marketplace_InvalidClientAddress, Marketplace_InvalidState, Marketplace_NothingToWithdraw, Marketplace_UnknownRequest].}
proc freeSlot*(marketplace: Marketplace, id: SlotId): Confirmable {.contract, errors:[Marketplace_InvalidSlotHost, Marketplace_AlreadyPaid, Marketplace_StartNotBeforeExpiry, Marketplace_UnknownRequest, Marketplace_SlotIsFree].}
proc freeSlot*(marketplace: Marketplace, id: SlotId, rewardRecipient: Address, collateralRecipient: Address): Confirmable {.contract, errors:[Marketplace_InvalidSlotHost, Marketplace_AlreadyPaid, Marketplace_StartNotBeforeExpiry, Marketplace_UnknownRequest, Marketplace_SlotIsFree].}
proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view, errors:[Marketplace_UnknownRequest].}
proc getHost*(marketplace: Marketplace, id: SlotId): Address {.contract, view.}
proc getActiveSlot*(marketplace: Marketplace, id: SlotId): Slot {.contract, view.}
proc getActiveSlot*(marketplace: Marketplace, id: SlotId): Slot {.contract, view, errors:[Marketplace_SlotIsFree].}
proc myRequests*(marketplace: Marketplace): seq[RequestId] {.contract, view.}
proc mySlots*(marketplace: Marketplace): seq[SlotId] {.contract, view.}
proc requestState*(marketplace: Marketplace, requestId: RequestId): RequestState {.contract, view.}
proc requestState*(marketplace: Marketplace, requestId: RequestId): RequestState {.contract, view, errors:[Marketplace_UnknownRequest].}
proc slotState*(marketplace: Marketplace, slotId: SlotId): SlotState {.contract, view.}
proc requestEnd*(marketplace: Marketplace, requestId: RequestId): SecondsSince1970 {.contract, view.}
proc requestExpiry*(marketplace: Marketplace, requestId: RequestId): SecondsSince1970 {.contract, view.}
@ -49,8 +78,8 @@ proc willProofBeRequired*(marketplace: Marketplace, id: SlotId): bool {.contract
proc getChallenge*(marketplace: Marketplace, id: SlotId): array[32, byte] {.contract, view.}
proc getPointer*(marketplace: Marketplace, id: SlotId): uint8 {.contract, view.}
proc submitProof*(marketplace: Marketplace, id: SlotId, proof: Groth16Proof): Confirmable {.contract.}
proc markProofAsMissing*(marketplace: Marketplace, id: SlotId, period: UInt256): Confirmable {.contract.}
proc submitProof*(marketplace: Marketplace, id: SlotId, proof: Groth16Proof): Confirmable {.contract, errors:[Proofs_ProofAlreadySubmitted, Proofs_InvalidProof, Marketplace_UnknownRequest].}
proc markProofAsMissing*(marketplace: Marketplace, id: SlotId, period: UInt256): Confirmable {.contract, errors:[Marketplace_SlotNotAcceptingProofs, Marketplace_StartNotBeforeExpiry, Proofs_PeriodNotEnded, Proofs_ValidationTimedOut, Proofs_ProofNotMissing, Proofs_ProofNotRequired, Proofs_ProofAlreadyMarkedMissing].}
proc reserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): Confirmable {.contract.}
proc canReserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): bool {.contract, view.}

View File

@ -1,5 +1,4 @@
import pkg/chronos
import pkg/ethers/testing
import pkg/ethers/erc20
import codex/contracts
import ../ethertest
@ -60,12 +59,14 @@ ethersuite "Marketplace contracts":
while not (
(await marketplace.isProofRequired(slotId)) and
(await marketplace.getPointer(slotId)) < 250
):
)
:
await ethProvider.advanceTime(periodicity.seconds)
proc startContract() {.async.} =
for slotIndex in 1..<request.ask.slots:
discard await token.approve(marketplace.address, request.ask.collateral).confirm(1)
for slotIndex in 1 ..< request.ask.slots:
discard
await token.approve(marketplace.address, request.ask.collateral).confirm(1)
discard await marketplace.reserveSlot(request.id, slotIndex.u256).confirm(1)
discard await marketplace.fillSlot(request.id, slotIndex.u256, proof).confirm(1)
@ -92,7 +93,8 @@ ethersuite "Marketplace contracts":
let startBalance = await token.balanceOf(address)
discard await marketplace.freeSlot(slotId).confirm(1)
let endBalance = await token.balanceOf(address)
check endBalance == (startBalance + expectedPayout(requestEnd.u256) + request.ask.collateral)
check endBalance ==
(startBalance + expectedPayout(requestEnd.u256) + request.ask.collateral)
test "can be paid out at the end, specifying reward and collateral recipient":
switchAccount(host)
@ -103,7 +105,9 @@ ethersuite "Marketplace contracts":
let startBalanceHost = await token.balanceOf(hostAddress)
let startBalanceReward = await token.balanceOf(rewardRecipient)
let startBalanceCollateral = await token.balanceOf(collateralRecipient)
discard await marketplace.freeSlot(slotId, rewardRecipient, collateralRecipient).confirm(1)
discard await marketplace
.freeSlot(slotId, rewardRecipient, collateralRecipient)
.confirm(1)
let endBalanceHost = await token.balanceOf(hostAddress)
let endBalanceReward = await token.balanceOf(rewardRecipient)
let endBalanceCollateral = await token.balanceOf(collateralRecipient)
@ -118,7 +122,5 @@ ethersuite "Marketplace contracts":
switchAccount(client)
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
await ethProvider.advanceTime(periodicity.seconds)
check await marketplace
.markProofAsMissing(slotId, missingPeriod)
.confirm(1)
.reverts("Slot not accepting proofs")
expect Marketplace_SlotNotAcceptingProofs:
discard await marketplace.markProofAsMissing(slotId, missingPeriod).confirm(1)

@ -1 +1 @@
Subproject commit dfab6102e71d2acaff86af45b87be2536530c624
Subproject commit 02e3b8d22b15975fd450ccdad30e32c0be7e593f