1
+ // SPDX-License-Identifier: MIT OR Apache-2.0
2
+ pragma solidity 0.8.18 ;
3
+
4
+ import "ds-test/test.sol " ;
5
+ import "cheats/Vm.sol " ;
6
+
7
+ struct Storage {
8
+ uint256 slot0;
9
+ uint256 slot1;
10
+ }
11
+
12
+ contract StateSnapshotTest is DSTest {
13
+ Vm constant vm = Vm (HEVM_ADDRESS);
14
+
15
+ Storage store;
16
+
17
+ function setUp () public {
18
+ store.slot0 = 10 ;
19
+ store.slot1 = 20 ;
20
+ }
21
+
22
+ function testStateSnapshot () public {
23
+ uint256 snapshotId = vm.snapshotState ();
24
+ store.slot0 = 300 ;
25
+ store.slot1 = 400 ;
26
+
27
+ assertEq (store.slot0, 300 );
28
+ assertEq (store.slot1, 400 );
29
+
30
+ vm.revertToState (snapshotId);
31
+ assertEq (store.slot0, 10 , "snapshot revert for slot 0 unsuccessful " );
32
+ assertEq (store.slot1, 20 , "snapshot revert for slot 1 unsuccessful " );
33
+ }
34
+
35
+ function testStateSnapshotRevertDelete () public {
36
+ uint256 snapshotId = vm.snapshotState ();
37
+ store.slot0 = 300 ;
38
+ store.slot1 = 400 ;
39
+
40
+ assertEq (store.slot0, 300 );
41
+ assertEq (store.slot1, 400 );
42
+
43
+ vm.revertToStateAndDelete (snapshotId);
44
+ assertEq (store.slot0, 10 , "snapshot revert for slot 0 unsuccessful " );
45
+ assertEq (store.slot1, 20 , "snapshot revert for slot 1 unsuccessful " );
46
+ // nothing to revert to anymore
47
+ assert (! vm.revertToState (snapshotId));
48
+ }
49
+
50
+ function testStateSnapshotDelete () public {
51
+ uint256 snapshotId = vm.snapshotState ();
52
+ store.slot0 = 300 ;
53
+ store.slot1 = 400 ;
54
+
55
+ vm.deleteStateSnapshot (snapshotId);
56
+ // nothing to revert to anymore
57
+ assert (! vm.revertToState (snapshotId));
58
+ }
59
+
60
+ function testStateSnapshotDeleteAll () public {
61
+ uint256 snapshotId = vm.snapshotState ();
62
+ store.slot0 = 300 ;
63
+ store.slot1 = 400 ;
64
+
65
+ vm.deleteStateSnapshots ();
66
+ // nothing to revert to anymore
67
+ assert (! vm.revertToState (snapshotId));
68
+ }
69
+
70
+ // <https://github.com/foundry-rs/foundry/issues/6411>
71
+ function testStateSnapshotsMany () public {
72
+ uint256 snapshotId;
73
+ for (uint256 c = 0 ; c < 10 ; c++ ) {
74
+ for (uint256 cc = 0 ; cc < 10 ; cc++ ) {
75
+ snapshotId = vm.snapshotState ();
76
+ vm.revertToStateAndDelete (snapshotId);
77
+ assert (! vm.revertToState (snapshotId));
78
+ }
79
+ }
80
+ }
81
+
82
+ // tests that snapshots can also revert changes to `block`
83
+ function testBlockValues () public {
84
+ uint256 num = block .number ;
85
+ uint256 time = block .timestamp ;
86
+ uint256 prevrandao = block .prevrandao ;
87
+
88
+ uint256 snapshotId = vm.snapshotState ();
89
+
90
+ vm.warp (1337 );
91
+ assertEq (block .timestamp , 1337 );
92
+
93
+ vm.roll (99 );
94
+ assertEq (block .number , 99 );
95
+
96
+ vm.prevrandao (uint256 (123 ));
97
+ assertEq (block .prevrandao , 123 );
98
+
99
+ assert (vm.revertToState (snapshotId));
100
+
101
+ assertEq (block .number , num, "snapshot revert for block.number unsuccessful " );
102
+ assertEq (block .timestamp , time, "snapshot revert for block.timestamp unsuccessful " );
103
+ assertEq (block .prevrandao , prevrandao, "snapshot revert for block.prevrandao unsuccessful " );
104
+ }
105
+ }
106
+
107
+ // TODO: remove this test suite once `snapshot*` has been deprecated in favor of `snapshotState*`.
108
+ contract DeprecatedStateSnapshotTest is DSTest {
109
+ Vm constant vm = Vm (HEVM_ADDRESS);
110
+
111
+ Storage store;
112
+
113
+ function setUp () public {
114
+ store.slot0 = 10 ;
115
+ store.slot1 = 20 ;
116
+ }
117
+
118
+ function testSnapshotState () public {
119
+ uint256 snapshotId = vm.snapshot ();
120
+ store.slot0 = 300 ;
121
+ store.slot1 = 400 ;
122
+
123
+ assertEq (store.slot0, 300 );
124
+ assertEq (store.slot1, 400 );
125
+
126
+ vm.revertTo (snapshotId);
127
+ assertEq (store.slot0, 10 , "snapshot revert for slot 0 unsuccessful " );
128
+ assertEq (store.slot1, 20 , "snapshot revert for slot 1 unsuccessful " );
129
+ }
130
+
131
+ function testSnapshotStateRevertDelete () public {
132
+ uint256 snapshotId = vm.snapshot ();
133
+ store.slot0 = 300 ;
134
+ store.slot1 = 400 ;
135
+
136
+ assertEq (store.slot0, 300 );
137
+ assertEq (store.slot1, 400 );
138
+
139
+ vm.revertToAndDelete (snapshotId);
140
+ assertEq (store.slot0, 10 , "snapshot revert for slot 0 unsuccessful " );
141
+ assertEq (store.slot1, 20 , "snapshot revert for slot 1 unsuccessful " );
142
+ // nothing to revert to anymore
143
+ assert (! vm.revertTo (snapshotId));
144
+ }
145
+
146
+ function testSnapshotStateDelete () public {
147
+ uint256 snapshotId = vm.snapshot ();
148
+ store.slot0 = 300 ;
149
+ store.slot1 = 400 ;
150
+
151
+ vm.deleteSnapshot (snapshotId);
152
+ // nothing to revert to anymore
153
+ assert (! vm.revertTo (snapshotId));
154
+ }
155
+
156
+ function testSnapshotStateDeleteAll () public {
157
+ uint256 snapshotId = vm.snapshot ();
158
+ store.slot0 = 300 ;
159
+ store.slot1 = 400 ;
160
+
161
+ vm.deleteSnapshots ();
162
+ // nothing to revert to anymore
163
+ assert (! vm.revertTo (snapshotId));
164
+ }
165
+
166
+ // <https://github.com/foundry-rs/foundry/issues/6411>
167
+ function testSnapshotStatesMany () public {
168
+ uint256 snapshotId;
169
+ for (uint256 c = 0 ; c < 10 ; c++ ) {
170
+ for (uint256 cc = 0 ; cc < 10 ; cc++ ) {
171
+ snapshotId = vm.snapshot ();
172
+ vm.revertToAndDelete (snapshotId);
173
+ assert (! vm.revertTo (snapshotId));
174
+ }
175
+ }
176
+ }
177
+
178
+ // tests that snapshots can also revert changes to `block`
179
+ function testBlockValues () public {
180
+ uint256 num = block .number ;
181
+ uint256 time = block .timestamp ;
182
+ uint256 prevrandao = block .prevrandao ;
183
+
184
+ uint256 snapshotId = vm.snapshot ();
185
+
186
+ vm.warp (1337 );
187
+ assertEq (block .timestamp , 1337 );
188
+
189
+ vm.roll (99 );
190
+ assertEq (block .number , 99 );
191
+
192
+ vm.prevrandao (uint256 (123 ));
193
+ assertEq (block .prevrandao , 123 );
194
+
195
+ assert (vm.revertTo (snapshotId));
196
+
197
+ assertEq (block .number , num, "snapshot revert for block.number unsuccessful " );
198
+ assertEq (block .timestamp , time, "snapshot revert for block.timestamp unsuccessful " );
199
+ assertEq (block .prevrandao , prevrandao, "snapshot revert for block.prevrandao unsuccessful " );
200
+ }
201
+ }
0 commit comments