[marketplace] remove _isCancelled and _isFinished

This commit is contained in:
Mark Spanbroek 2023-01-16 14:14:15 +01:00 committed by markspanbroek
parent 9aa4773392
commit e496ac3550
3 changed files with 13 additions and 59 deletions

View File

@ -179,8 +179,10 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
RequestId requestId, RequestId requestId,
SlotId slotId SlotId slotId
) private marketplaceInvariant { ) private marketplaceInvariant {
RequestState requestState = state(requestId);
require( require(
_isFinished(requestId) || _isCancelled(requestId), requestState == RequestState.Finished ||
requestState == RequestState.Cancelled,
"Contract not ended" "Contract not ended"
); );
RequestContext storage context = _context(requestId); RequestContext storage context = _context(requestId);
@ -225,30 +227,6 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
require(token.transfer(msg.sender, amount), "Withdraw failed"); require(token.transfer(msg.sender, amount), "Withdraw failed");
} }
/// @notice Return true if the request state is RequestState.Cancelled or if the request expiry time has elapsed and the request was never started.
/// @dev Handles the case when a request may have been cancelled, but the client has not withdrawn its funds yet, and therefore the state has not yet been updated.
/// @param requestId the id of the request
/// @return true if request is cancelled
function _isCancelled(RequestId requestId) internal view returns (bool) {
RequestContext storage context = _context(requestId);
return
context.state == RequestState.Cancelled ||
(context.state == RequestState.New &&
block.timestamp > _request(requestId).expiry);
}
/// @notice Return true if the request state is RequestState.Finished or if the request duration has elapsed and the request was started.
/// @dev Handles the case when a request may have been finished, but the state has not yet been updated by a transaction.
/// @param requestId the id of the request
/// @return true if request is finished
function _isFinished(RequestId requestId) internal view returns (bool) {
RequestContext memory context = _context(requestId);
return
context.state == RequestState.Finished ||
(context.state == RequestState.Started &&
block.timestamp > context.endsAt);
}
/// @notice Return id of request that slot belongs to /// @notice Return id of request that slot belongs to
/// @dev Returns requestId that is mapped to the slotId /// @dev Returns requestId that is mapped to the slotId
/// @param slotId id of the slot /// @param slotId id of the slot
@ -354,12 +332,17 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
} }
function state(RequestId requestId) public view returns (RequestState) { function state(RequestId requestId) public view returns (RequestState) {
if (_isCancelled(requestId)) { RequestContext storage context = _context(requestId);
if (
context.state == RequestState.New &&
block.timestamp > _request(requestId).expiry
) {
return RequestState.Cancelled; return RequestState.Cancelled;
} else if (_isFinished(requestId)) { } else if (
context.state == RequestState.Started && block.timestamp > context.endsAt
) {
return RequestState.Finished; return RequestState.Finished;
} else { } else {
RequestContext storage context = _context(requestId);
return context.state; return context.state;
} }
} }

View File

@ -30,10 +30,6 @@ contract TestMarketplace is Marketplace {
} }
function isCancelled(RequestId requestId) public view returns (bool) {
return _isCancelled(requestId);
}
function forciblyFreeSlot(SlotId slotId) public { function forciblyFreeSlot(SlotId slotId) public {
_forciblyFreeSlot(slotId); _forciblyFreeSlot(slotId);
} }

View File

@ -653,12 +653,6 @@ describe("Marketplace", function () {
) )
}) })
it("changes isCancelled to true once request is cancelled", async function () {
await expect(await marketplace.isCancelled(slot.request)).to.be.false
await waitUntilCancelled(request)
await expect(await marketplace.isCancelled(slot.request)).to.be.true
})
it("changes proofEnd to the past when request is cancelled", async function () { it("changes proofEnd to the past when request is cancelled", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof) await marketplace.fillSlot(slot.request, slot.index, proof)
await expect(await marketplace.proofEnd(slotId(slot))).to.be.gt( await expect(await marketplace.proofEnd(slotId(slot))).to.be.gt(
@ -836,7 +830,7 @@ describe("Marketplace", function () {
}) })
describe("accepting proofs", function () { describe("accepting proofs", function () {
it("fails when request Cancelled (isCancelled is true)", async function () { it("fails when request Cancelled", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof) await marketplace.fillSlot(slot.request, slot.index, proof)
await waitUntilCancelled(request) await waitUntilCancelled(request)
await expect( await expect(
@ -844,17 +838,7 @@ describe("Marketplace", function () {
).to.be.revertedWith("Slot not accepting proofs") ).to.be.revertedWith("Slot not accepting proofs")
}) })
it("fails when request Cancelled (state set to Cancelled)", async function () { it("fails when request Finished", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof)
await waitUntilCancelled(request)
switchAccount(client)
await marketplace.withdrawFunds(slot.request)
await expect(
marketplace.testAcceptsProofs(slotId(slot))
).to.be.revertedWith("Slot not accepting proofs")
})
it("fails when request Finished (isFinished is true)", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request)) await waitUntilFinished(marketplace, requestId(request))
await expect( await expect(
@ -862,15 +846,6 @@ describe("Marketplace", function () {
).to.be.revertedWith("Slot not accepting proofs") ).to.be.revertedWith("Slot not accepting proofs")
}) })
it("fails when request Finished (state set to Finished)", async function () {
await waitUntilStarted(marketplace, request, proof)
await waitUntilFinished(marketplace, requestId(request))
await marketplace.freeSlot(slotId(slot))
await expect(
marketplace.testAcceptsProofs(slotId(slot))
).to.be.revertedWith("Slot not accepting proofs")
})
it("fails when request Failed", async function () { it("fails when request Failed", async function () {
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFailed(marketplace, request) await waitUntilFailed(marketplace, request)