forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateSnapshots.t.sol
More file actions
201 lines (157 loc) · 5.95 KB
/
Copy pathStateSnapshots.t.sol
File metadata and controls
201 lines (157 loc) · 5.95 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
struct Storage {
uint256 slot0;
uint256 slot1;
}
contract StateSnapshotTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
Storage store;
function setUp() public {
store.slot0 = 10;
store.slot1 = 20;
}
function testStateSnapshot() public {
uint256 snapshotId = vm.snapshotState();
store.slot0 = 300;
store.slot1 = 400;
assertEq(store.slot0, 300);
assertEq(store.slot1, 400);
vm.revertToState(snapshotId);
assertEq(store.slot0, 10, "snapshot revert for slot 0 unsuccessful");
assertEq(store.slot1, 20, "snapshot revert for slot 1 unsuccessful");
}
function testStateSnapshotRevertDelete() public {
uint256 snapshotId = vm.snapshotState();
store.slot0 = 300;
store.slot1 = 400;
assertEq(store.slot0, 300);
assertEq(store.slot1, 400);
vm.revertToStateAndDelete(snapshotId);
assertEq(store.slot0, 10, "snapshot revert for slot 0 unsuccessful");
assertEq(store.slot1, 20, "snapshot revert for slot 1 unsuccessful");
// nothing to revert to anymore
assert(!vm.revertToState(snapshotId));
}
function testStateSnapshotDelete() public {
uint256 snapshotId = vm.snapshotState();
store.slot0 = 300;
store.slot1 = 400;
vm.deleteStateSnapshot(snapshotId);
// nothing to revert to anymore
assert(!vm.revertToState(snapshotId));
}
function testStateSnapshotDeleteAll() public {
uint256 snapshotId = vm.snapshotState();
store.slot0 = 300;
store.slot1 = 400;
vm.deleteStateSnapshots();
// nothing to revert to anymore
assert(!vm.revertToState(snapshotId));
}
// <https://github.com/foundry-rs/foundry/issues/6411>
function testStateSnapshotsMany() public {
uint256 snapshotId;
for (uint256 c = 0; c < 10; c++) {
for (uint256 cc = 0; cc < 10; cc++) {
snapshotId = vm.snapshotState();
vm.revertToStateAndDelete(snapshotId);
assert(!vm.revertToState(snapshotId));
}
}
}
// tests that snapshots can also revert changes to `block`
function testBlockValues() public {
uint256 num = block.number;
uint256 time = block.timestamp;
uint256 prevrandao = block.prevrandao;
uint256 snapshotId = vm.snapshotState();
vm.warp(1337);
assertEq(block.timestamp, 1337);
vm.roll(99);
assertEq(block.number, 99);
vm.prevrandao(uint256(123));
assertEq(block.prevrandao, 123);
assert(vm.revertToState(snapshotId));
assertEq(block.number, num, "snapshot revert for block.number unsuccessful");
assertEq(block.timestamp, time, "snapshot revert for block.timestamp unsuccessful");
assertEq(block.prevrandao, prevrandao, "snapshot revert for block.prevrandao unsuccessful");
}
}
// TODO: remove this test suite once `snapshot*` has been deprecated in favor of `snapshotState*`.
contract DeprecatedStateSnapshotTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
Storage store;
function setUp() public {
store.slot0 = 10;
store.slot1 = 20;
}
function testSnapshotState() public {
uint256 snapshotId = vm.snapshot();
store.slot0 = 300;
store.slot1 = 400;
assertEq(store.slot0, 300);
assertEq(store.slot1, 400);
vm.revertTo(snapshotId);
assertEq(store.slot0, 10, "snapshot revert for slot 0 unsuccessful");
assertEq(store.slot1, 20, "snapshot revert for slot 1 unsuccessful");
}
function testSnapshotStateRevertDelete() public {
uint256 snapshotId = vm.snapshot();
store.slot0 = 300;
store.slot1 = 400;
assertEq(store.slot0, 300);
assertEq(store.slot1, 400);
vm.revertToAndDelete(snapshotId);
assertEq(store.slot0, 10, "snapshot revert for slot 0 unsuccessful");
assertEq(store.slot1, 20, "snapshot revert for slot 1 unsuccessful");
// nothing to revert to anymore
assert(!vm.revertTo(snapshotId));
}
function testSnapshotStateDelete() public {
uint256 snapshotId = vm.snapshot();
store.slot0 = 300;
store.slot1 = 400;
vm.deleteSnapshot(snapshotId);
// nothing to revert to anymore
assert(!vm.revertTo(snapshotId));
}
function testSnapshotStateDeleteAll() public {
uint256 snapshotId = vm.snapshot();
store.slot0 = 300;
store.slot1 = 400;
vm.deleteSnapshots();
// nothing to revert to anymore
assert(!vm.revertTo(snapshotId));
}
// <https://github.com/foundry-rs/foundry/issues/6411>
function testSnapshotStatesMany() public {
uint256 snapshotId;
for (uint256 c = 0; c < 10; c++) {
for (uint256 cc = 0; cc < 10; cc++) {
snapshotId = vm.snapshot();
vm.revertToAndDelete(snapshotId);
assert(!vm.revertTo(snapshotId));
}
}
}
// tests that snapshots can also revert changes to `block`
function testBlockValues() public {
uint256 num = block.number;
uint256 time = block.timestamp;
uint256 prevrandao = block.prevrandao;
uint256 snapshotId = vm.snapshot();
vm.warp(1337);
assertEq(block.timestamp, 1337);
vm.roll(99);
assertEq(block.number, 99);
vm.prevrandao(uint256(123));
assertEq(block.prevrandao, 123);
assert(vm.revertTo(snapshotId));
assertEq(block.number, num, "snapshot revert for block.number unsuccessful");
assertEq(block.timestamp, time, "snapshot revert for block.timestamp unsuccessful");
assertEq(block.prevrandao, prevrandao, "snapshot revert for block.prevrandao unsuccessful");
}
}