2

I am doing some experiments with block.timestamp(which is now's alias). I wrote some code to see if block.timestamp really represents the timestamp of the last mined block.

From the documentation:

block.timestamp (uint): current block timestamp as seconds since unix epoch

Below code is expected to return the timestamp of the last mined block(since it's just a call, not a transaction), but it seems to be returning the "current timestamp" which is unexpected.

(in solidity contract, tested with solidity v0.4.23):

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

(in truffle js test case, tested with truffle v4.1.7):

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

Sample output with truffle test command:

[ { [String: '1524691464'] s: 1, e: 9, c: [ 1524691464 ] },
{ [String: '166'] s: 1, e: 2, c: [ 166 ] } ]
[ { [String: '1524691465'] s: 1, e: 9, c: [ 1524691465 ] },
{ [String: '166'] s: 1, e: 2, c: [ 166 ] } ]
[ { [String: '1524691466'] s: 1, e: 9, c: [ 1524691466 ] },
{ [String: '166'] s: 1, e: 2, c: [ 166 ] } ]
[ { [String: '1524691467'] s: 1, e: 9, c: [ 1524691467 ] },
{ [String: '166'] s: 1, e: 2, c: [ 166 ] } ]
[ { [String: '1524691468'] s: 1, e: 9, c: [ 1524691468 ] },
{ [String: '166'] s: 1, e: 2, c: [ 166 ] } ]
[ { [String: '1524691469'] s: 1, e: 9, c: [ 1524691469 ] },
{ [String: '166'] s: 1, e: 2, c: [ 166 ] } ]

On the other hand, when I manually call the js function using the web3 interface in the browser(contractInstance.methods.test().call()), it works as expected.

Sample output:

p {0: "1524690793", 1: "157"}
p {0: "1524690793", 1: "157"}
p {0: "1524690793", 1: "157"}
p {0: "1524690793", 1: "157"}
p {0: "1524690793", 1: "157"}
p {0: "1524690793", 1: "157"}
p {0: "1524690793", 1: "157"}

So why is this? What's the catch when I call the function in truffle test? Any ideas?

Note: I've created a repo for this. If you would like to test, you can simply run truffle test after setting up local mining node(e.g.ganache).

  • 2
    Doesn't seem weird to me. JavaScript can't handle large integers like Solidity can, so Web3 and Truffle use BigNumber objects to represent them. Check out the docs – Henk Apr 26 '18 at 12:25
  • @Henk I'm not saying it's weird because the uint return type gets converted to string or to big number. It's weird because the timestamp value increases in truffle test! Do you have any idea what's going on? – viz Apr 26 '18 at 15:03
  • I just tried your contract and for me this works just fine, so I'd say this is not related to your code but to ganache/truffle? I was using TestRPC v6.0.3 and truffle v4.1.7. – Kai Kälberer Apr 26 '18 at 15:36
  • 1
    Yes - cloned the repo, started testrpc and ran truffle test. – Kai Kälberer Apr 26 '18 at 16:10
  • 1
    I guess what I learned by now is that you always should try to check on a "real rpc" if you are unsure about the behaviour there. it mostly comes down to testrpc, ganache, ... :) happy to have helped! – Kai Kälberer Apr 26 '18 at 16:43

Your Answer

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

Browse other questions tagged or ask your own question.