Skip to content

feat: add useSeed cheatcode to set RNG seed #10698

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Another-DevX
Copy link

@Another-DevX Another-DevX commented Jun 4, 2025

Closes #10664

@Another-DevX Another-DevX changed the title WIP: mock deterministic shuffle implementation WIP: deterministic shuffle implementation Jun 4, 2025
@Another-DevX Another-DevX changed the title WIP: deterministic shuffle implementation feat: add useSeed cheatcode to set RNG seed Jun 5, 2025
@Another-DevX
Copy link
Author

Another-DevX commented Jun 5, 2025

Gm @DaniPopes @mattsse @grandizzy
Could you 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, makes sense. Please correct tests and run forge fmt for the new Seed.t.sol file in order to fix CI

vm.setSeed(seed);

// Call a foundry cheatcode to get a random value (this depends on the integration)
uint256 rand1 = uint256(vm.randUint());
Copy link
Collaborator

Choose a reason for hiding this comment

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

should be vm.randomUint() instead vm.randUint()

Copy link
Author

Choose a reason for hiding this comment

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

Gm, just did it

Copy link
Author

Choose a reason for hiding this comment

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

BTW @grandizzy, how can I run the tests on my end? It's not really clear in the docs.
I just want to run only Seed.t.sol using the forge binary to verify that the implementation works properly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

for those you can cargo run test_cheats_local_default

uint256 seed = 123456789;
vm.setSeed(seed);

// Call a foundry cheatcode to get a random value (this depends on the integration)
Copy link
Collaborator

@grandizzy grandizzy Jun 6, 2025

Choose a reason for hiding this comment

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

I think should only assert same values when seed set?

Copy link
Author

Choose a reason for hiding this comment

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

I found the issue. It was related to how the seed was being stored in the config. Since config is wrapped in an Arc, calling Arc::make_mut only modifies the underlying data if there are no other strong references. In my case, the config was being cloned elsewhere, so make_mut was creating a new copy, and the mutation wasn't visible where it needed to be.

I fixed it by explicitly cloning the config, updating the seed, and replacing the entire Arc inside Cheatcodes. I also made sure to reset the RNG instance so the new seed takes effect.
The tests are working properly now. Let me know if you can take another look, sorry for the noise! :b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

feat(Vm): add deterministic shuffling vm.shuffle(salt)
2 participants