validator, cert: name the checked add the way that is not deprecated

luxfi/math v1.5.1 marks Add64 deprecated in favour of Add[uint64]. The two
are the same function by construction — safe.go declares the deprecated
name as `Add64 = Add[uint64]`, a var holding that exact instantiation — so
this moves no arithmetic: same overflow predicate, same (sum, error), same
refusal at the same boundary. It also drops a call through a package-level
func value, which nothing should be able to reassign.

Instantiated explicitly rather than left to inference. These three are the
overflow-refusal sites for stake: the two weight tallies Register and
FlattenValidatorSet run, and the cert tally votedStake runs. If a weight
field ever narrowed, `Add` would infer the narrower T and quietly start
refusing at a lower ceiling while the caller still believed it was guarding
a uint64; `Add[uint64]` fails at compile time instead. A tally that refuses
at the wrong ceiling is the fault these calls exist to catch.

Co-authored-by: Hanzo Dev <[email protected]>
This commit is contained in:
2026-09-02 03:46:08 -07:00
co-authored by Hanzo Dev
parent 292d7b8cd6
commit 18fe0f8f1c
2 changed files with 3 additions and 3 deletions
+1 -1
View File
@@ -754,7 +754,7 @@ func (c *QuorumCert) verifyQuasarSupermajority(stake StakeSource, epochHeight ui
func (c *QuorumCert) votedStake(stake StakeSource, epochHeight uint64) (uint64, error) {
var voted uint64
for i := range c.Votes {
sum, err := math.Add64(voted, stake.Weight(c.Votes[i].NodeID, epochHeight))
sum, err := math.Add[uint64](voted, stake.Weight(c.Votes[i].NodeID, epochHeight))
if err != nil {
return 0, fmt.Errorf("%w: cert tally at epoch %d: %w",
validators.ErrWeightOverflow, epochHeight, err)
+2 -2
View File
@@ -137,7 +137,7 @@ func Register(rs []Registration) (CanonicalValidatorSet, error) {
byNode[r.NodeID] = struct{}{}
// WEIGHT, last.
sum, err := math.Add64(totalWeight, r.Weight)
sum, err := math.Add[uint64](totalWeight, r.Weight)
if err != nil {
return CanonicalValidatorSet{}, fmt.Errorf("%w: %w", ErrWeightOverflow, err)
}
@@ -204,7 +204,7 @@ func FlattenValidatorSet(vdrSet map[ids.NodeID]*GetValidatorOutput) (CanonicalVa
if vdr == nil {
continue
}
totalWeight, err = math.Add64(totalWeight, vdr.Weight)
totalWeight, err = math.Add[uint64](totalWeight, vdr.Weight)
if err != nil {
return CanonicalValidatorSet{}, fmt.Errorf("%w: %w", ErrWeightOverflow, err)
}