I've heard block.timestamp
can be manipulated by miners, and as I am writing a contract that uses block.timestamp
in crucial functions, I am wondering how best to mirror its behavior using block.number
1 Answer
The timestamp can be manipulated to a small degree (unless a majority of miners are colluding, in which case all bets are off) since overtly abnormal timestamps will be rejected by other mining nodes.
block.number
is presently a very unreliable method of measuring time (even for medium-term applications). Firstly, the time between blocks is already changing regularly due to the difficulty bomb. Then, there is the proposed delay of the difficulty bomb -- which means you have to figure out whether it will happen. Then there's a potential for a change in block time at some other point in the future -- and all bets are off when we have proof of stake. So not only would I not recommend using block.number
for counting time for medium- and long-term periods, it is not really possible, unless you can predict the future.
For short-term intervals (and before integral steps in difficulty change from the difficulty bomb are accounted for), you can use the average time for a block to be mined and multiply to create a time measurement. Approximate time elapsed is average time multiplied by block time.
-
That makes sense - but I think timestamp could matter a lot in my application. I have a betting pool essentially that, if someone bet and then submitted a block immediately after, could shut out other bets and win a portion of the pool. What I'm trying to get at is that the timestamp may be manipulatable by a small degree, but it could be pretty important for my application.– A. WadeCommented Aug 14, 2017 at 18:33
-
1If you're trying to do something like betting on some event at a particular time, the average block time calculation will be less accurate than the timestamp. Even changing difficulty will cause problems -- and variance will cause more issues by itself in a single block than timestamp drift from attack.– lungjCommented Aug 14, 2017 at 19:16