-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Another-DevX
wants to merge
12
commits into
foundry-rs:master
Choose a base branch
from
Another-DevX:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7d998fb
feat: mock deterministic shuffle implementation
Another-DevX e162ff2
feat: implement useSeed()
Another-DevX 80f49bb
fix(test): typo
Another-DevX 4db2f6b
Merge branch 'master' into master
grandizzy b976586
Merge branch 'foundry-rs:master' into master
Another-DevX ae4ee91
fix(test): correct implementation of randomUint()
Another-DevX 306cc95
fix: refactor seed handling logic and remove redundant assertion message
Another-DevX a46802c
chore: remove unused imports and redundant newline
Another-DevX 68b1b1d
fix(test): add seed validation and tests for shuffle consistency
Another-DevX d1044e8
chore: add Shuffle test
Another-DevX 7395696
Merge branch 'master' into master
Another-DevX 9aab236
Merge branch 'master' into master
Another-DevX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
import "cheats/Vm.sol"; | ||
|
||
contract SeedTest is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
|
||
function testSeedAffectsRandom() public { | ||
// Use a known seed | ||
uint256 seed = 123456789; | ||
vm.setSeed(seed); | ||
|
||
// Call a foundry cheatcode to get a random value (this depends on the integration) | ||
uint256 rand1 = uint256(vm.randomUint()); | ||
|
||
// Reset the seed and verify the result is the same | ||
vm.setSeed(seed); | ||
uint256 rand2 = uint256(vm.randomUint()); | ||
|
||
uint256 rand3 = uint256(vm.randomUint()); | ||
// If the seed is the same, the random value must be equal | ||
assertEq(rand1, rand2); | ||
assertTrue(rand1 != rand3); | ||
} | ||
|
||
Another-DevX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function testSeedChangesRandom() public { | ||
// Use one seed | ||
vm.setSeed(1); | ||
uint256 randA = uint256(vm.randomUint()); | ||
|
||
// Use a different seed | ||
vm.setSeed(2); | ||
uint256 randB = uint256(vm.randomUint()); | ||
|
||
// Values must be different | ||
assertTrue(randA != randB, "Random value must be different if seed is different"); | ||
} | ||
|
||
function testSeedAffectsShuffle() public { | ||
// Use a known seed | ||
uint256 seed = 123456789; | ||
vm.setSeed(seed); | ||
|
||
// Create two identical arrays | ||
uint256[] memory array1 = new uint256[](5); | ||
uint256[] memory array2 = new uint256[](5); | ||
for (uint256 i = 0; i < 5; i++) { | ||
array1[i] = i; | ||
array2[i] = i; | ||
} | ||
|
||
// Shuffle both arrays with the same seed | ||
array1 = vm.shuffle(array1); | ||
vm.setSeed(seed); // Reset the seed to get the same shuffle pattern | ||
array2 = vm.shuffle(array2); | ||
|
||
// Compare elements - they should be identical after shuffle | ||
for (uint256 i = 0; i < array1.length; i++) { | ||
assertEq(array1[i], array2[i], "Arrays should be identical with same seed"); | ||
} | ||
} | ||
|
||
function testDifferentSeedsProduceDifferentShuffles() public { | ||
// Create the initial array | ||
uint256[] memory array1 = new uint256[](5); | ||
uint256[] memory array2 = new uint256[](5); | ||
for (uint256 i = 0; i < 5; i++) { | ||
array1[i] = i; | ||
array2[i] = i; | ||
} | ||
|
||
// Use first seed | ||
vm.setSeed(1); | ||
array1 = vm.shuffle(array1); | ||
|
||
// Use second seed | ||
vm.setSeed(2); | ||
array2 = vm.shuffle(array2); | ||
|
||
// Arrays should be different (we'll check at least one difference exists) | ||
bool foundDifference = false; | ||
for (uint256 i = 0; i < array1.length; i++) { | ||
if (array1[i] != array2[i]) { | ||
foundDifference = true; | ||
break; | ||
} | ||
} | ||
assertTrue(foundDifference, "Arrays should be different with different seeds"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
import "cheats/Vm.sol"; | ||
|
||
contract ShuffleTest is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
|
||
function testDeterministicShuffle() public { | ||
// Use a known seed | ||
uint256 seed = 123456789; | ||
vm.setSeed(seed); | ||
|
||
// Create two identical arrays | ||
uint256[] memory array1 = new uint256[](5); | ||
uint256[] memory array2 = new uint256[](5); | ||
for (uint256 i = 0; i < 5; i++) { | ||
array1[i] = i; | ||
array2[i] = i; | ||
} | ||
|
||
// Shuffle both arrays with the same seed | ||
array1 = vm.shuffle(array1); | ||
vm.setSeed(seed); // Reset the seed to get the same shuffle pattern | ||
array2 = vm.shuffle(array2); | ||
|
||
// Compare elements - they should be identical after shuffle | ||
for (uint256 i = 0; i < array1.length; i++) { | ||
assertEq(array1[i], array2[i], "Arrays should be identical with same seed"); | ||
} | ||
} | ||
|
||
function testDifferentSeedsProduceDifferentShuffles() public { | ||
// Create the initial array | ||
uint256[] memory array1 = new uint256[](5); | ||
uint256[] memory array2 = new uint256[](5); | ||
for (uint256 i = 0; i < 5; i++) { | ||
array1[i] = i; | ||
array2[i] = i; | ||
} | ||
|
||
// Use first seed | ||
vm.setSeed(1); | ||
array1 = vm.shuffle(array1); | ||
|
||
// Use second seed | ||
vm.setSeed(2); | ||
array2 = vm.shuffle(array2); | ||
|
||
// Arrays should be different (we'll check at least one difference exists) | ||
bool foundDifference = false; | ||
for (uint256 i = 0; i < array1.length; i++) { | ||
if (array1[i] != array2[i]) { | ||
foundDifference = true; | ||
break; | ||
} | ||
} | ||
assertTrue(foundDifference, "Arrays should be different with different seeds"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, somake_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