From cd3abca86726fced69a62d44b84707563b23cb9e Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Mon, 22 May 2023 08:55:10 -0300 Subject: [PATCH] refactor(x/mint)!: use KVStoreService and context.Context (#16179) --- CHANGELOG.md | 1 + UPGRADING.md | 2 + simapp/app.go | 2 +- testutil/integration/example_test.go | 8 ++- x/mint/abci.go | 19 ++++-- x/mint/keeper/genesis.go | 12 +++- x/mint/keeper/genesis_test.go | 13 ++-- x/mint/keeper/grpc_query.go | 15 ++++- x/mint/keeper/grpc_query_test.go | 14 +++-- x/mint/keeper/keeper.go | 85 ++++++++++++++++----------- x/mint/keeper/keeper_test.go | 12 ++-- x/mint/keeper/migrator.go | 2 +- x/mint/migrations/v2/migrate.go | 6 +- x/mint/migrations/v2/migrator_test.go | 7 ++- x/mint/module.go | 9 ++- x/mint/types/genesis.go | 7 ++- 16 files changed, 140 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8ae717b83c2..ddccd6a9b001 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -132,6 +132,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/mint) [#16179](https://github.com/cosmos/cosmos-sdk/issues/16179) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error`. * (x/crisis) [#16216](https://github.com/cosmos/cosmos-sdk/issues/16216) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error` instead of panicking. * (x/gov) [#15988](https://github.com/cosmos/cosmos-sdk/issues/15988) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error` (instead of panicking or returning a `found bool`). Iterators callback functions now return an error instead of a `bool`. * (x/auth) [#15985](https://github.com/cosmos/cosmos-sdk/pull/15985) The `AccountKeeper` does not expose the `QueryServer` and `MsgServer` APIs anymore. diff --git a/UPGRADING.md b/UPGRADING.md index 0e1a81416a00..cb0205c0e6c0 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -82,6 +82,7 @@ The following modules `NewKeeper` function now take a `KVStoreService` instead o * `x/evidence` * `x/feegrant` * `x/gov` +* `x/mint` * `x/nft` User manually wiring their chain need to use the `runtime.NewKVStoreService` method to create a `KVStoreService` from a `StoreKey`: @@ -99,6 +100,7 @@ The following modules' `Keeper` methods now take in a `context.Context` instead * `x/authz` * `x/bank` +* `x/mint` * `x/crisis` * `x/distribution` * `x/evidence` diff --git a/simapp/app.go b/simapp/app.go index 38fe85e21214..990a0969883e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -281,7 +281,7 @@ func NewSimApp( app.StakingKeeper = stakingkeeper.NewKeeper( appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), ) - app.MintKeeper = mintkeeper.NewKeeper(appCodec, keys[minttypes.StoreKey], app.StakingKeeper, app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String()) + app.MintKeeper = mintkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[minttypes.StoreKey]), app.StakingKeeper, app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String()) app.DistrKeeper = distrkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[distrtypes.StoreKey]), app.AccountKeeper, app.BankKeeper, app.StakingKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String()) diff --git a/testutil/integration/example_test.go b/testutil/integration/example_test.go index 02c888d41566..3490dc34191b 100644 --- a/testutil/integration/example_test.go +++ b/testutil/integration/example_test.go @@ -53,7 +53,7 @@ func Example() { // here bankkeeper and staking keeper is nil because we are not testing them // subspace is nil because we don't test params (which is legacy anyway) - mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, keys[minttypes.StoreKey], nil, accountKeeper, nil, authtypes.FeeCollectorName, authority) + mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, runtime.NewKVStoreService(keys[minttypes.StoreKey]), nil, accountKeeper, nil, authtypes.FeeCollectorName, authority) mintModule := mint.NewAppModule(encodingCfg.Codec, mintKeeper, accountKeeper, nil, nil) // create the application and register all the modules from the previous step @@ -98,7 +98,11 @@ func Example() { sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) // we should also check the state of the application - got := mintKeeper.GetParams(sdkCtx) + got, err := mintKeeper.GetParams(sdkCtx) + if err != nil { + panic(err) + } + if diff := cmp.Diff(got, params); diff != "" { panic(diff) } diff --git a/x/mint/abci.go b/x/mint/abci.go index 4f7a3e2adca6..edbaa1a641ce 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -1,6 +1,7 @@ package mint import ( + "context" "time" "github.com/cosmos/cosmos-sdk/telemetry" @@ -10,12 +11,19 @@ import ( ) // BeginBlocker mints new tokens for the previous block. -func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculationFn) error { +func BeginBlocker(ctx context.Context, k keeper.Keeper, ic types.InflationCalculationFn) error { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) // fetch stored minter & params - minter := k.GetMinter(ctx) - params := k.GetParams(ctx) + minter, err := k.GetMinter(ctx) + if err != nil { + return err + } + + params, err := k.GetParams(ctx) + if err != nil { + return err + } // recalculate inflation rate totalStakingSupply := k.StakingTokenSupply(ctx) @@ -28,7 +36,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio mintedCoin := minter.BlockProvision(params) mintedCoins := sdk.NewCoins(mintedCoin) - err := k.MintCoins(ctx, mintedCoins) + err = k.MintCoins(ctx, mintedCoins) if err != nil { return err } @@ -43,7 +51,8 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens") } - ctx.EventManager().EmitEvent( + sdkCtx := sdk.UnwrapSDKContext(ctx) + sdkCtx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeMint, sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()), diff --git a/x/mint/keeper/genesis.go b/x/mint/keeper/genesis.go index ce0d9361b7cf..ed5c567e62bd 100644 --- a/x/mint/keeper/genesis.go +++ b/x/mint/keeper/genesis.go @@ -18,7 +18,15 @@ 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 := keeper.GetMinter(ctx) - params := keeper.GetParams(ctx) + minter, err := keeper.GetMinter(ctx) + if err != nil { + panic(err) + } + + params, err := keeper.GetParams(ctx) + if err != nil { + panic(err) + } + return types.NewGenesisState(minter, params) } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index af777b7d96cc..a142d702f949 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -10,6 +10,7 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" @@ -54,7 +55,7 @@ func (s *GenesisTestSuite) SetupTest() { 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, "", "") + s.keeper = keeper.NewKeeper(s.cdc, runtime.NewKVStoreService(key), stakingKeeper, accountKeeper, bankKeeper, "", "") } func (s *GenesisTestSuite) TestImportExportGenesis() { @@ -71,13 +72,17 @@ func (s *GenesisTestSuite) TestImportExportGenesis() { s.keeper.InitGenesis(s.sdkCtx, s.accountKeeper, genesisState) - minter := s.keeper.GetMinter(s.sdkCtx) + minter, err := s.keeper.GetMinter(s.sdkCtx) s.Require().Equal(genesisState.Minter, minter) + s.Require().NoError(err) invalidCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.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) + _, err = s.keeper.GetMinter(invalidCtx.Ctx) + s.Require().EqualError(err, "stored minter should not have been nil") + + params, err := s.keeper.GetParams(s.sdkCtx) s.Require().Equal(genesisState.Params, params) + s.Require().NoError(err) genesisState2 := s.keeper.ExportGenesis(s.sdkCtx) s.Require().Equal(genesisState, genesisState2) diff --git a/x/mint/keeper/grpc_query.go b/x/mint/keeper/grpc_query.go index 6ad3bf309c7a..46d5f9f42c1f 100644 --- a/x/mint/keeper/grpc_query.go +++ b/x/mint/keeper/grpc_query.go @@ -12,7 +12,10 @@ var _ types.QueryServer = Keeper{} // Params returns params of the mint module. func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { ctx := sdk.UnwrapSDKContext(c) - params := k.GetParams(ctx) + params, err := k.GetParams(ctx) + if err != nil { + return nil, err + } return &types.QueryParamsResponse{Params: params}, nil } @@ -20,7 +23,10 @@ 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) { ctx := sdk.UnwrapSDKContext(c) - minter := k.GetMinter(ctx) + minter, err := k.GetMinter(ctx) + if err != nil { + return nil, err + } return &types.QueryInflationResponse{Inflation: minter.Inflation}, nil } @@ -28,7 +34,10 @@ 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) { ctx := sdk.UnwrapSDKContext(c) - minter := k.GetMinter(ctx) + minter, err := k.GetMinter(ctx) + if err != nil { + return nil, err + } return &types.QueryAnnualProvisionsResponse{AnnualProvisions: minter.AnnualProvisions}, nil } diff --git a/x/mint/keeper/grpc_query_test.go b/x/mint/keeper/grpc_query_test.go index 28a62b664b3b..f49aebd6556d 100644 --- a/x/mint/keeper/grpc_query_test.go +++ b/x/mint/keeper/grpc_query_test.go @@ -10,6 +10,7 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" @@ -32,6 +33,7 @@ type MintTestSuite struct { func (suite *MintTestSuite) SetupTest() { encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{}) key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test")) suite.ctx = testCtx.Ctx @@ -45,7 +47,7 @@ func (suite *MintTestSuite) SetupTest() { suite.mintKeeper = keeper.NewKeeper( encCfg.Codec, - key, + storeService, stakingKeeper, accountKeeper, bankKeeper, @@ -66,15 +68,19 @@ func (suite *MintTestSuite) SetupTest() { func (suite *MintTestSuite) TestGRPCParams() { params, err := suite.queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{}) suite.Require().NoError(err) - suite.Require().Equal(params.Params, suite.mintKeeper.GetParams(suite.ctx)) + kparams, err := suite.mintKeeper.GetParams(suite.ctx) + suite.Require().NoError(err) + suite.Require().Equal(params.Params, kparams) inflation, err := suite.queryClient.Inflation(gocontext.Background(), &types.QueryInflationRequest{}) suite.Require().NoError(err) - suite.Require().Equal(inflation.Inflation, suite.mintKeeper.GetMinter(suite.ctx).Inflation) + minter, err := suite.mintKeeper.GetMinter(suite.ctx) + suite.Require().NoError(err) + suite.Require().Equal(inflation.Inflation, minter.Inflation) annualProvisions, err := suite.queryClient.AnnualProvisions(gocontext.Background(), &types.QueryAnnualProvisionsRequest{}) suite.Require().NoError(err) - suite.Require().Equal(annualProvisions.AnnualProvisions, suite.mintKeeper.GetMinter(suite.ctx).AnnualProvisions) + suite.Require().Equal(annualProvisions.AnnualProvisions, minter.AnnualProvisions) } func TestMintTestSuite(t *testing.T) { diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 56c0664dd90a..1386bcee3ef4 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -1,12 +1,13 @@ package keeper import ( + "context" "fmt" "cosmossdk.io/log" "cosmossdk.io/math" - storetypes "cosmossdk.io/store/types" + storetypes "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,7 +17,7 @@ import ( // Keeper of the mint store type Keeper struct { cdc codec.BinaryCodec - storeKey storetypes.StoreKey + storeService storetypes.KVStoreService stakingKeeper types.StakingKeeper bankKeeper types.BankKeeper feeCollectorName string @@ -29,7 +30,7 @@ type Keeper struct { // NewKeeper creates a new mint Keeper instance func NewKeeper( cdc codec.BinaryCodec, - key storetypes.StoreKey, + storeService storetypes.KVStoreService, sk types.StakingKeeper, ak types.AccountKeeper, bk types.BankKeeper, @@ -43,7 +44,7 @@ func NewKeeper( return Keeper{ cdc: cdc, - storeKey: key, + storeService: storeService, stakingKeeper: sk, bankKeeper: bk, feeCollectorName: feeCollectorName, @@ -57,65 +58,81 @@ func (k Keeper) GetAuthority() string { } // Logger returns a module-specific logger. -func (k Keeper) Logger(ctx sdk.Context) log.Logger { - return ctx.Logger().With("module", "x/"+types.ModuleName) +func (k Keeper) Logger(ctx context.Context) log.Logger { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return sdkCtx.Logger().With("module", "x/"+types.ModuleName) } // GetMinter returns the minter. -func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.MinterKey) +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 { - panic("stored minter should not have been nil") + return minter, fmt.Errorf("stored minter should not have been nil") } - k.cdc.MustUnmarshal(bz, &minter) - return + err = k.cdc.Unmarshal(bz, &minter) + return minter, err } // SetMinter sets the minter. -func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) { - store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshal(&minter) - store.Set(types.MinterKey, bz) +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) } // SetParams sets the x/mint module parameters. -func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { - store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshal(¶ms) - store.Set(types.ParamsKey, bz) - - return nil +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) } // GetParams returns the current x/mint module parameters. -func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ParamsKey) +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 + return p, nil } - k.cdc.MustUnmarshal(bz, &p) - return p + 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 sdk.Context) math.Int { - return k.stakingKeeper.StakingTokenSupply(ctx) +func (k Keeper) StakingTokenSupply(ctx context.Context) math.Int { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return k.stakingKeeper.StakingTokenSupply(sdkCtx) } // BondedRatio implements an alias call to the underlying staking keeper's // BondedRatio to be used in BeginBlocker. -func (k Keeper) BondedRatio(ctx sdk.Context) math.LegacyDec { - return k.stakingKeeper.BondedRatio(ctx) +func (k Keeper) BondedRatio(ctx context.Context) math.LegacyDec { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return k.stakingKeeper.BondedRatio(sdkCtx) } // MintCoins implements an alias call to the underlying supply keeper's // MintCoins to be used in BeginBlocker. -func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error { +func (k Keeper) MintCoins(ctx context.Context, newCoins sdk.Coins) error { if newCoins.Empty() { // skip as no coins need to be minted return nil @@ -126,6 +143,6 @@ func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error { // AddCollectedFees implements an alias call to the underlying supply keeper's // AddCollectedFees to be used in BeginBlocker. -func (k Keeper) AddCollectedFees(ctx sdk.Context, fees sdk.Coins) error { +func (k Keeper) AddCollectedFees(ctx context.Context, fees sdk.Coins) error { return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.feeCollectorName, fees) } diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index ca65cfca2123..a93c6f372f78 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -9,6 +9,7 @@ import ( "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" @@ -37,6 +38,7 @@ func TestKeeperTestSuite(t *testing.T) { func (s *IntegrationTestSuite) SetupTest() { encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{}) key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) s.ctx = testCtx.Ctx @@ -50,7 +52,7 @@ func (s *IntegrationTestSuite) SetupTest() { s.mintKeeper = keeper.NewKeeper( encCfg.Codec, - key, + storeService, stakingKeeper, accountKeeper, bankKeeper, @@ -106,8 +108,9 @@ func (s *IntegrationTestSuite) TestParams() { tc := tc s.Run(tc.name, func() { - expected := s.mintKeeper.GetParams(s.ctx) - err := s.mintKeeper.SetParams(s.ctx, tc.input) + expected, err := s.mintKeeper.GetParams(s.ctx) + s.Require().NoError(err) + err = s.mintKeeper.SetParams(s.ctx, tc.input) if tc.expectErr { s.Require().Error(err) } else { @@ -115,7 +118,8 @@ func (s *IntegrationTestSuite) TestParams() { s.Require().NoError(err) } - p := s.mintKeeper.GetParams(s.ctx) + p, err := s.mintKeeper.GetParams(s.ctx) + s.Require().NoError(err) s.Require().Equal(expected, p) }) } diff --git a/x/mint/keeper/migrator.go b/x/mint/keeper/migrator.go index 78096173ccea..3e85e6edba08 100644 --- a/x/mint/keeper/migrator.go +++ b/x/mint/keeper/migrator.go @@ -25,5 +25,5 @@ func NewMigrator(k Keeper, ss exported.Subspace) Migrator { // and managed by the x/params modules and stores them directly into the x/mint // module state. func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v2.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.legacySubspace, m.keeper.cdc) + return v2.Migrate(ctx, m.keeper.storeService.OpenKVStore(ctx), m.legacySubspace, m.keeper.cdc) } diff --git a/x/mint/migrations/v2/migrate.go b/x/mint/migrations/v2/migrate.go index 98f4eda7f51f..82f267f3c704 100644 --- a/x/mint/migrations/v2/migrate.go +++ b/x/mint/migrations/v2/migrate.go @@ -1,7 +1,7 @@ package v2 import ( - storetypes "cosmossdk.io/store/types" + storetypes "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -33,7 +33,5 @@ func Migrate( } bz := cdc.MustMarshal(&currParams) - store.Set(ParamsKey, bz) - - return nil + return store.Set(ParamsKey, bz) } diff --git a/x/mint/migrations/v2/migrator_test.go b/x/mint/migrations/v2/migrator_test.go index a5adc9317c58..70fd27084a45 100644 --- a/x/mint/migrations/v2/migrator_test.go +++ b/x/mint/migrations/v2/migrator_test.go @@ -7,6 +7,7 @@ import ( storetypes "cosmossdk.io/store/types" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" @@ -35,13 +36,15 @@ func TestMigrate(t *testing.T) { storeKey := storetypes.NewKVStoreKey(v2.ModuleName) tKey := storetypes.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) - store := ctx.KVStore(storeKey) + kvStoreService := runtime.NewKVStoreService(storeKey) + store := kvStoreService.OpenKVStore(ctx) legacySubspace := newMockSubspace(types.DefaultParams()) require.NoError(t, v2.Migrate(ctx, store, legacySubspace, cdc)) var res types.Params - bz := store.Get(v2.ParamsKey) + bz, err := store.Get(v2.ParamsKey) + require.NoError(t, err) require.NoError(t, cdc.Unmarshal(bz, &res)) require.Equal(t, legacySubspace.ps, res) } diff --git a/x/mint/module.go b/x/mint/module.go index ae5bda3dd2cd..7a1fb3c03c4f 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -14,7 +14,7 @@ import ( "cosmossdk.io/depinject" - store "cosmossdk.io/store/types" + "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -180,8 +180,7 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } // BeginBlock returns the begin blocker for the mint module. func (am AppModule) BeginBlock(ctx context.Context) error { - c := sdk.UnwrapSDKContext(ctx) - return BeginBlocker(c, am.keeper, am.inflationCalculator) + return BeginBlocker(ctx, am.keeper, am.inflationCalculator) } // AppModuleSimulation functions @@ -221,7 +220,7 @@ type ModuleInputs struct { ModuleKey depinject.OwnModuleKey Config *modulev1.Module - Key *store.KVStoreKey + StoreService store.KVStoreService Cdc codec.Codec InflationCalculationFn types.InflationCalculationFn `optional:"true"` @@ -254,7 +253,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { k := keeper.NewKeeper( in.Cdc, - in.Key, + in.StoreService, in.StakingKeeper, in.AccountKeeper, in.BankKeeper, diff --git a/x/mint/types/genesis.go b/x/mint/types/genesis.go index dd710b61bc79..696177ff5a4f 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -1,8 +1,9 @@ package types import ( + context "context" + "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" ) // InflationCalculationFn defines the function required to calculate inflation rate during @@ -10,10 +11,10 @@ import ( // bondedRatio and returns the newly calculated inflation rate. // It can be used to specify a custom inflation calculation logic, instead of relying on the // default logic provided by the sdk. -type InflationCalculationFn func(ctx sdk.Context, minter Minter, params Params, bondedRatio math.LegacyDec) math.LegacyDec +type InflationCalculationFn func(ctx context.Context, minter Minter, params Params, bondedRatio math.LegacyDec) math.LegacyDec // DefaultInflationCalculationFn is the default function used to calculate inflation. -func DefaultInflationCalculationFn(_ sdk.Context, minter Minter, params Params, bondedRatio math.LegacyDec) math.LegacyDec { +func DefaultInflationCalculationFn(_ context.Context, minter Minter, params Params, bondedRatio math.LegacyDec) math.LegacyDec { return minter.NextInflationRate(params, bondedRatio) }