From 8c6891f1e210424c4b9d257a5194f17f56ae3f26 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 17 Jan 2023 09:05:44 +0100 Subject: [PATCH] [marketplace] slotIsNotFree() modifier Replaces _slot getter --- contracts/Marketplace.sol | 15 +++++++-------- contracts/TestMarketplace.sol | 4 ---- test/Marketplace.test.js | 8 +------- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index db67205..9f9d942 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -102,8 +102,8 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { } } - function freeSlot(SlotId slotId) public { - Slot storage slot = _slot(slotId); + function freeSlot(SlotId slotId) public slotIsNotFree(slotId) { + Slot storage slot = slots[slotId]; require(slot.host == msg.sender, "Slot filled by other host"); SlotState state = slotState(slotId); require(state != SlotState.Paid, "Already paid"); @@ -133,7 +133,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { } function _forciblyFreeSlot(SlotId slotId) internal marketplaceInvariant { - Slot storage slot = _slot(slotId); + Slot storage slot = slots[slotId]; RequestId requestId = slot.requestId; RequestContext storage context = requestContexts[requestId]; @@ -174,7 +174,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { Request storage request = requests[requestId]; context.state = RequestState.Finished; removeFromMyRequests(request.client, requestId); - Slot storage slot = _slot(slotId); + Slot storage slot = slots[slotId]; removeFromMySlots(slot.host, slotId); @@ -226,10 +226,9 @@ contract Marketplace is Collateral, Proofs, StateRetrieval { return requests[requestId]; } - function _slot(SlotId slotId) internal view returns (Slot storage) { - Slot storage slot = slots[slotId]; - require(slot.state != SlotState.Free, "Slot empty"); - return slot; + modifier slotIsNotFree(SlotId slotId) { + require(slots[slotId].state != SlotState.Free, "Slot is free"); + _; } function proofPeriod() public view returns (uint256) { diff --git a/contracts/TestMarketplace.sol b/contracts/TestMarketplace.sol index 3815386..2e3a08b 100644 --- a/contracts/TestMarketplace.sol +++ b/contracts/TestMarketplace.sol @@ -33,8 +33,4 @@ contract TestMarketplace is Marketplace { function forciblyFreeSlot(SlotId slotId) public { _forciblyFreeSlot(slotId); } - - function slot(SlotId slotId) public view returns (Slot memory) { - return _slot(slotId); - } } diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index 18043a6..52be8db 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -297,7 +297,7 @@ describe("Marketplace", function () { slot.index = 5 let nonExistentId = slotId(slot) await expect(marketplace.freeSlot(nonExistentId)).to.be.revertedWith( - "Slot empty" + "Slot is free" ) }) @@ -320,12 +320,6 @@ describe("Marketplace", function () { .to.emit(marketplace, "SlotFreed") .withArgs(slot.request, id) }) - - it("cannot get slot once freed", async function () { - await waitUntilStarted(marketplace, request, proof) - await marketplace.freeSlot(id) - await expect(marketplace.slot(id)).to.be.revertedWith("Slot empty") - }) }) describe("paying out a slot", function () {