SWC-116_Block values as a proxy for time
Block values as a proxy for time
Description: Contracts often need access to time values to perform certain types of functionality. Values such as
block.timestamp
, andblock.number
can give you a sense of the current time or a time delta, however, they are not safe to use for most purposes.In the case of
block.timestamp
, developers often attempt to use it to trigger time-dependent events. As Ethereum is decentralized, nodes can synchronize time only to some degree. Moreover, malicious miners can alter the timestamp of their blocks, especially if they can gain advantages by doing so. However, miners can’t set a timestamp smaller than the previous one (otherwise the block will be rejected), nor can they set the timestamp too far ahead in the future. Taking all of the above into consideration, developers can’t rely on the preciseness of the provided timestamp.As for
block.number
, considering the block time on Ethereum is generally about 14 seconds, it’s possible to predict the time delta between blocks. However, block times are not constant and are subject to change for a variety of reasons, e.g. fork reorganisations and the difficulty bomb. Due to variable block times,block.number
should also not be relied on for precise calculations of time.Remediation: Developers should write smart contracts with the notion that block values are not precise, and the use of them can lead to unexpected effects. Alternatively, they may make use oracles.
vulnerability contract 1:
1 | pragma solidity ^0.5.0; |
vulnerability contract 2:
1 | pragma solidity ^0.5.0; |