-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix(fuzz) - consistent snapshot results between runs #7951
Conversation
I think we should go after the initial reason behind non-deterministic results without disabling features for snapshot runs. fuzz tests do not collect values from state since #7552. So I believe we are dealing here with non-deterministic initial dictionary construction. This is something I've already seen in #7106 (comment). Basically, we want to ensure that this fn is deterministic: foundry/crates/evm/fuzz/src/strategies/state.rs Lines 108 to 129 in 1ddea96
At the moment I can see at least 2 issues here:
I don't think 2 really matters as setup state always contains at least a single account of a test contract. I've addressed 1 locally by sorting storage keys before inserting and it seems to fix the issue at least for the #7942 case. |
@klkvr makes perfect sense, thanks! If you can push your local fix I can use it to test some others projects that were having similar issues |
@grandizzy pushed my local approach, Ig you'd have to drop your previous commit for it to actually take effect for snapshot |
This reverts commit cf187fb.
@klkvr I tested #5689 (comment) and it looks good too |
thanks! @grandizzy one thing i noticed is that the snapshots for the exact same tests that used to be flaky now change with unrelated code changes, even just natspec. here is an example in the same reproduction repository i created for the issue: cruzdanilo/foundry-snapshot-repro@adbd43b it's not so bad, but it does create a lot of noise in the snapshot diff and create snapshot diffs in commits that shouldn't have any, confusing collaborators. |
@cruzdanilo most probably this happens because we also populate fuzz dict from push bytes (motivated by #1168 ) |
@grandizzy how can a natspec-only change impact the push bytes? |
@cruzdanilo I meant probably the order of values we collect here is not ordered either and affected by such changes as well, will investigate more why this happens |
@cruzdanilo so actually it is different, for example for contract
output of
|
such change only affects metadata, we should be able to just ignore it when collecting push bytes though small unrelated test contract changes would still probably affect snapshot data |
yeah, would be nice to have such, going to look into updating function to ignore metadata then foundry/crates/evm/fuzz/src/strategies/state.rs Lines 322 to 355 in 44d98ea
|
@cruzdanilo btw, if you find acceptable as an workaround until a fix is implemented - you could use |
Motivation
Closes #4917
Closes #5199 - tested with #5199 (comment)
Closes #5689 - tested with #5689 (comment)
Closes #6179
Closes #7942 - tested with https://github.com/cruzdanilo/foundry-snapshot-repro
Solution
see #7942 (comment) when fuzzing values from state there are cases when input could slightly differ between subsequent runs. Until we fuzz based on values type the proposed workaround is to avoid using state values when running forge snapshot.
The fix was tested with projects above but I couldn't minimize it to a simpler one suitable for unit test (in the sample I debugged the difference in snapshot appears when using an unchecked and assembly block).
CC @klkvr