Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
add snapLastCall
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Apr 24, 2024
1 parent 1a5804e commit 1cb8abc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions .forge-snapshots/singleSstoreLastCall.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
43429
13 changes: 11 additions & 2 deletions src/GasSnapshot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ contract GasSnapshot is Script {
}

/// @notice Snapshot the given external closure
/// @dev most accurate as storage cost semantics are not involved
function snap(string memory name, function() external fn) internal {
uint256 gasBefore = gasleft();
fn();
Expand All @@ -68,7 +67,6 @@ contract GasSnapshot is Script {
}

/// @notice Snapshot the given internal closure
/// @dev most accurate as storage cost semantics are not involved
function snap(string memory name, function() internal fn) internal {
uint256 gasBefore = gasleft();
fn();
Expand All @@ -80,6 +78,17 @@ contract GasSnapshot is Script {
}
}

/// @notice Snapshot using forge isolate of gas of the previous call
/// @dev most accurate as this uses a complete transaction and no storage semantics
function snapLastCall(string memory name) internal {
uint256 gasUsed = vm.lastCallGas().gasTotalUsed;
if (check) {
_checkSnapshot(name, gasUsed);
} else {
_writeSnapshot(name, gasUsed);
}
}

/// @notice Start a snapshot with the given name
/// @dev The next call to `snapEnd` will end the snapshot
function snapStart(string memory name) internal {
Expand Down
4 changes: 4 additions & 0 deletions src/test/SimpleOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ contract SimpleOperations {
}
}

function singleSstore() public {
test = block.timestamp + 3;
}

function manySstore() public {
for (uint256 i = 0; i < 100; i++) {
test = i + 2;
Expand Down
9 changes: 9 additions & 0 deletions test/GasSnapshot.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ contract GasSnapshotTest is Test, GasSnapshot {
assertEq(value, "5247");
}

function testAddLastCall() public {
simpleOperations.singleSstore();
snapLastCall("singleSstoreLastCall");

string memory value = vm.readLine(".forge-snapshots/singleSstoreLastCall.snap");
// includes 21,000 overhead for transaction, 20,000 clean SSTORE
assertEq(value, "43429");
}

function testAddClosure() public {
snap("addClosure", simpleOperations.add);

Expand Down

0 comments on commit 1cb8abc

Please sign in to comment.