From 0cc8f6c73f6e459b390e013f46c5db2c2dc697ab Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 30 Jan 2025 09:34:42 +0100 Subject: [PATCH] simplify time-based logic in tests, and fix requestEnd() - use the `allowBlocksWithSameTimestamp` hardhat option - remove block time gymnastics from marketplace tests - fix erroneous implementation of requestEnd() which surfaced because of the the improved tests --- contracts/Marketplace.sol | 9 +++-- hardhat.config.js | 1 + test/Marketplace.test.js | 78 +++++++++++++-------------------------- test/Proofs.test.js | 49 ++++++++---------------- test/Vault.tests.js | 17 +++------ test/evm.js | 37 ++++--------------- test/marketplace.js | 8 ++-- 7 files changed, 65 insertions(+), 134 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index f01938d..daf8b4b 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -512,13 +512,14 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { } function requestEnd(RequestId requestId) public view returns (uint256) { - uint256 end = _requestContexts[requestId].endsAt; RequestState state = requestState(requestId); if (state == RequestState.New || state == RequestState.Started) { - return end; - } else { - return Math.min(end, block.timestamp - 1); + return _requestContexts[requestId].endsAt; } + if (state == RequestState.Cancelled) { + return _requestContexts[requestId].expiresAt; + } + return Math.min(_requestContexts[requestId].endsAt, block.timestamp); } function requestExpiry(RequestId requestId) public view returns (uint256) { diff --git a/hardhat.config.js b/hardhat.config.js index c9c8564..6b23933 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -24,6 +24,7 @@ module.exports = { networks: { hardhat: { tags: ["local"], + allowBlocksWithSameTimestamp: true }, codexdisttestnetwork: { url: `${process.env.DISTTEST_NETWORK_URL}`, diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index e2fefd6..5c9de3a 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -23,14 +23,13 @@ const { waitUntilSlotFailed, patchOverloads, } = require("./marketplace") -const { maxPrice, payoutForDuration } = require("./price") +const { maxPrice } = require("./price") const { snapshot, revert, - mine, ensureMinimumBlockHeight, - advanceTimeForNextBlock, - advanceTimeToForNextBlock, + advanceTime, + advanceTimeTo, currentTime, } = require("./evm") const { arrayify } = require("ethers/lib/utils") @@ -172,9 +171,7 @@ describe("Marketplace", function () { it("emits event when storage is requested", async function () { await token.approve(marketplace.address, maxPrice(request)) - - // We +1 second to the expiry because the time will advance with the mined transaction for requestStorage because of Hardhat - const expectedExpiry = (await currentTime()) + request.expiry + 1 + const expectedExpiry = (await currentTime()) + request.expiry await expect(marketplace.requestStorage(request)) .to.emit(marketplace, "StorageRequested") .withArgs(requestId(request), askToArray(request.ask), expectedExpiry) @@ -274,7 +271,7 @@ describe("Marketplace", function () { // We need to advance the time to next period, because filling slot // must not be done in the same period as for that period there was already proof // submitted with the previous `fillSlot` and the transaction would revert with "Proof already submitted". - await advanceTimeForNextBlock(config.proofs.period + 1) + await advanceTime(config.proofs.period + 1) const startBalance = await token.balanceOf(host.address) const discountedCollateral = @@ -421,7 +418,7 @@ describe("Marketplace", function () { await token.approve(marketplace.address, request.ask.collateral) await marketplace.reserveSlot(slot.request, slot.index) await marketplace.fillSlot(slot.request, slot.index, proof) - await advanceTimeForNextBlock(config.proofs.period) + await advanceTime(config.proofs.period) }) it("allows proofs to be submitted", async function () { @@ -464,13 +461,12 @@ describe("Marketplace", 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) + ).to.equal(requestTime + request.ask.duration) }) it("sets request end time to the past once failed", async function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilFailed(marketplace, request) - let slot0 = { ...slot, index: request.ask.maxSlotLoss + 1 } const now = await currentTime() await expect(await marketplace.requestEnd(requestId(request))).to.be.eq( now - 1 @@ -481,7 +477,6 @@ describe("Marketplace", function () { await marketplace.reserveSlot(slot.request, slot.index) await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilCancelled(request) - await mine() const now = await currentTime() await expect(await marketplace.requestEnd(requestId(request))).to.be.eq( now - 1 @@ -491,11 +486,7 @@ describe("Marketplace", function () { it("checks that request end time is in the past once finished", async function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilFinished(marketplace, requestId(request)) - await mine() const now = await currentTime() - // in the process of calling currentTime and requestEnd, - // 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 ) @@ -558,7 +549,7 @@ describe("Marketplace", function () { // We are advancing the time because most of the slots will be filled somewhere // in the "expiry window" and not at its beginning. This is more "real" setup // and demonstrates the partial payout feature better. - await advanceTimeForNextBlock(request.expiry / 2) + await advanceTime(request.expiry / 2) const expectedPayouts = await waitUntilStarted( marketplace, @@ -636,7 +627,7 @@ describe("Marketplace", function () { ).toNumber() await marketplace.reserveSlot(slot.request, slot.index) - await advanceTimeToForNextBlock(filledAt) + await advanceTimeTo(filledAt) await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilCancelled(request) await marketplace.freeSlot(slotId(slot)) @@ -656,7 +647,7 @@ describe("Marketplace", function () { ).toNumber() await marketplace.reserveSlot(slot.request, slot.index) - await advanceTimeToForNextBlock(filledAt) + await advanceTimeTo(filledAt) await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilCancelled(request) const startBalanceHost = await token.balanceOf(host.address) @@ -791,6 +782,10 @@ describe("Marketplace", function () { switchAccount(client) await token.approve(marketplace.address, maxPrice(request)) await marketplace.requestStorage(request) + + // wait a bit, so that there are funds for the client to withdraw + await advanceTime(10) + switchAccount(host) await token.approve(marketplace.address, request.ask.collateral) }) @@ -932,7 +927,7 @@ describe("Marketplace", function () { ).toNumber() await marketplace.reserveSlot(slot.request, slot.index) - await advanceTimeToForNextBlock(filledAt) + await advanceTimeTo(filledAt) await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilCancelled(request) const expectedPartialhostRewardRecipient = @@ -989,7 +984,6 @@ describe("Marketplace", function () { it("changes to 'Cancelled' once request is cancelled", async function () { await waitUntilCancelled(request) - await mine() expect(await marketplace.requestState(slot.request)).to.equal(Cancelled) }) @@ -1011,7 +1005,6 @@ describe("Marketplace", function () { it("changes to 'Failed' once too many slots are freed", async function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilFailed(marketplace, request) - await mine() expect(await marketplace.requestState(slot.request)).to.equal(Failed) }) @@ -1035,7 +1028,6 @@ describe("Marketplace", function () { it("changes to 'Finished' when the request ends", async function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilFinished(marketplace, requestId(request)) - await mine() expect(await marketplace.requestState(slot.request)).to.equal(Finished) }) @@ -1064,16 +1056,14 @@ describe("Marketplace", function () { }) async function waitUntilProofIsRequired(id) { - await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime()))) - await mine() + await advanceTimeTo(periodEnd(periodOf(await currentTime()))) while ( !( (await marketplace.isProofRequired(id)) && (await marketplace.getPointer(id)) < 250 ) ) { - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) } } @@ -1090,7 +1080,6 @@ describe("Marketplace", function () { it("changes to 'Finished' when request finishes", async function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilFinished(marketplace, slot.request) - await mine() expect(await marketplace.slotState(slotId(slot))).to.equal(Finished) }) @@ -1098,7 +1087,6 @@ describe("Marketplace", function () { await marketplace.reserveSlot(slot.request, slot.index) await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilCancelled(request) - await mine() expect(await marketplace.slotState(slotId(slot))).to.equal(Cancelled) }) @@ -1114,8 +1102,7 @@ describe("Marketplace", function () { while ((await marketplace.slotState(slotId(slot))) === Filled) { await waitUntilProofIsRequired(slotId(slot)) const missedPeriod = periodOf(await currentTime()) - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period + 1) await marketplace.markProofAsMissing(slotId(slot), missedPeriod) } expect(await marketplace.slotState(slotId(slot))).to.equal(Repair) @@ -1124,7 +1111,6 @@ describe("Marketplace", function () { it("changes to 'Failed' when request fails", async function () { await waitUntilStarted(marketplace, request, proof, token) await waitUntilSlotFailed(marketplace, request, slot) - await mine() expect(await marketplace.slotState(slotId(slot))).to.equal(Failed) }) @@ -1152,20 +1138,19 @@ describe("Marketplace", function () { async function waitUntilProofWillBeRequired(id) { while (!(await marketplace.willProofBeRequired(id))) { - await mine() + await advanceTime(period) } } async function waitUntilProofIsRequired(id) { - await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime()))) + await advanceTimeTo(periodEnd(periodOf(await currentTime()))) while ( !( (await marketplace.isProofRequired(id)) && (await marketplace.getPointer(id)) < 250 ) ) { - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) } } @@ -1183,7 +1168,6 @@ describe("Marketplace", function () { await waitUntilProofWillBeRequired(id) await expect(await marketplace.willProofBeRequired(id)).to.be.true await waitUntilCancelled(request) - await mine() await expect(await marketplace.willProofBeRequired(id)).to.be.false }) @@ -1194,7 +1178,6 @@ describe("Marketplace", function () { await waitUntilProofIsRequired(id) await expect(await marketplace.isProofRequired(id)).to.be.true await waitUntilCancelled(request) - await mine() await expect(await marketplace.isProofRequired(id)).to.be.false }) @@ -1203,11 +1186,9 @@ describe("Marketplace", function () { await marketplace.reserveSlot(slot.request, slot.index) await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilProofIsRequired(id) - await mine() const challenge1 = await marketplace.getChallenge(id) expect(BigNumber.from(challenge1).gt(0)) await waitUntilCancelled(request) - await mine() const challenge2 = await marketplace.getChallenge(id) expect(BigNumber.from(challenge2).isZero()) }) @@ -1217,11 +1198,9 @@ describe("Marketplace", function () { await marketplace.reserveSlot(slot.request, slot.index) await marketplace.fillSlot(slot.request, slot.index, proof) await waitUntilProofIsRequired(id) - await mine() const challenge1 = await marketplace.getChallenge(id) expect(BigNumber.from(challenge1).gt(0)) await waitUntilCancelled(request) - await mine() const challenge2 = await marketplace.getChallenge(id) expect(BigNumber.from(challenge2).isZero()) }) @@ -1242,16 +1221,14 @@ describe("Marketplace", function () { }) async function waitUntilProofIsRequired(id) { - await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime()))) - await mine() + await advanceTimeTo(periodEnd(periodOf(await currentTime()))) while ( !( (await marketplace.isProofRequired(id)) && (await marketplace.getPointer(id)) < 250 ) ) { - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) } } @@ -1274,7 +1251,7 @@ describe("Marketplace", function () { for (let i = 0; i < slashCriterion; i++) { await waitUntilProofIsRequired(id) let missedPeriod = periodOf(await currentTime()) - await advanceTimeForNextBlock(period + 1) + await advanceTime(period + 1) await marketplace.markProofAsMissing(id, missedPeriod) } const expectedBalance = @@ -1302,7 +1279,7 @@ describe("Marketplace", function () { ) await waitUntilProofIsRequired(slotId(slot)) const missedPeriod = periodOf(await currentTime()) - await advanceTimeForNextBlock(period + 1) + await advanceTime(period + 1) await marketplace.markProofAsMissing(slotId(slot), missedPeriod) } expect(await marketplace.slotState(slotId(slot))).to.equal( @@ -1328,7 +1305,7 @@ describe("Marketplace", function () { ) await waitUntilProofIsRequired(slotId(slot)) const missedPeriod = periodOf(await currentTime()) - await advanceTimeForNextBlock(period + 1) + await advanceTime(period + 1) expect(await marketplace.missingProofs(slotId(slot))).to.equal( missedProofs ) @@ -1361,7 +1338,6 @@ describe("Marketplace", function () { it("keeps request in list when cancelled", async function () { await marketplace.requestStorage(request) await waitUntilCancelled(request) - await mine() expect(await marketplace.myRequests()).to.deep.equal([requestId(request)]) }) @@ -1380,7 +1356,6 @@ describe("Marketplace", function () { switchAccount(host) await waitUntilStarted(marketplace, request, proof, token) await waitUntilFailed(marketplace, request) - await mine() switchAccount(client) expect(await marketplace.myRequests()).to.deep.equal([requestId(request)]) }) @@ -1439,7 +1414,6 @@ describe("Marketplace", function () { await marketplace.reserveSlot(slot.request, slot1.index) await marketplace.fillSlot(slot.request, slot1.index, proof) await waitUntilCancelled(request) - await mine() expect(await marketplace.mySlots()).to.have.members([ slotId(slot), slotId(slot1), diff --git a/test/Proofs.test.js b/test/Proofs.test.js index 4cd5b73..c1e193b 100644 --- a/test/Proofs.test.js +++ b/test/Proofs.test.js @@ -7,8 +7,8 @@ const { mine, ensureMinimumBlockHeight, currentTime, - advanceTimeForNextBlock, - advanceTimeToForNextBlock, + advanceTime, + advanceTimeTo, } = require("./evm") const { periodic } = require("./time") const { loadProof, loadPublicInput } = require("../verifier/verifier") @@ -52,15 +52,13 @@ describe("Proofs", function () { const samples = 256 // 256 samples avoids bias due to pointer downtime await proofs.startRequiringProofs(slotId, probability) - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) let amount = 0 for (let i = 0; i < samples; i++) { if (await proofs.isProofRequired(slotId)) { amount += 1 } - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) } const p = 1 / probability // expected probability @@ -72,8 +70,7 @@ describe("Proofs", function () { it("supports probability 1 (proofs are always required)", async function () { await proofs.startRequiringProofs(slotId, 1) - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) while ((await proofs.getPointer(slotId)) < downtime) { await mine() } @@ -86,8 +83,7 @@ describe("Proofs", function () { await proofs.startRequiringProofs(slotId, probability) while (Math.floor((await currentTime()) / period) == startPeriod) { expect(await proofs.isProofRequired(slotId)).to.be.false - await advanceTimeForNextBlock(Math.floor(period / 10)) - await mine() + await advanceTime(Math.floor(period / 10)) } }) @@ -104,14 +100,11 @@ describe("Proofs", function () { req1 = await proofs.isProofRequired(id1) req2 = await proofs.isProofRequired(id2) req3 = await proofs.isProofRequired(id3) - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) } }) it("moves pointer one block at a time", async function () { - await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime()))) - await mine() for (let i = 0; i < 256; i++) { let previous = await proofs.getPointer(slotId) await mine() @@ -124,14 +117,13 @@ describe("Proofs", function () { describe("when proof requirement is upcoming", function () { async function waitUntilProofWillBeRequired() { while (!(await proofs.willProofBeRequired(slotId))) { - await mine() + await advanceTime(period) } } beforeEach(async function () { await proofs.setSlotState(slotId, SlotState.Filled) await proofs.startRequiringProofs(slotId, probability) - await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime()))) await waitUntilProofWillBeRequired() }) @@ -169,23 +161,20 @@ describe("Proofs", function () { }) async function waitUntilProofIsRequired(slotId) { - await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime()))) - await mine() - while ( !( (await proofs.isProofRequired(slotId)) && (await proofs.getPointer(slotId)) < 250 ) ) { - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) } } it("provides different challenges per period", async function () { await waitUntilProofIsRequired(slotId) const challenge1 = await proofs.getChallenge(slotId) + await advanceTime(period) await waitUntilProofIsRequired(slotId) const challenge2 = await proofs.getChallenge(slotId) expect(challenge2).not.to.equal(challenge1) @@ -225,7 +214,6 @@ describe("Proofs", function () { }) it("fails proof submission when already submitted", async function () { - await advanceTimeToForNextBlock(periodEnd(periodOf(await currentTime()))) await proofs.proofReceived(slotId, proof, pubSignals) await expect( proofs.proofReceived(slotId, proof, pubSignals) @@ -236,8 +224,7 @@ describe("Proofs", function () { expect(await proofs.missingProofs(slotId)).to.equal(0) await waitUntilProofIsRequired(slotId) let missedPeriod = periodOf(await currentTime()) - await advanceTimeToForNextBlock(periodEnd(missedPeriod)) - await mine() + await advanceTimeTo(periodEnd(missedPeriod) + 1) await proofs.markProofAsMissing(slotId, missedPeriod) expect(await proofs.missingProofs(slotId)).to.equal(1) }) @@ -253,7 +240,7 @@ describe("Proofs", function () { it("does not mark a proof as missing after timeout", async function () { await waitUntilProofIsRequired(slotId) let currentPeriod = periodOf(await currentTime()) - await advanceTimeToForNextBlock(periodEnd(currentPeriod) + timeout) + await advanceTimeTo(periodEnd(currentPeriod) + timeout + 1) await expect( proofs.markProofAsMissing(slotId, currentPeriod) ).to.be.revertedWith("Proofs_ValidationTimedOut") @@ -263,8 +250,7 @@ describe("Proofs", function () { await waitUntilProofIsRequired(slotId) let receivedPeriod = periodOf(await currentTime()) await proofs.proofReceived(slotId, proof, pubSignals) - await advanceTimeToForNextBlock(periodEnd(receivedPeriod)) - await mine() + await advanceTimeTo(periodEnd(receivedPeriod) + 1) await expect( proofs.markProofAsMissing(slotId, receivedPeriod) ).to.be.revertedWith("Proofs_ProofNotMissing") @@ -272,12 +258,10 @@ describe("Proofs", function () { it("does not mark proof as missing when not required", async function () { while (await proofs.isProofRequired(slotId)) { - await advanceTimeForNextBlock(period) - await mine() + await advanceTime(period) } let currentPeriod = periodOf(await currentTime()) - await advanceTimeToForNextBlock(periodEnd(currentPeriod)) - await mine() + await advanceTimeTo(periodEnd(currentPeriod) + 1) await expect( proofs.markProofAsMissing(slotId, currentPeriod) ).to.be.revertedWith("Proofs_ProofNotRequired") @@ -286,8 +270,7 @@ describe("Proofs", function () { it("does not mark proof as missing twice", async function () { await waitUntilProofIsRequired(slotId) let missedPeriod = periodOf(await currentTime()) - await advanceTimeToForNextBlock(periodEnd(missedPeriod)) - await mine() + await advanceTimeTo(periodEnd(missedPeriod) + 1) await proofs.markProofAsMissing(slotId, missedPeriod) await expect( proofs.markProofAsMissing(slotId, missedPeriod) diff --git a/test/Vault.tests.js b/test/Vault.tests.js index 1501408..4412dbe 100644 --- a/test/Vault.tests.js +++ b/test/Vault.tests.js @@ -3,7 +3,7 @@ const { ethers } = require("hardhat") const { randomBytes } = ethers.utils const { currentTime, - advanceTimeToForNextBlock, + advanceTimeTo, mine, setAutomine, snapshot, @@ -324,7 +324,7 @@ describe("Vault", function () { it("does not allow withdrawal before lock expires", async function () { await vault.lock(context, expiry, expiry) - await advanceTimeToForNextBlock(expiry - 1) + await advanceTimeTo(expiry - 1) const withdrawing = vault.withdraw(context, account.address) await expect(withdrawing).to.be.revertedWith("Locked") }) @@ -350,7 +350,7 @@ describe("Vault", function () { it("allows withdrawal after lock expires", async function () { await vault.lock(context, expiry, expiry) - await advanceTimeToForNextBlock(expiry) + await advanceTimeTo(expiry) const before = await token.balanceOf(account.address) await vault.withdraw(context, account.address) const after = await token.balanceOf(account.address) @@ -379,7 +379,7 @@ describe("Vault", function () { it("cannot extend an expired lock", async function () { await vault.lock(context, expiry, maximum) - await advanceTimeToForNextBlock(expiry) + await advanceTimeTo(expiry) const extending = vault.extendLock(context, maximum) await expect(extending).to.be.revertedWith("LockRequired") }) @@ -392,7 +392,7 @@ describe("Vault", function () { it("deletes lock when funds are withdrawn", async function () { await vault.lock(context, expiry, expiry) - await advanceTimeToForNextBlock(expiry) + await advanceTimeTo(expiry) await vault.withdraw(context, account.address) expect((await vault.getLock(context))[0]).to.equal(0) expect((await vault.getLock(context))[1]).to.equal(0) @@ -419,11 +419,6 @@ describe("Vault", function () { return await vault.getBalance(context, recipient) } - async function advanceTimeTo(timestamp) { - await advanceTimeToForNextBlock(timestamp) - await mine() - } - it("requires that a lock is set", async function () { await expect(vault.flow(context, sender, receiver, 2)).to.be.revertedWith( "LockRequired" @@ -512,7 +507,7 @@ describe("Vault", function () { await vault.flow(context, sender, receiver2, 2) await mine() const start = await currentTime() - advanceTimeToForNextBlock(start + 4) + advanceTimeTo(start + 4) await vault.flow(context, receiver2, receiver, 1) await mine() expect(await getBalance(sender)).to.equal(deposit - 12) diff --git a/test/evm.js b/test/evm.js index 8242781..6f52fad 100644 --- a/test/evm.js +++ b/test/evm.js @@ -12,7 +12,7 @@ async function snapshot() { async function revert() { const { id, time, automine } = snapshots.pop() await ethers.provider.send("evm_revert", [id]) - await ethers.provider.send("evm_setNextBlockTimestamp", [time + 1]) + await ethers.provider.send("evm_setNextBlockTimestamp", [time]) await ethers.provider.send("evm_setAutomine", [automine]) } @@ -20,13 +20,6 @@ async function setAutomine(enabled) { await ethers.provider.send("evm_setAutomine", [enabled]) } -/** - * Mines new block. - * - * This call increases the block's timestamp by 1! - * - * @returns {Promise} - */ async function mine() { await ethers.provider.send("evm_mine") } @@ -42,30 +35,14 @@ async function currentTime() { return block.timestamp } -/** - * Function that advances time by adding seconds to current timestamp for **next block**. - * - * If you need the timestamp to be already applied for current block then mine a new block with `mine()` after this call. - * This is mainly needed when doing assertions on top of view calls that does not create transactions and mine new block. - * - * @param timestamp - * @returns {Promise} - */ -async function advanceTimeForNextBlock(seconds) { +async function advanceTime(seconds) { await ethers.provider.send("evm_increaseTime", [seconds]) + await mine() } -/** - * Function that sets specific timestamp for **next block**. - * - * If you need the timestamp to be already applied for current block then mine a new block with `mine()` after this call. - * This is mainly needed when doing assertions on top of view calls that does not create transactions and mine new block. - * - * @param timestamp - * @returns {Promise} - */ -async function advanceTimeToForNextBlock(timestamp) { +async function advanceTimeTo(timestamp) { await ethers.provider.send("evm_setNextBlockTimestamp", [timestamp]) + await mine() } module.exports = { @@ -75,6 +52,6 @@ module.exports = { mine, ensureMinimumBlockHeight, currentTime, - advanceTimeForNextBlock, - advanceTimeToForNextBlock, + advanceTime, + advanceTimeTo, } diff --git a/test/marketplace.js b/test/marketplace.js index 218e6c3..98ac111 100644 --- a/test/marketplace.js +++ b/test/marketplace.js @@ -1,6 +1,6 @@ -const { advanceTimeToForNextBlock, currentTime } = require("./evm") +const { advanceTimeTo, currentTime, mine } = require("./evm") const { slotId, requestId } = require("./ids") -const { maxPrice, payoutForDuration } = require("./price") +const { payoutForDuration } = require("./price") /** * @dev This will not advance the time right on the "expiry threshold" but will most probably "overshoot it" @@ -11,7 +11,7 @@ const { maxPrice, payoutForDuration } = require("./price") */ async function waitUntilCancelled(request) { // We do +1, because the expiry check in contract is done as `>` and not `>=`. - await advanceTimeToForNextBlock((await currentTime()) + request.expiry + 1) + await advanceTimeTo((await currentTime()) + request.expiry + 1) } async function waitUntilSlotsFilled(contract, request, proof, token, slots) { @@ -46,7 +46,7 @@ async function waitUntilStarted(contract, request, proof, token) { async function waitUntilFinished(contract, requestId) { const end = (await contract.requestEnd(requestId)).toNumber() // We do +1, because the end check in contract is done as `>` and not `>=`. - await advanceTimeToForNextBlock(end + 1) + await advanceTimeTo(end + 1) } async function waitUntilFailed(contract, request) {