Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: evm_setTime doesn't work correctly with `miner.timestampIncrem…
Browse files Browse the repository at this point in the history
…ent` (#3506)
  • Loading branch information
jeffsmale90 committed Aug 21, 2022
1 parent cbc697c commit ca003b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/chains/ethereum/ethereum/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,12 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
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,
Expand Down
49 changes: 49 additions & 0 deletions src/chains/ethereum/ethereum/tests/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ca003b8

Please sign in to comment.