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(cheatcodes): add ability to ignore (multiple) specific and partial reverts in fuzz and invariant tests #9179

Merged
merged 13 commits into from
Jan 22, 2025

Conversation

emo-eth
Copy link
Contributor

@emo-eth emo-eth commented Oct 23, 2024

Motivation

As raised in #4271, sometimes the fuzzer dictionary will uncover real but irrelevant errors in contract code, especially when testing against stateful forks. Runs that encounter anticipated errors should be thrown out and count towards the global assume counter.

Closes #4271.

Solution

This PR adds overloaded and related function definitions to vm.assumeNoRevert, introduced in #8780, as follows:

    /// Represents a "potential" revert reason from a single subsequent call when using `vm.assumeNoReverts`.
    /// Reverts that match will result in a FOUNDRY::ASSUME rejection, whereas unmatched reverts will be surfaced
    /// as normal.
    struct PotentialRevert {
        /// The allowed origin of the revert opcode; address(0) allows reverts from any address
        address reverter;
        /// When true, only matches on the beginning of the revert data, otherwise, matches on entire revert data
        bool partialMatch;
        /// The data to use to match encountered reverts
        bytes revertData;
    }

    /// Discard this run's fuzz inputs and generate new ones if next call reverts with the potential revert parameters.
    #[cheatcode(group = Evm, safety = Safe)]
    function assumeNoRevert(PotentialRevert calldata potentialRevert) external pure;

    /// Discard this run's fuzz inputs and generate new ones if next call reverts with the any of the potential revert parameters.
    #[cheatcode(group = Evm, safety = Safe)]
    function assumeNoRevert(PotentialRevert[] calldata potentialReverts) external pure;

Behavior

  • it should be possible to to reject any specific error without data (by encoding a 4-byte selector as revertData)
  • it should be possible to reject any specific error with arbitrary data (by encoding a signature including data as revertData)
  • it should be possible to reject any specific of error regardless of extra data (by encoding a 4-byte selector and specifying partialMatch as true)
  • it should be possible to reject multiple errors that a single call may produce (overloaded array param)
  • it should be possible to restrict these rejections to a specific reverter (the optional overloaded address reverter param)
  • anticipated reverts should reset after next non-cheatcode external call
  • combining a catch-all vm.assumeNoRevert() with a vm.assumeNoRevert(args) call should result in an error

(Currently an invariant dict related test is failing)

@emo-eth emo-eth force-pushed the emo/expand-assume-no-revert branch 2 times, most recently from 23bb2c6 to 3d057c1 Compare October 24, 2024 00:33
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

looks good! left couple of comments, going to move back in draft until fix added

crates/cheatcodes/src/inspector.rs Outdated Show resolved Hide resolved
crates/cheatcodes/src/inspector.rs Outdated Show resolved Hide resolved
crates/cheatcodes/src/test/revert.rs Outdated Show resolved Hide resolved
crates/forge/tests/cli/test_cmd.rs Outdated Show resolved Hide resolved
@grandizzy grandizzy marked this pull request as draft October 24, 2024 05:56
@grandizzy
Copy link
Collaborator

@mds1 would be great to have your thoughts re new proposed cheatcodes. Thanks!

@emo-eth emo-eth force-pushed the emo/expand-assume-no-revert branch from 59efe2d to 3800b7a Compare November 2, 2024 00:02
@emo-eth
Copy link
Contributor Author

emo-eth commented Nov 2, 2024

@grandizzy should be in a good state now – apologies, had a hectic week. let me know if anything else looks off.

@jenpaff jenpaff marked this pull request as ready for review November 4, 2024 10:05
@jenpaff
Copy link
Collaborator

jenpaff commented Nov 4, 2024

@grandizzy should be in a good state now – apologies, had a hectic week. let me know if anything else looks off.

moving PR to ready for review

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

thank you, left couple of comments.

crates/forge/tests/cli/test_cmd.rs Outdated Show resolved Hide resolved
crates/cheatcodes/spec/src/vm.rs Outdated Show resolved Hide resolved
crates/cheatcodes/src/test/revert_handlers.rs Show resolved Hide resolved
crates/cheatcodes/src/test/assume.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

Thank you, looks good! I pushed a change to simplify test and have one more nit re assume_no_revert fn. @zerosnacks @yash-atreya @klkvr pls chime in when time permits. thank you!

crates/cheatcodes/src/test/assume.rs Outdated Show resolved Hide resolved
@emo-eth emo-eth requested a review from grandizzy November 6, 2024 22:57
@emo-eth emo-eth force-pushed the emo/expand-assume-no-revert branch from ac4a7d5 to c215515 Compare November 7, 2024 05:23
grandizzy
grandizzy previously approved these changes Nov 7, 2024
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

Lgtm, thank you! Pending additional review before merging

@mds1
Copy link
Collaborator

mds1 commented Nov 7, 2024

In general UX LGTM! One thing is let's make sure the behavior between this and the various expectRevert cheats is consistent, e.g. if you pass a 4byte selector does it only match if the revert data is exact, or do partials match also? Given that this cheat is assumeNoRevert for partial match I think it's inconsistent as spec'd in the PR description

https://github.com/foundry-rs/forge-std/blob/da591f56d8884c5824c0c1b3103fbcfd81123c4c/src/Vm.sol#L2102-L2124

@emo-eth
Copy link
Contributor Author

emo-eth commented Nov 8, 2024

In general UX LGTM! One thing is let's make sure the behavior between this and the various expectRevert cheats is consistent, e.g. if you pass a 4byte selector does it only match if the revert data is exact, or do partials match also? Given that this cheat is assumeNoRevert for partial match I think it's inconsistent as spec'd in the PR description

https://github.com/foundry-rs/forge-std/blob/da591f56d8884c5824c0c1b3103fbcfd81123c4c/src/Vm.sol#L2102-L2124

@mds1 apologies, i can update the description (pending final decision) – @grandizzy suggested having the default behavior to accept partial matches.

having the granularity of assumeNoRevert vs assumeNoPartialRevert would be useful in the scenario of colliding selectors – but that's pretty rare unless intentional. i am having a hard time thinking of cases where a test might be pulling in (arbitrary) external or random revert bytes that might have collisions – but there are possibly some, and it might be worth having the disambiguation.

i do appreciate the explicitness, though i do think assumeNoRevert is already kind of a counter-intuitive name, and assumeNoPartialRevert adds onto that complexity. eg assumeNoRevertPartial is maybe slightly less confusing, but breaks with the expectPartialRevert naming convention...

happy to add back in if there's consensus

@mds1
Copy link
Collaborator

mds1 commented Nov 8, 2024

Thanks for those details, so my thoughts are:

  • We already have expectRevert(bytes4) and expectPartialRevert(bytes4). To this point—"would be useful in the scenario of colliding selectors – but that's pretty rare unless intentional"—I'm unsure of the motivation for this split
  • The least surprising assume behavior would therefore be analogous meaning assumeNoRevert(bytes4) and assumeNoPartialRevert(bytes4)
  • It sounds like there are some breaking change considerations here, which I am not fully up to speed on, in which case that is suitable rationale for having inconsistent behavior between the expect and assume cheats
  • Therefore I'll defer to you and @grandizzy to decide
  • But, we should track this and unify / simplify for foundry v1

@emo-eth emo-eth force-pushed the emo/expand-assume-no-revert branch from a28cc9e to 585e1e0 Compare January 8, 2025 04:05
@emo-eth
Copy link
Contributor Author

emo-eth commented Jan 9, 2025

It looks like the CI is failing related to the proposed changes:
https://github.com/foundry-rs/foundry/actions/runs/12635122448/job/35204363739?pr=9179#step:12:361

weird, I don't see why this test would fail with proposed changes and cannot reproduce the test failure locally either ... 🤔

Likewise not sure why my changes would affect it - but does seem to consistently be failing for polygon between runs 🤔

@grandizzy
Copy link
Collaborator

It looks like the CI is failing related to the proposed changes:
https://github.com/foundry-rs/foundry/actions/runs/12635122448/job/35204363739?pr=9179#step:12:361

weird, I don't see why this test would fail with proposed changes and cannot reproduce the test failure locally either ... 🤔

Likewise not sure why my changes would affect it - but does seem to consistently be failing for polygon between runs 🤔

I rebased PR with master containing fixes for other flaky tests (was thinking that failure could be an effect of those) but same test failing, checking some more why's that

@grandizzy grandizzy added T-feature Type: feature C-forge Command: forge labels Jan 13, 2025
@grandizzy grandizzy force-pushed the emo/expand-assume-no-revert branch 9 times, most recently from 131c1bf to 8e6f3e4 Compare January 13, 2025 16:17
@grandizzy grandizzy force-pushed the emo/expand-assume-no-revert branch from 8e6f3e4 to 31f1e43 Compare January 13, 2025 16:28
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

thank you, lgtm! had to rework the failing test and split tested contracts and test contract in different files as they were affecting outcome. waiting for one more review before merging

@grandizzy grandizzy self-assigned this Jan 20, 2025
@emo-eth
Copy link
Contributor Author

emo-eth commented Jan 21, 2025

@zerosnacks @yash-atreya @klkvr would love to get this merged if there aren't any more blockers

Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

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

Thanks @emo-eth!

LGTM

@zerosnacks zerosnacks merged commit aa04294 into foundry-rs:master Jan 22, 2025
22 checks passed
Copy link
Member

Choose a reason for hiding this comment

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

this looks unused?

This comment was marked as outdated.

Copy link
Collaborator

@grandizzy grandizzy Jan 22, 2025

Choose a reason for hiding this comment

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

yeah, tests within test_cmd.rs are not importing this file but rather adding same source inline, so this can be removed

zerosnacks added a commit that referenced this pull request Jan 22, 2025
zerosnacks added a commit that referenced this pull request Jan 22, 2025
remove redundant test.sol, follow up of #9179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge T-feature Type: feature
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

feat(cheatcodes): add ability to exclude certain custom errors and revert reason strings from failing tests
7 participants