Skip to content
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: gas benchmark setup #176

Merged
merged 2 commits into from
Sep 16, 2024
Merged

feat: gas benchmark setup #176

merged 2 commits into from
Sep 16, 2024

Conversation

adamegyed
Copy link
Contributor

Motivation

As we optimize MAv2, we want accurate gas benchmarking that can be automatically calculated with low effort, and checked in to VCS. This is following the general plan outlined here.

Solution

Install forge-gas-snapshot.

Create a new foundry profile called gas, with a separate set of tests used to generate the gas snapshots. Included in this PR are ports of the "Runtime: Account Creation" tests from aa-benchmarks, which are both very close the previously observed values in hardhat:

  • SimpleAccount creation in foundry: 174143 gas
  • SimpleAccount creation in hardhat: 174219 gas
  • LightAccount v2 creation in foundry: 143836
  • LightAccount v2 creation in hardhat: 143842

I'm not sure how to account for the small remaining gas discrepancies, given the these setups use the exact same bytecode. But the error is small enough that I think we can proceed here.

Also, I'm not sure if we should keep these test cases in this repo long-term, or eventually migrate them somewhere else. I'm leaving them here for now, while we work to create the test cases for MAv2.

Unfortunately, the forge-gas-snapshot dependency expects forge-std to be remapped differently from the rest of the repo. In theory there should be a way to use foundry's "remappings context" (more info) to specify this, but I wasn't able to get it to work, so I created separate remappings within foundry.toml for the two different profiles.

Also adds a check in CI for having gas snapshots up-to-date, and moves the linting check out to a different workflow file. Both of these still use the shared CI setup action.

Now that there's a fourth linter config, this also moves the solhint config files and the slither config file into a new folder called config/, to de-clutter the repo root.

Also adds common actions as scripts in package.json:

  • pnpm gas runs the gas tests and updates values
  • pnpm gas:check checks that the gas tests have their updated values
  • pnpm fmt to run forge fmt for all folders, and a checker pnpm fmt:check
  • Updated pnpm lint to include the new gas/ folder.

Delete an accidentally-committed yarn.lock.

@adamegyed adamegyed requested a review from a team September 13, 2024 22:20
Copy link

Contract sizes:

| Contract                     | Size (B) | Margin (B) |
|------------------------------|----------|------------|
| AccountFactory               |    4,763 |     19,813 |
| ERC1967Proxy                 |      104 |     24,472 |
| ModularAccount               |   23,890 |        686 |
| SemiModularAccount           |   25,019 |       -443 |
| SingleSignerValidationModule |    3,444 |     21,132 |
| TokenReceiverModule          |    2,189 |     22,387 |

Code coverage:

File % Lines % Statements % Branches % Funcs
src/account/AccountExecutor.sol 75.00% (3/4) 75.00% (3/4) 0.00% (0/1) 100.00% (1/1)
src/account/AccountFactory.sol 33.33% (10/30) 35.71% (15/42) 50.00% (1/2) 18.18% (2/11)
src/account/AccountStorageInitializable.sol 84.21% (16/19) 84.62% (22/26) 60.00% (3/5) 100.00% (2/2)
src/account/ModularAccount.sol 88.41% (183/207) 89.29% (250/280) 74.29% (26/35) 97.14% (34/35)
src/account/ModularAccountView.sol 96.55% (28/29) 95.24% (40/42) 100.00% (2/2) 100.00% (2/2)
src/account/ModuleManagerInternals.sol 84.92% (107/126) 84.62% (143/169) 42.86% (6/14) 100.00% (11/11)
src/account/SemiModularAccount.sol 0.00% (0/51) 0.00% (0/68) 0.00% (0/9) 0.00% (0/18)
src/helpers/HookConfigLib.sol 47.06% (8/17) 65.62% (21/32) 100.00% (0/0) 66.67% (8/12)
src/helpers/KnownSelectors.sol 100.00% (27/27) 100.00% (60/60) 100.00% (0/0) 100.00% (3/3)
src/helpers/ModuleEntityLib.sol 62.50% (5/8) 45.00% (9/20) 100.00% (0/0) 50.00% (3/6)
src/helpers/SparseCalldataSegmentLib.sol 100.00% (23/23) 100.00% (29/29) 100.00% (5/5) 100.00% (6/6)
src/helpers/ValidationConfigLib.sol 44.44% (8/18) 52.63% (20/38) 100.00% (0/0) 61.54% (8/13)
src/modules/BaseModule.sol 9.09% (1/11) 26.67% (4/15) 0.00% (0/1) 50.00% (1/2)
src/modules/ModuleEIP712.sol 100.00% (1/1) 100.00% (2/2) 100.00% (0/0) 100.00% (1/1)
src/modules/ReplaySafeWrapper.sol 100.00% (6/6) 100.00% (7/7) 100.00% (0/0) 100.00% (2/2)
src/modules/TokenReceiverModule.sol 69.23% (9/13) 69.23% (9/13) 100.00% (0/0) 28.57% (2/7)
src/modules/validation/SingleSignerValidationModule.sol 85.71% (18/21) 86.96% (20/23) 100.00% (3/3) 88.89% (8/9)
Total 74.14% (453/611) 75.17% (654/870) 59.74% (46/77) 66.67% (94/141)

contract LightAccountGasTest is GasSnapshot {
address internal constant _LIGHT_ACCOUNT_FACTORY = 0x0000000000400CdFef5E2714E63d8040b700BC24;

bytes internal constant _LIGHT_ACCOUNT_FACTORY_BYTECODE =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So LightAccount and SimpleAccount are just here for benchmarking right-- so we're not expecting them to get any changes, as the bytecode is frozen completely, yeah?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, these are frozen at specific version tags. I think we might be OK with removing these in the future, I just added them for now to verify that the gas benchmarking setup works.

@adamegyed adamegyed merged commit a8e7cc8 into develop Sep 16, 2024
5 checks passed
@adamegyed adamegyed deleted the adam/gas-benchmark-setup branch September 16, 2024 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants