From ffe9f9b5c6cc8224d584c862c09206d42214067f Mon Sep 17 00:00:00 2001 From: Lockwarr Date: Mon, 4 Nov 2024 17:59:52 +0200 Subject: [PATCH] refactor: start using KVStoreService for feerefunder module --- testutil/feerefunder/keeper/fee.go | 4 +-- x/feerefunder/genesis.go | 6 +++- x/feerefunder/keeper/grpc_query_params.go | 7 ++++- x/feerefunder/keeper/keeper.go | 34 +++++++++++++---------- x/feerefunder/keeper/params.go | 17 +++++++----- x/feerefunder/keeper/params_test.go | 6 +++- 6 files changed, 47 insertions(+), 27 deletions(-) diff --git a/testutil/feerefunder/keeper/fee.go b/testutil/feerefunder/keeper/fee.go index a9084831..8f6ab606 100644 --- a/testutil/feerefunder/keeper/fee.go +++ b/testutil/feerefunder/keeper/fee.go @@ -12,6 +12,7 @@ import ( db2 "github.com/cosmos/cosmos-db" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/stretchr/testify/require" @@ -35,8 +36,7 @@ func FeeKeeper(t testing.TB, channelKeeper types.ChannelKeeper, bankKeeper types k := keeper.NewKeeper( cdc, - storeKey, - memStoreKey, + runtime.NewKVStoreService(storeKey), channelKeeper, bankKeeper, authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(), diff --git a/x/feerefunder/genesis.go b/x/feerefunder/genesis.go index eb0aea28..9a43c6c4 100644 --- a/x/feerefunder/genesis.go +++ b/x/feerefunder/genesis.go @@ -22,8 +22,12 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) // ExportGenesis returns the module's exported genesis. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { + var err error genesis := types.DefaultGenesis() - genesis.Params = k.GetParams(ctx) + genesis.Params, err = k.GetParams(ctx) + if err != nil { + panic(err) + } genesis.FeeInfos = k.GetAllFeeInfos(ctx) diff --git a/x/feerefunder/keeper/grpc_query_params.go b/x/feerefunder/keeper/grpc_query_params.go index bb116159..45671e0e 100644 --- a/x/feerefunder/keeper/grpc_query_params.go +++ b/x/feerefunder/keeper/grpc_query_params.go @@ -16,5 +16,10 @@ func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types } ctx := sdk.UnwrapSDKContext(c) - return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil + params, err := k.GetParams(ctx) + if err != nil { + return nil, status.Error(codes.NotFound, err.Error()) + } + + return &types.QueryParamsResponse{Params: params}, err } diff --git a/x/feerefunder/keeper/keeper.go b/x/feerefunder/keeper/keeper.go index 4cb2cddc..81d7f227 100644 --- a/x/feerefunder/keeper/keeper.go +++ b/x/feerefunder/keeper/keeper.go @@ -5,12 +5,14 @@ import ( "fmt" "strconv" + "cosmossdk.io/core/store" "cosmossdk.io/errors" "cosmossdk.io/log" "cosmossdk.io/store/prefix" storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" @@ -22,8 +24,7 @@ type ( Keeper struct { cdc codec.BinaryCodec bankKeeper types.BankKeeper - storeKey storetypes.StoreKey - memKey storetypes.StoreKey + storeService store.KVStoreService channelKeeper types.ChannelKeeper authority string } @@ -31,16 +32,14 @@ type ( func NewKeeper( cdc codec.BinaryCodec, - storeKey, - memKey storetypes.StoreKey, + storeService store.KVStoreService, channelKeeper types.ChannelKeeper, bankKeeper types.BankKeeper, authority string, ) *Keeper { return &Keeper{ cdc: cdc, - storeKey: storeKey, - memKey: memKey, + storeService: storeService, channelKeeper: channelKeeper, bankKeeper: bankKeeper, authority: authority, @@ -51,8 +50,9 @@ func (k Keeper) GetAuthority() string { return k.authority } -func (k Keeper) Logger(ctx sdk.Context) log.Logger { - return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +func (k Keeper) Logger(ctx context.Context) log.Logger { + c := sdk.UnwrapSDKContext(ctx) + return c.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } func (k Keeper) LockFees(ctx context.Context, payer sdk.AccAddress, packetID types.PacketID, fee types.Fee) error { @@ -175,10 +175,14 @@ func (k Keeper) DistributeTimeoutFee(ctx context.Context, receiver sdk.AccAddres } func (k Keeper) GetFeeInfo(ctx sdk.Context, packetID types.PacketID) (*types.FeeInfo, error) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) var feeInfo types.FeeInfo - bzFeeInfo := store.Get(types.GetFeePacketKey(packetID)) + bzFeeInfo, err := store.Get(types.GetFeePacketKey(packetID)) + if err != nil { + panic(err) + } + if bzFeeInfo == nil { return nil, errors.Wrapf(sdkerrors.ErrKeyNotFound, "no fee info for the given channelID = %s, portID = %s and sequence = %d", packetID.ChannelId, packetID.PortId, packetID.Sequence) } @@ -188,7 +192,7 @@ func (k Keeper) GetFeeInfo(ctx sdk.Context, packetID types.PacketID) (*types.Fee } func (k Keeper) GetAllFeeInfos(ctx sdk.Context) []types.FeeInfo { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FeeKey) + store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.FeeKey) infos := make([]types.FeeInfo, 0) @@ -205,25 +209,25 @@ func (k Keeper) GetAllFeeInfos(ctx sdk.Context) []types.FeeInfo { } func (k Keeper) StoreFeeInfo(ctx sdk.Context, feeInfo types.FeeInfo) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bzFeeInfo := k.cdc.MustMarshal(&feeInfo) store.Set(types.GetFeePacketKey(feeInfo.PacketId), bzFeeInfo) } func (k Keeper) GetMinFee(ctx sdk.Context) types.Fee { - params := k.GetParams(ctx) + params, _ := k.GetParams(ctx) return params.GetMinFee() } func (k Keeper) removeFeeInfo(ctx sdk.Context, packetID types.PacketID) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.GetFeePacketKey(packetID)) } func (k Keeper) checkFees(ctx sdk.Context, fees types.Fee) error { - params := k.GetParams(ctx) + params, _ := k.GetParams(ctx) if !fees.TimeoutFee.IsAnyGTE(params.MinFee.TimeoutFee) { return errors.Wrapf(sdkerrors.ErrInsufficientFee, "provided timeout fee is less than min governance set timeout fee: %v < %v", fees.TimeoutFee, params.MinFee.TimeoutFee) diff --git a/x/feerefunder/keeper/params.go b/x/feerefunder/keeper/params.go index 8743b843..adef02cb 100644 --- a/x/feerefunder/keeper/params.go +++ b/x/feerefunder/keeper/params.go @@ -7,20 +7,23 @@ import ( ) // GetParams get all parameters as types.Params. -func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ParamsKey) +func (k Keeper) GetParams(ctx sdk.Context) (params types.Params, err error) { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ParamsKey) + if err != nil { + return params, err + } if bz == nil { - return params + return params, nil } - k.cdc.MustUnmarshal(bz, ¶ms) - return params + err = k.cdc.Unmarshal(bz, ¶ms) + return params, err } // SetParams set the params. func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := k.cdc.Marshal(¶ms) if err != nil { return err diff --git a/x/feerefunder/keeper/params_test.go b/x/feerefunder/keeper/params_test.go index 5ea363ef..aa1a5dcc 100644 --- a/x/feerefunder/keeper/params_test.go +++ b/x/feerefunder/keeper/params_test.go @@ -19,5 +19,9 @@ func TestGetParams(t *testing.T) { panic(err) } - require.EqualValues(t, params, k.GetParams(ctx)) + keeperParams, err := k.GetParams(ctx) + if err != nil { + panic(err) + } + require.EqualValues(t, params, keeperParams) }