diff --git a/src/chains/ethereum/ethereum/src/blockchain.ts b/src/chains/ethereum/ethereum/src/blockchain.ts index 1e35b9fa15..8196bf2850 100644 --- a/src/chains/ethereum/ethereum/src/blockchain.ts +++ b/src/chains/ethereum/ethereum/src/blockchain.ts @@ -596,6 +596,12 @@ export default class Blockchain extends Emittery { onlyOneBlock: boolean = false ) => { const nextBlock = this.#readyNextBlock(this.blocks.latest, timestamp); + + // if block time is incremental, adjustments should only apply once, + // otherwise they accumulate with each block. + if (this.#options.miner.timestampIncrement !== "clock") { + this.#timeAdjustment = 0; + } const transactions = await this.#miner.mine( nextBlock, maxTransactions, diff --git a/src/chains/ethereum/ethereum/tests/provider.test.ts b/src/chains/ethereum/ethereum/tests/provider.test.ts index 9021b6c816..34f0ae02ac 100644 --- a/src/chains/ethereum/ethereum/tests/provider.test.ts +++ b/src/chains/ethereum/ethereum/tests/provider.test.ts @@ -141,6 +141,55 @@ describe("provider", () => { await provider.disconnect(); }); + it("applies the adjustment only once when `timestampIncrement` is used", async () => { + const time = new Date("2019-01-01T00:00:00.000Z"); + const timestampIncrement = 5; // seconds + const fastForwardSeconds = 100; + const provider = await getProvider({ + chain: { time }, + miner: { timestampIncrement } + }); + + await provider.request({ + method: "evm_increaseTime", + params: [`0x${fastForwardSeconds.toString(16)}`] + }); + + const mineAndAssertTimestamp = async ( + expectedTimestampSeconds: number, + message?: string + ) => { + await provider.request({ + method: "evm_mine", + params: [] + }); + const { timestamp } = await provider.request({ + method: "eth_getBlockByNumber", + params: ["latest", false] + }); + assert.strictEqual( + timestamp, + `0x${expectedTimestampSeconds.toString(16)}`, + message + ); + }; + + let startTimeSeconds = Math.floor(+time / 1000); + + await mineAndAssertTimestamp( + startTimeSeconds + fastForwardSeconds + timestampIncrement, + "unexpected timestamp for the first block mined" + ); + await mineAndAssertTimestamp( + startTimeSeconds + fastForwardSeconds + timestampIncrement * 2, + "unexpected timestamp for the second block mined" + ); + await mineAndAssertTimestamp( + startTimeSeconds + fastForwardSeconds + timestampIncrement * 3 + ), + "unexpected timestamp for the third block mined"; + }); + it("uses the `timestampIncrement` for the first block when forking", async () => { const time = new Date("2019-01-01T00:00:00.000Z"); const timestampIncrement = 5;