-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): replace gas report #1049
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5315460
copy over vulcan gas report strategy
holic a5c74a8
simplify
holic 971afae
flesh it out a bit more
holic e1917b7
move to std-contracts
holic f8eb4db
subtract constant gas, add gas reporter test
holic ac28fae
update gas report command
holic 0244cae
update comment
holic 884afc0
move tests to new gas report
holic 3439b53
move CI gas report to tests, since it'll be cached
holic 3289c2b
make gas report default off
holic 20b2285
update gas report
holic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,62 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
import { Test, console } from "forge-std/Test.sol"; | ||
|
||
contract GasReporter is Test { | ||
string private __currentGasReportName; | ||
uint256 private __currentGasReportValue = gasleft(); | ||
mapping(string => uint256) private __gasReports; | ||
string[] private __gasReportNames; | ||
|
||
function startGasReport(string memory name) internal { | ||
if (!vm.envOr("GAS_REPORTER_ENABLED", false)) return; | ||
require( | ||
bytes(__currentGasReportName).length == 0, | ||
string.concat( | ||
'gas report "', | ||
__currentGasReportName, | ||
'" is already running and only one report can be run at a time' | ||
) | ||
); | ||
require(__gasReports[name] == 0, string.concat('gas report "', name, '" already used for this test')); | ||
__currentGasReportName = name; | ||
vm.pauseGasMetering(); | ||
__currentGasReportValue = gasleft(); | ||
vm.resumeGasMetering(); | ||
} | ||
|
||
function endGasReport() internal { | ||
uint256 gas = gasleft(); | ||
if (!vm.envOr("GAS_REPORTER_ENABLED", false)) return; | ||
// subtract 160 gas used by the GasReporter itself | ||
// add 1 gas so we can later check that this is set | ||
uint256 gasUsed = __currentGasReportValue - gas - 160 + 1; | ||
require(gasUsed > 0, "gas report didn't use gas"); | ||
__gasReports[__currentGasReportName] = gasUsed; | ||
__gasReportNames.push(__currentGasReportName); | ||
printGasReport(__currentGasReportName); | ||
__currentGasReportName = ""; | ||
} | ||
|
||
modifier gasReport(string memory name) { | ||
startGasReport(name); | ||
_; | ||
endGasReport(); | ||
} | ||
|
||
function getGasUsed(string memory name) internal view returns (uint256) { | ||
require(__gasReports[name] > 0, string.concat('gas report "', name, '" not found')); | ||
return __gasReports[name]; | ||
} | ||
|
||
function printGasReport(string memory name) internal view { | ||
console.log(string.concat("GAS REPORT: ", vm.toString(__gasReports[name]), " ", name)); | ||
} | ||
|
||
function printGasReports() internal view { | ||
for (uint256 i = 0; i < __gasReportNames.length; i++) { | ||
printGasReport(__gasReportNames[i]); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moved here since the gas report runs best if the tests are already compiled/cached.