Working with time on Virtual Environments
For certain types of tests it’s useful to skip through time. This is possible by relying on 2 Admin RPC methods available on Virtual Environments:evm_setNextBlockTimestamp and evm_setNextBlockTimestamp.
Getting the correct time
During execution of a transaction oreth_call accesses block.timestamp within a contract, the value will depend on whether the target block (latest or pending) block:
- For
pendingblock:block.timestamp == time.now() + adjustment_offset. This represents the current time adjusted by the time-adjustment methods. - For
latestblock:block.timestamp == latest_block.timestamp. This is the timestamp of the most recently mined block.
adjustment_offset comes from time-adjustment methods:
-
evm_setNextBlockTimestamp: Skips time to the timestamp for the next block:adjustment_offset = timestamp - time.now(). -
evm_increaseTime: Advances time by a relative amount.adjustment_offset += increase_value
Examples
Increase time by an offset
showLineNumbers
Set timestamp for pending block
showLineNumbers