Skip to content

Commit

Permalink
Merge PR: cosmos#3918 Allow Unrated Decreases to Validator Commission…
Browse files Browse the repository at this point in the history
… Rates

* Allow arbitrary decreases to validator commission rates

* Implement TestCommissionValidate

* Implement TestCommissionValidateNewRate

* Add comments to unit tests
  • Loading branch information
alexanderbez authored and rigelrozanski committed Mar 20, 2019
1 parent b9837e3 commit 386116f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#3917 Allow arbitrary decreases to validator commission rates.
3 changes: 1 addition & 2 deletions x/staking/types/commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func (c Commission) ValidateNewRate(newRate sdk.Dec, blockTime time.Time) sdk.Er
// new rate cannot be greater than the max rate
return ErrCommissionGTMaxRate(DefaultCodespace)

// TODO: why do we need an absolute value, do we care if the validator decreases their rate rapidly?
case newRate.Sub(c.Rate).Abs().GT(c.MaxChangeRate):
case newRate.Sub(c.Rate).GT(c.MaxChangeRate):
// new rate % points change cannot be greater than the max change rate
return ErrCommissionGTMaxChangeRate(DefaultCodespace)
}
Expand Down
72 changes: 72 additions & 0 deletions x/staking/types/commission_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package types

import (
"testing"
"time"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestCommissionValidate(t *testing.T) {
testCases := []struct {
input Commission
expectErr bool
}{
// invalid commission; max rate < 0%
{NewCommission(sdk.ZeroDec(), sdk.MustNewDecFromStr("-1.00"), sdk.ZeroDec()), true},
// invalid commission; max rate > 100%
{NewCommission(sdk.ZeroDec(), sdk.MustNewDecFromStr("2.00"), sdk.ZeroDec()), true},
// invalid commission; rate < 0%
{NewCommission(sdk.MustNewDecFromStr("-1.00"), sdk.ZeroDec(), sdk.ZeroDec()), true},
// invalid commission; rate > max rate
{NewCommission(sdk.MustNewDecFromStr("0.75"), sdk.MustNewDecFromStr("0.50"), sdk.ZeroDec()), true},
// invalid commission; max change rate < 0%
{NewCommission(sdk.OneDec(), sdk.OneDec(), sdk.MustNewDecFromStr("-1.00")), true},
// invalid commission; max change rate > max rate
{NewCommission(sdk.OneDec(), sdk.MustNewDecFromStr("0.75"), sdk.MustNewDecFromStr("0.90")), true},
// valid commission
{NewCommission(sdk.MustNewDecFromStr("0.20"), sdk.OneDec(), sdk.MustNewDecFromStr("0.10")), false},
}

for i, tc := range testCases {
err := tc.input.Validate()
require.Equal(t, tc.expectErr, err != nil, "unexpected result; tc #%d, input: %v", i, tc.input)
}
}

func TestCommissionValidateNewRate(t *testing.T) {
now := time.Now().UTC()
c1 := NewCommission(sdk.MustNewDecFromStr("0.40"), sdk.MustNewDecFromStr("0.80"), sdk.MustNewDecFromStr("0.10"))
c1.UpdateTime = now

testCases := []struct {
input Commission
newRate sdk.Dec
blockTime time.Time
expectErr bool
}{
// invalid new commission rate; last update < 24h ago
{c1, sdk.MustNewDecFromStr("0.50"), now, true},
// invalid new commission rate; new rate < 0%
{c1, sdk.MustNewDecFromStr("-1.00"), now.Add(48 * time.Hour), true},
// invalid new commission rate; new rate > max rate
{c1, sdk.MustNewDecFromStr("0.90"), now.Add(48 * time.Hour), true},
// invalid new commission rate; new rate > max change rate
{c1, sdk.MustNewDecFromStr("0.60"), now.Add(48 * time.Hour), true},
// valid commission
{c1, sdk.MustNewDecFromStr("0.50"), now.Add(48 * time.Hour), false},
// valid commission
{c1, sdk.MustNewDecFromStr("0.10"), now.Add(48 * time.Hour), false},
}

for i, tc := range testCases {
err := tc.input.ValidateNewRate(tc.newRate, tc.blockTime)
require.Equal(
t, tc.expectErr, err != nil,
"unexpected result; tc #%d, input: %v, newRate: %s, blockTime: %s",
i, tc.input, tc.newRate, tc.blockTime,
)
}
}

0 comments on commit 386116f

Please sign in to comment.