From 4a470c7dc738824b84f5aca9617dbf1c4322714e Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Wed, 26 Oct 2022 13:15:42 +1100 Subject: [PATCH] [marketplace] improve requestEnd to check all states MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There may be cases where the the request end is not accurate as the state of the request hasn’t yet been updated. For example, when a request is cancelled, the request end would not have been updated to be in the past, and would still be set for the end of the request (which could be in the future). --- contracts/Marketplace.sol | 9 ++++-- test/Marketplace.test.js | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 54d68d5..0b54d34 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -268,7 +268,7 @@ contract Marketplace is Collateral, Proofs { function proofEnd(SlotId slotId) public view returns (uint256) { Slot memory slot = _slot(slotId); - uint256 end = requestEnd(slot.requestId); + uint256 end = _end(_toEndId(slot.requestId)); if (_slotAcceptsProofs(slotId)) { return end; } else { @@ -277,7 +277,12 @@ contract Marketplace is Collateral, Proofs { } function requestEnd(RequestId requestId) public view returns (uint256) { - return _end(_toEndId(requestId)); + uint256 end = _end(_toEndId(requestId)); + if(_requestAcceptsProofs(requestId)) { + return end; + } else { + return Math.min(end, block.timestamp - 1); + } } function _price( diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index ce352bf..674429d 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -271,6 +271,72 @@ describe("Marketplace", function () { }) }) + describe("request end", function () { + var requestTime + beforeEach(async function () { + switchAccount(client) + await token.approve(marketplace.address, price(request)) + await marketplace.requestStorage(request) + requestTime = await currentTime() + switchAccount(host) + await token.approve(marketplace.address, collateral) + await marketplace.deposit(collateral) + }) + + it("shares request end time for all slots in request", async function () { + const lastSlot = request.ask.slots - 1 + for (let i = 0; i < lastSlot; i++) { + await marketplace.fillSlot(slot.request, i, proof) + } + advanceTime(minutes(10)) + await marketplace.fillSlot(slot.request, lastSlot, proof) + let slot0 = { ...slot, index: 0 } + let end = await marketplace.requestEnd(requestId(request)) + for (let i = 1; i <= lastSlot; i++) { + let sloti = { ...slot, index: i } + await expect((await marketplace.proofEnd(slotId(sloti))) === end) + } + }) + + it("sets the request end time to now + duration", async function () { + await marketplace.fillSlot(slot.request, slot.index, proof) + await expect( + (await marketplace.requestEnd(requestId(request))).toNumber() + ).to.be.closeTo(requestTime + request.ask.duration, 1) + }) + + it("sets request end time to the past once failed", async function () { + await waitUntilStarted(marketplace, request, slot, proof) + await waitUntilFailed(marketplace, request, slot) + let slot0 = { ...slot, index: request.ask.maxSlotLoss + 1 } + const now = await currentTime() + await expect(await marketplace.requestEnd(requestId(request))).to.be.eq( + now - 1 + ) + }) + + it("sets request end time to the past once cancelled", async function () { + await marketplace.fillSlot(slot.request, slot.index, proof) + await waitUntilCancelled(request) + const now = await currentTime() + await expect(await marketplace.requestEnd(requestId(request))).to.be.eq( + now - 1 + ) + }) + + it("checks that request end time is in the past once finished", async function () { + await waitUntilStarted(marketplace, request, slot, proof) + await waitUntilFinished(marketplace, requestId(request)) + const now = await currentTime() + // in the process of calling currentTime and proofEnd, + // block.timestamp has advanced by 1, so the expected proof end time will + // be block.timestamp - 1. + await expect(await marketplace.requestEnd(requestId(request))).to.be.eq( + now - 1 + ) + }) + }) + describe("freeing a slot", function () { var id beforeEach(async function () {