Skip to content

fix: use secure rng for PoA validator selection - #4666

Closed
godd-ctrl wants to merge 1 commit into
Scottcjn:mainfrom
godd-ctrl:codex/fix-poa-validator-secure-rng
Closed

godd-ctrl wants to merge 1 commit into
Scottcjn:mainfrom
godd-ctrl:codex/fix-poa-validator-secure-rng

Conversation

@godd-ctrl

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) tests Test suite changes size/M PR: 51-200 lines labels May 11, 2026

@saim256 saim256 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 douglance left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved current head fe6faa0 after validating the PoA validator-selection RNG hardening.

What I checked:

  • select_block_validator() no longer imports/uses the default random module 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_RANDOM and 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 repo tests/conftest.py imports 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 passed
  • python3 -m py_compile rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py -> passed
  • git diff --check origin/main...HEAD -- rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> OK
  • import smoke confirmed _SECURE_RANDOM exists and is SystemRandom
  • 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 jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review: LGTM! Thanks for contributing to RustChain. Approved.

@loganoe loganoe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.py
  • PYTHONPATH=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.py
  • git diff --check origin/main...HEAD

@TJCurnutte TJCurnutte left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.05s

I 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.

@guangningsun

Copy link
Copy Markdown
Contributor

PR Review — Standard Quality ✓

PR: #4666 — Fix: use secure RNG for PoA validator selection

What I reviewed

  • rips/python/rustchain/proof_of_antiquity.py
  • tests/test_proof_of_antiquity_validator_selection.py (new 53-line test)

Observations

  1. Adding import secrets and using secrets.choice() for validator selection replaces insecure random.choice(). This is the same fix as FIX(#4643): replace predictable random.choice/uniform with secrets in select_proof() #4644, submitted by a different author.

  2. New test file validates the CSPRNG-based validator selection — ensuring the proof-of-antiquity system uses cryptographically secure randomness for fair validator election.

  3. Proof-of-antiquity validator selection affects consensus security — using CSPRNG ensures the selection process is unpredictable and resistant to manipulation.

LGTM.

Bounty: #2782
Disclosure: I received RTC compensation for this review.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

@Scottcjn

Copy link
Copy Markdown
Owner

Closing per codex audit — superseded by #4645, which is the cleaner implementation. Bounty credit goes to the winning PR.

Reason: Real secure-RNG fix, but #4645 is the cleaner duplicate on the same validator-selection bug.

@Scottcjn Scottcjn closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) size/M PR: 51-200 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SECURITY] Proof of Antiquity select_proof() uses predictable random.choice/uniform for weighted selection

8 participants