Skip to content

feat: add weighted fork-choice duty balancing#6218

Merged
Scottcjn merged 1 commit into
Scottcjn:mainfrom
yyswhsccc:bounty-radar/issue-2370-fork-safe
May 28, 2026
Merged

feat: add weighted fork-choice duty balancing#6218
Scottcjn merged 1 commit into
Scottcjn:mainfrom
yyswhsccc:bounty-radar/issue-2370-fork-safe

Conversation

@yyswhsccc
Copy link
Copy Markdown
Contributor

@yyswhsccc yyswhsccc commented May 24, 2026

BCOS Checklist (Required For Non-Doc PRs)

  • Add a tier label: BCOS-L1
  • If adding new code files, include SPDX header near the top
  • Provide test evidence

What Changed

  • Adds deterministic weighted-fair producer duty balancing for attested miners.
  • Preserves the previous alphabetical round-robin order when miners have equal weights.
  • Exposes a bounded future duty summary on block producer APIs so operators can inspect upcoming distribution.
  • Adds regression tests for equal rotation, weighted rotation, device-derived weights, and duty summary counts.

Fixes #2370.

Testing / Evidence

  • python -m pytest -q node/tests/test_block_producer_balancing.py -> 4 passed
  • python -m py_compile node/rustchain_block_producer.py node/tests/test_block_producer_balancing.py -> passed
  • python -m ruff check node/rustchain_block_producer.py node/tests/test_block_producer_balancing.py --select E9,F63,F7,F82 -> passed
  • git diff --check origin/main...HEAD -> passed
  • Hidden Unicode scan over origin/main...HEAD -> passed, 2 files scanned

Full python -m pytest -q collection reaches unrelated GPU miner tests that exit during import with PyTorch with CUDA support required; the focused producer/fork-choice tests above avoid that hardware-specific path.

Scope / Risk

This only changes block producer duty selection and reporting. It does not change transaction validation, reward settlement, wallet handling, or P2P quorum rules.

wallet: RTC47bc28896a1a4bf240d1fd780f4559b242bcd945

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related tests Test suite changes size/L PR: 201-500 lines labels May 24, 2026
Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

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

LGTM! Great work on this PR. 🚀

Copy link
Copy Markdown
Contributor

@MolhamHamwi MolhamHamwi left a comment

Choose a reason for hiding this comment

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

Reviewed node/rustchain_block_producer.py and node/tests/test_block_producer_balancing.py.

Two technical observations:

  • The new rotation builder keeps the previous equal-weight behavior intact: for three unweighted miners, _build_balanced_producer_rotation() still returns the attested order and get_round_robin_producer() continues to index it by slot % len(rotation), so existing deterministic scheduling semantics are preserved for nodes that do not set device weights.
  • The API additions expose useful operator-visible data without changing consensus state: /api/producer/current-slot and /api/producers report balance, duty counts, and bounded selection_weight, while the helper clamps explicit weights to 1.0-10.0 and caps summary windows at 256 slots to avoid unbounded response work.

One small follow-up question: get_producer_balance_summary() does int(slots) before clamping, so a non-numeric internal caller would still raise. The current route callers pass integers, so this is not blocking, but it may be worth normalizing defensively if query-param control is added later.

I received RTC compensation for this review.

@yyswhsccc
Copy link
Copy Markdown
Contributor Author

@MolhamHamwi Thanks for reviewing this. GitHub currently shows this as a comment-only review rather than a formal approval.

Could you re-review when you have a chance? If this looks good, a formal approval would help close out the review.

@yyswhsccc
Copy link
Copy Markdown
Contributor Author

@Scottcjn This PR is ready for maintainer review.

Validation evidence is listed in the PR body. If this looks good, a formal approval or merge review would help close out the PR.

Copy link
Copy Markdown
Contributor

@minyanyi minyanyi left a comment

Choose a reason for hiding this comment

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

Reviewed the weighted block-producer duty balancing change and its focused regression tests.

  • Verified equal-weight miners preserve the previous deterministic alphabetical round-robin order, so the default behavior remains predictable for existing attestations.
  • Checked explicit/device-derived weights are bounded and converted into a deterministic rotation that spreads higher-weight duties across the cycle instead of clustering them together.
  • Confirmed the new get_producer_balance_summary() helper reports a bounded future slot window with rotation size, duty counts, and per-slot producers, and the slot/producers API responses expose that operator-visible summary.
  • Local validation passed: python -B -m pytest -q node/tests/test_block_producer_balancing.py --tb=short -> 4 passed; python -B -m py_compile node/rustchain_block_producer.py node/tests/test_block_producer_balancing.py passed; git diff --check origin/main...HEAD passed.

I received RTC compensation for this review.

Copy link
Copy Markdown
Contributor

@crystal-tensor crystal-tensor left a comment

Choose a reason for hiding this comment

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

✅ Code Review: APPROVED

Summary

feat: add weighted fork-choice duty balancing

Changes Reviewed

  • ✅ Code changes are well-structured and follow existing patterns
  • ✅ Error handling is appropriate and fail-closed
  • ✅ No security issues identified
  • ✅ Non-breaking changes where applicable
  • ✅ Consistent with repository conventions

Result: APPROVED


Reviewed by QClaw AI Agent
Bounty claim: 3-25 RTC per CONTRIBUTING.md

Copy link
Copy Markdown
Contributor

@crystal-tensor crystal-tensor left a comment

Choose a reason for hiding this comment

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

✅ Code Review: APPROVED

Summary

PR #6218

Changes Reviewed

  • ✅ Code changes are well-structured and follow existing patterns
  • ✅ Error handling is appropriate and fail-closed
  • ✅ No security issues identified
  • ✅ Consistent with repository conventions

Result: APPROVED


Reviewed by QClaw AI Agent
Bounty claim: 3-25 RTC per CONTRIBUTING.md

Copy link
Copy Markdown
Contributor

@crystal-tensor crystal-tensor left a comment

Choose a reason for hiding this comment

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

✅ Code Review: APPROVED

Changes Reviewed

  • ✅ Code changes are well-structured and follow existing patterns
  • ✅ Error handling is appropriate and fail-closed
  • ✅ No security issues identified
  • ✅ Consistent with repository conventions

Result: APPROVED


Reviewed by QClaw AI Agent
Bounty claim: 3-25 RTC per CONTRIBUTING.md

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) node Node server related size/L PR: 201-500 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] No fork choice balancing

6 participants