1

I need to execute a function 24hrs after a certain timestamp is recorded in my contract. So my front-end starts a countdown, taking that contract timestamp as start.

After 24 hrs, my front-end calls the contract function.

Question: if my contract function has this:

require(block.timestamp >= countdownStartTimestamp + ONE_DAY);

Where ONE_DAY = 86400 (24hrs)

Will my function possibly fail if last block mined is closer than 24hrs? Or does block.timestamp get updated with actual current block timestamp before executing my function, even if still not mined entirely? Sorry I don't know these transactions mining details :/

1
  • 2
    Keep in mind that the correctly constructed contract will work, but it won't be dependably accurate in terms of temporal time because of the error margin in block.timestamp. Commented Feb 10 at 16:13

2 Answers 2

2

Will my function possibly fail if last block mined is closer than 24hrs? Or does block.timestamp get updated with actual current block timestamp before executing my function, even if still not mined entirely? Sorry I don't know these transactions mining details :/

In Solidity, block.timestamp is a crucial global variable that provides the current timestamp of the block being mined. (https://metaschool.so/articles/what-is-block-timestamp-in-solidity/)

So it's pretty safe to trust this Solidity timer, because it'll compare the EPOCH timestamp of the block that is being mined during your transaction.

The only concern would be, if you were checking expiry timestamps, your function might fail because it can be front-run by other accounts, and the front-end timer will call the blockchain function too late. But that's not the case here, because you're checking >=.

1

Due to the network distributed nature and transactions propagation time. It could be possible that block producers clock aren't synchronized with the frontend clock. It is feasible that the transaction could fail.

If you want to be absolutely sure it won't fail you have to wait for a block with a timestamp that is greater that the value you are expecting. In the Ethereum blockchain block's timestamp are always going forward.

1
  • 1
    So you suggest to periodically read the mined blocks timestamps and triggering the function only when the last mined block timestamp is 24hrs past my set "start". Makes sense and sounds way safer. I was exactly worried about miners clocks that could assign a timestamp shorter than 24 hrs. Nice safety tip, thanks much!
    – user130657
    Commented Feb 14 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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