Skip to content

Commit

Permalink
test: add genesis test for mint module
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsam committed Feb 6, 2022
1 parent 4c5bc46 commit 7bb9d61
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 8 deletions.
4 changes: 2 additions & 2 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
if !schedule.EndTime.Before(ctx.BlockTime()) && !schedule.StartTime.After(ctx.BlockTime()) {
lastBlockTime := k.GetLastBlockTime(ctx)
// if not set LastBlockTime(e.g. fist block), skip minting inflation
if lastBlockTime.Equal(time.Time{}) {
if lastBlockTime == nil {
break
}
lastBlockTimeDiff := ctx.BlockTime().Sub(lastBlockTime)
lastBlockTimeDiff := ctx.BlockTime().Sub(*lastBlockTime)
if lastBlockTimeDiff > params.BlockTimeThreshold {
lastBlockTimeDiff = params.BlockTimeThreshold
}
Expand Down
31 changes: 31 additions & 0 deletions x/mint/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
squadtypes "github.com/cosmosquad-labs/squad/types"
"github.com/cosmosquad-labs/squad/x/mint/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abcitypes "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -51,6 +52,36 @@ func (suite *ModuleTestSuite) SetupTest() {
}
}

func (s *ModuleTestSuite) TestInitGenesis() {
genState := types.DefaultGenesisState()
mint.InitGenesis(s.ctx, s.app.MintKeeper, s.app.AccountKeeper, genState)
got := mint.ExportGenesis(s.ctx, s.app.MintKeeper)
s.Require().Equal(*genState, *got)
}

func (s *ModuleTestSuite) TestImportExportGenesis() {
k, ctx := s.keeper, s.ctx
genState := mint.ExportGenesis(ctx, k)
bz := s.app.AppCodec().MustMarshalJSON(genState)

var genState2, genState5 types.GenesisState
s.app.AppCodec().MustUnmarshalJSON(bz, &genState2)
mint.InitGenesis(ctx, s.app.MintKeeper, s.app.AccountKeeper, &genState2)

genState3 := mint.ExportGenesis(ctx, k)
s.Require().Equal(*genState, genState2, *genState3)

ctx = ctx.WithBlockTime(squadtypes.MustParseRFC3339("2022-01-01T00:00:00Z"))
mint.BeginBlocker(ctx, k)
genState4 := mint.ExportGenesis(ctx, k)
bz = s.app.AppCodec().MustMarshalJSON(genState4)
s.app.AppCodec().MustUnmarshalJSON(bz, &genState5)
s.Require().Equal(*genState5.LastBlockTime, squadtypes.MustParseRFC3339("2022-01-01T00:00:00Z"))
mint.InitGenesis(s.ctx, s.app.MintKeeper, s.app.AccountKeeper, &genState5)
genState6 := mint.ExportGenesis(ctx, k)
s.Require().Equal(*genState4, genState5, genState6)
}

func TestConstantInflation(t *testing.T) {
app := app.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
Expand Down
5 changes: 4 additions & 1 deletion x/mint/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (

// InitGenesis new mint genesis
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, ak types.AccountKeeper, data *types.GenesisState) {
if err := types.ValidateGenesis(*data); err != nil {
panic(err)
}
keeper.SetParams(ctx, data.Params)
if data.LastBlockTime != nil {
keeper.SetLastBlockTime(ctx, *data.LastBlockTime)
Expand All @@ -19,5 +22,5 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, ak types.AccountKeeper,
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
lastBlockTime := keeper.GetLastBlockTime(ctx)
params := keeper.GetParams(ctx)
return types.NewGenesisState(params, &lastBlockTime)
return types.NewGenesisState(params, lastBlockTime)
}
6 changes: 3 additions & 3 deletions x/mint/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
)

// GetLastBlockTime returns the last block time.
func (k Keeper) GetLastBlockTime(ctx sdk.Context) time.Time {
func (k Keeper) GetLastBlockTime(ctx sdk.Context) *time.Time {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastBlockTimeKey)
if bz == nil {
return time.Time{}
return nil
} else {
ts, err := sdk.ParseTimeBytes(bz)
if err != nil {
panic(err)
}
return ts
return &ts
}
}

Expand Down
10 changes: 9 additions & 1 deletion x/mint/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package types

import "time"
import (
"fmt"
"time"

squadtypes "github.com/cosmosquad-labs/squad/types"
)

// NewGenesisState creates a new GenesisState object
func NewGenesisState(params Params, lastBlockTime *time.Time) *GenesisState {
Expand All @@ -21,5 +26,8 @@ func DefaultGenesisState() *GenesisState {
// ValidateGenesis validates the provided genesis state to ensure the
// expected invariants holds.
func ValidateGenesis(data GenesisState) error {
if data.LastBlockTime != nil && data.LastBlockTime.Before(squadtypes.MustParseRFC3339("0001-01-01T00:00:00Z")) {
return fmt.Errorf("invalid last block time")
}
return data.Params.Validate()
}
75 changes: 75 additions & 0 deletions x/mint/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package types_test

import (
"testing"
"time"

squadtypes "github.com/cosmosquad-labs/squad/types"
"github.com/stretchr/testify/require"

"github.com/cosmosquad-labs/squad/x/mint/types"
)

func TestGenesisState_Validate(t *testing.T) {
for _, tc := range []struct {
name string
malleate func(genState *types.GenesisState)
expectedErr string
}{
{
"default is valid",
func(genState *types.GenesisState) {},
"",
},
{
"valid last block time",
func(genState *types.GenesisState) {
tmpTime := squadtypes.MustParseRFC3339("0001-01-01T00:00:00Z")
genState.LastBlockTime = &tmpTime
},
"",
},
{
"valid last block time2",
func(genState *types.GenesisState) {
tmpTime := squadtypes.MustParseRFC3339("9999-12-31T00:00:00Z")
genState.LastBlockTime = &tmpTime
},
"",
},
{
"invalid last block time",
func(genState *types.GenesisState) {

tmpTime := time.UnixMicro(-62136697900000001).UTC()
genState.LastBlockTime = &tmpTime
},
"invalid last block time",
},
{
"invalid mint denom",
func(genState *types.GenesisState) {
genState.Params.MintDenom = ""
},
"mint denom cannot be blank",
},
{
"invalid mint denom2",
func(genState *types.GenesisState) {
genState.Params.MintDenom = "a"
},
"invalid denom: a",
},
} {
t.Run(tc.name, func(t *testing.T) {
genState := types.DefaultGenesisState()
tc.malleate(genState)
err := types.ValidateGenesis(*genState)
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.expectedErr)
}
})
}
}
2 changes: 1 addition & 1 deletion x/mint/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func validateBlockTimeThreshold(i interface{}) error {
}

if v <= 0 {
return fmt.Errorf("unbonding time must be positive: %d", v)
return fmt.Errorf("block time threshold must be positive: %d", v)
}

return nil
Expand Down

0 comments on commit 7bb9d61

Please sign in to comment.