Skip to content

Commit

Permalink
add gas snapshots, update state snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnacks committed Oct 3, 2024
1 parent 8423c1b commit 240ec61
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 70 deletions.
3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@
- [`txGasPrice`](./cheatcodes/tx-gas-price.md)
- [`startStateDiffRecording`](./cheatcodes/start-state-diff-recording.md)
- [`stopAndReturnStateDiff`](./cheatcodes/stop-and-return-state-diff.md)
- [`snapshotState`](./cheatcodes/state-snapshots.md)
- [`snapshotGas`](./cheatcodes/gas-snapshots.md)
- [Assertions](./cheatcodes/assertions.md)
- [`expectRevert`](./cheatcodes/expect-revert.md)
- [`expectEmit`](./cheatcodes/expect-emit.md)
Expand Down Expand Up @@ -493,7 +495,6 @@
- [`createWallet`](./cheatcodes/create-wallet.md)
- [`copyStorage`](./cheatcodes/copy-storage.md)
- [`setArbitraryStorage`](./cheatcodes/set-arbitrary-storage.md)
- [Snapshots](./cheatcodes/snapshots.md)
- [RPC](./cheatcodes/rpc.md)
- [Files](./cheatcodes/fs.md)
- [Forge Standard Library Reference](./reference/forge-std/README.md)
Expand Down
175 changes: 175 additions & 0 deletions src/cheatcodes/gas-snapshots.md
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();
}
}
```
63 changes: 0 additions & 63 deletions src/cheatcodes/snapshots.md

This file was deleted.

81 changes: 81 additions & 0 deletions src/cheatcodes/state-snapshots.md
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");
}
}
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Gas Snapshots
## Gas Function Snapshots

Forge can generate gas snapshots for all your test functions. This can
be useful to get a general feel for how much gas your contract will consume,
Expand Down
37 changes: 37 additions & 0 deletions src/forge/gas-section-snapshots.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Gas Section Snapshots

Forge can capture gas snapshots over arbitrary sections inside of your test functions. This can be useful to get a granular measurement of how much gas your logic is consuming as both external calls and internal gas usage are measured.

To usage gas snapshots, use the following [`cheatcodes`](../cheatcodes/gas-snapshots.md) in your test functions:

```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);
```
Loading

0 comments on commit 240ec61

Please sign in to comment.