Guess the number
pragma solidity ^0.4.21;
contract GuessTheNumberChallenge {
uint8 answer = 42;
function GuessTheNumberChallenge() public payable {
require(msg.value == 1 ether);
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 ether);
if (n == answer) {
msg.sender.transfer(2 ether);
}
}
}
Goal
Guess the number to win ETH
Exploit
This is a simple excercise. One thing to have in mind while wokring with blockchains and smart contracts is that everything on the blockchain is public and accessible to anyone. Even though variables and functions can be named as private, this does not mean they can not be viewed by others.
In our case we can guess the number directly from looking into the smart contract code, or by using blockchain explorer as Etherscan (we will need to take a look into the transaction in which the GuessTheNumberChallenge contract is deployed).
The correct number is 42, so we just need to call guess(uint8 n) with 42 as argument for n.