question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behavior with `block.timestamp` in truffle test

See original GitHub issue
  • [O] I’ve asked for help in the Truffle Gitter before filing this issue.

Issue

According to the documentation, block.timestamp should be the timestamp of the last mined block.

However, in truffle test environment, block.timestamp keeps increasing(seemingly the current timestamp) when returned in a view function.

Calling the function manually through web3 interface, it returns the expected value(unchanged timestamp of last mined block).

Steps to Reproduce

Fork https://github.com/vzts/block-timestamp-test, setup local mining node(e.g. ganache), run truffle test.

Below is the minimum example code: (in a solidity contract):

function test() public view returns(uint, uint) {
    return (block.timestamp, block.number);
}

(in a truffle js test case):

it("test", async function () {
    for (let i = 0; i < 100; i++) {
        console.log(await contractInstance.test())
        sleep(1000) // pseudo code for sleep
    }
})

Expected Behavior

Should return the same timestamp and the number for the last mined block upon every request (in an environment where no new transaction is executed(no new block is mined))

Sample output:

[β€œ1524690793”, β€œ157”] [β€œ1524690793”, β€œ157”] [β€œ1524690793”, β€œ157”] [β€œ1524690793”, β€œ157”] [β€œ1524690793”, β€œ157”] [β€œ1524690793”, β€œ157”] [β€œ1524690793”, β€œ157”]

Actual Results

Returning the β€œcurrent”(not sure what is happening, but it seems so) timestamp.

Sample output:

[β€œ1524690799”, β€œ157”] [β€œ1524690800”, β€œ157”] [β€œ1524690801”, β€œ157”] [β€œ1524690802”, β€œ157”] [β€œ1524690803”, β€œ157”] [β€œ1524690804”, β€œ157”] [β€œ1524690805”, β€œ157”]

Environment

  • Operating System: macOS High Sierra 10.13.4
  • Ethereum client: Ganache 1.1.0
  • Truffle version (truffle version): 4.1.7
  • node version (node --version): 8.4.0
  • npm version (npm --version): 6.0.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
cgeweckecommented, Apr 26, 2018

@vzts Update, just had a chat with @benjamincburns, the engineer who writes ganache. He suggested there’s enough ambiguity about correct behavior here that we should really look at how geth and parity handle this. If they both let the clock tick, ganache should tick. If only one lets the clock tick, ganache should side with keeping block.timestamp fixed at the latest block because that’s more intuitive (and more useful for tests ).

1reaction
cgeweckecommented, May 6, 2018

@vzts Update: Have run your test against both geth dev and a Parity POA network and both report a static time within the block, so Ganache’s behavior is non-normative. Will report back to @benjamincburns and open a PR over there fixing this.

Hope you don’t mind but I’m going to close this and open a referral issue over there. Thanks again for discovering this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected behavior with `block.timestamp` in truffle test
I wrote some code to see if block.timestamp really represents the timestamp of the last mined block. From the documentation: block.timestamp (Β ...
Read more >
Writing Accurate Time-Dependent Truffle Tests - Medium
I logged the unix timestamp in mocha's before block and I measured how long it takes for the test to run using node's...
Read more >
RSK Workshop: Smart Contract Testing using Truffle
This Truffle's analogue of using Mocha describe blocks to group the tests when testing a Javascript implementation. Note also that the callback function...
Read more >
Interact with your contracts - Truffle Suite
However, if you try to convert a BN that's larger than the largest integer supported by Javascript, you'll likely run into errors or...
Read more >
PROPS Token Contracts Audit - OpenZeppelin blog
Consider adding the test coverage report, and making it reach at least 95% ... Unexpected behavior in decreaseApprovalPreSigned function ofΒ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found