Skip to content

Commit

Permalink
refactor: start using KVStoreService for feerefunder module
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockwarr committed Nov 11, 2024
1 parent baf7e4a commit ffe9f9b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
4 changes: 2 additions & 2 deletions testutil/feerefunder/keeper/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down
6 changes: 5 additions & 1 deletion x/feerefunder/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 6 additions & 1 deletion x/feerefunder/keeper/grpc_query_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 19 additions & 15 deletions x/feerefunder/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,25 +24,22 @@ type (
Keeper struct {
cdc codec.BinaryCodec
bankKeeper types.BankKeeper
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
storeService store.KVStoreService
channelKeeper types.ChannelKeeper
authority string
}
)

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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)

Expand All @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions x/feerefunder/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params)
return params
err = k.cdc.Unmarshal(bz, &params)
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(&params)
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion x/feerefunder/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ffe9f9b

Please sign in to comment.