[marketplace] slotIsNotFree() modifier

Replaces _slot getter
This commit is contained in:
Mark Spanbroek 2023-01-17 09:05:44 +01:00 committed by markspanbroek
parent 944e9c9da2
commit 8c6891f1e2
3 changed files with 8 additions and 19 deletions

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -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 () {