Skip to content

fix(consensus): guard against zero total stake in VRF validator selection - #1794

Closed
HusseinAdeiza wants to merge 1 commit into
genlayerlabs:v0.123-devfrom
HusseinAdeiza:fix/1732-vrf-zero-stake-clean
Closed

HusseinAdeiza wants to merge 1 commit into
genlayerlabs:v0.123-devfrom
HusseinAdeiza:fix/1732-vrf-zero-stake-clean

Conversation

@HusseinAdeiza

@HusseinAdeiza HusseinAdeiza commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #1732. get_validators_for_transaction divided each validator's stake by the total stake with no zero check. When all validators have stake=0 (e.g. the internally-created validators in protocol_rpc/endpoints.py), this raised ZeroDivisionError and crashed the consensus worker, halting transaction processing.

Change

Fall back to a uniform probability distribution when total_stake == 0; non-zero totals are unaffected. Adds a unit test (test_get_validators_for_transaction_all_zero_stake) covering the zero-stake path.

Testing

  • New unit test covers the zero-stake → uniform path.
  • Verified the function logic in isolation (the pre-existing vrf↔base circular import blocks importing vrf first, on pristine main too): zero-stake → [1/3,1/3,1/3], normal stakes → [1/6,2/6,3/6], mixed → stake weights.

Summary by CodeRabbit

  • Bug Fixes
    • Validator selection now assigns equal probabilities to all validators when their combined stake is zero. When the combined stake is greater than zero, selection continues to use stake-proportional probabilities. This ensures that validators with zero stake can still be selected fairly when no stake is available to guide the selection.

…tion

get_validators_for_transaction divided each validator's stake by the total
stake without checking for zero. When all validators have stake=0 (e.g.
internally-created validators in protocol_rpc/endpoints.py), this raised
ZeroDivisionError and crashed the consensus worker, halting transaction
processing.

Fall back to a uniform probability distribution when the total stake is
zero; non-zero totals are unaffected. Adds a unit test covering the
zero-stake path.

Closes genlayerlabs#1732

Signed-off-by: HusseinAdeiza <HusseinAdeiza@users.noreply.github.com>
@github-actions
github-actions Bot changed the base branch from main to v0.123-dev October 14, 2026 09:28
@github-actions

Copy link
Copy Markdown
Contributor

This PR targeted main, which is only the default/static branch.

I retargeted it to v0.123-dev, the active development branch. Pushes to v0.123-dev automatically fast-forward main.

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2e8732cc-bee7-4572-85ee-94cc50e047f7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@HusseinAdeiza

Copy link
Copy Markdown
Contributor Author

Closing in favor of the upstream fix that landed on v0.123-dev: get_validators_for_transaction now guards total_stake <= 0 (raises NoValidatorsAvailableError). Same root cause, maintainer-preferred fail-loud approach. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ZeroDivisionError in VRF validator selection when all stakes are zero

1 participant