-
Notifications
You must be signed in to change notification settings - Fork 4
03. CoinFlip
Guess the correct outcome 10 times in a row.**
The contract tries to create randomness by relying on blockhashes, block number and a given FACTOR
. This data isn't secret:
-
blockhash()
andblock.number
are global variables in solidity - the
FACTOR
used to compute thecoinFlip
value is public can be reused by the attacker
blockhash(uint blockNumber) returns (bytes32)
: hash of the given block - only works for 256 most recent blocks
block.number (uint)
: current block number
Deploy an attacker contract.
- The attacker contract computes the
blockValue
by using theblock.number
andblockhash()
global variables. - As
FACTOR
is known, the attacker contract can computecoinFlip
andside
. - Pass the right
side
argument to the originalflip
function that we call from the attacker contract.
There’s no true randomness on Ethereum blockchain, only "pseudo-randomness" i.e. random generators that are considered “good enough”.
There currently isn't a native way to generate true randomness in the EVM.
Everything used in smart contracts is publicly visible, including the local variables and state variables marked as private.
Miners also have control over things like blockhashes, timestamps, and whether to include certain transactions - which allows them to bias these values in their favor.