diff --git a/CHANGELOG.md b/CHANGELOG.md index c439ec69a2df..a6fec52e63c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,6 +102,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/consensus) [#15517](https://github.com/cosmos/cosmos-sdk/pull/15517) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`. * (x/bank) [#15477](https://github.com/cosmos/cosmos-sdk/pull/15477) `banktypes.NewMsgMultiSend` and `keeper.InputOutputCoins` only accept one input. * (mempool) [#15328](https://github.com/cosmos/cosmos-sdk/pull/15328) The `PriorityNonceMempool` is now generic over type `C comparable` and takes a single `PriorityNonceMempoolConfig[C]` argument. See `DefaultPriorityNonceMempoolConfig` for how to construct the configuration and a `TxPriority` type. * (server) [#15358](https://github.com/cosmos/cosmos-sdk/pull/15358) Remove `server.ErrorCode` that was not used anywhere. diff --git a/UPGRADING.md b/UPGRADING.md index aa57db85f8e3..19da06f04ccd 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -79,6 +79,15 @@ All the store imports are now renamed to use `cosmossdk.io/store` instead of `gi Capability was moved to [IBC-GO](https://github.com/cosmos/ibc-go). IBC V8 will contain the necessary changes to incorporate the new module location +#### `x/consensus` + +The `NewKeeper` method now takes a `KVStoreService` instead of a `StoreKey`. When not using depinject, the `runtime.NewKVStoreService` method can be used to create a `KVStoreService` from a `StoreKey`. + +```diff +- app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamtypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String()) ++ app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String()) +``` + #### `x/gov` ##### Expedited Proposals diff --git a/baseapp/abci.go b/baseapp/abci.go index dc89c364c12d..09c22b6e3a3e 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -66,7 +66,10 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC // done after the deliver state and context have been set as it's persisted // to state. if req.ConsensusParams != nil { - app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams) + err := app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams) + if err != nil { + panic(err) + } } if app.initChainer == nil { diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 5cff5a206b2c..1b3f627034a9 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -448,16 +448,16 @@ func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *cmtproto.ConsensusParam } // StoreConsensusParams sets the consensus parameters to the baseapp's param store. -func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *cmtproto.ConsensusParams) { +func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *cmtproto.ConsensusParams) error { if app.paramStore == nil { panic("cannot store consensus params with no params store set") } if cp == nil { - return + return nil } - app.paramStore.Set(ctx, cp) + return app.paramStore.Set(ctx, cp) // We're explicitly not storing the CometBFT app_version in the param store. It's // stored instead in the x/upgrade store, with its own bump logic. } diff --git a/baseapp/params.go b/baseapp/params.go index 1ac07da3668e..35628f5870dc 100644 --- a/baseapp/params.go +++ b/baseapp/params.go @@ -1,15 +1,15 @@ package baseapp import ( - cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + "context" - sdk "github.com/cosmos/cosmos-sdk/types" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" ) // ParamStore defines the interface the parameter store used by the BaseApp must // fulfill. type ParamStore interface { - Get(ctx sdk.Context) (*cmtproto.ConsensusParams, error) - Has(ctx sdk.Context) bool - Set(ctx sdk.Context, cp *cmtproto.ConsensusParams) + Get(ctx context.Context) (*cmtproto.ConsensusParams, error) + Has(ctx context.Context) (bool, error) + Set(ctx context.Context, cp *cmtproto.ConsensusParams) error } diff --git a/baseapp/utils_test.go b/baseapp/utils_test.go index 7184506895f8..385184af5e77 100644 --- a/baseapp/utils_test.go +++ b/baseapp/utils_test.go @@ -239,28 +239,25 @@ type paramStore struct { db *dbm.MemDB } -func (ps *paramStore) Set(_ sdk.Context, value *cmtproto.ConsensusParams) { +var _ baseapp.ParamStore = (*paramStore)(nil) + +func (ps *paramStore) Set(_ context.Context, value *cmtproto.ConsensusParams) error { bz, err := json.Marshal(value) if err != nil { - panic(err) + return err } - ps.db.Set(ParamStoreKey, bz) + return ps.db.Set(ParamStoreKey, bz) } -func (ps *paramStore) Has(_ sdk.Context) bool { - ok, err := ps.db.Has(ParamStoreKey) - if err != nil { - panic(err) - } - - return ok +func (ps *paramStore) Has(_ context.Context) (bool, error) { + return ps.db.Has(ParamStoreKey) } -func (ps paramStore) Get(ctx sdk.Context) (*cmtproto.ConsensusParams, error) { +func (ps paramStore) Get(ctx context.Context) (*cmtproto.ConsensusParams, error) { bz, err := ps.db.Get(ParamStoreKey) if err != nil { - panic(err) + return nil, err } if len(bz) == 0 { @@ -269,7 +266,7 @@ func (ps paramStore) Get(ctx sdk.Context) (*cmtproto.ConsensusParams, error) { var params cmtproto.ConsensusParams if err := json.Unmarshal(bz, ¶ms); err != nil { - panic(err) + return nil, err } return ¶ms, nil diff --git a/simapp/app.go b/simapp/app.go index 74ba81ee18a0..61ff5568319d 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -285,7 +285,7 @@ func NewSimApp( app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey]) // set the BaseApp's parameter store - app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamtypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String()) + app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String()) bApp.SetParamStore(&app.ConsensusParamsKeeper) // add keepers diff --git a/x/consensus/exported/exported.go b/x/consensus/exported/exported.go index 1e0a138e95a7..7baa5b1a6d5a 100644 --- a/x/consensus/exported/exported.go +++ b/x/consensus/exported/exported.go @@ -1,6 +1,8 @@ package exported import ( + "context" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -18,8 +20,8 @@ type ( // ConsensusParamSetter defines the interface fulfilled by BaseApp's // ParamStore which allows setting its appVersion field. ConsensusParamSetter interface { - Get(ctx sdk.Context) (*cmtproto.ConsensusParams, error) - Has(ctx sdk.Context) bool - Set(ctx sdk.Context, cp *cmtproto.ConsensusParams) + Get(ctx context.Context) (*cmtproto.ConsensusParams, error) + Has(ctx context.Context) (bool, error) + Set(ctx context.Context, cp *cmtproto.ConsensusParams) error } ) diff --git a/x/consensus/keeper/keeper.go b/x/consensus/keeper/keeper.go index 2702f49abc91..d2e9efef6fd3 100644 --- a/x/consensus/keeper/keeper.go +++ b/x/consensus/keeper/keeper.go @@ -1,12 +1,13 @@ package keeper import ( + "context" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" - storetypes "cosmossdk.io/store/types" + storetypes "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/consensus/exported" "github.com/cosmos/cosmos-sdk/x/consensus/types" ) @@ -14,17 +15,17 @@ import ( var _ exported.ConsensusParamSetter = (*Keeper)(nil) type Keeper struct { - storeKey storetypes.StoreKey - cdc codec.BinaryCodec + storeService storetypes.KVStoreService + cdc codec.BinaryCodec authority string } -func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, authority string) Keeper { +func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string) Keeper { return Keeper{ - storeKey: storeKey, - cdc: cdc, - authority: authority, + storeService: storeService, + cdc: cdc, + authority: authority, } } @@ -33,12 +34,15 @@ func (k *Keeper) GetAuthority() string { } // Get gets the consensus parameters -func (k *Keeper) Get(ctx sdk.Context) (*cmtproto.ConsensusParams, error) { - store := ctx.KVStore(k.storeKey) +func (k *Keeper) Get(ctx context.Context) (*cmtproto.ConsensusParams, error) { + store := k.storeService.OpenKVStore(ctx) - cp := &cmtproto.ConsensusParams{} - bz := store.Get(types.ParamStoreKeyConsensusParams) + bz, err := store.Get(types.ParamStoreKeyConsensusParams) + if err != nil { + return nil, err + } + cp := &cmtproto.ConsensusParams{} if err := k.cdc.Unmarshal(bz, cp); err != nil { return nil, err } @@ -46,14 +50,13 @@ func (k *Keeper) Get(ctx sdk.Context) (*cmtproto.ConsensusParams, error) { return cp, nil } -func (k *Keeper) Has(ctx sdk.Context) bool { - store := ctx.KVStore(k.storeKey) - +func (k *Keeper) Has(ctx context.Context) (bool, error) { + store := k.storeService.OpenKVStore(ctx) return store.Has(types.ParamStoreKeyConsensusParams) } // Set sets the consensus parameters -func (k *Keeper) Set(ctx sdk.Context, cp *cmtproto.ConsensusParams) { - store := ctx.KVStore(k.storeKey) - store.Set(types.ParamStoreKeyConsensusParams, k.cdc.MustMarshal(cp)) +func (k *Keeper) Set(ctx context.Context, cp *cmtproto.ConsensusParams) error { + store := k.storeService.OpenKVStore(ctx) + return store.Set(types.ParamStoreKeyConsensusParams, k.cdc.MustMarshal(cp)) } diff --git a/x/consensus/keeper/keeper_test.go b/x/consensus/keeper/keeper_test.go index e7d4dc1ed44b..2ac0108c3581 100644 --- a/x/consensus/keeper/keeper_test.go +++ b/x/consensus/keeper/keeper_test.go @@ -9,6 +9,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,8 +33,9 @@ func (s *KeeperTestSuite) SetupTest() { testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{}) encCfg := moduletestutil.MakeTestEncodingConfig() + storeService := runtime.NewKVStoreService(key) - keeper := consensusparamkeeper.NewKeeper(encCfg.Codec, key, authtypes.NewModuleAddress(govtypes.ModuleName).String()) + keeper := consensusparamkeeper.NewKeeper(encCfg.Codec, storeService, authtypes.NewModuleAddress(govtypes.ModuleName).String()) s.ctx = ctx s.consensusParamsKeeper = &keeper diff --git a/x/consensus/module.go b/x/consensus/module.go index 3dd825782ed1..5cf7693c346c 100644 --- a/x/consensus/module.go +++ b/x/consensus/module.go @@ -11,7 +11,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - store "cosmossdk.io/store/types" + storetypes "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" @@ -139,9 +139,9 @@ func init() { type ConsensusInputs struct { depinject.In - Config *modulev1.Module - Cdc codec.Codec - Key *store.KVStoreKey + Config *modulev1.Module + Cdc codec.Codec + StoreService storetypes.KVStoreService } //nolint:revive @@ -160,7 +160,7 @@ func ProvideModule(in ConsensusInputs) ConsensusOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } - k := keeper.NewKeeper(in.Cdc, in.Key, authority.String()) + k := keeper.NewKeeper(in.Cdc, in.StoreService, authority.String()) m := NewAppModule(in.Cdc, k) baseappOpt := func(app *baseapp.BaseApp) { app.SetParamStore(&k)