ganache icon indicating copy to clipboard operation
ganache copied to clipboard

`evm_setTime` doesn't work correctly with `miner.timestampIncrement`

Open jeffsmale90 opened this issue 3 years ago • 1 comments

When using miner.timestampIncrement, calling evm_setTime can result in unexpected values, and potentially with an error being thrown.

This is because the timeAdjustment is applied to the previous blocktime, so this offset is applied for every block that is mined.

With the following example, the initial block is mined correctly, as is the first block after setting the time, but subsequent blocks will have a descending block time, until the value becomes negative.

const p = await getProvider({
  miner: {timestampIncrement: 1},
  chain: {time : 10000}
});

const mineAndShowBlock = async () => {
  await p.send("evm_mine");
  const {number, timestamp} = await p.send("eth_getBlockByNumber", ["latest"]);

  console.log({number, timestamp: Quantity.toNumber(timestamp)});  
}
await mineAndShowBlock();

await p.send("evm_setTime", [5000]);

while(true) {
  await mineAndShowBlock();
}
{ number: '0x1', timestamp: 11 }
{ number: '0x2', timestamp: 6 }
{ number: '0x3', timestamp: 1 }

Error: Cannot wrap a negative value as a json-rpc type

jeffsmale90 avatar Aug 07 '22 23:08 jeffsmale90

I have decided to close #3265 in favour of this approach. I still think there's an opportunity to improve the implementation, and to add validation, but I will open that in a separate PR.

jeffsmale90 avatar Aug 07 '22 23:08 jeffsmale90

Is there a specific issue for what this PR fixes? If so, can you update the PR description to indicate that this PR fixes it before you squash and merge? If not, squash and merge away!

I think this is the issue #3330.

Also, don't forget to update the PR title!

davidmurdoch avatar Aug 18 '22 17:08 davidmurdoch