Skip to content

Commit

Permalink
test(mint): types package (#57)
Browse files Browse the repository at this point in the history
* update params

* init test

* partial update

* cover params

* minter

* genesis

* Update x/mint/types/params_test.go

Co-authored-by: Lucas Bertrand <[email protected]>

* fix

* refactor minter validate test

* refactor genesis test

* refactor params test

* refactor params test

* refactor params test

* refactor params test

* add more test cases

* make tests visual

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
Alex Johnson and lumtis authored Oct 5, 2022
1 parent 50c8583 commit 4b181af
Show file tree
Hide file tree
Showing 9 changed files with 572 additions and 103 deletions.
9 changes: 6 additions & 3 deletions x/mint/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.DefaultGenesis()

genesis.Minter = keeper.GetMinter(ctx)
genesis.Params = keeper.GetParams(ctx)

return genesis
}
7 changes: 5 additions & 2 deletions x/mint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 1 addition & 7 deletions x/mint/types/genesis.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

// DefaultGenesis creates a default GenesisState object
func DefaultGenesis() *GenesisState {
Expand All @@ -23,5 +17,5 @@ func (gs GenesisState) Validate() error {
return err
}

return ValidateMinter(gs.Minter)
return gs.Minter.Validate()
}
42 changes: 42 additions & 0 deletions x/mint/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ignite/modules/x/mint/types"
)

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
isValid bool
}{
{
name: "should validate valid genesis",
genesis: types.DefaultGenesis(),
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.isValid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
8 changes: 4 additions & 4 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
56 changes: 45 additions & 11 deletions x/mint/types/minter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package types_test

import (
"math/rand"
Expand All @@ -7,11 +7,45 @@ 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 TestValidateMinter(t *testing.T) {
invalid := types.DefaultInitialMinter()
invalid.Inflation = sdk.NewDec(-1)

tests := []struct {
name string
minter types.Minter
isValid bool
}{
{
name: "should validate valid minter",
minter: types.DefaultInitialMinter(),
isValid: true,
},
{
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 := tc.minter.Validate()
if !tc.isValid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

func TestNextInflation(t *testing.T) {
minter := DefaultInitialMinter()
params := DefaultParams()
minter := types.DefaultInitialMinter()
params := types.DefaultParams()
blocksPerYr := sdk.NewDec(int64(params.BlocksPerYear))

// Governing Mechanism:
Expand Down Expand Up @@ -59,8 +93,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)

Expand Down Expand Up @@ -94,8 +128,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)
Expand All @@ -111,8 +145,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
Expand All @@ -125,8 +159,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
Expand Down
113 changes: 37 additions & 76 deletions x/mint/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
Expand All @@ -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
Expand Down
Loading

0 comments on commit 4b181af

Please sign in to comment.