Level 1 Noob Hacker
Ethernaut 03 Coin Flip
0 / 100 EXP 100 EXP to Junior Hacker
0%

Challenge 3 / Ethernaut

Coin Flip

Goal

  1. Win the game 10 times in a row.

Your task

Read CoinFlip.sol, then complete Exploit.s.sol.

Starting facts

Goal: make consecutiveWins reach 10.

:~/targets/ethernaut-coin-flip$
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CoinFlip {
    uint256 public consecutiveWins;
    uint256 lastHash;
    uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

    function flip(bool _guess) public returns (bool) {
        uint256 blockValue = uint256(blockhash(block.number - 1));
        if (lastHash == blockValue) {
            revert();
        }
        lastHash = blockValue;
        uint256 coinFlip = blockValue / FACTOR;
        bool side = coinFlip == 1 ? true : false;

        if (side == _guess) {
            consecutiveWins++;
            return true;
        } else {
            consecutiveWins = 0;
            return false;
        }
    }
}