Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(mint): types package #57

Merged
merged 18 commits into from
Oct 5, 2022
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.DefaultGenesisState()

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)
}
6 changes: 0 additions & 6 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 Down
34 changes: 34 additions & 0 deletions x/mint/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -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
}{
{
aljo242 marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}
}
58 changes: 47 additions & 11 deletions x/mint/types/minter_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
package types
package types_test

import (
"errors"
"math/rand"
"testing"

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
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"),
aljo242 marked this conversation as resolved.
Show resolved Hide resolved
},
{
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 := DefaultInitialMinter()
params := DefaultParams()
minter := types.DefaultInitialMinter()
params := types.DefaultParams()
blocksPerYr := sdk.NewDec(int64(params.BlocksPerYear))

// Governing Mechanism:
Expand Down Expand Up @@ -59,8 +95,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 +130,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 +147,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 +161,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