1

My contract has time dependent actions, which I want to test. How can I mock the clock in the smart contract such that I can set it to any time I want?

An example of one action that is time dependent is as follows:

void sampleact(const account_name user) {
    // Check for user authentication.
    require_auth(user);
    // Check if passed day is between the start and end.
    eosio_assert(getToday() > 0 && getToday() <= 30, "Invalid day");
    // getToday() returns floor(current_time() - start_time) / (24 * 3600). 
    // I WANT TO MOCK CURRENT_TIME().

   ...
   code
   ...

}

Then in the unit test I want to do the following.

  • test sampleact().
  • advance the blockchain clock 31 days.
  • test sampleact(), which will fail the assert seen above.
1

You can use timestamp to have this functionality. You can use now() this function will return you current timestamp(in seconds),you can set your own timestamp with help of this function Let say for example you want to increase timestamp by 24 hour then you can simpally do it by now() + 84600.

| improve this answer | |
  • Does EOS automated test tools provide a way to set the timestamp of the block? Using proper test suite and manipulating blocks directly is more throughout approach to test this. This comes from my experience with other blockchains, I do not have necessary experience with EOS development tools yet to give a definitive answer. – Mikko Ohtamaa Apr 22 '19 at 21:16

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.