Skip to content

fix: secure Proof of Antiquity validator selection - #4645

Closed
godd-ctrl wants to merge 2 commits into
Scottcjn:mainfrom
godd-ctrl:codex/fix-poa-validator-rng
Closed

godd-ctrl wants to merge 2 commits into
Scottcjn:mainfrom
godd-ctrl:codex/fix-poa-validator-rng

Conversation

@godd-ctrl

Copy link
Copy Markdown
Contributor

Summary

  • replace predictable random.choice/random.uniform validator selection with secrets.randbelow()
  • keep the zero-score fallback and weighted selection behavior deterministic under mocked secure randomness
  • add focused regression coverage for empty, zero-score, and weighted validator selection paths

Fixes #4643

Verification

  • python -m pytest tests\test_proof_of_antiquity_validator_selection.py -q
  • python -m py_compile rips\python\rustchain\proof_of_antiquity.py tests\test_proof_of_antiquity_validator_selection.py
  • git diff --check
  • python tools\bcos_spdx_check.py --base-ref origin/main

@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

@Asti1982 Asti1982 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 the version of the #4643 fix that keeps the module importable, actually removes the active random selection sites from select_block_validator(), and adds focused regression coverage for the zero-score and weighted-selection paths.

I verified locally:

python -m pytest tests\test_proof_of_antiquity_validator_selection.py -q
# 3 passed

python -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

python tools\bcos_spdx_check.py --base-ref origin/main
# BCOS SPDX check: OK

The regression tests monkeypatch poa.secrets.randbelow, which proves the production selection path now uses the cryptographic-randomness boundary rather than the previous random.choice / random.uniform calls. No blocking findings from this review.

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

Proof of Antiquity validator RNG review:

The patch correctly removes the direct random.choice() / random.uniform() dependency and the included tests cover normal zero-score and weighted paths. I found one remaining edge-case blocker in the weighted path:

precision = 1_000_000
r = secrets.randbelow(int(total_as * precision)) / float(precision)

If total_as is positive but smaller than 1 / precision, int(total_as * precision) becomes 0, and secrets.randbelow(0) raises ValueError. That turns tiny-but-positive validator score sets into selection failures even though the old random.uniform(0, total_as) handled them.

Local reproduction:

python - <<'PY'
import sys
from types import SimpleNamespace
sys.path.insert(0, r'rips/python')
from rustchain.proof_of_antiquity import select_block_validator
select_block_validator([SimpleNamespace(antiquity_score=1e-9)])
PY
# ValueError: Upper bound must be positive.

A robust fix would avoid deriving a possibly-zero integer bound from total_as, for example by using a fixed-width secure fraction (secrets.randbelow(2**53) / 2**53 * total_as) or otherwise guaranteeing the bound is at least one while preserving weighted-selection semantics. Please add a regression test for a tiny positive antiquity_score as well.

Other validation run locally:

python -m pytest tests\test_proof_of_antiquity_validator_selection.py -q
# 3 passed

python -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

python tools\bcos_spdx_check.py --base-ref origin/main
# BCOS SPDX check: OK

@godd-ctrl

Copy link
Copy Markdown
Contributor Author

Addressed the tiny-positive-score blocker from the latest review.

Change made:

  • Replaced the int(total_as * precision) random bound with a fixed-width secure unit interval:
    secrets.randbelow(2**53) / 2**53 * total_as
  • Added a regression test for a tiny positive antiquity_score (1e-9) so the selection path no longer calls secrets.randbelow(0).
  • Updated the weighted-selection test to assert the fixed secure bound.

Validation:

python -m pytest tests\test_proof_of_antiquity_validator_selection.py -q
....                                                                     [100%]
4 passed in 0.21s

python -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

python tools\bcos_spdx_check.py --base-ref origin/main
BCOS SPDX check: OK

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

Re-reviewed the updated fix after the tiny-positive-score issue was addressed. The switch to secrets.randbelow(253) / 253 keeps the weighted draw in [0, total_as), preserves the zero-score fallback via secrets, and the new regression covers the previous single tiny-score failure.\n\nLocal checks run:\n- python -m pytest tests\test_proof_of_antiquity_validator_selection.py -q\n- python -m py_compile 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\n- tiny-score smoke test for select_block_validator([1e-9])\n\nAll passed. Approved.

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

Reviewed the Proof of Antiquity validator-selection RNG fix. The patch replaces predictable Mersenne Twister selection with secrets.randbelow for both zero-score fallback and weighted selection, while preserving the existing cumulative weighted-selection behavior.

I specifically checked the tiny-positive-score follow-up: using a fixed 2**53 secure unit interval avoids randbelow(0), keeps r in [0, total_as), and the regression covers a 1e-9 score.

Validation:

  • uv run --no-project --with pytest python -m pytest tests/test_proof_of_antiquity_validator_selection.py -q -> initially failed because repo conftest imports Flask
  • uv run --no-project --with pytest --with flask python -m pytest tests/test_proof_of_antiquity_validator_selection.py -q -> 4 passed
  • python3 -m py_compile 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 -> BCOS SPDX check: OK
  • git diff --check origin/main...HEAD -- rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py -> passed

No blockers from my 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.

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.

Reviewed the Proof of Antiquity validator selection RNG hardening.

The patch removes random from validator selection, uses secrets.randbelow() for both the zero-score fallback and weighted selection, and handles tiny positive score totals without collapsing the selection bound to zero. The focused tests cover empty input, zero-score fallback, weighted selection, and tiny score handling.

Validation run:

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /tmp/rustchain-flask-venv/bin/python -m pytest -q tests/test_proof_of_antiquity_validator_selection.py -> 4 passed
  • /tmp/rustchain-flask-venv/bin/python -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

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

Verified review

Approved for PR #4645 at head 15a2bae6963c431810d03168e2985c4ba4f682f0.

I inspected the PR metadata/diff for: fix: secure Proof of Antiquity validator selection.
Changed scope: rips/python/rustchain/proof_of_antiquity.py (+5/-5), tests/test_proof_of_antiquity_validator_selection.py (+71/-0).

Local validation

  • git diff --check origin/main...HEAD — pass
  • python3 tools/bcos_spdx_check.py --base-ref origin/main — pass
  • python3 -B -m py_compile rips/python/rustchain/proof_of_antiquity.py tests/test_proof_of_antiquity_validator_selection.py — pass
  • python3 -B -m pytest tests/test_proof_of_antiquity_validator_selection.py -q — pass
  • added-line secret pattern scan — pass

Review reasoning

  • The diff is focused on the stated security/validation/bugfix scope and is small enough to review in this bounty tick.
  • Whitespace, SPDX, syntax/static, and added-line secret checks passed for this exact head.
  • I did not find a blocker in the changed files or validation output.

Boundary: this review certifies the current diff/head only; payout/merge is not assumed.

@Scottcjn

Copy link
Copy Markdown
Owner

Codex audit ✓ — this PR is approved for merge at medium-tier (25 RTC) (Cleaner of the PoA RNG duplicates: uses secrets.randbelow for both tie-breaking and weighted selection.).

Branch is currently behind main; rebase against latest tip and this'll merge.

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

@HCIE2054 HCIE2054 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.

Reviewed PR 4645. Standard review.

@HCIE2054 HCIE2054 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! Clean implementation. Thanks for your contribution to RustChain!

@HCIE2054 HCIE2054 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! Great contribution to the RustChain ecosystem. Thanks for keeping the codebase clean and well-tested. Approved ✅

@HCIE2054 HCIE2054 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!

@HCIE2054 HCIE2054 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!

@Scottcjn

Copy link
Copy Markdown
Owner

Closing as stale branch — would cause destructive deletions if merged.

Your branch is 966 commits behind current main. Filed during the May 11-13 contributor burst, the codebase has since moved substantially. GitHub's mergeable=clean indicator doesn't detect this; only an explicit git diff main..your-branch --shortstat does.

Bounty credit acknowledged where applicable

Most of the canonical fixes from your work-period have already shipped via other contributors' parallel PRs that landed earlier this week. Specific cases credited via the Codex audit batches:

If you want fresh review

Rebase against current main and verify your diff matches the size of your original changes:

git fetch upstream main
git rebase upstream/main
git diff main..HEAD --shortstat

If the deletion count is much higher than what you intended, the branch is still picking up stale assumptions — recreate from a fresh main.

Thanks for the contribution work.

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

9 participants