1

Sometimes, when I use the ganache specific evm_increaseTime method and advance to the next block, the next timestamp is NOT:

current timestamp + advanced time

But instead:

current timestamp + advanced time + 1 second

Where does this second come from? Is it the core compute time of the transaction? Leap seconds?

RPC logs:

eth_sendTransaction

  Transaction: 0x9cc8e2006d13f3349594a284a8f9ec8489c8bff6d57022143bb4457b30ee50b1
  Gas usage: 224072
  Block Number: 226
  Block Time: Fri Aug 09 2019 11:01:40 GMT+0200 (Central European Summer Time)

eth_getTransactionReceipt
evm_increaseTime
evm_mine
eth_getBlockByNumber
eth_getBlockByNumber
eth_getBlockByNumber
eth_call
eth_getBlockByNumber
eth_call
eth_getBlockByNumber
eth_sendTransaction

  Transaction: 0x2126e0a1ac901864c7dacabb7a6228f6753e5ad3ceb65f1957ecaaa395bc8569
  Gas usage: 72408
  Block Number: 228
  Block Time: Fri Aug 09 2019 11:02:46 GMT+0200 (Central European Summer Time)

eth_getTransactionReceipt
eth_getBlockByNumber
eth_call
eth_getBlockByNumber
eth_call

And you can see, the second time stamp is "11:02:46" whereas it should be "11:02:45", because the code that I'm executing in the tests is this:

web3.utils.advanceTimeAndBlock(new BigNumber(65));
  • 1
    Definitely not leap seconds. Everything is managed in seconds (not days, months, years), so the term leap is not applicable here. – goodvibration Aug 9 at 7:21
4

I wrote an entire article on this: Writing Accurate Time-Dependent Truffle Tests

TL;DR

  1. The 1 second comes from run time costs, i.e. the time it takes for the javascript promises to resolve.
  2. For increased accuracy, you must use evm_mine with a parameter for the number of seconds you want to jump forwards or backwards in time. Read this PR for more details.
const advanceBlockAtTime = (time) => {
  return new Promise((resolve, reject) => {
    web3.currentProvider.send(
      {
        jsonrpc: "2.0",
        method: "evm_mine",
        params: [time],
        id: new Date().getTime(),
      },
      (err, _) => {
        if (err) {
          return reject(err);
        }
        const newBlockHash = web3.eth.getBlock("latest").hash;

        return resolve(newBlockHash);
      },
    );
  });
};
0

this is because evm_mine increase arbitrary times to the latest block and there is no way to fix it because it is how ganache work you can read more at here: https://github.com/trufflesuite/ganache-cli/issues/463

Update: You can't fix the arbitrary times but to get consistency every time you run evm_mine add params in your send method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.