Skip to content

Commit

Permalink
fix(x/distribution): add nil check in Params.ValidateBasic (#17236)
Browse files Browse the repository at this point in the history
  • Loading branch information
n0b0dyCN authored Aug 1, 2023
1 parent c33b4db commit b9b325e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (baseapp) [#17159](https://github.com/cosmos/cosmos-sdk/pull/17159) Validators can propose blocks that exceed the gas limit.
* (x/group) [#17146](https://github.com/cosmos/cosmos-sdk/pull/17146) Rename x/group legacy ORM package's error codespace from "orm" to "legacy_orm", preventing collisions with ORM's error codespace "orm".
* (x/bank) [#17170](https://github.com/cosmos/cosmos-sdk/pull/17170) Avoid empty spendable error message on send coins.
* (x/distribution) [#17236](https://github.com/cosmos/cosmos-sdk/pull/17236) Using "validateCommunityTax" in "Params.ValidateBasic", preventing panic when field "CommunityTax" is nil.

### API Breaking Changes

Expand Down
18 changes: 16 additions & 2 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,20 @@ func TestMsgUpdateParams(t *testing.T) {
expErr: true,
expErrMsg: "invalid authority",
},
{
name: "community tax is nil",
msg: &distrtypes.MsgUpdateParams{
Authority: f.distrKeeper.GetAuthority(),
Params: distrtypes.Params{
CommunityTax: math.LegacyDec{},
WithdrawAddrEnabled: withdrawAddrEnabled,
BaseProposerReward: math.LegacyZeroDec(),
BonusProposerReward: math.LegacyZeroDec(),
},
},
expErr: true,
expErrMsg: "community tax must be not nil",
},
{
name: "community tax > 1",
msg: &distrtypes.MsgUpdateParams{
Expand All @@ -696,7 +710,7 @@ func TestMsgUpdateParams(t *testing.T) {
},
},
expErr: true,
expErrMsg: "community tax should be non-negative and less than one",
expErrMsg: "community tax too large: 2.000000000000000000",
},
{
name: "negative community tax",
Expand All @@ -710,7 +724,7 @@ func TestMsgUpdateParams(t *testing.T) {
},
},
expErr: true,
expErrMsg: "community tax should be non-negative and less than one",
expErrMsg: "community tax must be positive: -0.200000000000000000",
},
{
name: "base proposer reward set",
Expand Down
8 changes: 1 addition & 7 deletions x/distribution/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ func DefaultParams() Params {

// ValidateBasic performs basic validation on distribution parameters.
func (p Params) ValidateBasic() error {
if p.CommunityTax.IsNegative() || p.CommunityTax.GT(math.LegacyOneDec()) {
return fmt.Errorf(
"community tax should be non-negative and less than one: %s", p.CommunityTax,
)
}

return nil
return validateCommunityTax(p.CommunityTax)
}

func validateCommunityTax(i interface{}) error {
Expand Down
1 change: 1 addition & 0 deletions x/distribution/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestParams_ValidateBasic(t *testing.T) {
{"negative bonus proposer reward (must not matter)", fields{toDec("0.1"), toDec("0"), toDec("-0.1"), false}, false},
{"total sum greater than 1 (must not matter)", fields{toDec("0.2"), toDec("0.5"), toDec("0.4"), false}, false},
{"community tax greater than 1", fields{toDec("1.1"), toDec("0"), toDec("0"), false}, true},
{"community tax nil", fields{sdkmath.LegacyDec{}, toDec("0"), toDec("0"), false}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit b9b325e

Please sign in to comment.