2

In my code there is a function which will be called once every 2 months by me. I would like to make it impossible to call it more often than once every 2 months for some security reasons.

I found out that I can store time and in the very beginning of the function simply add assert until time gets greater than variable, and when function goes complete, it stores current time + 2 months to the variable.

It's pretty simple, but is it safe? Can time be anyhow hacked or written by block miners? Or maybe is there any other way to make it possible to lock function for specific amount of time?

  • Side note: if you're using Truffle for testing, then you can test it via web3.currentProvider.send({method: "evm_increaseTime", params: [60 * 60 * 24 * 60]}). – goodvibration Nov 14 '18 at 3:39
  • And if you're using web3.py for testing, then you can test it via web3.providers[0].make_request("evm_increaseTime", [60 * 60 * 24 * 60]). – goodvibration Nov 14 '18 at 3:41
1

The timestamp in a block is required by the protocol to be at most 900 seconds ahead of a node's current clock time, see the block protocol wiki. So yes, it is safe to do what you are suggesting, and is pretty common in practice. You will unlock at most 15 minutes or so too early.

1

This is a fine example of no absolute security, only security under a given set of circumstances.

You can require() the time to be after something and it is reasonably secure. As @Tjaden pointed out, however, miners can distort the time so you shouldn't treat it as a reliable time source.

If the function is only for you, that's better. A more worrisome scenario would emerge if there was a significant financial advantage to distorting the time. For example, miners making an auction end early perchance to scoop an asset below its fair value. In the case of non-trivial incentives, it's safer to set deadlines by block number and communicate it that way.

Hope it helps.

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.