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

Commit

Permalink
Merge pull request #10 from hensha256/add-snap-last-call
Browse files Browse the repository at this point in the history
Add snapLastCall
  • Loading branch information
marktoda authored Apr 25, 2024
2 parents 1a5804e + c1de941 commit 9161f7c
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 36 deletions.
1 change: 0 additions & 1 deletion .forge-snapshots/add.snap

This file was deleted.

1 change: 0 additions & 1 deletion .forge-snapshots/addClosure.snap

This file was deleted.

2 changes: 1 addition & 1 deletion .forge-snapshots/checkManyAdd.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24330
17530
2 changes: 1 addition & 1 deletion .forge-snapshots/checkSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
303
349
2 changes: 1 addition & 1 deletion .forge-snapshots/internalClosure.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19177
22217
2 changes: 1 addition & 1 deletion .forge-snapshots/manyAdd.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24330
17530
2 changes: 1 addition & 1 deletion .forge-snapshots/manySstore.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56084
70348
1 change: 1 addition & 0 deletions .forge-snapshots/singleSstore.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
48459
1 change: 1 addition & 0 deletions .forge-snapshots/singleSstoreClosure.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
46269
1 change: 1 addition & 0 deletions .forge-snapshots/singleSstoreLastCall.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
43429
2 changes: 1 addition & 1 deletion .forge-snapshots/sizeTarget.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
303
349
2 changes: 1 addition & 1 deletion .forge-snapshots/sstoreClosure.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53894
68158
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ jobs:

- name: Run Forge tests
run: |
forge test -vvv
forge test --isolate -vvv
id: test
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
54 changes: 31 additions & 23 deletions test/GasSnapshot.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,43 @@ contract GasSnapshotTest is Test, GasSnapshot {
assertEq(value, "1234");
}

function testAdd() public {
snapStart("add");
simpleOperations.add();
function testSingleSstore() public {
snapStart("singleSstore");
simpleOperations.singleSstore();
snapEnd();

string memory value = vm.readLine(".forge-snapshots/add.snap");
assertEq(value, "5247");
string memory value = vm.readLine(".forge-snapshots/singleSstore.snap");
assertEq(value, "48459");
}

function testAddClosure() public {
snap("addClosure", simpleOperations.add);
function testSingleSstoreLastCall() public {
simpleOperations.singleSstore();
snapLastCall("singleSstoreLastCall");

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

function testSstoreClosure() public {
function testSingleSstoreClosure() public {
snap("singleSstoreClosure", simpleOperations.singleSstore);

string memory value = vm.readLine(".forge-snapshots/singleSstoreClosure.snap");
assertEq(value, "46269");
}

function testManySstoreClosure() public {
snap("sstoreClosure", simpleOperations.manySstore);

string memory value = vm.readLine(".forge-snapshots/sstoreClosure.snap");
assertEq(value, "53894");
assertEq(value, "68158");
}

function testInternalClosure() public {
snap("internalClosure", add);
snap("internalClosure", singleSstore);

string memory value = vm.readLine(".forge-snapshots/internalClosure.snap");
assertEq(value, "19177");
assertEq(value, "22217");
}

function testAddTwice() public {
Expand Down Expand Up @@ -78,7 +87,7 @@ contract GasSnapshotTest is Test, GasSnapshot {
snapEnd();

string memory value = vm.readLine(".forge-snapshots/manyAdd.snap");
assertEq(value, "24330");
assertEq(value, "17530");
}

function testManySstore() public {
Expand All @@ -87,14 +96,14 @@ contract GasSnapshotTest is Test, GasSnapshot {
snapEnd();

string memory value = vm.readLine(".forge-snapshots/manySstore.snap");
assertEq(value, "56084");
assertEq(value, "70348");
}

function testSnapshotCodeSize() public {
SimpleOperations sizeTarget = new SimpleOperations();
snapSize("sizeTarget", address(sizeTarget));
string memory size = vm.readLine(".forge-snapshots/sizeTarget.snap");
assertEq(size, "303");
assertEq(size, "349");
}

function testSnapshotCheckSize() public {
Expand All @@ -106,7 +115,7 @@ contract GasSnapshotTest is Test, GasSnapshot {
function testSnapshotCheckSizeFail() public {
setCheckMode(true);
SimpleOperations sizeTarget = new SimpleOperations();
vm.expectRevert(abi.encodeWithSelector(GasSnapshot.GasMismatch.selector, 1, 303));
vm.expectRevert(abi.encodeWithSelector(GasSnapshot.GasMismatch.selector, 1, 349));
snapSize("checkSizeFail", address(sizeTarget));
}

Expand All @@ -123,14 +132,13 @@ contract GasSnapshotTest is Test, GasSnapshot {
// preloaded with the wrong value
snapStart("checkManySstore");
simpleOperations.manySstore();
vm.expectRevert(abi.encodeWithSelector(GasSnapshot.GasMismatch.selector, 1, 59561));
vm.expectRevert(abi.encodeWithSelector(GasSnapshot.GasMismatch.selector, 1, 73825));
snapEnd();
}

function add() internal pure {
uint256 x = 0;
for (uint256 i = 0; i < 100; i++) {
x += i;
}
uint256 internal test;

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

0 comments on commit 9161f7c

Please sign in to comment.