Timestamp dependence: what block.timestamp can and can't tell you
Timestamp dependence is when a contract's outcome depends on block.timestamp in a way that the block producer, the order of transactions or the chain's block timing can bend. Using it for deadlines, vesting and interest measured in hours or days is normal; using it as randomness, as a precise clock, as an exact-second condition or as a count of blocks is the bug. Give every time check a margin wider than the leeway the chain allows, never draw randomness from time, and never assume a fixed block time.
Why is block.timestamp a risk?
The timestamp is set by whoever produces the block, within the rules of the chain. The Solidity docs say: 'Both the timestamp and the block hash can be influenced by miners to some degree', and 'Do not rely on block.timestamp or blockhash as a source of randomness, unless you know what you are doing.' SWC-116 puts it as: 'Developers should write smart contracts with the notion that block values are not precise'.
Four things bend time inside a contract:
- The block producer chooses the timestamp inside the range the chain accepts.
- Transaction order. Every transaction in a block sees the same
block.timestamp, so a condition tied to one second is decided by which transactions get into that block. - Inclusion delay. A transaction signed before a deadline can be mined after it, and one that is held back can be mined much later than its sender expected.
- Block timing. The time between blocks differs from chain to chain and can change with upgrades, so
block.numberis a count of blocks, not a clock.
When is it safe to use block.timestamp?
| Use | Safe? | Why |
|---|---|---|
| Deadlines days away: vesting, timelocks, auction ends | Yes | A few seconds either way don't change the outcome |
| Interest or rewards accrued per second | Yes, with care | Two calls in one block see zero elapsed time; handle that case |
| Picking a winner, a random number or a seed | No | Whoever influences the timestamp or the block's contents influences the result |
A condition on an exact second, such as block.timestamp % 10 == 0 |
No | The block producer and transaction order decide it |
Time computed as block.number times seconds per block |
No | Block times differ between chains and change over time |
A swap deadline of block.timestamp |
No protection | It is always met, whenever the transaction lands |
What does vulnerable code look like?
This contract opens claims a day after launch, measured in blocks, and pays a bonus to claims made in a block whose timestamp is a multiple of 10.
Vulnerable: blocks counted as time and a timestamp jackpot (illustrative, not audited code) TimedDrop.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
contract TimedDrop {
uint256 public constant SECONDS_PER_BLOCK = 12; // assumed, not guaranteed
uint256 public immutable startBlock;
mapping(address => bool) public claimed;
constructor() payable {
startBlock = block.number;
}
// Bug 1: time from a block count. If blocks get faster or slower, this clock drifts,
// and on another chain it is simply wrong.
function elapsed() public view returns (uint256) {
return (block.number - startBlock) * SECONDS_PER_BLOCK;
}
// Bug 2: an exact-second rule decides who gets the bonus, so whoever can pick
// or predict the block's timestamp can claim it.
function claim() external {
require(elapsed() >= 1 days, "too early");
require(!claimed[msg.sender], "already claimed");
claimed[msg.sender] = true;
uint256 amount = 0.01 ether;
if (block.timestamp % 10 == 0) amount = 1 ether;
(bool ok, ) = msg.sender.call{value: amount}("");
require(ok, "payment failed");
}
}
On a chain with faster blocks, the claim window opens hours early. And the bonus goes to whoever can get a claim into a block with the right timestamp, which is a matter of predicting or choosing block timing, not luck.
How do you fix it?
Fixed: a timestamp deadline with a wide margin and no time-based prizes (illustrative, not audited code) TimedDropFixed.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
contract TimedDropFixed {
uint256 public constant CLAIM_AMOUNT = 0.01 ether;
uint256 public immutable openAt; // a timestamp, not a block number
mapping(address => bool) public claimed;
constructor(uint256 delay) payable {
require(delay >= 1 hours, "delay too short to be precise");
openAt = block.timestamp + delay;
}
// A coarse check: seconds of leeway can't change who may claim.
function claim() external {
require(block.timestamp >= openAt, "too early");
require(!claimed[msg.sender], "already claimed");
claimed[msg.sender] = true;
// No bonus that depends on time. If a winner is needed, use a randomness
// source built for it, not a block value.
(bool ok, ) = msg.sender.call{value: CLAIM_AMOUNT}("");
require(ok, "payment failed");
}
}
- Measure time with timestamps, and only coarsely. Use
block.timestampfor periods of minutes or longer, compare with>=, and never require an exact second. - Never count blocks as time. If a rate is per block, it changes the day the chain's block time does; store rates per second instead.
- Keep randomness away from block values, and use a source that was built to produce random numbers instead.
- Handle zero elapsed time. Interest, rewards and time-weighted prices must behave when two calls land in the same block.
- Take deadlines from the user. A deadline set when the user signed limits how long a transaction can wait;
block.timestamplimits nothing. - Check each chain you deploy to. How a chain sets timestamps and how fast it makes blocks are part of its documentation; read it before relying on either.
Has timestamp dependence been reported in a live contract?
| Protocol | RuletkaIo, CVE-2018-17968 |
|---|---|
| Date | 23 October 2018 |
| Chain | Ethereum |
| Loss | None recorded: a disclosed vulnerability, not an exploit |
| What happened | CVE-2018-17968 covers RuletkaIo, an Ethereum gambling game whose random() function was built from the block timestamp and block hash. An exploit contract can reproduce those values, so, in the CVE's words, the game 'generates a random value that is predictable by an external contract call'. The CVE was published on 23 October 2018 and is rated 7.5, High, under CVSS 3.x. No loss is recorded with it. |
| Sources | INCIBE-CERT |
This is a disclosure, not a reported exploit, and it is a randomness bug as much as a timing one: the timestamp is known to every transaction in the block before any of them runs, which is why the table above rules block values out for picking winners.
What should a reviewer check?
- List every read of
block.timestampandblock.number, including in libraries and inherited contracts. - For each, how many seconds would have to move to change the outcome? If it is within the leeway the chain allows, the check is unsafe.
- Is any block value used as randomness, a seed or a tie-breaker?
- Is
block.numberturned into time anywhere, for example in an interest rate model that assumes a number of blocks per year? - Do swaps and orders take their deadline from the user rather than from
block.timestamp? - What happens when two calls land in the same block: zero elapsed time, a division by zero, double accrual?
- For each chain in the deployment plan, how are timestamps set and how fast are blocks made?
How does QuillShield check for timestamp dependence?
Whether a time check is safe depends on the margin around it and on what it decides, which means reading the check together with the money that moves because of it. QuillShield runs a full audit of the Solidity codebase in a Foundry project: it builds a security graph of the code, lists the invariants the code must keep, and puts every finding in front of a separate reviewer agent that tries to disprove it. Findings that hold up come with the exact lines, the impact and a recommended fix, plus a proof of concept on a full audit.
List the chains you deploy to in the audit context, along with the whitepaper, trust assumptions and privileged roles, since block times and timestamp rules are chain properties the code can't show. QuillShield then checks every pull request on GitHub or GitLab.com against the audit's security graph.
Time logic tends to break when a contract moves to a second chain, so run a full audit before that deployment, not after it. New accounts get $50 in credit, good for up to half of an order.
FAQ
Can validators manipulate block.timestamp?
To the degree the chain's rules allow. The Solidity docs say the timestamp can be influenced by miners to some degree, and the same is true of whoever produces blocks today. Design for a margin, never for an exact second.
Is block.number a better clock than block.timestamp?
No. Block times differ between chains and change with upgrades, and the SWC-116 page links guidance titled 'Avoid using block.number as a timestamp'.
Is it safe to use block.timestamp for vesting or a timelock?
Yes, for periods of hours or days, where a few seconds either way change nothing. Store start and end times as timestamps and compare with >=.
What deadline should a swap use?
One the user chose when they signed, a few minutes ahead. A deadline of block.timestamp is met in whatever block the transaction lands, so it protects nothing.
Which SWC entry covers timestamp dependence?
SWC-116, Block values as a proxy for time. It covers both block.timestamp and block.number, and suggests oracles where precise time matters.
Sources
- Units and globally available variables: block and transaction properties, Solidity documentation checked 3 December 2026
- SWC-116: Block values as a proxy for time, SWC Registry checked 3 December 2026
- CVE-2018-17968, INCIBE-CERT checked 3 December 2026