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

This commit is contained in:
Facundo Medica
2023-03-23 17:03:45 +00:00
committed by GitHub
parent 460bd0ad26
commit aeb4f02645
11 changed files with 67 additions and 50 deletions
+21 -18
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))
}
+3 -1
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