fix(fsm): guard against division by zero in PollsToResults#454
Open
amathxbt wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
In
fsm/gov.go,PollsToResults()computes governance poll vote percentages by dividing byTotalTokenswithout checking whether it is zero:Validators.TotalTokensis set frommembers.TotalPowerandAccounts.TotalTokensis set fromsupply.Total - supply.Staked - dao.Amount. Both can legitimately be0(e.g. an empty/zero-power validator committee, or a chain where the entire circulating supply is staked/DAO-held soAccounts.TotalTokensnets to0).When
TotalTokens == 0,float64(x) / float64(0)produces+Inf(whenx > 0) orNaN(whenx == 0). Converting either touint64is undefined/implementation-defined behavior in Go and produces garbage values (observed as0or platform-dependent large numbers), silently corrupting the reported approve/reject/voted percentages returned by this RPC-facing governance function.Fix
Guard each percentage block with a
TotalTokens != 0check, leaving the percentages at their zero-value default (0) when there is no token base to compute a percentage against. This is a minimal, behavior-preserving change for the non-zero case and eliminates the undefined-behavior float-to-uint64 conversion for the zero case.Testing
Reviewed the full
PollsToResultsfunction end-to-end against the currentmainbranch; the fix only affects the zero-denominator edge case and does not alter the non-zero-denominator control flow or existing percentage math.