From 44e510d1ebccab73d2e071ff564c115f41e16534 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:07:58 +0200 Subject: [PATCH 01/10] decouple keeper from query server --- x/mint/keeper/grpc_query.go | 22 +++++++++++++++------- x/mint/module.go | 2 +- x/mint/types/keys.go | 5 ----- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/x/mint/keeper/grpc_query.go b/x/mint/keeper/grpc_query.go index 46d5f9f42c1f..7a68ccf15cc8 100644 --- a/x/mint/keeper/grpc_query.go +++ b/x/mint/keeper/grpc_query.go @@ -7,12 +7,20 @@ import ( "github.com/cosmos/cosmos-sdk/x/mint/types" ) -var _ types.QueryServer = Keeper{} +var _ types.QueryServer = queryServer{} + +func NewQueryServerImpl(k Keeper) types.QueryServer { + return queryServer{k} +} + +type queryServer struct { + k Keeper +} // Params returns params of the mint module. -func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { +func (q queryServer) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { ctx := sdk.UnwrapSDKContext(c) - params, err := k.GetParams(ctx) + params, err := q.k.GetParams(ctx) if err != nil { return nil, err } @@ -21,9 +29,9 @@ func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.Q } // Inflation returns minter.Inflation of the mint module. -func (k Keeper) Inflation(c context.Context, _ *types.QueryInflationRequest) (*types.QueryInflationResponse, error) { +func (q queryServer) Inflation(c context.Context, _ *types.QueryInflationRequest) (*types.QueryInflationResponse, error) { ctx := sdk.UnwrapSDKContext(c) - minter, err := k.GetMinter(ctx) + minter, err := q.k.GetMinter(ctx) if err != nil { return nil, err } @@ -32,9 +40,9 @@ func (k Keeper) Inflation(c context.Context, _ *types.QueryInflationRequest) (*t } // AnnualProvisions returns minter.AnnualProvisions of the mint module. -func (k Keeper) AnnualProvisions(c context.Context, _ *types.QueryAnnualProvisionsRequest) (*types.QueryAnnualProvisionsResponse, error) { +func (q queryServer) AnnualProvisions(c context.Context, _ *types.QueryAnnualProvisionsRequest) (*types.QueryAnnualProvisionsResponse, error) { ctx := sdk.UnwrapSDKContext(c) - minter, err := k.GetMinter(ctx) + minter, err := q.k.GetMinter(ctx) if err != nil { return nil, err } diff --git a/x/mint/module.go b/x/mint/module.go index b7794487ac7a..1dea1406f3c2 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -149,7 +149,7 @@ func (AppModule) Name() string { // module-specific gRPC queries. func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) + types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper)) m := keeper.NewMigrator(am.keeper, am.legacySubspace) diff --git a/x/mint/types/keys.go b/x/mint/types/keys.go index 69deb872680a..5f4005219f02 100644 --- a/x/mint/types/keys.go +++ b/x/mint/types/keys.go @@ -12,9 +12,4 @@ const ( // StoreKey is the default store key for mint StoreKey = ModuleName - - // Query endpoints supported by the minting querier - QueryParameters = "parameters" - QueryInflation = "inflation" - QueryAnnualProvisions = "annual_provisions" ) From cb7b7abb067d2966aefc3d1c12eab0e1d7836666 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:11:48 +0200 Subject: [PATCH 02/10] start migrating params --- x/mint/keeper/grpc_query_test.go | 2 +- x/mint/keeper/keeper.go | 22 +++++++++++++++------- x/mint/types/keys.go | 4 +++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index f49aebd6556d..00a8a469821f 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -60,7 +60,7 @@ func (suite *MintTestSuite) SetupTest() { suite.mintKeeper.SetMinter(suite.ctx, types.DefaultInitialMinter()) queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, encCfg.InterfaceRegistry) - types.RegisterQueryServer(queryHelper, suite.mintKeeper) + types.RegisterQueryServer(queryHelper, keeper.NewQueryServerImpl(suite.mintKeeper)) suite.queryClient = types.NewQueryClient(queryHelper) } diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 1386bcee3ef4..54f2ea440b4d 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "cosmossdk.io/collections" "cosmossdk.io/log" "cosmossdk.io/math" @@ -25,6 +26,9 @@ type Keeper struct { // the address capable of executing a MsgUpdateParams message. Typically, this // should be the x/gov module account. authority string + + Schema collections.Schema + Params collections.Item[types.Params] } // NewKeeper creates a new mint Keeper instance @@ -42,14 +46,23 @@ func NewKeeper( panic(fmt.Sprintf("the x/%s module account has not been set", types.ModuleName)) } - return Keeper{ + sb := collections.NewSchemaBuilder(storeService) + k := Keeper{ cdc: cdc, storeService: storeService, stakingKeeper: sk, bankKeeper: bk, feeCollectorName: feeCollectorName, authority: authority, + Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), + } + + schema, err := sb.Build() + if err != nil { + panic(err) } + k.Schema = schema + return k } // GetAuthority returns the x/mint module's authority. @@ -92,12 +105,7 @@ func (k Keeper) SetMinter(ctx context.Context, minter types.Minter) error { // SetParams sets the x/mint module parameters. func (k Keeper) SetParams(ctx context.Context, params types.Params) error { - store := k.storeService.OpenKVStore(ctx) - bz, err := k.cdc.Marshal(¶ms) - if err != nil { - return err - } - return store.Set(types.ParamsKey, bz) + return k.Params.Set(ctx, params) } // GetParams returns the current x/mint module parameters. diff --git a/x/mint/types/keys.go b/x/mint/types/keys.go index 5f4005219f02..64eb761303d9 100644 --- a/x/mint/types/keys.go +++ b/x/mint/types/keys.go @@ -1,8 +1,10 @@ package types +import "cosmossdk.io/collections" + var ( // MinterKey is the key to use for the keeper store. - MinterKey = []byte{0x00} + MinterKey = collections.NewPrefix(0) ParamsKey = []byte{0x01} ) From f8ccb031b5466da10109c6d42b461648648cd915 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:15:00 +0200 Subject: [PATCH 03/10] remove GetParams --- testutil/integration/example_test.go | 4 ++-- x/mint/abci.go | 2 +- x/mint/keeper/genesis.go | 2 +- x/mint/keeper/genesis_test.go | 2 +- x/mint/keeper/grpc_query.go | 2 +- x/mint/keeper/grpc_query_test.go | 2 +- x/mint/keeper/keeper.go | 16 ---------------- x/mint/keeper/keeper_test.go | 4 ++-- 8 files changed, 9 insertions(+), 25 deletions(-) diff --git a/testutil/integration/example_test.go b/testutil/integration/example_test.go index f9527d635f3b..549b2cc12005 100644 --- a/testutil/integration/example_test.go +++ b/testutil/integration/example_test.go @@ -68,7 +68,7 @@ func Example() { // register the message and query servers authtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), authkeeper.NewMsgServerImpl(accountKeeper)) minttypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), mintkeeper.NewMsgServerImpl(mintKeeper)) - minttypes.RegisterQueryServer(integrationApp.QueryHelper(), mintKeeper) + minttypes.RegisterQueryServer(integrationApp.QueryHelper(), mintkeeper.NewQueryServerImpl(mintKeeper)) params := minttypes.DefaultParams() params.BlocksPerYear = 10000 @@ -98,7 +98,7 @@ func Example() { sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) // we should also check the state of the application - got, err := mintKeeper.GetParams(sdkCtx) + got, err := mintKeeper.Params.Get(sdkCtx) if err != nil { panic(err) } diff --git a/x/mint/abci.go b/x/mint/abci.go index edbaa1a641ce..afa23fcfdf2e 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -20,7 +20,7 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper, ic types.InflationCalcul return err } - params, err := k.GetParams(ctx) + params, err := k.Params.Get(ctx) if err != nil { return err } diff --git a/x/mint/keeper/genesis.go b/x/mint/keeper/genesis.go index ed5c567e62bd..033381a9f037 100644 --- a/x/mint/keeper/genesis.go +++ b/x/mint/keeper/genesis.go @@ -23,7 +23,7 @@ func (keeper Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { panic(err) } - params, err := keeper.GetParams(ctx) + params, err := keeper.Params.Get(ctx) if err != nil { panic(err) } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index a142d702f949..555062c13919 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -80,7 +80,7 @@ func (s *GenesisTestSuite) TestImportExportGenesis() { _, err = s.keeper.GetMinter(invalidCtx.Ctx) s.Require().EqualError(err, "stored minter should not have been nil") - params, err := s.keeper.GetParams(s.sdkCtx) + params, err := s.keeper.Params.Get(s.sdkCtx) s.Require().Equal(genesisState.Params, params) s.Require().NoError(err) diff --git a/x/mint/keeper/grpc_query.go b/x/mint/keeper/grpc_query.go index 7a68ccf15cc8..7dd347bcf7c4 100644 --- a/x/mint/keeper/grpc_query.go +++ b/x/mint/keeper/grpc_query.go @@ -20,7 +20,7 @@ type queryServer struct { // Params returns params of the mint module. func (q queryServer) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { ctx := sdk.UnwrapSDKContext(c) - params, err := q.k.GetParams(ctx) + params, err := q.k.Params.Get(ctx) if err != nil { return nil, err } diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index 00a8a469821f..eb074732a97d 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -68,7 +68,7 @@ func (suite *MintTestSuite) SetupTest() { func (suite *MintTestSuite) TestGRPCParams() { params, err := suite.queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{}) suite.Require().NoError(err) - kparams, err := suite.mintKeeper.GetParams(suite.ctx) + kparams, err := suite.mintKeeper.Params.Get(suite.ctx) suite.Require().NoError(err) suite.Require().Equal(params.Params, kparams) diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 54f2ea440b4d..a6833e9f71ce 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -108,22 +108,6 @@ func (k Keeper) SetParams(ctx context.Context, params types.Params) error { return k.Params.Set(ctx, params) } -// GetParams returns the current x/mint module parameters. -func (k Keeper) GetParams(ctx context.Context) (p types.Params, err error) { - store := k.storeService.OpenKVStore(ctx) - bz, err := store.Get(types.ParamsKey) - if err != nil { - return p, err - } - - if bz == nil { - return p, nil - } - - err = k.cdc.Unmarshal(bz, &p) - return p, err -} - // StakingTokenSupply implements an alias call to the underlying staking keeper's // StakingTokenSupply to be used in BeginBlocker. func (k Keeper) StakingTokenSupply(ctx context.Context) math.Int { diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index a93c6f372f78..23ecfa1b19b2 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -108,7 +108,7 @@ func (s *IntegrationTestSuite) TestParams() { tc := tc s.Run(tc.name, func() { - expected, err := s.mintKeeper.GetParams(s.ctx) + expected, err := s.mintKeeper.Params.Get(s.ctx) s.Require().NoError(err) err = s.mintKeeper.SetParams(s.ctx, tc.input) if tc.expectErr { @@ -118,7 +118,7 @@ func (s *IntegrationTestSuite) TestParams() { s.Require().NoError(err) } - p, err := s.mintKeeper.GetParams(s.ctx) + p, err := s.mintKeeper.Params.Get(s.ctx) s.Require().NoError(err) s.Require().Equal(expected, p) }) From 374952a0266d0a4838bd87a4f7ccc881b14d924f Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:18:06 +0200 Subject: [PATCH 04/10] remove SetParams --- x/mint/keeper/genesis.go | 2 +- x/mint/keeper/grpc_query_test.go | 2 +- x/mint/keeper/keeper.go | 5 --- x/mint/keeper/keeper_test.go | 55 +----------------------- x/mint/keeper/msg_server.go | 2 +- x/slashing/simulation/operations_test.go | 2 +- x/staking/simulation/operations_test.go | 2 +- 7 files changed, 6 insertions(+), 64 deletions(-) diff --git a/x/mint/keeper/genesis.go b/x/mint/keeper/genesis.go index 033381a9f037..329e913bce2a 100644 --- a/x/mint/keeper/genesis.go +++ b/x/mint/keeper/genesis.go @@ -9,7 +9,7 @@ import ( func (keeper Keeper) InitGenesis(ctx sdk.Context, ak types.AccountKeeper, data *types.GenesisState) { keeper.SetMinter(ctx, data.Minter) - if err := keeper.SetParams(ctx, data.Params); err != nil { + if err := keeper.Params.Set(ctx, data.Params); err != nil { panic(err) } diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index eb074732a97d..366855a88c55 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -55,7 +55,7 @@ func (suite *MintTestSuite) SetupTest() { authtypes.NewModuleAddress(govtypes.ModuleName).String(), ) - err := suite.mintKeeper.SetParams(suite.ctx, types.DefaultParams()) + err := suite.mintKeeper.Params.Set(suite.ctx, types.DefaultParams()) suite.Require().NoError(err) suite.mintKeeper.SetMinter(suite.ctx, types.DefaultInitialMinter()) diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index a6833e9f71ce..7607857065e9 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -103,11 +103,6 @@ func (k Keeper) SetMinter(ctx context.Context, minter types.Minter) error { return store.Set(types.MinterKey, bz) } -// SetParams sets the x/mint module parameters. -func (k Keeper) SetParams(ctx context.Context, params types.Params) error { - return k.Params.Set(ctx, params) -} - // StakingTokenSupply implements an alias call to the underlying staking keeper's // StakingTokenSupply to be used in BeginBlocker. func (k Keeper) StakingTokenSupply(ctx context.Context) math.Int { diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index 23ecfa1b19b2..fc325a07b4c7 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -65,66 +65,13 @@ func (s *IntegrationTestSuite) SetupTest() { s.Require().Equal(testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName), s.mintKeeper.Logger(testCtx.Ctx)) - err := s.mintKeeper.SetParams(s.ctx, types.DefaultParams()) + err := s.mintKeeper.Params.Set(s.ctx, types.DefaultParams()) s.Require().NoError(err) s.mintKeeper.SetMinter(s.ctx, types.DefaultInitialMinter()) s.msgServer = keeper.NewMsgServerImpl(s.mintKeeper) } -func (s *IntegrationTestSuite) TestParams() { - testCases := []struct { - name string - input types.Params - expectErr bool - }{ - { - name: "set invalid params (⚠️ not validated in keeper)", - input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - InflationRateChange: math.LegacyNewDecWithPrec(-13, 2), - InflationMax: math.LegacyNewDecWithPrec(20, 2), - InflationMin: math.LegacyNewDecWithPrec(7, 2), - GoalBonded: math.LegacyNewDecWithPrec(67, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), - }, - expectErr: false, - }, - { - name: "set full valid params", - input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - InflationRateChange: math.LegacyNewDecWithPrec(8, 2), - InflationMax: math.LegacyNewDecWithPrec(20, 2), - InflationMin: math.LegacyNewDecWithPrec(2, 2), - GoalBonded: math.LegacyNewDecWithPrec(37, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), - }, - expectErr: false, - }, - } - - for _, tc := range testCases { - tc := tc - - s.Run(tc.name, func() { - expected, err := s.mintKeeper.Params.Get(s.ctx) - s.Require().NoError(err) - err = s.mintKeeper.SetParams(s.ctx, tc.input) - if tc.expectErr { - s.Require().Error(err) - } else { - expected = tc.input - s.Require().NoError(err) - } - - p, err := s.mintKeeper.Params.Get(s.ctx) - s.Require().NoError(err) - s.Require().Equal(expected, p) - }) - } -} - func (s *IntegrationTestSuite) TestAliasFunctions() { stakingTokenSupply := math.NewIntFromUint64(100000000000) s.stakingKeeper.EXPECT().StakingTokenSupply(s.ctx).Return(stakingTokenSupply) diff --git a/x/mint/keeper/msg_server.go b/x/mint/keeper/msg_server.go index 20f9848f6416..43fe1b758a65 100644 --- a/x/mint/keeper/msg_server.go +++ b/x/mint/keeper/msg_server.go @@ -35,7 +35,7 @@ func (ms msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdatePara } ctx := sdk.UnwrapSDKContext(goCtx) - if err := ms.SetParams(ctx, msg.Params); err != nil { + if err := ms.Params.Set(ctx, msg.Params); err != nil { return nil, err } diff --git a/x/slashing/simulation/operations_test.go b/x/slashing/simulation/operations_test.go index 0d11cec1175e..40e284e842e6 100644 --- a/x/slashing/simulation/operations_test.go +++ b/x/slashing/simulation/operations_test.go @@ -115,7 +115,7 @@ func (suite *SimTestSuite) SetupTest() { suite.Require().NoError(banktestutil.FundAccount(suite.ctx, suite.bankKeeper, account.Address, initCoins)) } - suite.mintKeeper.SetParams(suite.ctx, minttypes.DefaultParams()) + suite.Require().NoError(suite.mintKeeper.Params.Set(suite.ctx, minttypes.DefaultParams())) suite.mintKeeper.SetMinter(suite.ctx, minttypes.DefaultInitialMinter()) } diff --git a/x/staking/simulation/operations_test.go b/x/staking/simulation/operations_test.go index 651ce0b96904..8451ff071109 100644 --- a/x/staking/simulation/operations_test.go +++ b/x/staking/simulation/operations_test.go @@ -101,7 +101,7 @@ func (s *SimTestSuite) SetupTest() { require.NoError(s.T(), err) ctx := app.BaseApp.NewContext(false, cmtproto.Header{}) - mintKeeper.SetParams(ctx, minttypes.DefaultParams()) + s.Require().NoError(mintKeeper.Params.Set(ctx, minttypes.DefaultParams())) mintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter()) initAmt := stakingKeeper.TokensFromConsensusPower(ctx, 200) From 18dbf6361c72855857e0bfe3214db33b21c63ef0 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:20:35 +0200 Subject: [PATCH 05/10] start migrating minter --- x/mint/keeper/genesis_test.go | 3 ++- x/mint/keeper/keeper.go | 16 +++------------- x/mint/types/keys.go | 2 +- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 555062c13919..b85704ae0e05 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -3,6 +3,7 @@ package keeper_test import ( "testing" + "cosmossdk.io/collections" "cosmossdk.io/math" "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" @@ -78,7 +79,7 @@ func (s *GenesisTestSuite) TestImportExportGenesis() { invalidCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test")) _, err = s.keeper.GetMinter(invalidCtx.Ctx) - s.Require().EqualError(err, "stored minter should not have been nil") + s.Require().ErrorIs(err, collections.ErrNotFound) params, err := s.keeper.Params.Get(s.sdkCtx) s.Require().Equal(genesisState.Params, params) diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 7607857065e9..73dead4bc413 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -29,6 +29,7 @@ type Keeper struct { Schema collections.Schema Params collections.Item[types.Params] + Minter collections.Item[types.Minter] } // NewKeeper creates a new mint Keeper instance @@ -55,6 +56,7 @@ func NewKeeper( feeCollectorName: feeCollectorName, authority: authority, Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), + Minter: collections.NewItem(sb, types.MinterKey, "minter", codec.CollValue[types.Minter](cdc)), } schema, err := sb.Build() @@ -78,19 +80,7 @@ func (k Keeper) Logger(ctx context.Context) log.Logger { // GetMinter returns the minter. func (k Keeper) GetMinter(ctx context.Context) (types.Minter, error) { - var minter types.Minter - store := k.storeService.OpenKVStore(ctx) - bz, err := store.Get(types.MinterKey) - if err != nil { - return minter, err - } - - if bz == nil { - return minter, fmt.Errorf("stored minter should not have been nil") - } - - err = k.cdc.Unmarshal(bz, &minter) - return minter, err + return k.Minter.Get(ctx) } // SetMinter sets the minter. diff --git a/x/mint/types/keys.go b/x/mint/types/keys.go index 64eb761303d9..83c365a517b3 100644 --- a/x/mint/types/keys.go +++ b/x/mint/types/keys.go @@ -5,7 +5,7 @@ import "cosmossdk.io/collections" var ( // MinterKey is the key to use for the keeper store. MinterKey = collections.NewPrefix(0) - ParamsKey = []byte{0x01} + ParamsKey = collections.NewPrefix(1) ) const ( From f548ef71e301d8a553d32255df7cc8f15f51eace Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:22:06 +0200 Subject: [PATCH 06/10] remove GetMinter --- x/mint/abci.go | 2 +- x/mint/keeper/genesis.go | 2 +- x/mint/keeper/genesis_test.go | 4 ++-- x/mint/keeper/grpc_query.go | 4 ++-- x/mint/keeper/grpc_query_test.go | 2 +- x/mint/keeper/keeper.go | 5 ----- 6 files changed, 7 insertions(+), 12 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index afa23fcfdf2e..c7266666dd33 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -15,7 +15,7 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper, ic types.InflationCalcul defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) // fetch stored minter & params - minter, err := k.GetMinter(ctx) + minter, err := k.Minter.Get(ctx) if err != nil { return err } diff --git a/x/mint/keeper/genesis.go b/x/mint/keeper/genesis.go index 329e913bce2a..4b99f0fcb4ae 100644 --- a/x/mint/keeper/genesis.go +++ b/x/mint/keeper/genesis.go @@ -18,7 +18,7 @@ func (keeper Keeper) InitGenesis(ctx sdk.Context, ak types.AccountKeeper, data * // ExportGenesis returns a GenesisState for a given context and keeper. func (keeper Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { - minter, err := keeper.GetMinter(ctx) + minter, err := keeper.Minter.Get(ctx) if err != nil { panic(err) } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index b85704ae0e05..d919926232f9 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -73,12 +73,12 @@ func (s *GenesisTestSuite) TestImportExportGenesis() { s.keeper.InitGenesis(s.sdkCtx, s.accountKeeper, genesisState) - minter, err := s.keeper.GetMinter(s.sdkCtx) + minter, err := s.keeper.Minter.Get(s.sdkCtx) s.Require().Equal(genesisState.Minter, minter) s.Require().NoError(err) invalidCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test")) - _, err = s.keeper.GetMinter(invalidCtx.Ctx) + _, err = s.keeper.Minter.Get(invalidCtx.Ctx) s.Require().ErrorIs(err, collections.ErrNotFound) params, err := s.keeper.Params.Get(s.sdkCtx) diff --git a/x/mint/keeper/grpc_query.go b/x/mint/keeper/grpc_query.go index 7dd347bcf7c4..eb29d8c3ff75 100644 --- a/x/mint/keeper/grpc_query.go +++ b/x/mint/keeper/grpc_query.go @@ -31,7 +31,7 @@ func (q queryServer) Params(c context.Context, _ *types.QueryParamsRequest) (*ty // Inflation returns minter.Inflation of the mint module. func (q queryServer) Inflation(c context.Context, _ *types.QueryInflationRequest) (*types.QueryInflationResponse, error) { ctx := sdk.UnwrapSDKContext(c) - minter, err := q.k.GetMinter(ctx) + minter, err := q.k.Minter.Get(ctx) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func (q queryServer) Inflation(c context.Context, _ *types.QueryInflationRequest // AnnualProvisions returns minter.AnnualProvisions of the mint module. func (q queryServer) AnnualProvisions(c context.Context, _ *types.QueryAnnualProvisionsRequest) (*types.QueryAnnualProvisionsResponse, error) { ctx := sdk.UnwrapSDKContext(c) - minter, err := q.k.GetMinter(ctx) + minter, err := q.k.Minter.Get(ctx) if err != nil { return nil, err } diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index 366855a88c55..86b98861a6db 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -74,7 +74,7 @@ func (suite *MintTestSuite) TestGRPCParams() { inflation, err := suite.queryClient.Inflation(gocontext.Background(), &types.QueryInflationRequest{}) suite.Require().NoError(err) - minter, err := suite.mintKeeper.GetMinter(suite.ctx) + minter, err := suite.mintKeeper.Minter.Get(suite.ctx) suite.Require().NoError(err) suite.Require().Equal(inflation.Inflation, minter.Inflation) diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 73dead4bc413..b4c3782df332 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -78,11 +78,6 @@ func (k Keeper) Logger(ctx context.Context) log.Logger { return sdkCtx.Logger().With("module", "x/"+types.ModuleName) } -// GetMinter returns the minter. -func (k Keeper) GetMinter(ctx context.Context) (types.Minter, error) { - return k.Minter.Get(ctx) -} - // SetMinter sets the minter. func (k Keeper) SetMinter(ctx context.Context, minter types.Minter) error { store := k.storeService.OpenKVStore(ctx) From fb1887c9f908d30fc43694ab05757d3a61713ebc Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:23:55 +0200 Subject: [PATCH 07/10] remove SetMinter --- x/mint/abci.go | 2 +- x/mint/keeper/genesis.go | 4 +++- x/mint/keeper/grpc_query_test.go | 2 +- x/mint/keeper/keeper.go | 10 ---------- x/mint/keeper/keeper_test.go | 2 +- x/slashing/simulation/operations_test.go | 2 +- x/staking/simulation/operations_test.go | 2 +- 7 files changed, 8 insertions(+), 16 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index c7266666dd33..a48aaabf5318 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -30,7 +30,7 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper, ic types.InflationCalcul bondedRatio := k.BondedRatio(ctx) minter.Inflation = ic(ctx, minter, params, bondedRatio) minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalStakingSupply) - k.SetMinter(ctx, minter) + k.Minter.Set(ctx, minter) // mint coins, update supply mintedCoin := minter.BlockProvision(params) diff --git a/x/mint/keeper/genesis.go b/x/mint/keeper/genesis.go index 4b99f0fcb4ae..4ed6ae1fcdea 100644 --- a/x/mint/keeper/genesis.go +++ b/x/mint/keeper/genesis.go @@ -7,7 +7,9 @@ import ( // InitGenesis new mint genesis func (keeper Keeper) InitGenesis(ctx sdk.Context, ak types.AccountKeeper, data *types.GenesisState) { - keeper.SetMinter(ctx, data.Minter) + if err := keeper.Minter.Set(ctx, data.Minter); err != nil { + panic(err) + } if err := keeper.Params.Set(ctx, data.Params); err != nil { panic(err) diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index 86b98861a6db..35905fc4fe02 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -57,7 +57,7 @@ func (suite *MintTestSuite) SetupTest() { err := suite.mintKeeper.Params.Set(suite.ctx, types.DefaultParams()) suite.Require().NoError(err) - suite.mintKeeper.SetMinter(suite.ctx, types.DefaultInitialMinter()) + suite.Require().NoError(suite.mintKeeper.Minter.Set(suite.ctx, types.DefaultInitialMinter())) queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, encCfg.InterfaceRegistry) types.RegisterQueryServer(queryHelper, keeper.NewQueryServerImpl(suite.mintKeeper)) diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index b4c3782df332..0eceb52ab50e 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -78,16 +78,6 @@ func (k Keeper) Logger(ctx context.Context) log.Logger { return sdkCtx.Logger().With("module", "x/"+types.ModuleName) } -// SetMinter sets the minter. -func (k Keeper) SetMinter(ctx context.Context, minter types.Minter) error { - store := k.storeService.OpenKVStore(ctx) - bz, err := k.cdc.Marshal(&minter) - if err != nil { - return err - } - return store.Set(types.MinterKey, bz) -} - // StakingTokenSupply implements an alias call to the underlying staking keeper's // StakingTokenSupply to be used in BeginBlocker. func (k Keeper) StakingTokenSupply(ctx context.Context) math.Int { diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index fc325a07b4c7..9cc7eb37d20c 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -67,7 +67,7 @@ func (s *IntegrationTestSuite) SetupTest() { err := s.mintKeeper.Params.Set(s.ctx, types.DefaultParams()) s.Require().NoError(err) - s.mintKeeper.SetMinter(s.ctx, types.DefaultInitialMinter()) + s.Require().NoError(s.mintKeeper.Minter.Set(s.ctx, types.DefaultInitialMinter())) s.msgServer = keeper.NewMsgServerImpl(s.mintKeeper) } diff --git a/x/slashing/simulation/operations_test.go b/x/slashing/simulation/operations_test.go index 40e284e842e6..cbcbf9002a4d 100644 --- a/x/slashing/simulation/operations_test.go +++ b/x/slashing/simulation/operations_test.go @@ -116,7 +116,7 @@ func (suite *SimTestSuite) SetupTest() { } suite.Require().NoError(suite.mintKeeper.Params.Set(suite.ctx, minttypes.DefaultParams())) - suite.mintKeeper.SetMinter(suite.ctx, minttypes.DefaultInitialMinter()) + suite.Require().NoError(suite.mintKeeper.Minter.Set(suite.ctx, minttypes.DefaultInitialMinter())) } func TestSimTestSuite(t *testing.T) { diff --git a/x/staking/simulation/operations_test.go b/x/staking/simulation/operations_test.go index 8451ff071109..e6aedd6d23cd 100644 --- a/x/staking/simulation/operations_test.go +++ b/x/staking/simulation/operations_test.go @@ -102,7 +102,7 @@ func (s *SimTestSuite) SetupTest() { ctx := app.BaseApp.NewContext(false, cmtproto.Header{}) s.Require().NoError(mintKeeper.Params.Set(ctx, minttypes.DefaultParams())) - mintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter()) + s.Require().NoError(mintKeeper.Minter.Set(ctx, minttypes.DefaultInitialMinter())) initAmt := stakingKeeper.TokensFromConsensusPower(ctx, 200) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) From 0b3d4b8797f830de0858de32f711ca3237342ddb Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:26:12 +0200 Subject: [PATCH 08/10] replace simulation store decoder with collections --- x/mint/module.go | 2 +- x/mint/simulation/decoder.go | 26 ---------------- x/mint/simulation/decoder_test.go | 49 ------------------------------- 3 files changed, 1 insertion(+), 76 deletions(-) delete mode 100644 x/mint/simulation/decoder.go delete mode 100644 x/mint/simulation/decoder_test.go diff --git a/x/mint/module.go b/x/mint/module.go index 1dea1406f3c2..8c7515202cd3 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -197,7 +197,7 @@ func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.Weight // RegisterStoreDecoder registers a decoder for mint module's types. func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { - sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) + sdr[types.StoreKey] = simtypes.NewStoreDecoderFuncFromCollectionsSchema(am.keeper.Schema) } // WeightedOperations doesn't return any mint module operation. diff --git a/x/mint/simulation/decoder.go b/x/mint/simulation/decoder.go deleted file mode 100644 index b9337fed0074..000000000000 --- a/x/mint/simulation/decoder.go +++ /dev/null @@ -1,26 +0,0 @@ -package simulation - -import ( - "bytes" - "fmt" - - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/types/kv" - "github.com/cosmos/cosmos-sdk/x/mint/types" -) - -// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's -// Value to the corresponding mint type. -func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { - return func(kvA, kvB kv.Pair) string { - switch { - case bytes.Equal(kvA.Key, types.MinterKey): - var minterA, minterB types.Minter - cdc.MustUnmarshal(kvA.Value, &minterA) - cdc.MustUnmarshal(kvB.Value, &minterB) - return fmt.Sprintf("%v\n%v", minterA, minterB) - default: - panic(fmt.Sprintf("invalid mint key %X", kvA.Key)) - } - } -} diff --git a/x/mint/simulation/decoder_test.go b/x/mint/simulation/decoder_test.go deleted file mode 100644 index e10468e4e541..000000000000 --- a/x/mint/simulation/decoder_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package simulation_test - -import ( - "fmt" - "testing" - - "cosmossdk.io/math" - "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/types/kv" - moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - "github.com/cosmos/cosmos-sdk/x/mint" - "github.com/cosmos/cosmos-sdk/x/mint/simulation" - "github.com/cosmos/cosmos-sdk/x/mint/types" -) - -func TestDecodeStore(t *testing.T) { - encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{}) - - dec := simulation.NewDecodeStore(encCfg.Codec) - - minter := types.NewMinter(math.LegacyOneDec(), math.LegacyNewDec(15)) - - kvPairs := kv.Pairs{ - Pairs: []kv.Pair{ - {Key: types.MinterKey, Value: encCfg.Codec.MustMarshal(&minter)}, - {Key: []byte{0x99}, Value: []byte{0x99}}, - }, - } - tests := []struct { - name string - expectedLog string - }{ - {"Minter", fmt.Sprintf("%v\n%v", minter, minter)}, - {"other", ""}, - } - - for i, tt := range tests { - i, tt := i, tt - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name) - } - }) - } -} From 0e5c431bb3227d82c178d4612c0f7668b5021e85 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Mon, 29 May 2023 17:36:14 +0200 Subject: [PATCH 09/10] error --- x/mint/abci.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index a48aaabf5318..2e4b699971a1 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -30,7 +30,10 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper, ic types.InflationCalcul bondedRatio := k.BondedRatio(ctx) minter.Inflation = ic(ctx, minter, params, bondedRatio) minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalStakingSupply) - k.Minter.Set(ctx, minter) + err = k.Minter.Set(ctx, minter) + if err != nil { + return err + } // mint coins, update supply mintedCoin := minter.BlockProvision(params) From 78b735c7fa2bbdb347e1180b9747682a03ad9a3a Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Tue, 30 May 2023 10:54:47 +0200 Subject: [PATCH 10/10] chore: CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06fc1183c42c..04115edec815 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -242,6 +242,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * Remove: types all the key related functions * (x/crisis) [#16328](https://github.com/cosmos/cosmos-sdk/pull/16328) Use collections for state management: * Removed: keeper `GetConstantFee`, `SetConstantFee` +* (x/mint) [#16329](https://github.com/cosmos/cosmos-sdk/pull/16329) Use collections for state management: + * Removed: keeper `GetParams`, `SetParams`, `GetMinter`, `SetMinter`. ### Client Breaking Changes