refactor!: use KVStoreService in x/consensus (#15517)

This commit is contained in:
Facundo Medica 2023-03-23 14:03:45 -03:00 committed by GitHub
parent 460bd0ad26
commit aeb4f02645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 67 additions and 50 deletions

View File

@ -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.

View File

@ -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

View File

@ -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 {

View File

@ -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.
}

View File

@ -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
}

View File

@ -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, &params); err != nil {
panic(err)
return nil, err
}
return &params, nil

View File

@ -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

View File

@ -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
}
)

View File

@ -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)
bz, err := store.Get(types.ParamStoreKeyConsensusParams)
if err != nil {
return nil, err
}
cp := &cmtproto.ConsensusParams{}
bz := store.Get(types.ParamStoreKeyConsensusParams)
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))
}

View File

@ -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

View File

@ -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)