Skip to content

Commit 3910948

Browse files
Wodannfrangio
andauthored
refactor: rename snapshot to state snapshot (#917)
Co-authored-by: Francisco Giordano <[email protected]>
1 parent a224140 commit 3910948

File tree

23 files changed

+711
-388
lines changed

23 files changed

+711
-388
lines changed

.changeset/long-eagles-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ignored/edr": patch
3+
---
4+
5+
Deprecated `deleteSnapshot`, `deleteSnapshots`, `revertTo`, `revertToAndDelete`, and `snapshot` cheatcodes in favor of `deleteStateSnapshot`, `deleteStateSnapshots`, `revertToState`, `revertToStateAndDelete`, and `snapshotState`

crates/edr_solidity_tests/tests/it/repros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ test_repro!(6355, false, None, |res| {
276276

277277
let test = res
278278
.test_results
279-
.remove("test_shouldFailWithRevertTo()")
279+
.remove("test_shouldFailWithRevertToState()")
280280
.unwrap();
281281
assert_eq!(test.status, TestStatus::Failure);
282282
});

crates/edr_solidity_tests/tests/testdata/cheats/Vm.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ interface Vm {
165165
function deal(address account, uint256 newBalance) external;
166166
function deleteSnapshot(uint256 snapshotId) external returns (bool success);
167167
function deleteSnapshots() external;
168+
function deleteStateSnapshot(uint256 snapshotId) external returns (bool success);
169+
function deleteStateSnapshots() external;
168170
function difficulty(uint256 newDifficulty) external;
169171
function dumpState(string calldata pathToStateJson) external;
170172
function ensNamehash(string calldata name) external pure returns (bytes32);
@@ -319,6 +321,8 @@ interface Vm {
319321
function resumeGasMetering() external;
320322
function revertTo(uint256 snapshotId) external returns (bool success);
321323
function revertToAndDelete(uint256 snapshotId) external returns (bool success);
324+
function revertToState(uint256 snapshotId) external returns (bool success);
325+
function revertToStateAndDelete(uint256 snapshotId) external returns (bool success);
322326
function revokePersistent(address account) external;
323327
function revokePersistent(address[] calldata accounts) external;
324328
function roll(uint256 newHeight) external;
@@ -355,6 +359,7 @@ interface Vm {
355359
function skip(bool skipTest) external;
356360
function sleep(uint256 duration) external;
357361
function snapshot() external returns (uint256 snapshotId);
362+
function snapshotState() external returns (uint256 snapshotId);
358363
function split(string calldata input, string calldata delimiter) external pure returns (string[] memory outputs);
359364
function startMappingRecording() external;
360365
function startPrank(address msgSender) external;

crates/edr_solidity_tests/tests/testdata/default/cheats/Prevrandao.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ contract PrevrandaoTest is DSTest {
2323
function testPrevrandaoSnapshotFuzzed(uint256 newPrevrandao) public {
2424
vm.assume(newPrevrandao != block.prevrandao);
2525
uint256 oldPrevrandao = block.prevrandao;
26-
uint256 snapshot = vm.snapshot();
26+
uint256 snapshotId = vm.snapshotState();
2727

2828
vm.prevrandao(newPrevrandao);
2929
assertEq(block.prevrandao, newPrevrandao);
3030

31-
assert(vm.revertTo(snapshot));
31+
assert(vm.revertToState(snapshotId));
3232
assertEq(block.prevrandao, oldPrevrandao);
3333
}
3434
}

crates/edr_solidity_tests/tests/testdata/default/cheats/Snapshots.t.sol

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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

Comments
 (0)