From d43e26e174ba0c1c9e08cf6695b9f71832d63fef Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 26 Sep 2022 10:05:42 -0400 Subject: [PATCH 01/16] update params --- x/mint/types/params.go | 113 ++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 76 deletions(-) diff --git a/x/mint/types/params.go b/x/mint/types/params.go index 5f1ba12..a78eb12 100644 --- a/x/mint/types/params.go +++ b/x/mint/types/params.go @@ -21,6 +21,19 @@ var ( KeyBlocksPerYear = []byte("BlocksPerYear") KeyDistributionProportions = []byte("DistributionProportions") KeyFundedAddresses = []byte("FundedAddresses") + + DefaultMintDenom = sdk.DefaultBondDenom + DefaultInflationRateChange = sdk.NewDecWithPrec(13, 2) + DefaultInflationMax = sdk.NewDecWithPrec(20, 2) + DefaultInflationMin = sdk.NewDecWithPrec(7, 2) + DefaultGoalBonded = sdk.NewDecWithPrec(67, 2) + DefaultBlocksPerYear = uint64(60 * 60 * 8766 / 5) // assuming 5 seconds block times + DefaultDistributionProportions = DistributionProportions{ + Staking: sdk.NewDecWithPrec(3, 1), // 0.3 + FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 + CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 + } + DefaultFundedAddresses []WeightedAddress ) // ParamTable for minting module. @@ -50,39 +63,35 @@ func NewParams( } } -// default minting module parameters +// DefaultParams returns default minting module parameters func DefaultParams() Params { - return Params{ - MintDenom: sdk.DefaultBondDenom, - InflationRateChange: sdk.NewDecWithPrec(13, 2), - InflationMax: sdk.NewDecWithPrec(20, 2), - InflationMin: sdk.NewDecWithPrec(7, 2), - GoalBonded: sdk.NewDecWithPrec(67, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5 seconds block times - DistributionProportions: DistributionProportions{ - Staking: sdk.NewDecWithPrec(3, 1), // 0.3 - FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 - CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 - }, - FundedAddresses: []WeightedAddress{}, - } + return NewParams( + DefaultMintDenom, + DefaultInflationRateChange, + DefaultInflationMax, + DefaultInflationMin, + DefaultGoalBonded, + DefaultBlocksPerYear, + DefaultDistributionProportions, + DefaultFundedAddresses, + ) } -// validate params +// Validate validates all params func (p Params) Validate() error { if err := validateMintDenom(p.MintDenom); err != nil { return err } - if err := validateInflationRateChange(p.InflationRateChange); err != nil { + if err := validateDec(p.InflationRateChange); err != nil { return err } - if err := validateInflationMax(p.InflationMax); err != nil { + if err := validateDec(p.InflationMax); err != nil { return err } - if err := validateInflationMin(p.InflationMin); err != nil { + if err := validateDec(p.InflationMin); err != nil { return err } - if err := validateGoalBonded(p.GoalBonded); err != nil { + if err := validateDec(p.GoalBonded); err != nil { return err } if err := validateBlocksPerYear(p.BlocksPerYear); err != nil { @@ -106,14 +115,14 @@ func (p Params) String() string { return string(out) } -// Implements params.ParamSet +// ParamSetPairs implements params.ParamSet func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { return paramtypes.ParamSetPairs{ paramtypes.NewParamSetPair(KeyMintDenom, &p.MintDenom, validateMintDenom), - paramtypes.NewParamSetPair(KeyInflationRateChange, &p.InflationRateChange, validateInflationRateChange), - paramtypes.NewParamSetPair(KeyInflationMax, &p.InflationMax, validateInflationMax), - paramtypes.NewParamSetPair(KeyInflationMin, &p.InflationMin, validateInflationMin), - paramtypes.NewParamSetPair(KeyGoalBonded, &p.GoalBonded, validateGoalBonded), + paramtypes.NewParamSetPair(KeyInflationRateChange, &p.InflationRateChange, validateDec), + paramtypes.NewParamSetPair(KeyInflationMax, &p.InflationMax, validateDec), + paramtypes.NewParamSetPair(KeyInflationMin, &p.InflationMin, validateDec), + paramtypes.NewParamSetPair(KeyGoalBonded, &p.GoalBonded, validateDec), paramtypes.NewParamSetPair(KeyBlocksPerYear, &p.BlocksPerYear, validateBlocksPerYear), paramtypes.NewParamSetPair(KeyDistributionProportions, &p.DistributionProportions, validateDistributionProportions), paramtypes.NewParamSetPair(KeyFundedAddresses, &p.FundedAddresses, validateWeightedAddresses), @@ -136,65 +145,17 @@ func validateMintDenom(i interface{}) error { return nil } -func validateInflationRateChange(i interface{}) error { - v, ok := i.(sdk.Dec) - if !ok { - return fmt.Errorf("invalid parameter type: %T", i) - } - - if v.IsNegative() { - return fmt.Errorf("inflation rate change cannot be negative: %s", v) - } - if v.GT(sdk.OneDec()) { - return fmt.Errorf("inflation rate change too large: %s", v) - } - - return nil -} - -func validateInflationMax(i interface{}) error { - v, ok := i.(sdk.Dec) - if !ok { - return fmt.Errorf("invalid parameter type: %T", i) - } - - if v.IsNegative() { - return fmt.Errorf("max inflation cannot be negative: %s", v) - } - if v.GT(sdk.OneDec()) { - return fmt.Errorf("max inflation too large: %s", v) - } - - return nil -} - -func validateInflationMin(i interface{}) error { - v, ok := i.(sdk.Dec) - if !ok { - return fmt.Errorf("invalid parameter type: %T", i) - } - - if v.IsNegative() { - return fmt.Errorf("min inflation cannot be negative: %s", v) - } - if v.GT(sdk.OneDec()) { - return fmt.Errorf("min inflation too large: %s", v) - } - - return nil -} - -func validateGoalBonded(i interface{}) error { +func validateDec(i interface{}) error { v, ok := i.(sdk.Dec) if !ok { return fmt.Errorf("invalid parameter type: %T", i) } if v.IsNegative() { - return fmt.Errorf("goal bonded cannot be negative: %s", v) + return fmt.Errorf("cannot be negative: %s", v) } if v.GT(sdk.OneDec()) { - return fmt.Errorf("goal bonded too large: %s", v) + return fmt.Errorf("dec too large: %s", v) } return nil From 9a5b121fdaed37236f20c3a21190da6e27a8eb77 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 26 Sep 2022 10:05:47 -0400 Subject: [PATCH 02/16] init test --- x/mint/types/params_test.go | 187 ++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 x/mint/types/params_test.go diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go new file mode 100644 index 0000000..9953d2b --- /dev/null +++ b/x/mint/types/params_test.go @@ -0,0 +1,187 @@ +package types + +import ( + "errors" + "github.com/stretchr/testify/require" + "testing" +) + +func TestParamsValidate(t *testing.T) { + invalid := DefaultParams() + // set inflation min to larger than inflation max + invalid.InflationMin = invalid.InflationMax.Add(invalid.InflationMax) + + tests := []struct { + name string + params Params + err error + }{ + { + name: "should prevent validate params with inflation min larger than inflation max", + params: invalid, + err: errors.New("max inflation (0.200000000000000000) must be greater than or equal " + + "to min inflation (0.400000000000000000)"), + }, + { + name: "should validate valid params", + params: DefaultParams(), + err: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.params.Validate() + if tt.err != nil { + require.Error(t, err, tt.err) + require.Equal(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +} + +func TestValidateMintDenom(t *testing.T) { + tests := []struct { + name string + denom interface{} + err error + }{ + { + name: "should prevent validate mint denom with invalid interface", + denom: 10, + err: errors.New("invalid parameter type: int"), + }, + { + name: "should validate valid mint denom", + denom: DefaultMintDenom, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateMintDenom(tt.denom) + if tt.err != nil { + require.Error(t, err, tt.err) + require.Equal(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +} + +func TestValidateDec(t *testing.T) { + tests := []struct { + name string + value interface{} + err error + }{ + { + name: "should prevent validate dec with invalid interface", + value: "string", + err: errors.New("invalid parameter type: string"), + }, + { + name: "should validate valid dec", + value: DefaultInflationRateChange, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateDec(tt.value) + if tt.err != nil { + require.Error(t, err, tt.err) + require.Equal(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +} + +func TestValidateBlocksPerYear(t *testing.T) { + tests := []struct { + name string + value interface{} + err error + }{ + { + name: "should prevent validate blocks per year with invalid interface", + value: "string", + err: errors.New("invalid parameter type: string"), + }, + { + name: "should validate valid blocks per year", + value: DefaultBlocksPerYear, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateBlocksPerYear(tt.value) + if tt.err != nil { + require.Error(t, err, tt.err) + require.Equal(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +} + +func TestValidateDistributionProportions(t *testing.T) { + tests := []struct { + name string + distrProportions interface{} + err error + }{ + { + name: "should prevent validate distribution proportions with invalid interface", + distrProportions: "string", + err: errors.New("invalid parameter type: string"), + }, + { + name: "should validate valid distribution proportions", + distrProportions: DefaultDistributionProportions, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateDistributionProportions(tt.distrProportions) + if tt.err != nil { + require.Error(t, err, tt.err) + require.Equal(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +} + +func TestValidateWeightedAddresses(t *testing.T) { + tests := []struct { + name string + weightedAddresses interface{} + err error + }{ + { + name: "should prevent validate weighed addresses with invalid interface", + weightedAddresses: "string", + err: errors.New("invalid parameter type: string"), + }, + { + name: "should validate valid weighted addresses", + weightedAddresses: DefaultFundedAddresses, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateWeightedAddresses(tt.weightedAddresses) + if tt.err != nil { + require.Error(t, err, tt.err) + require.Equal(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +} From 3a6c8a59e98046a69ae0849ac9827dad62f59214 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 26 Sep 2022 11:37:54 -0400 Subject: [PATCH 03/16] partial update --- x/mint/types/params_test.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index 9953d2b..98ee7c2 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -2,8 +2,10 @@ package types import ( "errors" - "github.com/stretchr/testify/require" + sdk "github.com/cosmos/cosmos-sdk/types" "testing" + + "github.com/stretchr/testify/require" ) func TestParamsValidate(t *testing.T) { @@ -52,6 +54,16 @@ func TestValidateMintDenom(t *testing.T) { denom: 10, err: errors.New("invalid parameter type: int"), }, + { + name: "should prevent validate empty mint denom", + denom: "", + err: errors.New("mint denom cannot be blank"), + }, + { + name: "should prevent validate mint denom with invalid value", + denom: "invalid&", + err: errors.New("invalid denom: invalid&"), + }, { name: "should validate valid mint denom", denom: DefaultMintDenom, @@ -81,6 +93,15 @@ func TestValidateDec(t *testing.T) { value: "string", err: errors.New("invalid parameter type: string"), }, + { + name: "should prevent validate dec with negative value", + value: sdk.NewDec(-1), + err: errors.New("cannot be negative: -1.000000000000000000"), + }, { + name: "should prevent validate dec too large a value", + value: sdk.NewDec(2), + err: errors.New("dec too large: 2.000000000000000000"), + }, { name: "should validate valid dec", value: DefaultInflationRateChange, @@ -110,6 +131,11 @@ func TestValidateBlocksPerYear(t *testing.T) { value: "string", err: errors.New("invalid parameter type: string"), }, + { + name: "should prevent validate blocks per year with zero value", + value: uint64(0), + err: errors.New("blocks per year must be positive: 0"), + }, { name: "should validate valid blocks per year", value: DefaultBlocksPerYear, @@ -139,6 +165,11 @@ func TestValidateDistributionProportions(t *testing.T) { distrProportions: "string", err: errors.New("invalid parameter type: string"), }, + { + name: "should prevent validate distribution proportions with negative staking", + distrProportions: "string", + err: errors.New("invalid parameter type: string"), + }, { name: "should validate valid distribution proportions", distrProportions: DefaultDistributionProportions, From d5bdd79a16569dde673d35cbc0043bc9464897fb Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 26 Sep 2022 12:21:07 -0400 Subject: [PATCH 04/16] cover params --- x/mint/types/minter_test.go | 24 +++++---- x/mint/types/params_test.go | 103 ++++++++++++++++++++++++++++++++++-- x/mint/types/types_test.go | 11 ++++ 3 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 x/mint/types/types_test.go diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index a394620..a6a5b0c 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -1,4 +1,4 @@ -package types +package types_test import ( "math/rand" @@ -7,11 +7,13 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + + "github.com/ignite/modules/x/mint/types" ) func TestNextInflation(t *testing.T) { - minter := DefaultInitialMinter() - params := DefaultParams() + minter := types.DefaultInitialMinter() + params := types.DefaultParams() blocksPerYr := sdk.NewDec(int64(params.BlocksPerYear)) // Governing Mechanism: @@ -59,8 +61,8 @@ func TestNextInflation(t *testing.T) { } func TestBlockProvision(t *testing.T) { - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) - params := DefaultParams() + minter := types.InitialMinter(sdk.NewDecWithPrec(1, 1)) + params := types.DefaultParams() secondsPerYear := int64(60 * 60 * 8766) @@ -94,8 +96,8 @@ func TestBlockProvision(t *testing.T) { // BenchmarkBlockProvision-4 3000000 429 ns/op func BenchmarkBlockProvision(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) - params := DefaultParams() + minter := types.InitialMinter(sdk.NewDecWithPrec(1, 1)) + params := types.DefaultParams() s1 := rand.NewSource(100) r1 := rand.New(s1) @@ -111,8 +113,8 @@ func BenchmarkBlockProvision(b *testing.B) { // BenchmarkNextInflation-4 1000000 1828 ns/op func BenchmarkNextInflation(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) - params := DefaultParams() + minter := types.InitialMinter(sdk.NewDecWithPrec(1, 1)) + params := types.DefaultParams() bondedRatio := sdk.NewDecWithPrec(1, 1) // run the NextInflationRate function b.N times @@ -125,8 +127,8 @@ func BenchmarkNextInflation(b *testing.B) { // BenchmarkNextAnnualProvisions-4 5000000 251 ns/op func BenchmarkNextAnnualProvisions(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) - params := DefaultParams() + minter := types.InitialMinter(sdk.NewDecWithPrec(1, 1)) + params := types.DefaultParams() totalSupply := sdkmath.NewInt(100000000000000) // run the NextAnnualProvisions function b.N times diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index 98ee7c2..27964e4 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -2,9 +2,11 @@ package types import ( "errors" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/ignite/modules/testutil/sample" + "math/rand" "testing" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" ) @@ -166,9 +168,40 @@ func TestValidateDistributionProportions(t *testing.T) { err: errors.New("invalid parameter type: string"), }, { - name: "should prevent validate distribution proportions with negative staking", - distrProportions: "string", - err: errors.New("invalid parameter type: string"), + name: "should prevent validate distribution proportions with negative staking ratio", + distrProportions: DistributionProportions{ + Staking: sdk.NewDecWithPrec(-3, 1), // -0.3 + FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 + CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 + }, + err: errors.New("staking distribution ratio should not be negative"), + }, + { + name: "should prevent validate distribution proportions with negative funded addresses ratio", + distrProportions: DistributionProportions{ + Staking: sdk.NewDecWithPrec(3, 1), // 0.3 + FundedAddresses: sdk.NewDecWithPrec(-4, 1), // -0.4 + CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 + }, + err: errors.New("funded addresses distribution ratio should not be negative"), + }, + { + name: "should prevent validate distribution proportions with negative community pool ratio", + distrProportions: DistributionProportions{ + Staking: sdk.NewDecWithPrec(3, 1), // 0.3 + FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 + CommunityPool: sdk.NewDecWithPrec(-3, 1), // -0.3 + }, + err: errors.New("community pool distribution ratio should not be negative"), + }, + { + name: "should prevent validate distribution proportions total ratio not equal to 1", + distrProportions: DistributionProportions{ + Staking: sdk.NewDecWithPrec(3, 1), // 0.3 + FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 + CommunityPool: sdk.NewDecWithPrec(31, 2), // 0.31 + }, + err: errors.New("total distributions ratio should be 1"), }, { name: "should validate valid distribution proportions", @@ -189,6 +222,9 @@ func TestValidateDistributionProportions(t *testing.T) { } func TestValidateWeightedAddresses(t *testing.T) { + s := rand.NewSource(1) + r := rand.New(s) + tests := []struct { name string weightedAddresses interface{} @@ -200,9 +236,66 @@ func TestValidateWeightedAddresses(t *testing.T) { err: errors.New("invalid parameter type: string"), }, { - name: "should validate valid weighted addresses", + name: "should prevent validate weighed addresses with invalid SDK address", + weightedAddresses: []WeightedAddress{ + { + Address: "invalid", + Weight: sdk.OneDec(), + }, + }, + err: errors.New("invalid address at index 0"), + }, + { + name: "should prevent validate weighed addresses with negative value", + weightedAddresses: []WeightedAddress{ + { + Address: sample.Address(r), + Weight: sdk.NewDec(-1), + }, + }, + err: errors.New("non-positive weight at index 0"), + }, + { + name: "should prevent validate weighed addresses with weight greater than 1", + weightedAddresses: []WeightedAddress{ + { + Address: sample.Address(r), + Weight: sdk.NewDec(2), + }, + }, + err: errors.New("more than 1 weight at index 0"), + }, + { + name: "should prevent validate weighed addresses with sum greater than 1", + weightedAddresses: []WeightedAddress{ + { + Address: sample.Address(r), + Weight: sdk.NewDecWithPrec(6, 1), + }, + { + Address: sample.Address(r), + Weight: sdk.NewDecWithPrec(5, 1), + }, + }, + err: errors.New("invalid weight sum: 1.100000000000000000"), + }, + { + name: "should validate valid empty weighted addresses", weightedAddresses: DefaultFundedAddresses, }, + { + name: "should validate valid weighted addresses", + weightedAddresses: []WeightedAddress{ + { + Address: sample.Address(r), + Weight: sdk.NewDecWithPrec(5, 1), + }, + { + Address: sample.Address(r), + Weight: sdk.NewDecWithPrec(5, 1), + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/x/mint/types/types_test.go b/x/mint/types/types_test.go new file mode 100644 index 0000000..a796ba8 --- /dev/null +++ b/x/mint/types/types_test.go @@ -0,0 +1,11 @@ +package types_test + +import "math/rand" + +var r *rand.Rand + +// initialize random generator +func init() { + s := rand.NewSource(1) + r = rand.New(s) +} From 0d5ed57c89e4c99dcabe903e8c0ed9f14d39169a Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 26 Sep 2022 12:28:38 -0400 Subject: [PATCH 05/16] minter --- x/mint/types/minter_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index a6a5b0c..c21b8fb 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -1,6 +1,7 @@ package types_test import ( + "errors" "math/rand" "testing" @@ -11,6 +12,39 @@ import ( "github.com/ignite/modules/x/mint/types" ) +func TestValidateMinter(t *testing.T) { + invalid := types.DefaultInitialMinter() + invalid.Inflation = sdk.NewDec(-1) + + tests := []struct { + name string + minter types.Minter + err error + }{ + { + name: "should prevent validate for minter with negative inflation", + minter: invalid, + err: errors.New("mint parameter Inflation should be positive, is -1.000000000000000000"), + }, + { + name: "should validate valid minter", + minter: types.DefaultInitialMinter(), + err: nil, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := types.ValidateMinter(tc.minter) + if tc.err != nil { + require.Error(t, err, tc.err) + require.Equal(t, err, tc.err) + return + } + require.NoError(t, err) + }) + } +} + func TestNextInflation(t *testing.T) { minter := types.DefaultInitialMinter() params := types.DefaultParams() From 24488b9918d1dea9e243d27dd8c4ce3672b06eda Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 26 Sep 2022 12:51:12 -0400 Subject: [PATCH 06/16] genesis --- x/mint/genesis.go | 9 ++++++--- x/mint/simulation/genesis.go | 7 +++++-- x/mint/types/genesis.go | 6 ------ x/mint/types/genesis_test.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 x/mint/types/genesis_test.go diff --git a/x/mint/genesis.go b/x/mint/genesis.go index c32b414..fea32aa 100644 --- a/x/mint/genesis.go +++ b/x/mint/genesis.go @@ -16,7 +16,10 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, ak types.AccountKeeper, // ExportGenesis returns a GenesisState for a given context and keeper. func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState { - minter := keeper.GetMinter(ctx) - params := keeper.GetParams(ctx) - return types.NewGenesisState(minter, params) + genesis := types.DefaultGenesisState() + + genesis.Minter = keeper.GetMinter(ctx) + genesis.Params = keeper.GetParams(ctx) + + return genesis } diff --git a/x/mint/simulation/genesis.go b/x/mint/simulation/genesis.go index 03652eb..7b539b8 100644 --- a/x/mint/simulation/genesis.go +++ b/x/mint/simulation/genesis.go @@ -141,12 +141,15 @@ func RandomizedGenState(simState *module.SimulationState) { blocksPerYear := uint64(60 * 60 * 8766 / 5) params := types.NewParams(mintDenom, inflationRateChange, inflationMax, inflationMin, goalBonded, blocksPerYear, distributionProportions, developmentFundRecipients) - mintGenesis := types.NewGenesisState(types.InitialMinter(inflation), params) + mintGenesis := types.GenesisState{ + Minter: types.InitialMinter(inflation), + Params: params, + } bz, err := json.MarshalIndent(&mintGenesis, "", " ") if err != nil { panic(err) } fmt.Printf("Selected randomly generated minting parameters:\n%s\n", bz) - simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(mintGenesis) + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&mintGenesis) } diff --git a/x/mint/types/genesis.go b/x/mint/types/genesis.go index 3d2f761..46bef2a 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -1,12 +1,6 @@ package types // NewGenesisState creates a new GenesisState object -func NewGenesisState(minter Minter, params Params) *GenesisState { - return &GenesisState{ - Minter: minter, - Params: params, - } -} // DefaultGenesisState creates a default GenesisState object func DefaultGenesisState() *GenesisState { diff --git a/x/mint/types/genesis_test.go b/x/mint/types/genesis_test.go new file mode 100644 index 0000000..ece7098 --- /dev/null +++ b/x/mint/types/genesis_test.go @@ -0,0 +1,34 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/ignite/modules/x/mint/types" +) + +func TestValidateGenesis(t *testing.T) { + tests := []struct { + name string + genesis *types.GenesisState + err error + }{ + { + name: "should validate valid genesis", + genesis: types.DefaultGenesisState(), + err: nil, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := types.ValidateGenesis(*tc.genesis) + if tc.err != nil { + require.Error(t, err, tc.err) + require.Equal(t, err, tc.err) + return + } + require.NoError(t, err) + }) + } +} From 7cd1bf18c4d1c60db5afaa0e6be2e553153af84a Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Mon, 3 Oct 2022 08:13:37 -0400 Subject: [PATCH 07/16] Update x/mint/types/params_test.go Co-authored-by: Lucas Bertrand --- x/mint/types/params_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index 27964e4..b3de80e 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -29,7 +29,6 @@ func TestParamsValidate(t *testing.T) { { name: "should validate valid params", params: DefaultParams(), - err: nil, }, } for _, tt := range tests { From 5411aa5d25dc072e89ba95ac8da7a6be016ab77e Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 08:39:33 -0400 Subject: [PATCH 08/16] fix --- x/mint/genesis.go | 2 +- x/mint/types/genesis_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x/mint/genesis.go b/x/mint/genesis.go index fea32aa..bbc871e 100644 --- a/x/mint/genesis.go +++ b/x/mint/genesis.go @@ -16,7 +16,7 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, ak types.AccountKeeper, // ExportGenesis returns a GenesisState for a given context and keeper. func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState { - genesis := types.DefaultGenesisState() + genesis := types.DefaultGenesis() genesis.Minter = keeper.GetMinter(ctx) genesis.Params = keeper.GetParams(ctx) diff --git a/x/mint/types/genesis_test.go b/x/mint/types/genesis_test.go index ece7098..5e18087 100644 --- a/x/mint/types/genesis_test.go +++ b/x/mint/types/genesis_test.go @@ -16,13 +16,13 @@ func TestValidateGenesis(t *testing.T) { }{ { name: "should validate valid genesis", - genesis: types.DefaultGenesisState(), + genesis: types.DefaultGenesis(), err: nil, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - err := types.ValidateGenesis(*tc.genesis) + err := tc.genesis.Validate() if tc.err != nil { require.Error(t, err, tc.err) require.Equal(t, err, tc.err) From 05ab9870bf61198502c6732521ea109263e71979 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 08:47:31 -0400 Subject: [PATCH 09/16] refactor minter validate test --- x/mint/types/genesis.go | 2 +- x/mint/types/minter.go | 8 ++++---- x/mint/types/minter_test.go | 26 ++++++++++++-------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/x/mint/types/genesis.go b/x/mint/types/genesis.go index 55b5d7c..3450761 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -17,5 +17,5 @@ func (gs GenesisState) Validate() error { return err } - return ValidateMinter(gs.Minter) + return gs.Minter.Validate() } diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index 0f0a4da..e9d2edb 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -32,11 +32,11 @@ func DefaultInitialMinter() Minter { ) } -// validate minter -func ValidateMinter(minter Minter) error { - if minter.Inflation.IsNegative() { +// Validate checks if inflation parameter is negative +func (m Minter) Validate() error { + if m.Inflation.IsNegative() { return fmt.Errorf("mint parameter Inflation should be positive, is %s", - minter.Inflation.String()) + m.Inflation.String()) } return nil } diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index c21b8fb..b2bc347 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -1,7 +1,6 @@ package types_test import ( - "errors" "math/rand" "testing" @@ -17,27 +16,26 @@ func TestValidateMinter(t *testing.T) { invalid.Inflation = sdk.NewDec(-1) tests := []struct { - name string - minter types.Minter - err error + name string + minter types.Minter + isValid bool }{ { - name: "should prevent validate for minter with negative inflation", - minter: invalid, - err: errors.New("mint parameter Inflation should be positive, is -1.000000000000000000"), + name: "should validate valid minter", + minter: types.DefaultInitialMinter(), + isValid: true, }, { - name: "should validate valid minter", - minter: types.DefaultInitialMinter(), - err: nil, + name: "should prevent validate for minter with negative inflation", + minter: invalid, + isValid: false, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - err := types.ValidateMinter(tc.minter) - if tc.err != nil { - require.Error(t, err, tc.err) - require.Equal(t, err, tc.err) + err := tc.minter.Validate() + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) From e18c291b2683023026c8b8f2755ba659c2529327 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 08:50:33 -0400 Subject: [PATCH 10/16] refactor genesis test --- x/mint/types/genesis_test.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/x/mint/types/genesis_test.go b/x/mint/types/genesis_test.go index 5e18087..8ab440c 100644 --- a/x/mint/types/genesis_test.go +++ b/x/mint/types/genesis_test.go @@ -9,23 +9,31 @@ import ( ) func TestValidateGenesis(t *testing.T) { + invalid := types.DefaultGenesis() + // set inflation min to larger than inflation max + invalid.Params.InflationMin = invalid.Params.InflationMax.Add(invalid.Params.InflationMax) + tests := []struct { name string genesis *types.GenesisState - err error + isValid bool }{ { name: "should validate valid genesis", genesis: types.DefaultGenesis(), - err: nil, + isValid: true, + }, + { + name: "should prevent invalid params", + genesis: invalid, + isValid: false, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { err := tc.genesis.Validate() - if tc.err != nil { - require.Error(t, err, tc.err) - require.Equal(t, err, tc.err) + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) From ea38b5f9bd6c1df28bbd5a2e0f44f0bf207f9749 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 08:52:57 -0400 Subject: [PATCH 11/16] refactor params test --- x/mint/types/params_test.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index b3de80e..5d34f3d 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -16,27 +16,26 @@ func TestParamsValidate(t *testing.T) { invalid.InflationMin = invalid.InflationMax.Add(invalid.InflationMax) tests := []struct { - name string - params Params - err error + name string + params Params + isValid bool }{ { - name: "should prevent validate params with inflation min larger than inflation max", - params: invalid, - err: errors.New("max inflation (0.200000000000000000) must be greater than or equal " + - "to min inflation (0.400000000000000000)"), + name: "should validate valid params", + params: DefaultParams(), + isValid: true, }, { - name: "should validate valid params", - params: DefaultParams(), + name: "should prevent validate params with inflation min larger than inflation max", + params: invalid, + isValid: false, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := tt.params.Validate() - if tt.err != nil { - require.Error(t, err, tt.err) - require.Equal(t, err, tt.err) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := tc.params.Validate() + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) From eb10437124e72a7d3a5889b9acba3cefe8ad66b6 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 08:57:58 -0400 Subject: [PATCH 12/16] refactor params test --- x/mint/types/params_test.go | 161 ++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 82 deletions(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index 5d34f3d..87b040b 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -1,7 +1,6 @@ package types import ( - "errors" "github.com/ignite/modules/testutil/sample" "math/rand" "testing" @@ -11,9 +10,9 @@ import ( ) func TestParamsValidate(t *testing.T) { - invalid := DefaultParams() + invalidInflationMin := DefaultParams() // set inflation min to larger than inflation max - invalid.InflationMin = invalid.InflationMax.Add(invalid.InflationMax) + invalidInflationMin.InflationMin = invalidInflationMin.InflationMax.Add(invalidInflationMin.InflationMax) tests := []struct { name string @@ -26,8 +25,8 @@ func TestParamsValidate(t *testing.T) { isValid: true, }, { - name: "should prevent validate params with inflation min larger than inflation max", - params: invalid, + name: "should prevent validate params with invalidInflationMin", + params: invalidInflationMin, isValid: false, }, } @@ -45,36 +44,36 @@ func TestParamsValidate(t *testing.T) { func TestValidateMintDenom(t *testing.T) { tests := []struct { - name string - denom interface{} - err error + name string + denom interface{} + isValid bool }{ { - name: "should prevent validate mint denom with invalid interface", - denom: 10, - err: errors.New("invalid parameter type: int"), + name: "should validate valid mint denom", + denom: DefaultMintDenom, + isValid: true, }, { - name: "should prevent validate empty mint denom", - denom: "", - err: errors.New("mint denom cannot be blank"), + name: "should prevent validate mint denom with invalid interface", + denom: 10, + isValid: false, }, { - name: "should prevent validate mint denom with invalid value", - denom: "invalid&", - err: errors.New("invalid denom: invalid&"), + name: "should prevent validate empty mint denom", + denom: "", + isValid: false, }, { - name: "should validate valid mint denom", - denom: DefaultMintDenom, + name: "should prevent validate mint denom with invalid value", + denom: "invalid&", + isValid: false, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := validateMintDenom(tt.denom) - if tt.err != nil { - require.Error(t, err, tt.err) - require.Equal(t, err, tt.err) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := validateMintDenom(tc.denom) + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) @@ -84,35 +83,34 @@ func TestValidateMintDenom(t *testing.T) { func TestValidateDec(t *testing.T) { tests := []struct { - name string - value interface{} - err error + name string + value interface{} + isValid bool }{ { - name: "should prevent validate dec with invalid interface", - value: "string", - err: errors.New("invalid parameter type: string"), + name: "should prevent validate dec with invalid interface", + value: "string", + isValid: false, }, { - name: "should prevent validate dec with negative value", - value: sdk.NewDec(-1), - err: errors.New("cannot be negative: -1.000000000000000000"), + name: "should prevent validate dec with negative value", + value: sdk.NewDec(-1), + isValid: false, }, { - name: "should prevent validate dec too large a value", - value: sdk.NewDec(2), - err: errors.New("dec too large: 2.000000000000000000"), + name: "should prevent validate dec too large a value", + value: sdk.NewDec(2), + isValid: false, }, { name: "should validate valid dec", value: DefaultInflationRateChange, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := validateDec(tt.value) - if tt.err != nil { - require.Error(t, err, tt.err) - require.Equal(t, err, tt.err) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := validateDec(tc.value) + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) @@ -122,31 +120,30 @@ func TestValidateDec(t *testing.T) { func TestValidateBlocksPerYear(t *testing.T) { tests := []struct { - name string - value interface{} - err error + name string + value interface{} + isValid bool }{ { - name: "should prevent validate blocks per year with invalid interface", - value: "string", - err: errors.New("invalid parameter type: string"), + name: "should prevent validate blocks per year with invalid interface", + value: "string", + isValid: false, }, { - name: "should prevent validate blocks per year with zero value", - value: uint64(0), - err: errors.New("blocks per year must be positive: 0"), + name: "should prevent validate blocks per year with zero value", + value: uint64(0), + isValid: false, }, { name: "should validate valid blocks per year", value: DefaultBlocksPerYear, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := validateBlocksPerYear(tt.value) - if tt.err != nil { - require.Error(t, err, tt.err) - require.Equal(t, err, tt.err) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := validateBlocksPerYear(tc.value) + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) @@ -158,12 +155,12 @@ func TestValidateDistributionProportions(t *testing.T) { tests := []struct { name string distrProportions interface{} - err error + isValid bool }{ { name: "should prevent validate distribution proportions with invalid interface", distrProportions: "string", - err: errors.New("invalid parameter type: string"), + isValid: false, }, { name: "should prevent validate distribution proportions with negative staking ratio", @@ -172,7 +169,7 @@ func TestValidateDistributionProportions(t *testing.T) { FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 }, - err: errors.New("staking distribution ratio should not be negative"), + isValid: false, }, { name: "should prevent validate distribution proportions with negative funded addresses ratio", @@ -181,7 +178,7 @@ func TestValidateDistributionProportions(t *testing.T) { FundedAddresses: sdk.NewDecWithPrec(-4, 1), // -0.4 CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 }, - err: errors.New("funded addresses distribution ratio should not be negative"), + isValid: false, }, { name: "should prevent validate distribution proportions with negative community pool ratio", @@ -190,7 +187,7 @@ func TestValidateDistributionProportions(t *testing.T) { FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 CommunityPool: sdk.NewDecWithPrec(-3, 1), // -0.3 }, - err: errors.New("community pool distribution ratio should not be negative"), + isValid: false, }, { name: "should prevent validate distribution proportions total ratio not equal to 1", @@ -199,19 +196,19 @@ func TestValidateDistributionProportions(t *testing.T) { FundedAddresses: sdk.NewDecWithPrec(4, 1), // 0.4 CommunityPool: sdk.NewDecWithPrec(31, 2), // 0.31 }, - err: errors.New("total distributions ratio should be 1"), + isValid: false, }, { name: "should validate valid distribution proportions", distrProportions: DefaultDistributionProportions, + isValid: true, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := validateDistributionProportions(tt.distrProportions) - if tt.err != nil { - require.Error(t, err, tt.err) - require.Equal(t, err, tt.err) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := validateDistributionProportions(tc.distrProportions) + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) @@ -226,12 +223,12 @@ func TestValidateWeightedAddresses(t *testing.T) { tests := []struct { name string weightedAddresses interface{} - err error + isValid bool }{ { name: "should prevent validate weighed addresses with invalid interface", weightedAddresses: "string", - err: errors.New("invalid parameter type: string"), + isValid: false, }, { name: "should prevent validate weighed addresses with invalid SDK address", @@ -241,7 +238,7 @@ func TestValidateWeightedAddresses(t *testing.T) { Weight: sdk.OneDec(), }, }, - err: errors.New("invalid address at index 0"), + isValid: false, }, { name: "should prevent validate weighed addresses with negative value", @@ -251,7 +248,7 @@ func TestValidateWeightedAddresses(t *testing.T) { Weight: sdk.NewDec(-1), }, }, - err: errors.New("non-positive weight at index 0"), + isValid: false, }, { name: "should prevent validate weighed addresses with weight greater than 1", @@ -261,7 +258,7 @@ func TestValidateWeightedAddresses(t *testing.T) { Weight: sdk.NewDec(2), }, }, - err: errors.New("more than 1 weight at index 0"), + isValid: false, }, { name: "should prevent validate weighed addresses with sum greater than 1", @@ -275,7 +272,7 @@ func TestValidateWeightedAddresses(t *testing.T) { Weight: sdk.NewDecWithPrec(5, 1), }, }, - err: errors.New("invalid weight sum: 1.100000000000000000"), + isValid: false, }, { name: "should validate valid empty weighted addresses", @@ -293,14 +290,14 @@ func TestValidateWeightedAddresses(t *testing.T) { Weight: sdk.NewDecWithPrec(5, 1), }, }, + isValid: true, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := validateWeightedAddresses(tt.weightedAddresses) - if tt.err != nil { - require.Error(t, err, tt.err) - require.Equal(t, err, tt.err) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := validateWeightedAddresses(tc.weightedAddresses) + if !tc.isValid { + require.Error(t, err) return } require.NoError(t, err) From 116be1d699a0e416e60998671827a4325af50d27 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 09:02:06 -0400 Subject: [PATCH 13/16] refactor params test --- x/mint/types/params_test.go | 65 +++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index 87b040b..b0fe4cd 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -87,6 +87,11 @@ func TestValidateDec(t *testing.T) { value interface{} isValid bool }{ + { + name: "should validate valid dec", + value: DefaultInflationRateChange, + isValid: true, + }, { name: "should prevent validate dec with invalid interface", value: "string", @@ -101,10 +106,6 @@ func TestValidateDec(t *testing.T) { value: sdk.NewDec(2), isValid: false, }, - { - name: "should validate valid dec", - value: DefaultInflationRateChange, - }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -124,6 +125,11 @@ func TestValidateBlocksPerYear(t *testing.T) { value interface{} isValid bool }{ + { + name: "should validate valid blocks per year", + value: DefaultBlocksPerYear, + isValid: true, + }, { name: "should prevent validate blocks per year with invalid interface", value: "string", @@ -134,10 +140,6 @@ func TestValidateBlocksPerYear(t *testing.T) { value: uint64(0), isValid: false, }, - { - name: "should validate valid blocks per year", - value: DefaultBlocksPerYear, - }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -157,6 +159,11 @@ func TestValidateDistributionProportions(t *testing.T) { distrProportions interface{} isValid bool }{ + { + name: "should validate valid distribution proportions", + distrProportions: DefaultDistributionProportions, + isValid: true, + }, { name: "should prevent validate distribution proportions with invalid interface", distrProportions: "string", @@ -198,11 +205,6 @@ func TestValidateDistributionProportions(t *testing.T) { }, isValid: false, }, - { - name: "should validate valid distribution proportions", - distrProportions: DefaultDistributionProportions, - isValid: true, - }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -225,6 +227,25 @@ func TestValidateWeightedAddresses(t *testing.T) { weightedAddresses interface{} isValid bool }{ + { + name: "should validate valid weighted addresses", + weightedAddresses: []WeightedAddress{ + { + Address: sample.Address(r), + Weight: sdk.NewDecWithPrec(5, 1), + }, + { + Address: sample.Address(r), + Weight: sdk.NewDecWithPrec(5, 1), + }, + }, + isValid: true, + }, + { + name: "should validate valid empty weighted addresses", + weightedAddresses: DefaultFundedAddresses, + isValid: true, + }, { name: "should prevent validate weighed addresses with invalid interface", weightedAddresses: "string", @@ -274,24 +295,6 @@ func TestValidateWeightedAddresses(t *testing.T) { }, isValid: false, }, - { - name: "should validate valid empty weighted addresses", - weightedAddresses: DefaultFundedAddresses, - }, - { - name: "should validate valid weighted addresses", - weightedAddresses: []WeightedAddress{ - { - Address: sample.Address(r), - Weight: sdk.NewDecWithPrec(5, 1), - }, - { - Address: sample.Address(r), - Weight: sdk.NewDecWithPrec(5, 1), - }, - }, - isValid: true, - }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { From d9af30198dd9d87b5059106614ec5752e5ca5e01 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 09:05:22 -0400 Subject: [PATCH 14/16] refactor params test --- x/mint/types/params_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index b0fe4cd..1195d77 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -11,9 +11,11 @@ import ( func TestParamsValidate(t *testing.T) { invalidInflationMin := DefaultParams() - // set inflation min to larger than inflation max invalidInflationMin.InflationMin = invalidInflationMin.InflationMax.Add(invalidInflationMin.InflationMax) + invalidInflationMax := DefaultParams() + invalidInflationMax.InflationMin = invalidInflationMax.InflationMax.Sub(invalidInflationMax.InflationMax) + tests := []struct { name string params Params @@ -29,6 +31,11 @@ func TestParamsValidate(t *testing.T) { params: invalidInflationMin, isValid: false, }, + { + name: "should prevent validate params with invalidInflationMax", + params: invalidInflationMax, + isValid: false, + }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { From 3a474fc423d559ee85169f5fbae93224d70eb480 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Mon, 3 Oct 2022 09:13:53 -0400 Subject: [PATCH 15/16] add more test cases --- x/mint/types/params_test.go | 73 +++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index 1195d77..c91036b 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -10,11 +10,38 @@ import ( ) func TestParamsValidate(t *testing.T) { - invalidInflationMin := DefaultParams() - invalidInflationMin.InflationMin = invalidInflationMin.InflationMax.Add(invalidInflationMin.InflationMax) - invalidInflationMax := DefaultParams() - invalidInflationMax.InflationMin = invalidInflationMax.InflationMax.Sub(invalidInflationMax.InflationMax) + invalidInflationMax.InflationMin = invalidInflationMax.InflationMax.Add(invalidInflationMax.InflationMax) + + negativeInflationMin := DefaultParams() + negativeInflationMin.InflationMin = sdk.NewDec(-1) + + negativeInflationMax := DefaultParams() + negativeInflationMax.InflationMax = sdk.NewDec(-1) + + negativeGoalBonded := DefaultParams() + negativeGoalBonded.GoalBonded = sdk.NewDec(-1) + + invalidMintDenom := DefaultParams() + invalidMintDenom.MintDenom = "" + + invalidBlocksPerYear := DefaultParams() + invalidBlocksPerYear.BlocksPerYear = 0 + + invalidDistrProportions := DefaultParams() + invalidDistrProportions.DistributionProportions = DistributionProportions{ + Staking: sdk.NewDecWithPrec(3, 1), // 0.3 + FundedAddresses: sdk.NewDecWithPrec(-4, 1), // -0.4 + CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 + } + + invalidWeightedAddresses := DefaultParams() + invalidWeightedAddresses.FundedAddresses = []WeightedAddress{ + { + Address: "invalid", + Weight: sdk.OneDec(), + }, + } tests := []struct { name string @@ -27,13 +54,43 @@ func TestParamsValidate(t *testing.T) { isValid: true, }, { - name: "should prevent validate params with invalidInflationMin", - params: invalidInflationMin, + name: "should prevent validate params with inflation max less than inflation min", + params: invalidInflationMax, isValid: false, }, { - name: "should prevent validate params with invalidInflationMax", - params: invalidInflationMax, + name: "should prevent validate params with negative inflation min", + params: negativeInflationMin, + isValid: false, + }, + { + name: "should prevent validate params with negative inflation max", + params: negativeInflationMax, + isValid: false, + }, + { + name: "should prevent validate params with negative goal bonded", + params: negativeGoalBonded, + isValid: false, + }, + { + name: "should prevent invalid mint denom", + params: invalidMintDenom, + isValid: false, + }, + { + name: "should prevent invalid blocks per year", + params: invalidBlocksPerYear, + isValid: false, + }, + { + name: "should prevent invalid distribution proportions", + params: invalidDistrProportions, + isValid: false, + }, + { + name: "should prevent invalid weighted addresses", + params: invalidWeightedAddresses, isValid: false, }, } From 5b48fef562753946c962e3ba35af01d77770b0e2 Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 5 Oct 2022 12:48:28 -0400 Subject: [PATCH 16/16] make tests visual --- x/mint/types/params_test.go | 146 ++++++++++++++++++++++++------------ 1 file changed, 97 insertions(+), 49 deletions(-) diff --git a/x/mint/types/params_test.go b/x/mint/types/params_test.go index c91036b..17a2935 100644 --- a/x/mint/types/params_test.go +++ b/x/mint/types/params_test.go @@ -10,39 +10,6 @@ import ( ) func TestParamsValidate(t *testing.T) { - invalidInflationMax := DefaultParams() - invalidInflationMax.InflationMin = invalidInflationMax.InflationMax.Add(invalidInflationMax.InflationMax) - - negativeInflationMin := DefaultParams() - negativeInflationMin.InflationMin = sdk.NewDec(-1) - - negativeInflationMax := DefaultParams() - negativeInflationMax.InflationMax = sdk.NewDec(-1) - - negativeGoalBonded := DefaultParams() - negativeGoalBonded.GoalBonded = sdk.NewDec(-1) - - invalidMintDenom := DefaultParams() - invalidMintDenom.MintDenom = "" - - invalidBlocksPerYear := DefaultParams() - invalidBlocksPerYear.BlocksPerYear = 0 - - invalidDistrProportions := DefaultParams() - invalidDistrProportions.DistributionProportions = DistributionProportions{ - Staking: sdk.NewDecWithPrec(3, 1), // 0.3 - FundedAddresses: sdk.NewDecWithPrec(-4, 1), // -0.4 - CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 - } - - invalidWeightedAddresses := DefaultParams() - invalidWeightedAddresses.FundedAddresses = []WeightedAddress{ - { - Address: "invalid", - Weight: sdk.OneDec(), - }, - } - tests := []struct { name string params Params @@ -54,43 +21,124 @@ func TestParamsValidate(t *testing.T) { isValid: true, }, { - name: "should prevent validate params with inflation max less than inflation min", - params: invalidInflationMax, + name: "should prevent validate params with inflation max less than inflation min", + params: Params{ + MintDenom: DefaultMintDenom, + InflationRateChange: DefaultInflationRateChange, + InflationMax: DefaultInflationMax, + InflationMin: DefaultInflationMin.Add(DefaultInflationMax), + GoalBonded: DefaultGoalBonded, + BlocksPerYear: DefaultBlocksPerYear, + DistributionProportions: DefaultDistributionProportions, + FundedAddresses: DefaultFundedAddresses, + }, isValid: false, }, { - name: "should prevent validate params with negative inflation min", - params: negativeInflationMin, + name: "should prevent validate params with negative inflation min", + params: Params{ + MintDenom: DefaultMintDenom, + InflationRateChange: DefaultInflationRateChange, + InflationMax: DefaultInflationMax, + InflationMin: sdk.NewDec(-1), + GoalBonded: DefaultGoalBonded, + BlocksPerYear: DefaultBlocksPerYear, + DistributionProportions: DefaultDistributionProportions, + FundedAddresses: DefaultFundedAddresses, + }, isValid: false, }, { - name: "should prevent validate params with negative inflation max", - params: negativeInflationMax, + name: "should prevent validate params with negative inflation max", + params: Params{ + MintDenom: DefaultMintDenom, + InflationRateChange: DefaultInflationRateChange, + InflationMax: sdk.NewDec(-1), + InflationMin: DefaultInflationMin, + GoalBonded: DefaultGoalBonded, + BlocksPerYear: DefaultBlocksPerYear, + DistributionProportions: DefaultDistributionProportions, + FundedAddresses: DefaultFundedAddresses, + }, isValid: false, }, { - name: "should prevent validate params with negative goal bonded", - params: negativeGoalBonded, + name: "should prevent validate params with negative goal bonded", + params: Params{ + MintDenom: DefaultMintDenom, + InflationRateChange: DefaultInflationRateChange, + InflationMax: DefaultInflationMax, + InflationMin: DefaultInflationMin, + GoalBonded: sdk.NewDec(-1), + BlocksPerYear: DefaultBlocksPerYear, + DistributionProportions: DefaultDistributionProportions, + FundedAddresses: DefaultFundedAddresses, + }, isValid: false, }, { - name: "should prevent invalid mint denom", - params: invalidMintDenom, + name: "should prevent invalid mint denom", + params: Params{ + MintDenom: "", + InflationRateChange: DefaultInflationRateChange, + InflationMax: DefaultInflationMax, + InflationMin: DefaultInflationMin, + GoalBonded: DefaultGoalBonded, + BlocksPerYear: DefaultBlocksPerYear, + DistributionProportions: DefaultDistributionProportions, + FundedAddresses: DefaultFundedAddresses, + }, isValid: false, }, { - name: "should prevent invalid blocks per year", - params: invalidBlocksPerYear, + name: "should prevent invalid blocks per year", + params: Params{ + MintDenom: DefaultMintDenom, + InflationRateChange: DefaultInflationRateChange, + InflationMax: DefaultInflationMax, + InflationMin: DefaultInflationMin, + GoalBonded: DefaultGoalBonded, + BlocksPerYear: 0, + DistributionProportions: DefaultDistributionProportions, + FundedAddresses: DefaultFundedAddresses, + }, isValid: false, }, { - name: "should prevent invalid distribution proportions", - params: invalidDistrProportions, + name: "should prevent invalid distribution proportions", + params: Params{ + MintDenom: DefaultMintDenom, + InflationRateChange: DefaultInflationRateChange, + InflationMax: DefaultInflationMax, + InflationMin: DefaultInflationMin, + GoalBonded: DefaultGoalBonded, + BlocksPerYear: DefaultBlocksPerYear, + DistributionProportions: DistributionProportions{ + Staking: sdk.NewDecWithPrec(3, 1), // 0.3 + FundedAddresses: sdk.NewDecWithPrec(-4, 1), // -0.4 + CommunityPool: sdk.NewDecWithPrec(3, 1), // 0.3 + }, + FundedAddresses: DefaultFundedAddresses, + }, isValid: false, }, { - name: "should prevent invalid weighted addresses", - params: invalidWeightedAddresses, + name: "should prevent invalid weighted addresses", + params: Params{ + MintDenom: DefaultMintDenom, + InflationRateChange: DefaultInflationRateChange, + InflationMax: DefaultInflationMax, + InflationMin: DefaultInflationMin, + GoalBonded: DefaultGoalBonded, + BlocksPerYear: DefaultBlocksPerYear, + DistributionProportions: DefaultDistributionProportions, + FundedAddresses: []WeightedAddress{ + { + Address: "invalid", + Weight: sdk.OneDec(), + }, + }, + }, isValid: false, }, }