1

I have read that it takes about 12 seconds for an ethereum block to be mined. Let’s us say I have a contract mined on block 100, can I set a function that can only be run at block 200 or more? Thus roughly taking 1200 seconds before the function can be called?

Does mining a block take long when the network is congested? Can miners manipulate block times of blocks already mined?

2 Answers 2

2

You can use block.number as a measure of duration, taking into account that you are considering an average time for block mining, which is independent of the network congestion.

By the way, the other answer is referring to block.timestamp, which is a different thing from your original question. You can find here further info when comparing timestamp and block

1

Yes you can do that, but it is not a secure and safe way to do it.

If you do not have much at stake if a malicious miner manipulates the block timestamp, it's most likely ok. Otherwise, it's not.

From the doc:

Note

Do not rely on block.timestamp or blockhash as a source of randomness, unless you know what you are doing.

Both the timestamp and the block hash can be influenced by miners to some degree. Bad actors in the mining community can for example run a casino payout function on a chosen hash and just retry a different hash if they did not receive any money.

The current block timestamp must be strictly larger than the timestamp of the last block, but the only guarantee is that it will be somewhere between the timestamps of two consecutive blocks in the canonical chain.

Regarding your other questions:

  • Network congestion should not impact the block time, as far as I know.
  • Malicious miners can always manipulate the state of the blockchain, but only to some extent. If you wait for 15 blocks to be mined after the block you are checking (typical confirmation delay), you can assume that the block is real and accepted by all miners.
2
  • The docs say not to rely on them as a source of randomness. I am not trying to rely on them as a source of randomness. I just want there to be a duration between when a contract is created and some function can be called. Like 5 minutes or 10 minutes. The duration does not have to be strict. So after 15 block the block time stamp of the earliest block(among the 15) is reliable?
    – YulePale
    Commented Apr 6, 2021 at 17:41
  • 1
    Careful: block.timestamp and block.number are different things. Commented Apr 6, 2021 at 17:51

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.