From d0778835771480fd142010b3df20ccd426086917 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 12 Mar 2025 14:18:30 +0100 Subject: [PATCH] contracts: slotCollateral() no longer supported Collateral that needs to be provided is now the same, regardless whether we're repairing or not. The repair reward is added as a bonus, not deducted as a discount. --- codex/contracts/market.nim | 37 ------------------------------ codex/market.nim | 10 -------- codex/sales.nim | 25 +++----------------- codex/sales/states/filling.nim | 8 +------ tests/codex/helpers/mockmarket.nim | 30 ------------------------ tests/codex/sales/testsales.nim | 4 ---- tests/contracts/testMarket.nim | 31 ------------------------- 7 files changed, 4 insertions(+), 141 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 9d0799f9..25e57fac 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -608,40 +608,3 @@ method queryPastStorageRequestedEvents*( let fromBlock = await market.contract.provider.pastBlockTag(blocksAgo) return await market.queryPastStorageRequestedEvents(fromBlock) - -method slotCollateral*( - market: OnChainMarket, requestId: RequestId, slotIndex: uint64 -): Future[?!UInt256] {.async: (raises: [CancelledError]).} = - let slotid = slotId(requestId, slotIndex) - - try: - let slotState = await market.slotState(slotid) - - without request =? await market.getRequest(requestId): - return failure newException( - MarketError, "Failure calculating the slotCollateral, cannot get the request" - ) - - return market.slotCollateral(request.ask.collateralPerSlot, slotState) - except MarketError as error: - error "Error when trying to calculate the slotCollateral", error = error.msg - return failure error - -method slotCollateral*( - market: OnChainMarket, collateralPerSlot: UInt256, slotState: SlotState -): ?!UInt256 {.raises: [].} = - if slotState == SlotState.Repair: - without repairRewardPercentage =? - market.configuration .? collateral .? repairRewardPercentage: - return failure newException( - MarketError, - "Failure calculating the slotCollateral, cannot get the reward percentage", - ) - - return success ( - collateralPerSlot - (collateralPerSlot * repairRewardPercentage.u256).div( - 100.u256 - ) - ) - - return success(collateralPerSlot) diff --git a/codex/market.nim b/codex/market.nim index 31c0687f..4350b2eb 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -301,13 +301,3 @@ method queryPastStorageRequestedEvents*( market: Market, blocksAgo: int ): Future[seq[StorageRequested]] {.base, async.} = raiseAssert("not implemented") - -method slotCollateral*( - market: Market, requestId: RequestId, slotIndex: uint64 -): Future[?!UInt256] {.base, async: (raises: [CancelledError]).} = - raiseAssert("not implemented") - -method slotCollateral*( - market: Market, collateralPerSlot: UInt256, slotState: SlotState -): ?!UInt256 {.base, gcsafe, raises: [].} = - raiseAssert("not implemented") diff --git a/codex/sales.nim b/codex/sales.nim index 37e2c06a..4d3f1acc 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -155,14 +155,9 @@ proc cleanUp( # Re-add items back into the queue to prevent small availabilities from # draining the queue. Seen items will be ordered last. + let collateral = data.ask.collateralPerSlot().stuint(256) if reprocessSlot and request =? data.request: try: - without collateral =? - await sales.context.market.slotCollateral(data.requestId, data.slotIndex), err: - error "Failed to re-add item back to the slot queue: unable to calculate collateral", - error = err.msg - return - let queue = sales.context.slotQueue var seenItem = SlotQueueItem.init( data.requestId, @@ -310,12 +305,7 @@ proc onStorageRequested( trace "storage requested, adding slots to queue" - let market = sales.context.market - - without collateral =? market.slotCollateral(ask.collateralPerSlot, SlotState.Free), - err: - error "Request failure, unable to calculate collateral", error = err.msg - return + let collateral = ask.collateralPerSlot().stuint(256) without items =? SlotQueueItem.init(requestId, ask, expiry, collateral).catch, err: if err of SlotsOutOfRangeError: @@ -352,16 +342,7 @@ proc onSlotFreed(sales: Sales, requestId: RequestId, slotIndex: uint64) = error "unknown request in contract", error = err.msgDetail return - # Take the repairing state into consideration to calculate the collateral. - # This is particularly needed because it will affect the priority in the queue - # and we want to give the user the ability to tweak the parameters. - # Adding the repairing state directly in the queue priority calculation - # would not allow this flexibility. - without collateral =? - market.slotCollateral(request.ask.collateralPerSlot, SlotState.Repair), err: - error "Failed to add freed slot to queue: unable to calculate collateral", - error = err.msg - return + let collateral = request.ask.collateralPerSlot.stuint(256) if slotIndex > uint16.high.uint64: error "Cannot cast slot index to uint16, value = ", slotIndex diff --git a/codex/sales/states/filling.nim b/codex/sales/states/filling.nim index 1b76150a..e4eb3d78 100644 --- a/codex/sales/states/filling.nim +++ b/codex/sales/states/filling.nim @@ -38,13 +38,8 @@ method run*( requestId = data.requestId slotIndex = data.slotIndex + let collateral = request.ask.collateralPerSlot() try: - without collateral =? await market.slotCollateral(data.requestId, data.slotIndex), - err: - error "Failure attempting to fill slot: unable to calculate collateral", - error = err.msg - return some State(SaleErrored(error: err)) - debug "Filling slot" try: await market.fillSlot(data.requestId, data.slotIndex, state.proof, collateral) @@ -53,7 +48,6 @@ method run*( return some State(SaleIgnored(reprocessSlot: false)) except MarketError as e: return some State(SaleErrored(error: e)) - # other CatchableErrors are handled "automatically" by the SaleState return some State(SaleFilled()) except CancelledError as e: diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index 55abeb14..52ca1a78 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -603,33 +603,3 @@ method unsubscribe*(subscription: ProofSubmittedSubscription) {.async.} = method unsubscribe*(subscription: SlotReservationsFullSubscription) {.async.} = subscription.market.subscriptions.onSlotReservationsFull.keepItIf(it != subscription) - -method slotCollateral*( - market: MockMarket, requestId: RequestId, slotIndex: uint64 -): Future[?!UInt256] {.async: (raises: [CancelledError]).} = - let slotid = slotId(requestId, slotIndex) - - try: - let state = await slotState(market, slotid) - - without request =? await market.getRequest(requestId): - return failure newException( - MarketError, "Failure calculating the slotCollateral, cannot get the request" - ) - - return market.slotCollateral(request.ask.collateralPerSlot, state) - except MarketError as error: - error "Error when trying to calculate the slotCollateral", error = error.msg - return failure error - -method slotCollateral*( - market: MockMarket, collateralPerSlot: UInt256, slotState: SlotState -): ?!UInt256 {.raises: [].} = - if slotState == SlotState.Repair: - let repairRewardPercentage = market.config.collateral.repairRewardPercentage.u256 - - return success ( - collateralPerSlot - (collateralPerSlot * repairRewardPercentage).div(100.u256) - ) - - return success collateralPerSlot diff --git a/tests/codex/sales/testsales.nim b/tests/codex/sales/testsales.nim index f4d9cbae..6fb42933 100644 --- a/tests/codex/sales/testsales.nim +++ b/tests/codex/sales/testsales.nim @@ -325,10 +325,6 @@ asyncchecksuite "Sales": market.emitSlotFreed(request.id, 2.uint64) - without collateralPerSlot =? await market.slotCollateral(request.id, 2.uint64), - error: - fail() - let expected = SlotQueueItem.init(request, 2.uint16, collateral = request.ask.collateralPerSlot) diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 0717e8fe..97b9bbdd 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -598,37 +598,6 @@ ethersuite "On-Chain Market": check endBalanceHost == (startBalanceHost + request.ask.collateralPerSlot) check endBalanceReward == (startBalanceReward + expectedPayout) - test "returns the collateral when the slot is not being repaired": - await market.requestStorage(request) - await market.reserveSlot(request.id, 0.uint64) - await market.fillSlot(request.id, 0.uint64, proof, request.ask.collateralPerSlot) - - let slotId = request.slotId(0.uint64) - without collateral =? await market.slotCollateral(request.id, 0.uint64), error: - fail() - - check collateral == request.ask.collateralPerSlot - - test "calculates correctly the collateral when the slot is being repaired": - # Ensure that the config is loaded and repairRewardPercentage is available - discard await market.repairRewardPercentage() - - await market.requestStorage(request) - await market.reserveSlot(request.id, 0.uint64) - await market.fillSlot(request.id, 0.uint64, proof, request.ask.collateralPerSlot) - await market.freeSlot(slotId(request.id, 0.uint64)) - - let slotId = request.slotId(0.uint64) - - without collateral =? await market.slotCollateral(request.id, 0.uint64), error: - fail() - - # slotCollateral - # repairRewardPercentage = 10 - # expected collateral = slotCollateral - slotCollateral * 0.1 - check collateral == - request.ask.collateralPerSlot - (request.ask.collateralPerSlot * 10).div(100.u256) - test "the request is added to cache after the first access": await market.requestStorage(request)