/
10_Reentrance.t.sol
74 lines (58 loc) · 2.38 KB
/
10_Reentrance.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "forge-std/Test.sol";
import {Level as ReentranceFactory} from "src/levels/base/Level.sol";
import {IReentrance} from "src/hack/interfaces/IReentrance.sol";
import {ReentranceHack} from "src/hack/ReentranceHack.sol";
contract ReentranceTest is Test {
ReentranceFactory private reentranceFactory;
IReentrance private reentrance;
ReentranceHack private reentranceHack;
address private attacker = makeAddr("attacker");
uint256 private constant INSERT_COIN = 0.001 ether;
function setUp() external {
// At the time of writing, Foundry does not support contract compilation if one of the contracts involved uses an older version of Solidity.
// Hence, we deploy the contract using compiler generated creation code
// Get the creation code and append the constructor arguments at the end
bytes memory bytecode = abi.encodePacked(
vm.getCode("ReentranceFactory.sol")
);
address reentranceFactoryAddress;
// This is the address at which runtime code is set, which will be executed on the chain.
assembly {
reentranceFactoryAddress := create(
0,
add(bytecode, 0x20),
mload(bytecode)
)
}
// Load ReentranceFactory contract instance using interface
reentranceFactory = ReentranceFactory(reentranceFactoryAddress);
// Create a level instance
address reentranceAddress = reentranceFactory.createInstance{
value: INSERT_COIN
}(attacker);
// Load Reentrance contract instance using interface
reentrance = IReentrance(reentranceAddress);
reentranceHack = new ReentranceHack(address(reentrance));
}
function test_ReentranceHack() external {
vm.deal(attacker, 1 ether);
vm.startPrank(attacker);
console2.log(
"Reentrance Balance Before : ",
address(reentrance).balance
);
reentranceHack.hack{value: INSERT_COIN}();
console2.log(
"Reentrance Balance After : ",
address(reentrance).balance
);
bool success = reentranceFactory.validateInstance(
payable(address(reentrance)),
attacker
);
assertTrue(success, "Validation Failed!!!");
vm.stopPrank();
}
}