Blockchain transactions sit in a public mempool before miners include them in a block. Bots and miners can observe pending transactions and insert their own transactions with higher gas fees to get ahead — a practice known as Miner Extractable Value (MEV) or front-running.
Common front-running scenarios include:
// VULNERABLE — first caller wins the reward (race condition)
function claimReward(uint solution) public {
require(keccak256(solution) == targetHash);
payable(msg.sender).transfer(reward); // front-runner can copy this call
}
// SAFER — commit-reveal scheme hides the solution until committed
mapping(address => bytes32) public commits;
function commit(bytes32 hash) public {
commits[msg.sender] = hash; // hash of (solution + secret)
}
function reveal(uint solution, bytes32 secret) public {
require(commits[msg.sender] == keccak256(abi.encode(solution, secret)));
require(keccak256(solution) == targetHash);
payable(msg.sender).transfer(reward);
}