Skip to content

Commit f88ce5d

Browse files
committed
go/staking/api: Sanity check staking reward threshold
1 parent d3a20f5 commit f88ce5d

4 files changed

Lines changed: 24 additions & 14 deletions

File tree

.changelog/6541.trivial.md

Whitespace-only changes.

go/consensus/cometbft/apps/staking/state/state.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,16 @@ func (es *EpochSigning) Update(signingEntities []signature.PublicKey) error {
555555

556556
func (es *EpochSigning) EligibleEntities(thresholdNumerator, thresholdDenominator uint64) ([]signature.PublicKey, error) {
557557
var eligibleEntities []signature.PublicKey
558-
if es.Total > math.MaxUint64/thresholdNumerator {
558+
if thresholdNumerator != 0 && es.Total > math.MaxUint64/thresholdNumerator {
559559
return nil, fmt.Errorf("overflow in total blocks, total=%d", es.Total)
560560
}
561561
thresholdPremultiplied := es.Total * thresholdNumerator
562562
for entityID, count := range es.ByEntity {
563-
if count > math.MaxUint64/thresholdDenominator {
563+
if thresholdDenominator != 0 && count > math.MaxUint64/thresholdDenominator {
564564
return nil, fmt.Errorf("entity %s: overflow in threshold comparison, count=%d", entityID, count)
565565
}
566-
if count*thresholdDenominator < thresholdPremultiplied {
566+
entityThreshold := count * thresholdDenominator
567+
if entityThreshold < thresholdPremultiplied {
567568
continue
568569
}
569570
eligibleEntities = append(eligibleEntities, entityID)

go/staking/api/api.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,17 +1235,21 @@ type ConsensusParameters struct {
12351235
// 1 token = 10**TokenValueExponent base units.
12361236
TokenValueExponent uint8 `json:"token_value_exponent,omitempty"`
12371237

1238-
Thresholds map[ThresholdKind]quantity.Quantity `json:"thresholds,omitempty"`
1239-
DebondingInterval beacon.EpochTime `json:"debonding_interval,omitempty"`
1240-
RewardSchedule []RewardStep `json:"reward_schedule,omitempty"`
1241-
SigningRewardThresholdNumerator uint64 `json:"signing_reward_threshold_numerator,omitempty"`
1242-
SigningRewardThresholdDenominator uint64 `json:"signing_reward_threshold_denominator,omitempty"`
1243-
CommissionScheduleRules CommissionScheduleRules `json:"commission_schedule_rules,omitempty"`
1244-
Slashing map[SlashReason]Slash `json:"slashing,omitempty"`
1245-
GasCosts transaction.Costs `json:"gas_costs,omitempty"`
1246-
MinDelegationAmount quantity.Quantity `json:"min_delegation"`
1247-
MinTransferAmount quantity.Quantity `json:"min_transfer"`
1248-
MinTransactBalance quantity.Quantity `json:"min_transact_balance"`
1238+
Thresholds map[ThresholdKind]quantity.Quantity `json:"thresholds,omitempty"`
1239+
DebondingInterval beacon.EpochTime `json:"debonding_interval,omitempty"`
1240+
RewardSchedule []RewardStep `json:"reward_schedule,omitempty"`
1241+
// SigningRewardThresholdNumerator is the numerator of the threshold used for
1242+
// block signing rewards.
1243+
SigningRewardThresholdNumerator uint64 `json:"signing_reward_threshold_numerator,omitempty"`
1244+
// SigningRewardThresholdDenominator is the denominator of the threshold used for
1245+
// block signing rewards. If the denominator is zero, the threshold is treated as zero.
1246+
SigningRewardThresholdDenominator uint64 `json:"signing_reward_threshold_denominator,omitempty"`
1247+
CommissionScheduleRules CommissionScheduleRules `json:"commission_schedule_rules,omitempty"`
1248+
Slashing map[SlashReason]Slash `json:"slashing,omitempty"`
1249+
GasCosts transaction.Costs `json:"gas_costs,omitempty"`
1250+
MinDelegationAmount quantity.Quantity `json:"min_delegation"`
1251+
MinTransferAmount quantity.Quantity `json:"min_transfer"`
1252+
MinTransactBalance quantity.Quantity `json:"min_transact_balance"`
12491253

12501254
DisableTransfers bool `json:"disable_transfers,omitempty"`
12511255
DisableDelegation bool `json:"disable_delegation,omitempty"`

go/staking/api/sanity_check.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ func (p *ConsensusParameters) SanityCheck() error {
6464
prevUntil = step.Until
6565
}
6666

67+
// Signing reward threshold must be a valid fraction between 0 and 1.
68+
if p.SigningRewardThresholdNumerator > p.SigningRewardThresholdDenominator {
69+
return fmt.Errorf("signing reward threshold numerator must be less than or equal to denominator")
70+
}
71+
6772
return nil
6873
}
6974

0 commit comments

Comments
 (0)