From 73b1b6cfc43538d45233fb7c0978b1b9470c3d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Thu, 2 Jan 2025 11:07:58 +0100 Subject: [PATCH] chore: rebase and update --- .editorconfig | 2 +- contracts/Marketplace.sol | 98 ++++++++++++++------------------------- contracts/Proofs.sol | 33 +++---------- test/Marketplace.test.js | 8 ++-- 4 files changed, 46 insertions(+), 95 deletions(-) diff --git a/.editorconfig b/.editorconfig index bad5012..012c59a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,7 +10,7 @@ insert_final_newline = true # Matches multiple files with brace expansion notation # Set default charset -[{*.js, *.sol}] +[{*.js,*.sol}] charset = utf-8 indent_style = space indent_size = 2 diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index f9450fc..d397555 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -18,6 +18,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { error Marketplace_MaximumSlashingTooHigh(); error Marketplace_InvalidExpiry(); error Marketplace_InvalidMaxSlotLoss(); + error Marketplace_InsufficientSlots(); error Marketplace_InvalidClientAddress(); error Marketplace_RequestAlreadyExists(); error Marketplace_InvalidSlot(); @@ -26,11 +27,12 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { error Marketplace_AlreadyPaid(); error Marketplace_TransferFailed(); error Marketplace_UnknownRequest(); - error Marketplace_RequestNotYetTimedOut(); error Marketplace_InvalidState(); error Marketplace_StartNotBeforeExpiry(); error Marketplace_SlotNotAcceptingProofs(); error Marketplace_SlotIsFree(); + error Marketplace_ReservationRequired(); + error Marketplace_NothingToWithdraw(); using EnumerableSet for EnumerableSet.Bytes32Set; using EnumerableSet for EnumerableSet.AddressSet; @@ -85,22 +87,17 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { IERC20 token_, IGroth16Verifier verifier ) - SlotReservations(configuration.reservations) - Proofs(configuration.proofs, verifier) + SlotReservations(configuration.reservations) + Proofs(configuration.proofs, verifier) { _token = token_; - if (configuration.collateral.repairRewardPercentage > 100) { - revert Marketplace_RepairRewardPercentageTooHigh(); - } - - if (configuration.collateral.slashPercentage > 100) { - revert Marketplace_SlashPercentageTooHigh(); - } + if (configuration.collateral.repairRewardPercentage > 100) revert Marketplace_RepairRewardPercentageTooHigh(); + if (configuration.collateral.slashPercentage > 100) revert Marketplace_SlashPercentageTooHigh(); if ( configuration.collateral.maxNumberOfSlashes * - configuration.collateral.slashPercentage > + configuration.collateral.slashPercentage > 100 ) { revert Marketplace_MaximumSlashingTooHigh(); @@ -117,21 +114,13 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { } function requestStorage(Request calldata request) public { - if (request.client != msg.sender) { - revert Marketplace_InvalidClientAddress(); - } - RequestId id = request.id(); - if (_requests[id].client != address(0)) { - revert Marketplace_RequestAlreadyExists(); - } - if (request.expiry == 0 || request.expiry >= request.ask.duration) { - revert Marketplace_InvalidExpiry(); - } - // TODO: require(request.ask.slots > 0, "Insufficient slots"); - if (request.ask.maxSlotLoss > request.ask.slots) { - revert Marketplace_InvalidMaxSlotLoss(); - } + + if (request.client != msg.sender) revert Marketplace_InvalidClientAddress(); + if (_requests[id].client != address(0)) revert Marketplace_RequestAlreadyExists(); + if (request.expiry == 0 || request.expiry >= request.ask.duration) revert Marketplace_InvalidExpiry(); + if (request.ask.slots == 0) revert Marketplace_InsufficientSlots(); + if (request.ask.maxSlotLoss > request.ask.slots) revert Marketplace_InvalidMaxSlotLoss(); _requests[id] = request; _requestContexts[id].endsAt = block.timestamp + request.ask.duration; @@ -161,12 +150,11 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { Groth16Proof calldata proof ) public requestIsKnown(requestId) { Request storage request = _requests[requestId]; - if (slotIndex >= request.ask.slots) { - revert Marketplace_InvalidSlot(); - } + if (slotIndex >= request.ask.slots) revert Marketplace_InvalidSlot(); SlotId slotId = Requests.slotId(requestId, slotIndex); - require(_reservations[slotId].contains(msg.sender), "Reservation required"); + + if(!_reservations[slotId].contains(msg.sender)) revert Marketplace_ReservationRequired(); Slot storage slot = _slots[slotId]; slot.requestId = requestId; @@ -174,7 +162,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { RequestContext storage context = _requestContexts[requestId]; if (slotState(slotId) != SlotState.Free && - slotState(slotId) != SlotState.Repair) { + slotState(slotId) != SlotState.Repair) { revert Marketplace_SlotNotFree(); } @@ -244,14 +232,10 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { address collateralRecipient ) public slotIsNotFree(slotId) { Slot storage slot = _slots[slotId]; - if (slot.host != msg.sender) { - revert Marketplace_InvalidSlotHost(); - } + if (slot.host != msg.sender) revert Marketplace_InvalidSlotHost(); SlotState state = slotState(slotId); - if (state == SlotState.Paid) { - revert Marketplace_AlreadyPaid(); - } + if (state == SlotState.Paid) revert Marketplace_AlreadyPaid(); if (state == SlotState.Finished) { _payoutSlot(slot.requestId, slotId, rewardRecipient, collateralRecipient); @@ -304,9 +288,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { } function markProofAsMissing(SlotId slotId, Period period) public { - if (slotState(slotId) != SlotState.Filled) { - revert Marketplace_SlotNotAcceptingProofs(); - } + if (slotState(slotId) != SlotState.Filled) revert Marketplace_SlotNotAcceptingProofs(); _markProofAsMissing(slotId, period); Slot storage slot = _slots[slotId]; @@ -442,24 +424,20 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { address withdrawRecipient ) public { Request storage request = _requests[requestId]; - if (block.timestamp <= requestExpiry(requestId)) { - revert Marketplace_RequestNotYetTimedOut(); // TODO: Delete - } + RequestContext storage context = _requestContexts[requestId]; - if (request.client != msg.sender) { - revert Marketplace_InvalidClientAddress(); - } + if (request.client != msg.sender) revert Marketplace_InvalidClientAddress(); RequestState state = requestState(requestId); if (state != RequestState.Cancelled && - state != RequestState.Failed && - state != RequestState.Finished) { + state != RequestState.Failed && + state != RequestState.Finished) { revert Marketplace_InvalidState(); } // fundsToReturnToClient == 0 is used for "double-spend" protection, once the funds are withdrawn // then this variable is set to 0. - require(context.fundsToReturnToClient != 0, "Nothing to withdraw"); + if (context.fundsToReturnToClient == 0) revert Marketplace_NothingToWithdraw(); if (state == RequestState.Cancelled) { context.state = RequestState.Cancelled; @@ -500,9 +478,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { } modifier requestIsKnown(RequestId requestId) { - if (_requests[requestId].client == address(0)) { - revert Marketplace_UnknownRequest(); - } + if (_requests[requestId].client == address(0)) revert Marketplace_UnknownRequest(); _; } @@ -514,9 +490,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { } modifier slotIsNotFree(SlotId slotId) { - if (_slots[slotId].state == SlotState.Free) { - revert Marketplace_SlotIsFree(); - } + if (_slots[slotId].state == SlotState.Free) revert Marketplace_SlotIsFree(); _; } @@ -551,10 +525,10 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { ) private view returns (uint256) { return _slotPayout( - requestId, - startingTimestamp, - _requestContexts[requestId].endsAt - ); + requestId, + startingTimestamp, + _requestContexts[requestId].endsAt + ); } /// @notice Calculates the amount that should be paid out to a host based on the specified time frame. @@ -564,9 +538,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { uint256 endingTimestamp ) private view returns (uint256) { Request storage request = _requests[requestId]; - if (startingTimestamp >= endingTimestamp) { - revert Marketplace_StartNotBeforeExpiry(); - } + if (startingTimestamp >= endingTimestamp) revert Marketplace_StartNotBeforeExpiry(); return (endingTimestamp - startingTimestamp) * request.ask.reward; } @@ -616,9 +588,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { function _transferFrom(address sender, uint256 amount) internal { address receiver = address(this); - if (!_token.transferFrom(sender, receiver, amount)) { - revert Marketplace_TransferFailed(); - } + if (!_token.transferFrom(sender, receiver, amount)) revert Marketplace_TransferFailed(); } event StorageRequested(RequestId requestId, Ask ask, uint256 expiry); diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index 31d2202..2ea5e74 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -201,13 +201,8 @@ abstract contract Proofs is Periods { Groth16Proof calldata proof, uint[] memory pubSignals ) internal { - if (_received[id][_blockPeriod()]) { - revert Proofs_ProofAlreadySubmitted(); - } - - if (!_verifier.verify(proof, pubSignals)) { - revert Proofs_InvalidProof(); - } + if (_received[id][_blockPeriod()]) revert Proofs_ProofAlreadySubmitted(); + if (!_verifier.verify(proof, pubSignals)) revert Proofs_InvalidProof(); _received[id][_blockPeriod()] = true; emit ProofSubmitted(id); @@ -227,25 +222,11 @@ abstract contract Proofs is Periods { */ function _markProofAsMissing(SlotId id, Period missedPeriod) internal { uint256 end = _periodEnd(missedPeriod); - if (end >= block.timestamp) { - revert Proofs_PeriodNotEnded(); - } - - if (block.timestamp >= end + _config.timeout) { - revert Proofs_ValidationTimedOut(); - } - - if (_received[id][missedPeriod]) { - revert Proofs_ProofNotMissing(); - } - - if (!_isProofRequired(id, missedPeriod)) { - revert Proofs_ProofNotRequired(); - } - - if (_missing[id][missedPeriod]) { - revert Proofs_ProofAlreadyMarkedMissing(); - } + if (end >= block.timestamp) revert Proofs_PeriodNotEnded(); + if (block.timestamp >= end + _config.timeout) revert Proofs_ValidationTimedOut(); + if (_received[id][missedPeriod]) revert Proofs_ProofNotMissing(); + if (!_isProofRequired(id, missedPeriod)) revert Proofs_ProofNotRequired(); + if (_missing[id][missedPeriod]) revert Proofs_ProofAlreadyMarkedMissing(); _missing[id][missedPeriod] = true; _missed[id] += 1; diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index aaeee61..e2fefd6 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -220,7 +220,7 @@ describe("Marketplace", function () { it("is rejected with insufficient slots ", async function () { request.ask.slots = 0 await expect(marketplace.requestStorage(request)).to.be.revertedWith( - "Insufficient slots" + "Marketplace_InsufficientSlots" ) }) @@ -381,7 +381,7 @@ describe("Marketplace", function () { it("fails if slot is not reserved first", async function () { await expect( marketplace.fillSlot(slot.request, slot.index, proof) - ).to.be.revertedWith("Reservation required") + ).to.be.revertedWith("Marketplace_ReservationRequired") }) }) @@ -824,7 +824,7 @@ describe("Marketplace", function () { switchAccount(client) await expect( marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address) - ).to.be.revertedWith("Marketplace_InvalidClientAddress") + ).to.be.revertedWith("Marketplace_InvalidState") }) it("rejects withdraw when already withdrawn", async function () { @@ -838,7 +838,7 @@ describe("Marketplace", function () { ) await expect( marketplace.withdrawFunds(slot.request, clientWithdrawRecipient.address) - ).to.be.revertedWith("Nothing to withdraw") + ).to.be.revertedWith("Marketplace_NothingToWithdraw") }) it("emits event once request is cancelled", async function () {