Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Switch to collections in the ibcratelimit module [#2493](https://github.com/provenance-io/provenance/issues/2493).
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func New(
app.IbcHooks,
)

rateLimitingKeeper := ibcratelimitkeeper.NewKeeper(appCodec, keys[ibcratelimit.StoreKey], nil)
rateLimitingKeeper := ibcratelimitkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[ibcratelimit.StoreKey]), nil)
app.RateLimitingKeeper = &rateLimitingKeeper

// Create Transfer Keepers
Expand Down
5 changes: 2 additions & 3 deletions testutil/ibc/testchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ func (chain *TestChain) RegisterRateLimiterContract(suite *suite.Suite, addr []b
addrStr, err := sdk.Bech32ifyAddressBytes("cosmos", addr)
suite.Require().NoError(err)
provenanceApp := chain.GetProvenanceApp()
provenanceApp.RateLimitingKeeper.SetParams(chain.GetContext(), ibcratelimit.Params{
ContractAddress: addrStr,
})
err = provenanceApp.RateLimitingKeeper.SetParams(chain.GetContext(), ibcratelimit.Params{ContractAddress: addrStr})
suite.Require().NoError(err, "failed to set rate limiter contract params")
}

// SendMsgsNoCheck is an alternative to ibctesting.TestChain.SendMsgs so that it doesn't check for errors. That should be handled by the caller
Expand Down
6 changes: 5 additions & 1 deletion x/ibcratelimit/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/provenance-io/provenance/x/ibcratelimit"
Expand All @@ -23,5 +25,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *ibcratelimit.GenesisState) {
if err := data.Validate(); err != nil {
panic(err)
}
k.SetParams(ctx, data.Params)
if err := k.SetParams(ctx, data.Params); err != nil {
panic(fmt.Errorf("failed to set ibcratelimit params during InitGenesis: %w", err))
}
Comment thread
nagarajdivine marked this conversation as resolved.
}
54 changes: 38 additions & 16 deletions x/ibcratelimit/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package keeper

import (
"errors"
"fmt"

"cosmossdk.io/collections"
"cosmossdk.io/core/store"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -15,24 +19,44 @@ import (

// Keeper for the ibcratelimit module
type Keeper struct {
storeKey storetypes.StoreKey
storeService store.KVStoreService
cdc codec.BinaryCodec
PermissionedKeeper ibcratelimit.PermissionedKeeper
authority string
// Schema is the collections schema for this module's store.
Schema collections.Schema

// Params is the single-item collection storing the module parameters.
ParamsItem collections.Item[ibcratelimit.Params]
}

// NewKeeper Creates a new Keeper for the module.
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
storeService store.KVStoreService,
permissionedKeeper ibcratelimit.PermissionedKeeper,
) Keeper {
return Keeper{
storeKey: key,
sb := collections.NewSchemaBuilder(storeService)
k := Keeper{
cdc: cdc,
storeService: storeService,
PermissionedKeeper: permissionedKeeper,
authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
ParamsItem: collections.NewItem(
sb,
ibcratelimit.ParamsKeyPrefix,
"params",
codec.CollValue[ibcratelimit.Params](cdc),
),
}

var err error
k.Schema, err = sb.Build()
if err != nil {
panic(fmt.Errorf("failed to build ibcratelimit collections schema: %w", err))
}

return k
}

// Logger Creates a new logger for the module.
Expand All @@ -42,21 +66,19 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {

// GetParams Gets the params for the module.
func (k Keeper) GetParams(ctx sdk.Context) (params ibcratelimit.Params, err error) {
store := ctx.KVStore(k.storeKey)
key := ibcratelimit.ParamsKey
bz := store.Get(key)
if len(bz) == 0 {
return ibcratelimit.Params{}, nil
params, err = k.ParamsItem.Get(ctx)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return ibcratelimit.Params{}, err
}
return ibcratelimit.Params{}, err
}
err = k.cdc.Unmarshal(bz, &params)
return params, err
return params, nil
Comment thread
nagarajdivine marked this conversation as resolved.
}

// SetParams Sets the params for the module.
func (k Keeper) SetParams(ctx sdk.Context, params ibcratelimit.Params) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set(ibcratelimit.ParamsKey, bz)
func (k Keeper) SetParams(ctx sdk.Context, params ibcratelimit.Params) error {
return k.ParamsItem.Set(ctx, params)
}

// GetContractAddress Gets the current value of the module's contract address.
Expand Down
5 changes: 4 additions & 1 deletion x/ibcratelimit/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -36,7 +37,9 @@ func (k MsgServer) UpdateParams(goCtx context.Context, msg *ibcratelimit.MsgUpda
}

ctx := sdk.UnwrapSDKContext(goCtx)
k.SetParams(ctx, msg.Params)
if err := k.SetParams(ctx, msg.Params); err != nil {
return nil, fmt.Errorf("failed to set ibcratelimit params during InitGenesis: %w", err)
}
Comment thread
nagarajdivine marked this conversation as resolved.
k.emitEvent(ctx, ibcratelimit.NewEventParamsUpdated())

return &ibcratelimit.MsgUpdateParamsResponse{}, nil
Expand Down
4 changes: 4 additions & 0 deletions x/ibcratelimit/keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ibcratelimit

import "cosmossdk.io/collections"

const (
// ModuleName defines the module name
ModuleName = "ratelimitedibc"
Expand All @@ -11,4 +13,6 @@ const (
var (
// ParamsKey is the key to obtain the module's params.
ParamsKey = []byte{0x01}
// ParamsKeyPrefix is the collections.Prefix for the Params Item.
ParamsKeyPrefix = collections.NewPrefix(ParamsKey)
)
Loading