diff --git a/CHANGELOG.md b/CHANGELOG.md index 79b22bfe139e..da675eba7f17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -130,6 +130,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (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. * (x/authz) [#15962](https://github.com/cosmos/cosmos-sdk/issues/15962) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`. The `Authorization` interface's `Accept` method now takes a `context.Context` instead of a `sdk.Context`. diff --git a/UPGRADING.md b/UPGRADING.md index 9985c196e936..0e1a81416a00 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -77,6 +77,7 @@ The following modules `NewKeeper` function now take a `KVStoreService` instead o * `x/authz` * `x/bank` * `x/consensus` +* `x/crisis` * `x/distribution` * `x/evidence` * `x/feegrant` @@ -98,6 +99,7 @@ The following modules' `Keeper` methods now take in a `context.Context` instead * `x/authz` * `x/bank` +* `x/crisis` * `x/distribution` * `x/evidence` * `x/gov` diff --git a/simapp/app.go b/simapp/app.go index 195d2d0dac11..636b0f17ed31 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -40,6 +40,7 @@ import ( "cosmossdk.io/x/circuit" circuitkeeper "cosmossdk.io/x/circuit/keeper" circuittypes "cosmossdk.io/x/circuit/types" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -289,7 +290,7 @@ func NewSimApp( ) invCheckPeriod := cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)) - app.CrisisKeeper = crisiskeeper.NewKeeper(appCodec, keys[crisistypes.StoreKey], invCheckPeriod, + app.CrisisKeeper = crisiskeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[crisistypes.StoreKey]), invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AccountKeeper.GetAddressCodec()) app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[feegrant.StoreKey]), app.AccountKeeper) diff --git a/x/crisis/abci.go b/x/crisis/abci.go index fa1b932b8f63..8ce275ecd799 100644 --- a/x/crisis/abci.go +++ b/x/crisis/abci.go @@ -1,6 +1,7 @@ package crisis import ( + "context" "time" "github.com/cosmos/cosmos-sdk/telemetry" @@ -10,12 +11,13 @@ import ( ) // check all registered invariants -func EndBlocker(ctx sdk.Context, k keeper.Keeper) { +func EndBlocker(ctx context.Context, k keeper.Keeper) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) - if k.InvCheckPeriod() == 0 || ctx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 { + sdkCtx := sdk.UnwrapSDKContext(ctx) + if k.InvCheckPeriod() == 0 || sdkCtx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 { // skip running the invariant check return } - k.AssertInvariants(ctx) + k.AssertInvariants(sdkCtx) } diff --git a/x/crisis/keeper/genesis.go b/x/crisis/keeper/genesis.go index c0249989a443..f44657736b0c 100644 --- a/x/crisis/keeper/genesis.go +++ b/x/crisis/keeper/genesis.go @@ -14,6 +14,9 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) { // ExportGenesis returns a GenesisState for a given context and keeper. func (k *Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { - constantFee := k.GetConstantFee(ctx) + constantFee, err := k.GetConstantFee(ctx) + if err != nil { + panic(err) + } return types.NewGenesisState(constantFee) } diff --git a/x/crisis/keeper/genesis_test.go b/x/crisis/keeper/genesis_test.go index 475e712f2629..ba1e0ef6ba31 100644 --- a/x/crisis/keeper/genesis_test.go +++ b/x/crisis/keeper/genesis_test.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" + "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" @@ -34,6 +35,7 @@ func TestGenesisTestSuite(t *testing.T) { func (s *GenesisTestSuite) SetupTest() { key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}) @@ -44,7 +46,7 @@ func (s *GenesisTestSuite) SetupTest() { supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl) - s.keeper = *keeper.NewKeeper(s.cdc, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) + s.keeper = *keeper.NewKeeper(s.cdc, storeService, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) } func (s *GenesisTestSuite) TestImportExportGenesis() { @@ -69,6 +71,7 @@ func (s *GenesisTestSuite) TestInitGenesis() { genesisState.ConstantFee = sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1000)) s.keeper.InitGenesis(s.sdkCtx, genesisState) - constantFee := s.keeper.GetConstantFee(s.sdkCtx) + constantFee, err := s.keeper.GetConstantFee(s.sdkCtx) + s.Require().NoError(err) s.Require().Equal(genesisState.ConstantFee, constantFee) } diff --git a/x/crisis/keeper/keeper.go b/x/crisis/keeper/keeper.go index e987f813e660..da9d9fa911ed 100644 --- a/x/crisis/keeper/keeper.go +++ b/x/crisis/keeper/keeper.go @@ -1,13 +1,14 @@ package keeper import ( + "context" "fmt" "time" "cosmossdk.io/core/address" "cosmossdk.io/log" - storetypes "cosmossdk.io/store/types" + storetypes "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -18,7 +19,7 @@ import ( type Keeper struct { routes []types.InvarRoute invCheckPeriod uint - storeKey storetypes.StoreKey + storeService storetypes.KVStoreService cdc codec.BinaryCodec // the address capable of executing a MsgUpdateParams message. Typically, this @@ -34,11 +35,11 @@ type Keeper struct { // NewKeeper creates a new Keeper object func NewKeeper( - cdc codec.BinaryCodec, storeKey storetypes.StoreKey, invCheckPeriod uint, + cdc codec.BinaryCodec, storeService storetypes.KVStoreService, invCheckPeriod uint, supplyKeeper types.SupplyKeeper, feeCollectorName, authority string, ac address.Codec, ) *Keeper { return &Keeper{ - storeKey: storeKey, + storeService: storeService, cdc: cdc, routes: make([]types.InvarRoute, 0), invCheckPeriod: invCheckPeriod, @@ -55,8 +56,9 @@ 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) } // RegisterRoute register the routes for each of the invariants @@ -108,6 +110,6 @@ func (k *Keeper) AssertInvariants(ctx sdk.Context) { func (k *Keeper) InvCheckPeriod() uint { return k.invCheckPeriod } // SendCoinsFromAccountToFeeCollector transfers amt to the fee collector account. -func (k *Keeper) SendCoinsFromAccountToFeeCollector(ctx sdk.Context, senderAddr sdk.AccAddress, amt sdk.Coins) error { +func (k *Keeper) SendCoinsFromAccountToFeeCollector(ctx context.Context, senderAddr sdk.AccAddress, amt sdk.Coins) error { return k.supplyKeeper.SendCoinsFromAccountToModule(ctx, senderAddr, k.feeCollectorName, amt) } diff --git a/x/crisis/keeper/keeper_test.go b/x/crisis/keeper/keeper_test.go index 1a1eaa38cda6..d958ef52af7b 100644 --- a/x/crisis/keeper/keeper_test.go +++ b/x/crisis/keeper/keeper_test.go @@ -9,6 +9,7 @@ import ( storetypes "cosmossdk.io/store/types" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" + "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" @@ -23,9 +24,10 @@ func TestLogger(t *testing.T) { supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl) key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}) - keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) + keeper := keeper.NewKeeper(encCfg.Codec, storeService, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) require.Equal(t, testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName), @@ -37,8 +39,9 @@ func TestInvariants(t *testing.T) { supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl) key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}) - keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) + keeper := keeper.NewKeeper(encCfg.Codec, storeService, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) require.Equal(t, keeper.InvCheckPeriod(), uint(5)) orgInvRoutes := keeper.Routes() @@ -52,9 +55,10 @@ func TestAssertInvariants(t *testing.T) { supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl) key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}) - keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) + keeper := keeper.NewKeeper(encCfg.Codec, storeService, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos")) keeper.RegisterRoute("testModule", "testRoute1", func(sdk.Context) (string, bool) { return "", false }) require.NotPanics(t, func() { keeper.AssertInvariants(testCtx.Ctx) }) diff --git a/x/crisis/keeper/migrator.go b/x/crisis/keeper/migrator.go index fa962e4db035..f3b692bebd0f 100644 --- a/x/crisis/keeper/migrator.go +++ b/x/crisis/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/crisis // module state. func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v2.MigrateStore(ctx, m.keeper.storeKey, m.legacySubspace, m.keeper.cdc) + return v2.MigrateStore(ctx, m.keeper.storeService, m.legacySubspace, m.keeper.cdc) } diff --git a/x/crisis/keeper/msg_server.go b/x/crisis/keeper/msg_server.go index c0db5a46e167..306d07656322 100644 --- a/x/crisis/keeper/msg_server.go +++ b/x/crisis/keeper/msg_server.go @@ -25,7 +25,11 @@ func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInva } ctx := sdk.UnwrapSDKContext(goCtx) - constantFee := sdk.NewCoins(k.GetConstantFee(ctx)) + params, err := k.GetConstantFee(ctx) + if err != nil { + return nil, err + } + constantFee := sdk.NewCoins(params) if err := k.SendCoinsFromAccountToFeeCollector(ctx, sender, constantFee); err != nil { return nil, err diff --git a/x/crisis/keeper/msg_server_test.go b/x/crisis/keeper/msg_server_test.go index 1bcedd606367..1f8ab50bf5af 100644 --- a/x/crisis/keeper/msg_server_test.go +++ b/x/crisis/keeper/msg_server_test.go @@ -11,6 +11,7 @@ import ( addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/crypto/keyring" + "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" @@ -34,9 +35,10 @@ func (s *KeeperTestSuite) SetupTest() { supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl) key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}) - keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", sdk.AccAddress([]byte("addr1_______________")).String(), addresscodec.NewBech32Codec("cosmos")) + keeper := keeper.NewKeeper(encCfg.Codec, storeService, 5, supplyKeeper, "", sdk.AccAddress([]byte("addr1_______________")).String(), addresscodec.NewBech32Codec("cosmos")) s.ctx = testCtx.Ctx s.keeper = keeper diff --git a/x/crisis/keeper/params.go b/x/crisis/keeper/params.go index 4724d9a6a306..fb3c83ede799 100644 --- a/x/crisis/keeper/params.go +++ b/x/crisis/keeper/params.go @@ -1,6 +1,8 @@ package keeper import ( + "context" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" @@ -9,28 +11,27 @@ import ( ) // GetConstantFee get's the constant fee from the store -func (k *Keeper) GetConstantFee(ctx sdk.Context) (constantFee sdk.Coin) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ConstantFeeKey) - if bz == nil { - return constantFee +func (k *Keeper) GetConstantFee(ctx context.Context) (constantFee sdk.Coin, err error) { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ConstantFeeKey) + if bz == nil || err != nil { + return constantFee, err } - k.cdc.MustUnmarshal(bz, &constantFee) - return constantFee + err = k.cdc.Unmarshal(bz, &constantFee) + return constantFee, err } // GetConstantFee set's the constant fee in the store -func (k *Keeper) SetConstantFee(ctx sdk.Context, constantFee sdk.Coin) error { +func (k *Keeper) SetConstantFee(ctx context.Context, constantFee sdk.Coin) error { if !constantFee.IsValid() || constantFee.IsNegative() { return errorsmod.Wrapf(errors.ErrInvalidCoins, "negative or invalid constant fee: %s", constantFee) } - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := k.cdc.Marshal(&constantFee) if err != nil { return err } - store.Set(types.ConstantFeeKey, bz) - return nil + return store.Set(types.ConstantFeeKey, bz) } diff --git a/x/crisis/keeper/params_test.go b/x/crisis/keeper/params_test.go index 2696c7ec74d4..9b24be22ab4a 100644 --- a/x/crisis/keeper/params_test.go +++ b/x/crisis/keeper/params_test.go @@ -36,8 +36,9 @@ func (s *KeeperTestSuite) TestParams() { for _, tc := range testCases { tc := tc s.Run(tc.name, func() { - expected := s.keeper.GetConstantFee(s.ctx) - err := s.keeper.SetConstantFee(s.ctx, tc.constantFee) + expected, err := s.keeper.GetConstantFee(s.ctx) + s.Require().NoError(err) + err = s.keeper.SetConstantFee(s.ctx, tc.constantFee) if tc.expErr { s.Require().Error(err) @@ -47,7 +48,8 @@ func (s *KeeperTestSuite) TestParams() { s.Require().NoError(err) } - params := s.keeper.GetConstantFee(s.ctx) + params, err := s.keeper.GetConstantFee(s.ctx) + s.Require().NoError(err) s.Require().Equal(expected, params) }) } diff --git a/x/crisis/migrations/v2/migrate.go b/x/crisis/migrations/v2/migrate.go index 78b19be5a8cf..47a34433b07a 100644 --- a/x/crisis/migrations/v2/migrate.go +++ b/x/crisis/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" @@ -22,8 +22,8 @@ var ( // version 2. Specifically, it takes the `ConstantFee` parameter that is currently stored // and managed by the x/params module and stores it directly into the x/crisis // module state. -func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error { - store := ctx.KVStore(storeKey) +func MigrateStore(ctx sdk.Context, storeService storetypes.KVStoreService, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error { + store := storeService.OpenKVStore(ctx) var currConstantFee sdk.Coin legacySubspace.Get(ctx, ConstantFee, &currConstantFee) @@ -36,7 +36,5 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, legacySubspace return err } - store.Set(ConstantFeeKey, bz) - - return nil + return store.Set(ConstantFeeKey, bz) } diff --git a/x/crisis/migrations/v2/migrate_test.go b/x/crisis/migrations/v2/migrate_test.go index a5357db3baa1..c1dddc05a53b 100644 --- a/x/crisis/migrations/v2/migrate_test.go +++ b/x/crisis/migrations/v2/migrate_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" @@ -30,12 +31,13 @@ func (ms mockSubspace) Get(ctx sdk.Context, key []byte, ptr interface{}) { func TestMigrate(t *testing.T) { cdc := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{}).Codec storeKey := storetypes.NewKVStoreKey(v2.ModuleName) + storeService := runtime.NewKVStoreService(storeKey) tKey := storetypes.NewTransientStoreKey("transient_test") ctx := testutil.DefaultContext(storeKey, tKey) store := ctx.KVStore(storeKey) legacySubspace := newMockSubspace(types.DefaultGenesisState().ConstantFee) - require.NoError(t, v2.MigrateStore(ctx, storeKey, legacySubspace, cdc)) + require.NoError(t, v2.MigrateStore(ctx, storeService, legacySubspace, cdc)) var res sdk.Coin bz := store.Get(v2.ConstantFeeKey) diff --git a/x/crisis/module.go b/x/crisis/module.go index a19419ee7b7e..25017566888e 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -14,10 +14,9 @@ import ( modulev1 "cosmossdk.io/api/cosmos/crisis/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" "cosmossdk.io/depinject" - store "cosmossdk.io/store/types" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -178,8 +177,7 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } // EndBlock returns the end blocker for the crisis module. It returns no validator // updates. func (am AppModule) EndBlock(ctx context.Context) error { - c := sdk.UnwrapSDKContext(ctx) - EndBlocker(c, *am.keeper) + EndBlocker(ctx, *am.keeper) return nil } @@ -195,10 +193,10 @@ func init() { type ModuleInputs struct { depinject.In - Config *modulev1.Module - Key *store.KVStoreKey - Cdc codec.Codec - AppOpts servertypes.AppOptions `optional:"true"` + Config *modulev1.Module + StoreService store.KVStoreService + Cdc codec.Codec + AppOpts servertypes.AppOptions `optional:"true"` BankKeeper types.SupplyKeeper AddressCodec address.Codec @@ -233,7 +231,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { k := keeper.NewKeeper( in.Cdc, - in.Key, + in.StoreService, invalidCheckPeriod, in.BankKeeper, feeCollectorName,