Transaction Ordering, Randomness and Time
On a public blockchain, pending transactions are visible and block producers influence ordering and some block values. This lesson explains front-running and MEV, why on-chain randomness is unsafe, and how to use timestamps correctly.
Learning outcomes
After completing this lesson, you will be able to:
- Explain front-running, sandwich attacks and MEV.
- Choose mitigations for transaction-ordering attacks.
- Explain why on-chain values are not a source of randomness.
- Explain the safe uses of block.timestamp.
Front-running and MEV
A transaction is visible in the public mempool before it is included. Specialised bots, called searchers, watch for profitable opportunities and submit their own transactions with higher priority fees or through block builders to be ordered around the victim's. The following terms describe the main patterns.
- Front-running
- Placing a transaction before a known pending transaction, for example claiming a reward or buying an asset first.
- Back-running
- Placing a transaction immediately after one, for example to arbitrage the price change it causes.
- Sandwich attack
- Buying before a victim's large trade and selling after it, profiting from the price movement the victim causes.
- MEV Maximal extractable value
- The value that can be captured by including, excluding or reordering transactions in a block.
The following diagram shows a searcher ordering transactions around a victim's trade.

Ordering attacks
Mitigating ordering attacks
The following table matches common situations with mitigations.
| Situation | Mitigation |
|---|---|
| Token swaps | A minimum output (slippage limit) and a deadline in each trade |
| Sealed bids, games and quiz answers | Commit-reveal: submit a hash first, reveal the value later |
| Approve then change an allowance | Set the allowance to zero first, approve only the amount each use needs, or use permit signatures |
| Sensitive transactions in general | Private transaction submission to block builders instead of the public mempool |
In a commit-reveal scheme, each participant first submits keccak256(abi.encode(value, salt, msg.sender)). After the commit phase ends, they reveal the value and salt, and the contract checks the hash. Observers cannot copy a commitment, because it reveals nothing and is bound to the sender's address.
Choosing a mitigation
Why on-chain randomness fails
The EVM is deterministic, so there is no source of true randomness inside a transaction. Values such as block.timestamp, block.number, blockhash and block.prevrandao (which replaced block.difficulty at the Merge) are known to anyone executing in the same block, and the block proposer can influence some of them, for example by choosing not to propose. An attacker's contract can compute the same "random" value and act only when the outcome favours it.
Valuable random outcomes should use a verifiable random function (VRF) service such as Chainlink VRF, which delivers a random value with a cryptographic proof in a later transaction, or a commit-reveal scheme among participants.
Safe source of randomness?
Using timestamps safely
block.timestamp is set by the block proposer. Under proof of stake it follows the 12-second slot schedule closely, so manipulation is far more limited than under proof of work, where miners could shift it by several seconds (older material's "15-second rule"). Timestamps are suitable for deadlines and durations measured in minutes, hours or days, such as auction end times and vesting. They should not decide outcomes that depend on precise seconds, and never serve as randomness. Block numbers are a poor clock, because block times have changed between upgrades.
Timestamps
A copied answer
A puzzle contract pays 10 ETH to the first account that submits a string whose hash matches a stored hash. A user solves it and submits the answer, but a bot's transaction with the same answer and a higher tip is included first. How should the contract be redesigned?
Use commit-reveal. Solvers first submit keccak256(abi.encode(answer, salt, msg.sender)); after a fixed commit period, they reveal answer and salt. The bot cannot derive the answer from the commitment, and copying a commitment is useless because it includes the original solver's address.
A predictable raffle
A raffle selects a winner with uint256(keccak256(abi.encode(block.prevrandao, block.timestamp))) % n when anyone calls draw(). Is hashing the values enough to make the result unpredictable?
No. Hashing does not add randomness; it transforms known inputs into a known output. An attacker's contract can compute the same hash in the same transaction and call draw() only when it wins. The raffle should request randomness from a VRF service and select the winner when the verified random value arrives.
Which scheme stops observers from copying an answer or bid seen in the mempool?
Show answer
Commit-reveal: participants first submit a hash of their value, a secret salt and their address, then reveal the value and salt after the commit phase.
Exam Tips
Remember for the exam
- Pending transactions are public and can be front-run, back-run or sandwiched (MEV).
- Use slippage limits, deadlines and commit-reveal to resist ordering attacks.
- Block values are not random, even when hashed; use a VRF service for valuable randomness.
- block.timestamp is fine for coarse deadlines, not for precise timing or randomness.