From e0875661b6884eca0b20346759691c3fc8400916 Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Wed, 30 Nov 2022 17:02:40 +0530 Subject: [PATCH] chore: (x/mint) improve code cov (#14066) ## Description missed the test coverage in last audit. ref: #13988 #13456 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- x/mint/keeper/genesis_test.go | 83 +++++++++++++++++++++++++++++++++++ x/mint/keeper/keeper.go | 2 +- x/mint/keeper/keeper_test.go | 32 ++++++++++++-- x/mint/types/params.go | 1 + 4 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 x/mint/keeper/genesis_test.go diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go new file mode 100644 index 000000000000..5a88558613c4 --- /dev/null +++ b/x/mint/keeper/genesis_test.go @@ -0,0 +1,83 @@ +package keeper_test + +import ( + "testing" + + "cosmossdk.io/math" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/suite" + + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/mint" + "github.com/cosmos/cosmos-sdk/x/mint/keeper" + minttestutil "github.com/cosmos/cosmos-sdk/x/mint/testutil" + "github.com/cosmos/cosmos-sdk/x/mint/types" +) + +var minterAcc = authtypes.NewEmptyModuleAccount(types.ModuleName, authtypes.Minter) + +type GenesisTestSuite struct { + suite.Suite + + sdkCtx sdk.Context + keeper keeper.Keeper + cdc codec.BinaryCodec + accountKeeper types.AccountKeeper + key *storetypes.KVStoreKey +} + +func TestGenesisTestSuite(t *testing.T) { + suite.Run(t, new(GenesisTestSuite)) +} + +func (s *GenesisTestSuite) SetupTest() { + key := sdk.NewKVStoreKey(types.StoreKey) + testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test")) + encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{}) + + // gomock initializations + ctrl := gomock.NewController(s.T()) + s.cdc = codec.NewProtoCodec(encCfg.InterfaceRegistry) + s.sdkCtx = testCtx.Ctx + s.key = key + + stakingKeeper := minttestutil.NewMockStakingKeeper(ctrl) + accountKeeper := minttestutil.NewMockAccountKeeper(ctrl) + bankKeeper := minttestutil.NewMockBankKeeper(ctrl) + s.accountKeeper = accountKeeper + accountKeeper.EXPECT().GetModuleAddress(minterAcc.Name).Return(minterAcc.GetAddress()) + accountKeeper.EXPECT().GetModuleAccount(s.sdkCtx, minterAcc.Name).Return(minterAcc) + + s.keeper = keeper.NewKeeper(s.cdc, key, stakingKeeper, accountKeeper, bankKeeper, "", "") +} + +func (s *GenesisTestSuite) TestImportExportGenesis() { + genesisState := types.DefaultGenesisState() + genesisState.Minter = types.NewMinter(sdk.NewDecWithPrec(20, 2), math.LegacyNewDec(1)) + genesisState.Params = types.NewParams( + "testDenom", + sdk.NewDecWithPrec(15, 2), + sdk.NewDecWithPrec(22, 2), + sdk.NewDecWithPrec(9, 2), + sdk.NewDecWithPrec(69, 2), + uint64(60*60*8766/5), + ) + + s.keeper.InitGenesis(s.sdkCtx, s.accountKeeper, genesisState) + + minter := s.keeper.GetMinter(s.sdkCtx) + s.Require().Equal(genesisState.Minter, minter) + + invalidCtx := testutil.DefaultContextWithDB(s.T(), s.key, sdk.NewTransientStoreKey("transient_test")) + s.Require().Panics(func() { s.keeper.GetMinter(invalidCtx.Ctx) }, "stored minter should not have been nil") + params := s.keeper.GetParams(s.sdkCtx) + s.Require().Equal(genesisState.Params, params) + + genesisState2 := s.keeper.ExportGenesis(s.sdkCtx) + s.Require().Equal(genesisState, genesisState2) +} diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 1872ef36c95b..9f2a44112a2e 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -72,7 +72,7 @@ func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) { return } -// GetMinter sets the minter. +// SetMinter sets the minter. func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshal(&minter) diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index c99fc65798eb..3f39387eede4 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -20,9 +20,11 @@ import ( type IntegrationTestSuite struct { suite.Suite - mintKeeper keeper.Keeper - ctx sdk.Context - msgServer types.MsgServer + mintKeeper keeper.Keeper + ctx sdk.Context + msgServer types.MsgServer + stakingKeeper *minttestutil.MockStakingKeeper + bankKeeper *minttestutil.MockBankKeeper } func TestKeeperTestSuite(t *testing.T) { @@ -52,6 +54,11 @@ func (s *IntegrationTestSuite) SetupTest() { authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String(), ) + s.stakingKeeper = stakingKeeper + s.bankKeeper = bankKeeper + + s.Require().Equal(testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName), + s.mintKeeper.Logger(testCtx.Ctx)) err := s.mintKeeper.SetParams(s.ctx, types.DefaultParams()) s.Require().NoError(err) @@ -110,3 +117,22 @@ func (s *IntegrationTestSuite) TestParams() { }) } } + +func (s *IntegrationTestSuite) TestAliasFunctions() { + stakingTokenSupply := sdk.NewIntFromUint64(100000000000) + s.stakingKeeper.EXPECT().StakingTokenSupply(s.ctx).Return(stakingTokenSupply) + s.Require().Equal(s.mintKeeper.StakingTokenSupply(s.ctx), stakingTokenSupply) + + bondedRatio := sdk.NewDecWithPrec(15, 2) + s.stakingKeeper.EXPECT().BondedRatio(s.ctx).Return(bondedRatio) + s.Require().Equal(s.mintKeeper.BondedRatio(s.ctx), bondedRatio) + + coins := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000000))) + s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, coins).Return(nil) + s.Require().Equal(s.mintKeeper.MintCoins(s.ctx, sdk.NewCoins()), nil) + s.Require().Nil(s.mintKeeper.MintCoins(s.ctx, coins)) + + fees := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))) + s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(s.ctx, types.ModuleName, authtypes.FeeCollectorName, fees).Return(nil) + s.Require().Nil(s.mintKeeper.AddCollectedFees(s.ctx, fees)) +} diff --git a/x/mint/types/params.go b/x/mint/types/params.go index aa9892a2c13b..375762b19762 100644 --- a/x/mint/types/params.go +++ b/x/mint/types/params.go @@ -10,6 +10,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// NewParams returns Params instance with the given values. func NewParams(mintDenom string, inflationRateChange, inflationMax, inflationMin, goalBonded sdk.Dec, blocksPerYear uint64) Params { return Params{ MintDenom: mintDenom,