-
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.numberare global variables in solidity - the
FACTORused to compute thecoinFlipvalue 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
blockValueby using theblock.numberandblockhash()global variables. - As
FACTORis known, the attacker contract can computecoinFlipandside. - Pass the right
sideargument to the originalflipfunction 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.