forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumpState.t.sol
139 lines (110 loc) · 6.03 KB
/
dumpState.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
contract SimpleContract {
constructor() {
assembly {
sstore(1, 2)
}
}
}
contract DumpStateTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
function testDumpStateCheatAccount() public {
// Path to temporary file that is deleted after the test
string memory path = string.concat(vm.projectRoot(), "/fixtures/Json/test_dump_state_cheat.json");
// Define some values to set in the state using cheatcodes
address target = address(1001);
bytes memory bytecode = hex"11223344";
uint256 balance = 1.2 ether;
uint64 nonce = 45;
vm.etch(target, bytecode);
vm.deal(target, balance);
vm.setNonce(target, nonce);
vm.store(target, bytes32(uint256(0x20)), bytes32(uint256(0x40)));
vm.store(target, bytes32(uint256(0x40)), bytes32(uint256(0x60)));
// Write the state to disk
vm.dumpState(path);
string memory json = vm.readFile(path);
string[] memory keys = vm.parseJsonKeys(json, "");
assertEq(keys.length, 1);
string memory key = keys[0];
assertEq(nonce, vm.parseJsonUint(json, string.concat(".", key, ".nonce")));
assertEq(balance, vm.parseJsonUint(json, string.concat(".", key, ".balance")));
assertEq(bytecode, vm.parseJsonBytes(json, string.concat(".", key, ".code")));
string[] memory slots = vm.parseJsonKeys(json, string.concat(".", key, ".storage"));
assertEq(slots.length, 2);
assertEq(
bytes32(uint256(0x40)),
vm.parseJsonBytes32(json, string.concat(".", key, ".storage.", vm.toString(bytes32(uint256(0x20)))))
);
assertEq(
bytes32(uint256(0x60)),
vm.parseJsonBytes32(json, string.concat(".", key, ".storage.", vm.toString(bytes32(uint256(0x40)))))
);
vm.removeFile(path);
}
function testDumpStateMultipleAccounts() public {
string memory path = string.concat(vm.projectRoot(), "/fixtures/Json/test_dump_state_multiple_accounts.json");
vm.setNonce(address(0x100), 1);
vm.deal(address(0x200), 1 ether);
vm.setNonce(address(0x300), 1);
vm.store(address(0x300), bytes32(uint256(1)), bytes32(uint256(2)));
vm.etch(address(0x400), hex"af");
vm.dumpState(path);
string memory json = vm.readFile(path);
string[] memory keys = vm.parseJsonKeys(json, "");
assertEq(keys.length, 4);
assertEq(4, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x100)))).length);
assertEq(1, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x100)), ".nonce")));
assertEq(0, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x100)), ".balance")));
assertEq(hex"", vm.parseJsonBytes(json, string.concat(".", vm.toString(address(0x100)), ".code")));
assertEq(0, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x100)), ".storage")).length);
assertEq(4, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x200)))).length);
assertEq(0, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x200)), ".nonce")));
assertEq(1 ether, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x200)), ".balance")));
assertEq(hex"", vm.parseJsonBytes(json, string.concat(".", vm.toString(address(0x200)), ".code")));
assertEq(0, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x200)), ".storage")).length);
assertEq(4, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x300)))).length);
assertEq(1, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x300)), ".nonce")));
assertEq(0, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x300)), ".balance")));
assertEq(hex"", vm.parseJsonBytes(json, string.concat(".", vm.toString(address(0x300)), ".code")));
assertEq(1, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x300)), ".storage")).length);
assertEq(
2,
vm.parseJsonUint(
json, string.concat(".", vm.toString(address(0x300)), ".storage.", vm.toString(bytes32(uint256(1))))
)
);
assertEq(4, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x400)))).length);
assertEq(0, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x400)), ".nonce")));
assertEq(0, vm.parseJsonUint(json, string.concat(".", vm.toString(address(0x400)), ".balance")));
assertEq(hex"af", vm.parseJsonBytes(json, string.concat(".", vm.toString(address(0x400)), ".code")));
assertEq(0, vm.parseJsonKeys(json, string.concat(".", vm.toString(address(0x400)), ".storage")).length);
vm.removeFile(path);
}
function testDumpStateDeployment() public {
string memory path = string.concat(vm.projectRoot(), "/fixtures/Json/test_dump_state_deployment.json");
SimpleContract s = new SimpleContract();
vm.dumpState(path);
string memory json = vm.readFile(path);
string[] memory keys = vm.parseJsonKeys(json, "");
assertEq(keys.length, 1);
assertEq(address(s), vm.parseAddress(keys[0]));
assertEq(1, vm.parseJsonKeys(json, string.concat(".", keys[0], ".storage")).length);
assertEq(2, vm.parseJsonUint(json, string.concat(".", keys[0], ".storage.", vm.toString(bytes32(uint256(1))))));
vm.removeFile(path);
}
function testDumpStateEmptyAccount() public {
string memory path = string.concat(vm.projectRoot(), "/fixtures/Json/test_dump_state_empty_account.json");
SimpleContract s = new SimpleContract();
vm.etch(address(s), hex"");
vm.resetNonce(address(s));
vm.dumpState(path);
string memory json = vm.readFile(path);
string[] memory keys = vm.parseJsonKeys(json, "");
assertEq(keys.length, 0);
vm.removeFile(path);
}
}