-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: snapshot and revertTo cheatcodes (#276)
- Loading branch information
1 parent
4a0f0c1
commit 3f6a58d
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
fuzzing/testdata/contracts/cheat_codes/vm/snapshot_and_revert_to.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// This test ensures that we can take a snapshot of the current state of the testchain and revert to the state at that snapshot using the snapshot and revertTo cheatcodes | ||
pragma solidity ^0.8.0; | ||
|
||
interface CheatCodes { | ||
function warp(uint256) external; | ||
|
||
function deal(address, uint256) external; | ||
|
||
function snapshot() external returns (uint256); | ||
|
||
function revertTo(uint256) external returns (bool); | ||
} | ||
|
||
struct Storage { | ||
uint slot0; | ||
uint slot1; | ||
} | ||
|
||
contract TestContract { | ||
Storage store; | ||
uint256 timestamp; | ||
|
||
function test() public { | ||
// Obtain our cheat code contract reference. | ||
CheatCodes cheats = CheatCodes( | ||
0x7109709ECfa91a80626fF3989D68f67F5b1DD12D | ||
); | ||
|
||
store.slot0 = 10; | ||
store.slot1 = 20; | ||
timestamp = block.timestamp; | ||
cheats.deal(address(this), 5 ether); | ||
|
||
// Save state | ||
uint256 snapshot = cheats.snapshot(); | ||
|
||
// Change state | ||
store.slot0 = 300; | ||
store.slot1 = 400; | ||
cheats.deal(address(this), 500 ether); | ||
cheats.warp(12345); | ||
|
||
// Assert that state has been changed | ||
assert(store.slot0 == 300); | ||
assert(store.slot1 == 400); | ||
assert(address(this).balance == 500 ether); | ||
assert(block.timestamp == 12345); | ||
|
||
// Revert to snapshot | ||
cheats.revertTo(snapshot); | ||
|
||
// Ensure state has been reset | ||
assert(store.slot0 == 10); | ||
assert(store.slot1 == 20); | ||
assert(address(this).balance == 5 ether); | ||
assert(block.timestamp == timestamp); | ||
} | ||
} |