Repository navigation
Conversation
saim256
left a comment
There was a problem hiding this comment.
Approved. This is a clean fix for #4643: it removes the local
andom import, uses a module-level secrets.SystemRandom() for both zero-score choice() and weighted uniform() selection, and keeps the existing cumulative lottery semantics without introducing the fixed-scale tiny-weight collapse risk.\n\nVerification on head e6faa0ae7059107b60a4cdd823455627385aed7:\n- PYTHONPATH=rips/python python -m pytest tests\test_proof_of_antiquity_validator_selection.py -q -> 2 passed\n- python -m py_compile rips\python\rustchain\proof_of_antiquity.py tests\test_proof_of_antiquity_validator_selection.py -> passed\n- git diff --check origin/main...HEAD -- rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py -> passed\n- python tools\bcos_spdx_check.py --base-ref origin/main -> OK\n\nScope note for maintainers: this overlaps #4645 and #4665; among the competing #4643 fixes, this one keeps the smallest behavioral delta by delegating to SystemRandom.uniform() rather than hand-rolled scaling.
douglance
left a comment
There was a problem hiding this comment.
Approved current head fe6faa0 after validating the PoA validator-selection RNG hardening.
What I checked:
select_block_validator()no longer imports/uses the defaultrandommodule for validator selection- zero-score fallback selection now uses the module-level
secrets.SystemRandom().choice(...) - weighted validator selection keeps the existing cumulative distribution semantics but draws with
SystemRandom.uniform(...) - the tests monkeypatch
_SECURE_RANDOMand assert both secure choice and secure weighted-pick paths are reached
Validation on this PR head:
- initial
PYTHONPATH=rips/python uv run --no-project --with pytest python -m pytest tests/test_proof_of_antiquity_validator_selection.py -q-> failed because repotests/conftest.pyimports Flask PYTHONPATH=rips/python uv run --no-project --with pytest --with flask python -m pytest tests/test_proof_of_antiquity_validator_selection.py -q-> 2 passedpython3 -m py_compile rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py-> passedgit diff --check origin/main...HEAD -- rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py-> passedpython3 tools/bcos_spdx_check.py --base-ref origin/main-> OK- import smoke confirmed
_SECURE_RANDOMexists and isSystemRandom uv run --no-project --with pytest --with flask python -m pytest tests/security_audit/test_security_findings_2867.py::test_mempool_add_manage_tx_undefined -q-> 1 passed, existing PytestReturnNotNoneWarning
No remaining blocker from this review.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
LGTM! Thanks for contributing. Approved.
jaxint
left a comment
There was a problem hiding this comment.
Code review: LGTM! Thanks for contributing to RustChain. Approved.
loganoe
left a comment
There was a problem hiding this comment.
This is a solid secure-randomness fix. Validator selection now uses a secrets.SystemRandom instance for both zero-score choice and weighted selection, and the tests verify both code paths can be controlled through the secure RNG abstraction.
Checked:
PYTHONPATH=rips/python PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /tmp/rustchain-flask-venv/bin/python -m pytest -q tests/test_proof_of_antiquity_validator_selection.pyPYTHONPATH=rips/python PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /tmp/rustchain-flask-venv/bin/python -m py_compile rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.pygit diff --check origin/main...HEAD
TJCurnutte
left a comment
There was a problem hiding this comment.
Approved. This is a narrow fix and the focused validation passed.
Validation I ran on the PR head:
git diff --check origin/main...HEAD -- rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py
# passed
PYTHONPATH=rips/python /tmp/rustchain-review-venv/bin/python -B -m py_compile \
rips/python/rustchain/proof_of_antiquity.py \
tests/test_proof_of_antiquity_validator_selection.py
# passed
PYTHONPATH=rips/python /tmp/rustchain-review-venv/bin/python -B -m pytest tests/test_proof_of_antiquity_validator_selection.py -q
# .. [100%]
# 2 passed in 0.05sI also checked the changed selection path directly: the PR removes the old random.choice / random.uniform use from proof_of_antiquity.py, adds a module-level secrets.SystemRandom(), and the new monkeypatch tests verify both the zero-score fallback and weighted validator selection call that secure RNG object.
That gives me enough confidence that the validator lottery no longer depends on the non-cryptographic random module while keeping the existing selection behavior intact.
PR Review — Standard Quality ✓PR: #4666 — Fix: use secure RNG for PoA validator selection What I reviewed
Observations
LGTM. Bounty: #2782 |
jaxint
left a comment
There was a problem hiding this comment.
PR Review
✅ Approved
This PR: fix: use secure rng for PoA validator selection
- Code is clean and correct
- No obvious issues
- Follows project conventions
Thanks for contributing! 🙏
Reviewed by jaxint
Summary\n- Fixes #4643 by replacing predictable random.choice/random.uniform in PoA validator selection with a secrets.SystemRandom-backed selector.\n- Covers both zero-score tie selection and weighted lottery selection.\n- Adds focused regression tests that monkeypatch the secure RNG path to prove selection no longer depends on the standard random module.\n\n## Validation\n- PYTHONPATH=rips\python python -m pytest tests\test_proof_of_antiquity_validator_selection.py -q -> 2 passed\n- python -m py_compile rips\python\rustchain\proof_of_antiquity.py tests\test_proof_of_antiquity_validator_selection.py\n- git diff --check origin/main...HEAD -- rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py\n- python tools\bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK\n\n## Notes\nThis is an alternative to PR #4644, which currently only adds a misplaced import and leaves the random-based selection logic unchanged.