Skip to content

Fix mutable default argument anti-pattern for Random objects #4433

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

Merged
merged 2 commits into from
Jul 9, 2025

Conversation

jtraglia
Copy link
Member

@jtraglia jtraglia commented Jul 7, 2025

For the previous release (commit), I noticed that many of the test files were different. I found that we were using rng=Random(...) in function declarations which will not work like we want. The core issue is that default arguments are evaluated once when the function is defined, not each time it's called. So when we use rng=Random(...) in a function declaration, that same Random object instance gets shared across all function calls, accumulating state changes. For reference, see Mutable Default Arguments.

I tested this by running the following multiple times, confirming that the hash was the same each time:

rm -rf ../consensus-spec-tests
make reftests preset=minimal
find ../consensus-spec-tests -type f -exec sha256sum {} + | sort | sha256sum
image

To be clear, the next release's diff will be large, but the next next release's diff will be small 😄

jtraglia added 2 commits July 7, 2025 15:37
Looks like there were some tests which dependended on the buggy logic. We want
the parent block from prepare_random_pow_block to be different.
leolara
leolara previously approved these changes Jul 8, 2025
@jtraglia jtraglia changed the base branch from dev to master July 9, 2025 19:48
@jtraglia jtraglia dismissed leolara’s stale review July 9, 2025 19:48

The base branch was changed.

@jtraglia jtraglia merged commit 5ff726f into ethereum:master Jul 9, 2025
12 checks passed
@jtraglia jtraglia deleted the deterministic-tests branch July 9, 2025 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing CI, actions, tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants