Skip to content

Commit 30f307a

Browse files
authored
feat: gas reports and snapshots (#12724)
## Gas Reports You can run `./bootstrap.sh gas_report` to generate a detailed gas report for the current state and update the gas_report.md file. When running CI or tests with `./bootstrap.sh test`, the script will automatically check if gas usage has changed by running `./bootstrap.sh gas_report check`. If gas usage has changed, the test will fail and show a diff of the changes. If the changes in gas usage are expected and desired: 1. Review the diff shown in the output 2. Run `./bootstrap.sh gas_report` to update the gas report file 3. Commit the updated gas_report.md file NOTE: Our gas reporting excludes certain tests due to Forge limitations: - FeeRollupTest and MinimalFeeModelTest test suites are excluded - testInvalidBlobHash and testInvalidBlobProof test cases are excluded This is related to [this Foundry issue](foundry-rs/foundry#10074). This means that we don't report gas for blob validation (currently 50k gas per blob, and we use 3 blobs per propose in production). If you want to run gas reports directly with `forge`, you must use the environment variable `FORGE_GAS_REPORT=true` instead of the `--gas-report` flag. The `./bootstrap.sh gas_report` command does this for you automatically.
1 parent d56f69b commit 30f307a

6 files changed

Lines changed: 233 additions & 4 deletions

File tree

l1-contracts/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ yarn.lock
2424

2525
# 'deploy_contracts' script output
2626
serve/
27+
28+
gas_report.new.*
29+
gas_report.diff

l1-contracts/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Alternatively you can use docker instead, it will handle installations and run t
1212

1313
The `src` folder contain contracts that is to be used by the local developer testnet. It is grouped into 3 categories:
1414

15-
- `core` contains the required contracts, the bare minimum
15+
- `core` contains the required contracts, the bare minimum.
16+
- `governance` contains the contracts for the governance system.
1617
- `mock` contains stubs, for now an always true verifier.
1718
- `periphery` stuff that is nice to have, convenience contracts and functions belong in here.
1819

@@ -31,6 +32,29 @@ We use `forge fmt` to format. But follow a few general guidelines beyond the sta
3132
- Do `function transfer(address _to, uint256 _amount);`
3233
- use `_` prefix for `internal` and `private` functions.
3334

35+
## Gas Reports
36+
37+
You can run `./bootstrap.sh gas_report` to generate a detailed gas report for the current state and update the gas_report.md file.
38+
39+
When running CI or tests with `./bootstrap.sh test`, the script will automatically check if gas usage has changed by running `./bootstrap.sh gas_report check`. If gas usage has changed, the test will fail and show a diff of the changes.
40+
41+
If the changes in gas usage are expected and desired:
42+
43+
1. Review the diff shown in the output
44+
2. Run `./bootstrap.sh gas_report` to update the gas report file
45+
3. Commit the updated gas_report.md file
46+
47+
NOTE: Our gas reporting excludes certain tests due to Forge limitations:
48+
49+
- FeeRollupTest and MinimalFeeModelTest test suites are excluded
50+
- testInvalidBlobHash and testInvalidBlobProof test cases are excluded
51+
52+
This is related to [this Foundry issue](https://github.com/foundry-rs/foundry/issues/10074).
53+
54+
This means that we don't report gas for blob validation (currently 50k gas per blob, and we use 3 blobs per propose in production).
55+
56+
If you want to run gas reports directly with `forge`, you must use the environment variable `FORGE_GAS_REPORT=true` instead of the `--gas-report` flag. The `./bootstrap.sh gas_report` command does this for you automatically.
57+
3458
## Contracts:
3559

3660
The contracts are in a very early stage, and don't worry about gas costs right now. Instead they prioritize development velocity.

l1-contracts/bootstrap.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function build {
5353
function test_cmds {
5454
echo "$hash cd l1-contracts && solhint --config ./.solhint.json \"src/**/*.sol\""
5555
echo "$hash cd l1-contracts && forge fmt --check"
56-
echo "$hash cd l1-contracts && forge test --no-match-contract UniswapPortalTest"
56+
echo "$hash cd l1-contracts && forge test && ./bootstrap.sh gas_report"
5757
}
5858

5959
function test {
@@ -100,6 +100,29 @@ function inspect {
100100
done
101101
}
102102

103+
function gas_report {
104+
check=${1:-"no"}
105+
echo_header "l1-contracts gas report"
106+
forge --version
107+
108+
FORGE_GAS_REPORT=true forge test \
109+
--no-match-contract "(FeeRollupTest)|(MinimalFeeModelTest)|(UniswapPortalTest)" \
110+
--no-match-test "(testInvalidBlobHash)|(testInvalidBlobProof)" \
111+
--fuzz-seed 42 \
112+
--isolate \
113+
> gas_report.new.tmp
114+
grep "^|" gas_report.new.tmp > gas_report.new.md
115+
rm gas_report.new.tmp
116+
diff gas_report.new.md gas_report.md > gas_report.diff || true
117+
118+
if [ -s gas_report.diff -a "$check" = "check" ]; then
119+
cat gas_report.diff
120+
echo "Gas report has changed. Please check the diffs above, then run './bootstrap.sh gas_report' to update the gas report."
121+
exit 1
122+
fi
123+
mv gas_report.new.md gas_report.md
124+
}
125+
103126
# First argument is a branch name (e.g. master, or the latest version e.g. 1.2.3) to push to the head of.
104127
# Second argument is the tag name (e.g. v1.2.3, or commit-<hash>).
105128
# Third argument is the semver for package.json (e.g. 1.2.3 or 1.2.3-commit.<hash>)
@@ -195,6 +218,10 @@ case "$cmd" in
195218
"inspect")
196219
inspect
197220
;;
221+
"gas_report")
222+
shift
223+
gas_report "$@"
224+
;;
198225
test_cmds|release)
199226
$cmd
200227
;;

l1-contracts/foundry.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ out = 'out'
44
libs = ['lib']
55
solc = "0.8.27"
66
evm_version = 'cancun'
7+
# Helper to get all the contract names in the src/governance and src/core directories
8+
# find ./src/governance ./src/core ./src/periphery/Forwarder -type f -name "*.sol" -exec grep -h "^contract [A-Za-z]" {} \; | sed -E 's/contract ([A-Za-z0-9_]+).*/"\1"/' | tr "\n" ", "
9+
gas_reports = ["Governance","Registry","RewardDistributor","GovernanceProposer","CoinIssuer","Rollup","Inbox","Outbox","FeeJuicePortal","RollupCore","SlashingProposer","Slasher","Forwarder"]
710

811
remappings = [
912
"@oz/=lib/openzeppelin-contracts/contracts/",
@@ -29,4 +32,5 @@ tab_width = 2
2932
variable_override_spacing=false
3033

3134
[rpc_endpoints]
32-
mainnet_fork="https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c"
35+
mainnet_fork="https://mainnet.infura.io/v3/9928b52099854248b3a096be07a6b23c"
36+

0 commit comments

Comments
 (0)