Skip to content

Commit

Permalink
staking/api/commission: fix possible panic in validation check
Browse files Browse the repository at this point in the history
The validation check would panic whenever the number of bound steps was
greater than `rate_steps + 2`.
  • Loading branch information
ptrus committed Mar 13, 2020
1 parent fe064e6 commit 9c5bbb7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .changelog/2763.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
staking/api/commission: fix possible panic in validation check

The validation check would panic whenever the number of bound steps was
greater than `rate_steps + 2`.
4 changes: 2 additions & 2 deletions go/staking/api/commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (cs *CommissionSchedule) validateNondegenerate(rules *CommissionScheduleRul
if step.Start%rules.RateChangeInterval != 0 {
return fmt.Errorf("bound step %d start epoch %d not aligned with commission rate change interval %d", i, step.Start, rules.RateChangeInterval)
}
if i > 0 && step.Start <= cs.Rates[i-1].Start {
return fmt.Errorf("bound step %d start epoch %d not after previous step start epoch %d", i, step.Start, cs.Rates[i-1].Start)
if i > 0 && step.Start <= cs.Bounds[i-1].Start {
return fmt.Errorf("bound step %d start epoch %d not after previous step start epoch %d", i, step.Start, cs.Bounds[i-1].Start)
}
if step.RateMin.Cmp(CommissionRateDenominator) > 0 {
return fmt.Errorf("bound step %d minimum rate %v/%v over unity", i, step.RateMin, CommissionRateDenominator)
Expand Down
52 changes: 52 additions & 0 deletions go/staking/api/commission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,58 @@ func TestCommissionSchedule(t *testing.T) {
}
require.NoError(t, cs.PruneAndValidateForGenesis(&rules, 0), "simultaneous rate and bound change")

cs = CommissionSchedule{
Rates: []CommissionRateStep{
{
Start: 0,
Rate: mustInitQuantity(t, 50_000),
},
},
Bounds: []CommissionRateBoundStep{
{
Start: 0,
RateMin: mustInitQuantity(t, 0),
RateMax: mustInitQuantity(t, 100_000),
},
{
Start: 10,
RateMin: mustInitQuantity(t, 0),
RateMax: mustInitQuantity(t, 90_000),
},
{
Start: 20,
RateMin: mustInitQuantity(t, 0),
RateMax: mustInitQuantity(t, 80_000),
},
},
}
require.NoError(t, cs.PruneAndValidateForGenesis(&rules, 0), "valid where len(rates) > len(bounds)")

cs = CommissionSchedule{
Rates: []CommissionRateStep{
{
Start: 0,
Rate: mustInitQuantity(t, 50_000),
},
{
Start: 10,
Rate: mustInitQuantity(t, 60_000),
},
{
Start: 20,
Rate: mustInitQuantity(t, 70_000),
},
},
Bounds: []CommissionRateBoundStep{
{
Start: 0,
RateMin: mustInitQuantity(t, 0),
RateMax: mustInitQuantity(t, 100_000),
},
},
}
require.NoError(t, cs.PruneAndValidateForGenesis(&rules, 0), "valid where len(bounds) > len(rates)")

cs = CommissionSchedule{
Rates: []CommissionRateStep{
{
Expand Down

0 comments on commit 9c5bbb7

Please sign in to comment.