Skip to content

Commit be6bfbf

Browse files
authored
adding rate limit module (#1359)
1 parent deffee7 commit be6bfbf

File tree

6 files changed

+61
-29
lines changed

6 files changed

+61
-29
lines changed

app/keepers/keepers.go

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ import (
5454
ibchooks "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8"
5555
ibchookskeeper "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8/keeper"
5656
ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8/types"
57+
ratelimit "github.com/cosmos/ibc-apps/modules/rate-limiting/v8"
58+
ratelimitkeeper "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/keeper"
59+
ratelimittypes "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/types"
5760
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
5861
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
5962
icacontroller "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller"
@@ -173,6 +176,7 @@ type AppKeepers struct {
173176
HooksICS4Wrapper ibchooks.ICS4Middleware
174177
Ics20WasmHooks *ibchooks.WasmHooks
175178
PacketForwardKeeper *packetforwardkeeper.Keeper
179+
RatelimitKeeper ratelimitkeeper.Keeper
176180
}
177181

178182
func (appKeepers AppKeepers) GetKVStoreKeys() map[string]*storetypes.KVStoreKey {
@@ -395,19 +399,6 @@ func NewAppKeeper(
395399
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
396400
)
397401

398-
// Configure the hooks keeper
399-
hooksKeeper := ibchookskeeper.NewKeeper(
400-
app.keys[ibchookstypes.StoreKey],
401-
)
402-
app.IBCHooksKeeper = &hooksKeeper
403-
404-
wasmHooks := ibchooks.NewWasmHooks(app.IBCHooksKeeper, &app.WasmKeeper, AccountAddressPrefix)
405-
app.Ics20WasmHooks = &wasmHooks
406-
app.HooksICS4Wrapper = ibchooks.NewICS4Middleware(
407-
app.IBCKeeper.ChannelKeeper,
408-
app.Ics20WasmHooks,
409-
)
410-
411402
transferKeeper := ibctransferkeeper.NewKeeper(
412403
appCodec,
413404
app.keys[ibctransfertypes.StoreKey],
@@ -422,17 +413,6 @@ func NewAppKeeper(
422413
)
423414
app.TransferKeeper = &transferKeeper
424415

425-
app.PacketForwardKeeper = packetforwardkeeper.NewKeeper(
426-
appCodec,
427-
app.keys[packetforwardtypes.StoreKey],
428-
app.TransferKeeper,
429-
app.IBCKeeper.ChannelKeeper,
430-
app.BankKeeper,
431-
// The ICS4Wrapper is replaced by the HooksICS4Wrapper instead of the channel so that sending can be overridden by the middleware
432-
app.HooksICS4Wrapper,
433-
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
434-
)
435-
436416
app.ICAHostKeeper = icahostkeeper.NewKeeper(
437417
appCodec,
438418
app.keys[icahosttypes.StoreKey],
@@ -449,6 +429,16 @@ func NewAppKeeper(
449429
// required since ibc-go v7.5.0
450430
app.ICAHostKeeper.WithQueryRouter(bApp.GRPCQueryRouter())
451431

432+
app.RatelimitKeeper = *ratelimitkeeper.NewKeeper(
433+
appCodec, // BinaryCodec
434+
runtime.NewKVStoreService(app.keys[ratelimittypes.StoreKey]), // StoreKey
435+
app.GetSubspace(ratelimittypes.ModuleName), // param Subspace
436+
authtypes.NewModuleAddress(govtypes.ModuleName).String(), // authority
437+
app.BankKeeper,
438+
app.IBCKeeper.ChannelKeeper, // ChannelKeeper
439+
app.IBCKeeper.ChannelKeeper, // ICS4Wrapper
440+
)
441+
452442
app.ICAControllerKeeper = icacontrollerkeeper.NewKeeper(
453443
appCodec,
454444
app.keys[icacontrollertypes.StoreKey],
@@ -461,6 +451,33 @@ func NewAppKeeper(
461451
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
462452
)
463453

454+
// Configure the hooks keeper
455+
hooksKeeper := ibchookskeeper.NewKeeper(
456+
app.keys[ibchookstypes.StoreKey],
457+
)
458+
app.IBCHooksKeeper = &hooksKeeper
459+
460+
wasmHooks := ibchooks.NewWasmHooks(app.IBCHooksKeeper, &app.WasmKeeper, AccountAddressPrefix)
461+
app.Ics20WasmHooks = &wasmHooks
462+
app.HooksICS4Wrapper = ibchooks.NewICS4Middleware(
463+
app.IBCKeeper.ChannelKeeper,
464+
app.Ics20WasmHooks,
465+
)
466+
// intentionally sending itself to wrap HooksICS4Wrapper with rate limiter
467+
// Note app.HooksICS4Wrapper is used in transfer stack as well
468+
app.HooksICS4Wrapper = ibchooks.NewICS4Middleware(app.HooksICS4Wrapper, app.RatelimitKeeper)
469+
470+
app.PacketForwardKeeper = packetforwardkeeper.NewKeeper(
471+
appCodec,
472+
app.keys[packetforwardtypes.StoreKey],
473+
app.TransferKeeper,
474+
app.IBCKeeper.ChannelKeeper,
475+
app.BankKeeper,
476+
// The ICS4Wrapper is replaced by the HooksICS4Wrapper instead of the channel so that sending can be overridden by the middleware
477+
app.HooksICS4Wrapper,
478+
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
479+
)
480+
464481
wasmDir := filepath.Join(homePath, "wasm")
465482
wasmConfig, err := wasm.ReadWasmConfig(appOpts)
466483
if err != nil {
@@ -733,14 +750,13 @@ func NewAppKeeper(
733750

734751
// Create Transfer Stack (from bottom to top of stack)
735752
// - core IBC
736-
// - ibcfee
737753
// - ratelimit
738754
// - pfm
739755
// - provider
740756
// - transfer
741757
//
742758
// This is how transfer stack will work in the end:
743-
// * RecvPacket -> IBC core -> Fee -> RateLimit -> PFM -> Provider -> Transfer (AddRoute)
759+
// * RecvPacket -> IBC core -> RateLimit -> PFM -> Provider -> Transfer (AddRoute)
744760
// * SendPacket -> Transfer -> Provider -> PFM -> RateLimit -> Fee -> IBC core (ICS4Wrapper)
745761

746762
var transferStack porttypes.IBCModule
@@ -752,6 +768,7 @@ func NewAppKeeper(
752768
0, // retries on timeout
753769
packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp,
754770
)
771+
transferStack = ratelimit.NewIBCMiddleware(app.RatelimitKeeper, transferStack)
755772
transferICS4Wrapper := transferStack.(porttypes.ICS4Wrapper)
756773
app.TransferKeeper.WithICS4Wrapper(transferICS4Wrapper)
757774

@@ -868,6 +885,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
868885
paramsKeeper.Subspace(icahosttypes.SubModuleName).WithKeyTable(icahosttypes.ParamKeyTable())
869886
paramsKeeper.Subspace(ccvconsumertypes.ModuleName).WithKeyTable(ccv.ParamKeyTable())
870887
paramsKeeper.Subspace(ibchookstypes.ModuleName)
888+
paramsKeeper.Subspace(ratelimittypes.ModuleName).WithKeyTable(ratelimittypes.ParamKeyTable())
871889

872890
// Can be removed as we are not using param subspace anymore anywhere
873891
paramsKeeper.Subspace(assetprofilemoduletypes.ModuleName)

app/keepers/keys.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
1919
packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/types"
2020
ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8/types"
21+
ratelimittypes "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/types"
2122
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
2223
icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types"
2324
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
@@ -71,6 +72,7 @@ func (appKeepers *AppKeepers) GenerateKeys() {
7172
ccvconsumertypes.StoreKey,
7273
wasmTypes.StoreKey,
7374
packetforwardtypes.StoreKey,
75+
ratelimittypes.StoreKey,
7476

7577
epochsmoduletypes.StoreKey,
7678
assetprofilemoduletypes.StoreKey,

app/modules.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import (
4646
packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/types"
4747
ibchooks "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8"
4848
ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8/types"
49+
ratelimit "github.com/cosmos/ibc-apps/modules/rate-limiting/v8"
50+
ratelimittypes "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/types"
4951
"github.com/cosmos/ibc-go/modules/capability"
5052
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
5153
ica "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
@@ -156,6 +158,7 @@ func appModules(
156158
app.ConsumerModule, // Not defining it here directly because ConsumerModule needed for IBC router
157159
ibchooks.NewAppModule(app.AccountKeeper),
158160
packetforward.NewAppModule(app.PacketForwardKeeper, app.GetSubspace(packetforwardtypes.ModuleName)),
161+
ratelimit.NewAppModule(appCodec, app.RatelimitKeeper),
159162
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmTypes.ModuleName)),
160163
epochsmodule.NewAppModule(appCodec, *app.EpochsKeeper),
161164
assetprofilemodule.NewAppModule(appCodec, app.AssetprofileKeeper, app.AccountKeeper, app.BankKeeper),
@@ -264,6 +267,8 @@ func orderBeginBlockers() []string {
264267
ibcexported.ModuleName,
265268
ibctransfertypes.ModuleName,
266269
icatypes.ModuleName,
270+
packetforwardtypes.ModuleName,
271+
ratelimittypes.ModuleName,
267272
genutiltypes.ModuleName,
268273
authz.ModuleName,
269274
feegrant.ModuleName,
@@ -287,7 +292,6 @@ func orderBeginBlockers() []string {
287292
tradeshieldmoduletypes.ModuleName,
288293
wasmTypes.ModuleName,
289294
ibchookstypes.ModuleName,
290-
packetforwardtypes.ModuleName,
291295
}
292296
}
293297

@@ -309,6 +313,8 @@ func orderEndBlockers() []string {
309313
ibcexported.ModuleName,
310314
ibctransfertypes.ModuleName,
311315
icatypes.ModuleName,
316+
packetforwardtypes.ModuleName,
317+
ratelimittypes.ModuleName,
312318
capabilitytypes.ModuleName,
313319
authtypes.ModuleName,
314320
banktypes.ModuleName,
@@ -340,7 +346,6 @@ func orderEndBlockers() []string {
340346
tradeshieldmoduletypes.ModuleName,
341347
wasmTypes.ModuleName,
342348
ibchookstypes.ModuleName,
343-
packetforwardtypes.ModuleName,
344349

345350
// Must be called after estaking and masterchef
346351
ccvconsumertypes.ModuleName,
@@ -398,6 +403,7 @@ func orderInitBlockers() []string {
398403
// ibc_hooks after auth keeper
399404
ibchookstypes.ModuleName,
400405
packetforwardtypes.ModuleName,
406+
ratelimittypes.ModuleName,
401407
// crisis needs to be last so that the genesis state is consistent
402408
// when it checks invariants
403409
crisistypes.ModuleName,

app/setup_handlers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"cosmossdk.io/math"
66
"fmt"
7+
ratelimittypes "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/types"
78
"strings"
89

910
storetypes "cosmossdk.io/store/types"
@@ -99,7 +100,7 @@ func (app *ElysApp) setUpgradeStore() {
99100

100101
if shouldLoadUpgradeStore(app, upgradeInfo) {
101102
storeUpgrades := storetypes.StoreUpgrades{
102-
//Added: []string{ibchookstypes.StoreKey, packetforwardtypes.StoreKey},
103+
Added: []string{ratelimittypes.StoreKey},
103104
//Renamed: []storetypes.StoreRename{},
104105
//Deleted: []string{ibcfeetypes.StoreKey},
105106
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ require (
5353
github.com/bufbuild/buf v1.31.0
5454
github.com/cosmos/cosmos-proto v1.0.0-beta.5
5555
github.com/cosmos/ibc-apps/modules/ibc-hooks/v8 v8.0.0
56+
github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0
5657
github.com/google/uuid v1.6.0
5758
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
5859
golang.org/x/tools v0.24.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8 v8.2.0 h1:rM+
400400
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8 v8.2.0/go.mod h1:O5H9Ic3Pe6cmJn1eqlj5N48sLb8WQ1VWmDP4/11g/4E=
401401
github.com/cosmos/ibc-apps/modules/ibc-hooks/v8 v8.0.0 h1:RBUq0cC9HJ9iIhifdWbV+kjDExzfhmAB7ktOAU1RWPU=
402402
github.com/cosmos/ibc-apps/modules/ibc-hooks/v8 v8.0.0/go.mod h1:6szYOdzw0cUzFj8ZW+qfss0b4mMN1/HWxPATKZKbCfI=
403+
github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0 h1:AQO9NIAP3RFqvBCj7IqM/V1LCxmuvcvGUdu0RIEz/c0=
404+
github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0/go.mod h1:/ZpKJSW/SKPkFS7jTqkPVn7kOHUUfRNzu+8aS7YOL8o=
403405
github.com/cosmos/ibc-go/modules/apps/callbacks v0.2.1-0.20231113120333-342c00b0f8bd h1:Lx+/5dZ/nN6qPXP2Ofog6u1fmlkCFA1ElcOconnofEM=
404406
github.com/cosmos/ibc-go/modules/apps/callbacks v0.2.1-0.20231113120333-342c00b0f8bd/go.mod h1:JWfpWVKJKiKtd53/KbRoKfxWl8FsT2GPcNezTOk0o5Q=
405407
github.com/cosmos/ibc-go/modules/capability v1.0.1 h1:ibwhrpJ3SftEEZRxCRkH0fQZ9svjthrX2+oXdZvzgGI=
@@ -1032,6 +1034,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
10321034
github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
10331035
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
10341036
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
1037+
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
1038+
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
10351039
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
10361040
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
10371041
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=

0 commit comments

Comments
 (0)