forked from foundry-rs/book
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from matter-labs/upstream-f5aa05e
chore: upstream f5aa05e
- Loading branch information
Showing
42 changed files
with
762 additions
and
242 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
## `snapshotGas` cheatcodes | ||
|
||
### Signature | ||
|
||
```solidity | ||
/// Start a snapshot capture of the current gas usage by name. | ||
/// The group name is derived from the contract name. | ||
function startSnapshotGas(string calldata name) external; | ||
/// Start a snapshot capture of the current gas usage by name in a group. | ||
function startSnapshotGas(string calldata group, string calldata name) external; | ||
/// Stop the snapshot capture of the current gas by latest snapshot name, capturing the gas used since the start. | ||
function stopSnapshotGas() external returns (uint256 gasUsed); | ||
/// Stop the snapshot capture of the current gas usage by name, capturing the gas used since the start. | ||
/// The group name is derived from the contract name. | ||
function stopSnapshotGas(string calldata name) external returns (uint256 gasUsed); | ||
/// Stop the snapshot capture of the current gas usage by name in a group, capturing the gas used since the start. | ||
function stopSnapshotGas(string calldata group, string calldata name) external returns (uint256 gasUsed); | ||
/// Snapshot capture an arbitrary numerical value by name. | ||
/// The group name is derived from the contract name. | ||
function snapshotValue(string calldata name, uint256 value) external; | ||
/// Snapshot capture an arbitrary numerical value by name in a group. | ||
function snapshotValue(string calldata group, string calldata name, uint256 value) external; | ||
/// Snapshot capture the gas usage of the last call by name from the callee perspective. | ||
function snapshotGasLastCall(string calldata name) external returns (uint256 gasUsed); | ||
/// Snapshot capture the gas usage of the last call by name in a group from the callee perspective. | ||
function snapshotGasLastCall(string calldata group, string calldata name) external returns (uint256 gasUsed); | ||
``` | ||
|
||
### Description | ||
|
||
`snapshotGas*` cheatcodes allow you to capture gas usage in your tests. This can be useful to track how much gas your logic is consuming. You can capture the gas usage of the last call by name, capture an arbitrary numerical value by name, or start and stop a snapshot capture of the current gas usage by name. | ||
|
||
In order to strictly compare gas usage across test runs, set the `FORGE_SNAPSHOT_CHECK` environment variable to `true` before running your tests. This will compare the gas usage of your tests against the last snapshot and fail if the gas usage has changed. By default the snapshots directory will be newly created and its contents removed before each test run to ensure no stale data is present. | ||
|
||
It is intended that the `snapshots` directory created when using the `snapshotGas*` cheatcodes is checked into version control. This allows you to track changes in gas usage over time and compare gas usage during code reviews. | ||
|
||
When running `forge clean` the `snapshots` directory will be deleted. | ||
|
||
### Examples | ||
|
||
Capturing the gas usage of a section of code that calls an external contract: | ||
|
||
```solidity | ||
contract SnapshotGasTest is Test { | ||
uint256 public slot0; | ||
Flare public flare; | ||
function setUp() public { | ||
flare = new Flare(); | ||
} | ||
function testSnapshotGas() public { | ||
vm.startSnapshotGas("externalA"); | ||
flare.run(256); | ||
uint256 gasUsed = vm.stopSnapshotGas(); | ||
} | ||
} | ||
``` | ||
|
||
Capturing the gas usage of multiple sections of code that modify the internal state: | ||
|
||
|
||
```solidity | ||
contract SnapshotGasTest is Test { | ||
uint256 public slot0; | ||
/// Writes to `snapshots/SnapshotGasTest.json` group with name `internalA`, `internalB`, and `internalC`. | ||
function testSnapshotGas() public { | ||
vm.startSnapshotGas("internalA"); | ||
slot0 = 1; | ||
vm.stopSnapshotGas(); | ||
vm.startSnapshotGas("internalB"); | ||
slot0 = 2; | ||
vm.stopSnapshotGas(); | ||
vm.startSnapshotGas("internalC"); | ||
slot0 = 0; | ||
vm.stopSnapshotGas(); | ||
} | ||
} | ||
``` | ||
|
||
Capturing the gas usage of a section of code that modifies both the internal state and calls an external contract: | ||
|
||
```solidity | ||
contract SnapshotGasTest is Test { | ||
uint256 public slot0; | ||
Flare public flare; | ||
function setUp() public { | ||
flare = new Flare(); | ||
} | ||
/// Writes to `snapshots/SnapshotGasTest.json` group with name `combinedA`. | ||
function testSnapshotGas() public { | ||
vm.startSnapshotGas("combinedA"); | ||
flare.run(256); | ||
slot0 = 1; | ||
vm.stopSnapshotGas(); | ||
} | ||
} | ||
``` | ||
|
||
```solidity | ||
Capturing an arbitrary numerical value (such as the bytecode size of a contract): | ||
```solidity | ||
contract SnapshotGasTest is Test { | ||
uint256 public slot0; | ||
/// Writes to `snapshots/SnapshotGasTest.json` group with name `valueA`, `valueB`, and `valueC`. | ||
function testSnapshotValue() public { | ||
uint256 a = 123; | ||
uint256 b = 456; | ||
uint256 c = 789; | ||
vm.snapshotValue("valueA", a); | ||
vm.snapshotValue("valueB", b); | ||
vm.snapshotValue("valueC", c); | ||
} | ||
} | ||
``` | ||
|
||
Capturing the gas usage of the last call from the callee perspective: | ||
|
||
```solidity | ||
contract SnapshotGasTest is Test { | ||
Flare public flare; | ||
function setUp() public { | ||
flare = new Flare(); | ||
} | ||
/// Writes to `snapshots/SnapshotGasTest.json` group with name `lastCallA`. | ||
function testSnapshotGasLastCall() public { | ||
flare.run(1); | ||
vm.snapshotGasLastCall("lastCallA"); | ||
} | ||
} | ||
``` | ||
|
||
For each of the above examples you can also use the `group` variant of the cheatcodes to group the snapshots together in a custom group. | ||
|
||
```solidity | ||
contract SnapshotGasTest is Test { | ||
uint256 public slot0; | ||
/// Writes to `snapshots/CustomGroup.json` group with name `internalA`, `internalB`, and `internalC`. | ||
function testSnapshotGas() public { | ||
vm.startSnapshotGas("CustomGroup", "internalA"); | ||
slot0 = 1; | ||
vm.stopSnapshotGas(); | ||
vm.startSnapshotGas("CustomGroup", "internalB"); | ||
slot0 = 2; | ||
vm.stopSnapshotGas(); | ||
vm.startSnapshotGas("CustomGroup", "internalC"); | ||
slot0 = 0; | ||
vm.stopSnapshotGas(); | ||
} | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
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,81 @@ | ||
## `snapshotState` cheatcodes | ||
|
||
### Signature | ||
|
||
```solidity | ||
/// Snapshot the current state of the evm. | ||
/// Returns the ID of the snapshot that was created. | ||
/// To revert a snapshot use `revertToState`. | ||
function snapshotState() external returns (uint256 snapshotId); | ||
/// Revert the state of the EVM to a previous snapshot | ||
/// Takes the snapshot ID to revert to. | ||
/// Returns `true` if the snapshot was successfully reverted. | ||
/// Returns `false` if the snapshot does not exist. | ||
/// **Note:** This does not automatically delete the snapshot. To delete the snapshot use `deleteStateSnapshot`. | ||
function revertToState(uint256 snapshotId) external returns (bool success); | ||
/// Revert the state of the EVM to a previous snapshot and automatically deletes the snapshots | ||
/// Takes the snapshot ID to revert to. | ||
/// Returns `true` if the snapshot was successfully reverted and deleted. | ||
/// Returns `false` if the snapshot does not exist. | ||
function revertToStateAndDelete(uint256 snapshotId) external returns (bool success); | ||
/// Removes the snapshot with the given ID created by `snapshot`. | ||
/// Takes the snapshot ID to delete. | ||
/// Returns `true` if the snapshot was successfully deleted. | ||
/// Returns `false` if the snapshot does not exist. | ||
function deleteStateSnapshot(uint256 snapshotId) external returns (bool success); | ||
/// Removes _all_ snapshots previously created by `snapshot`. | ||
function deleteStateSnapshots() external; | ||
``` | ||
|
||
### Description | ||
|
||
`snapshotState` takes a snapshot of the state of the blockchain and returns the identifier of the created snapshot. | ||
|
||
`revertToState` reverts the state of the blockchain to the given state snapshot. This deletes the given snapshot, as well as any snapshots taken after (e.g.: reverting to id 2 will delete snapshots with ids 2, 3, 4, etc.). | ||
|
||
### Examples | ||
|
||
```solidity | ||
struct Storage { | ||
uint slot0; | ||
uint slot1; | ||
} | ||
contract SnapshotStateTest is Test { | ||
Storage store; | ||
uint256 timestamp; | ||
function setUp() public { | ||
store.slot0 = 10; | ||
store.slot1 = 20; | ||
vm.deal(address(this), 5 ether); // balance = 5 ether | ||
timestamp = block.timestamp; | ||
} | ||
function testSnapshotState() public { | ||
uint256 snapshot = vm.snapshotState(); // saves the state | ||
// let's change the state | ||
store.slot0 = 300; | ||
store.slot1 = 400; | ||
vm.deal(address(this), 500 ether); | ||
vm.warp(12345); // block.timestamp = 12345 | ||
assertEq(store.slot0, 300); | ||
assertEq(store.slot1, 400); | ||
assertEq(address(this).balance, 500 ether); | ||
assertEq(block.timestamp, 12345); | ||
vm.revertToState(snapshot); // restores the state | ||
assertEq(store.slot0, 10, "snapshot revert for slot 0 unsuccessful"); | ||
assertEq(store.slot1, 20, "snapshot revert for slot 1 unsuccessful"); | ||
assertEq(address(this).balance, 5 ether, "snapshot revert for balance unsuccessful"); | ||
assertEq(block.timestamp, timestamp, "snapshot revert for timestamp unsuccessful"); | ||
} | ||
} | ||
``` |
Oops, something went wrong.