Skip to content

Commit

Permalink
x/distribution: fix module's parameters validation
Browse files Browse the repository at this point in the history
closes: #8914
  • Loading branch information
Alessio Treglia committed Mar 17, 2021
1 parent 0c2d4a8 commit af872fe
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
8 changes: 4 additions & 4 deletions x/distribution/types/fee_pool_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package types
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

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

func TestValidateGenesis(t *testing.T) {

fp := InitialFeePool()
fp := types.InitialFeePool()
require.Nil(t, fp.ValidateGenesis())

fp2 := FeePool{CommunityPool: sdk.DecCoins{{Denom: "stake", Amount: sdk.NewDec(-1)}}}
fp2 := types.FeePool{CommunityPool: sdk.DecCoins{{Denom: "stake", Amount: sdk.NewDec(-1)}}}
require.NotNil(t, fp2.ValidateGenesis())

}
6 changes: 3 additions & 3 deletions x/distribution/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
func (p Params) ValidateBasic() error {
if p.CommunityTax.IsNegative() || p.CommunityTax.GT(sdk.OneDec()) {
return fmt.Errorf(
"community tax should non-negative and less than one: %s", p.CommunityTax,
"community tax should be non-negative and less than one: %s", p.CommunityTax,
)
}
if p.BaseProposerReward.IsNegative() {
Expand All @@ -64,9 +64,9 @@ func (p Params) ValidateBasic() error {
"bonus proposer reward should be positive: %s", p.BonusProposerReward,
)
}
if v := p.BaseProposerReward.Add(p.BonusProposerReward); v.GT(sdk.OneDec()) {
if v := p.BaseProposerReward.Add(p.BonusProposerReward).Add(p.CommunityTax); v.GT(sdk.OneDec()) {
return fmt.Errorf(
"sum of base and bonus proposer reward cannot greater than one: %s", v,
"sum of base and bonus proposer rewards, and community tax cannot be greater than one: %s", v,
)
}

Expand Down
34 changes: 34 additions & 0 deletions x/distribution/types/params_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"

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

func Test_validateAuxFuncs(t *testing.T) {
type args struct {
i interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{"wrong type", args{10.5}, true},
{"nil Int pointer", args{sdk.Dec{}}, true},
{"negative", args{sdk.NewDec(-1)}, true},
{"one dec", args{sdk.NewDec(1)}, false},
{"two dec", args{sdk.NewDec(2)}, true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantErr, validateCommunityTax(tt.args.i) != nil)
require.Equal(t, tt.wantErr, validateBaseProposerReward(tt.args.i) != nil)
require.Equal(t, tt.wantErr, validateBonusProposerReward(tt.args.i) != nil)
})
}
}
43 changes: 29 additions & 14 deletions x/distribution/types/params_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package types
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

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

func Test_validateAuxFuncs(t *testing.T) {
type args struct {
i interface{}
func TestParams_ValidateBasic(t *testing.T) {
toDec := sdk.MustNewDecFromStr

type fields struct {
CommunityTax sdk.Dec
BaseProposerReward sdk.Dec
BonusProposerReward sdk.Dec
WithdrawAddrEnabled bool
}
tests := []struct {
name string
args args
fields fields
wantErr bool
}{
{"wrong type", args{10.5}, true},
{"nil Int pointer", args{sdk.Dec{}}, true},
{"negative", args{sdk.NewDec(-1)}, true},
{"one dec", args{sdk.NewDec(1)}, false},
{"two dec", args{sdk.NewDec(2)}, true},
{"success", fields{toDec("0.1"), toDec("0.5"), toDec("0.4"), false}, false},
{"negative community tax", fields{toDec("-0.1"), toDec("0.5"), toDec("0.4"), false}, true},
{"negative base proposer reward", fields{toDec("0.1"), toDec("-0.5"), toDec("0.4"), false}, true},
{"negative bonus proposer reward", fields{toDec("0.1"), toDec("0.5"), toDec("-0.4"), false}, true},
{"total sum greater than 1", fields{toDec("0.2"), toDec("0.5"), toDec("0.4"), false}, true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantErr, validateCommunityTax(tt.args.i) != nil)
require.Equal(t, tt.wantErr, validateBaseProposerReward(tt.args.i) != nil)
require.Equal(t, tt.wantErr, validateBonusProposerReward(tt.args.i) != nil)
p := types.Params{
CommunityTax: tt.fields.CommunityTax,
BaseProposerReward: tt.fields.BaseProposerReward,
BonusProposerReward: tt.fields.BonusProposerReward,
WithdrawAddrEnabled: tt.fields.WithdrawAddrEnabled,
}
if err := p.ValidateBasic(); (err != nil) != tt.wantErr {
t.Errorf("ValidateBasic() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestDefaultParams(t *testing.T) {
require.NoError(t, types.DefaultParams().ValidateBasic())
}

0 comments on commit af872fe

Please sign in to comment.