foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
7.95k stars 1.61k forks source link

feat: expand snapshot functionality #2056

Open mds1 opened 2 years ago

mds1 commented 2 years ago

Component

Forge

Describe the feature you would like

Inspired by this twitter convo

Feature Spec

Right now forge snapshot saves the gas usage of each test to a file called .gas-snapshot. This is useful for things like gas golfing, or ensuring that a changeset does not increase gas costs. Similar functionality can be applied to other properties of a codebase.

I propose modifying forge snapshot as follows:

Instead of being a single command, it now has subcommands to snapshot various things:

Instead of saving to the project root, these commands save a file called <property>.snapshot to a folder named something like snapshot, snap, or ss. Examples:

The --check, --diff, and --snap options should be supported for each subcommand and behave the same as they do now. Some subcommands may have additional options, such as:

Open questions

  1. Are we ok with implementing this breaking change to snapshot behavior without waiting for a specific breaking change foundry release? Let's get thoughts from snapshot users, but IMO this is useful enough and a small enough breaking change that we should be ok with it.
  2. forge inspect supports multiple variants for a given property. For example, you can see that forge inspect MyContract storage, forge inspect MyContract storageLayout, forge inspect MyContract storage-layout, and forge inspect MyContract storage_layout all print the storage layout. We'd want to standardize on which name we use for the output files. My suggestion is using the JSON field name output by solc, which for this example I believe is storage.

Additional context

No response

onbjerg commented 2 years ago

Ref #137 #1795

Also might complicate https://github.com/foundry-rs/foundry/issues/1980 because of command arg flattening, see https://github.com/foundry-rs/foundry/issues/1980#issuecomment-1160987510

Edit: Another consideration is that snapshot files are nameable right now, so it's possible to have e.g. a .fuzz-snapshot and a .unit-snapshot that you diff/check separately and we should probably preserve that behavior

One final consideration is that there's been some talk of merging snapshot/gas report but I can't find the convo

Edit 2: Given all above points an alternative suggestion (no strong preference, just wanted to add context to the issue):

maurelian commented 2 years ago

Only comment is that we'd definitely love to see this given I was speccing out a similar script for ourselves around the time this issue was created. For now I will likely follow the approach used by nomad.

mds1 commented 1 year ago

Some other ideas for gas snapshots specifically can be found in https://github.com/foundry-rs/foundry/issues/137

zerosnacks commented 1 month ago

Would be great to upstream the core ideas from here: https://github.com/marktoda/forge-gas-snapshot as used here: https://github.com/Uniswap/v4-core/tree/main/.forge-snapshots

zerosnacks commented 1 week ago

Considering the growing popularity of https://github.com/marktoda/forge-gas-snapshot (used in Uniswap V4, Balancer V3, Euler V2) I've proposed to add native functionality for its feature set.

There is rough consensus on moving ahead with the following changes:

Some of my concerns:

@mds1 curious on your thoughts about this proposal

marktoda commented 1 week ago

Thanks for proposing this @zerosnacks - I'm (obviously) very in favor of upstreaming this into Foundry. For a bit more context, the primary reason we prefer this type of snapshotting at Uniswap is to get maximal granularity, allowing us to check how the gas of specific flows change over time in version control.

Generally I think the approach makes sense. A couple ideas -