From e4f5b049ac16c6a82c0f1fbffeb6fc83a168ccf3 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Thu, 1 Aug 2024 13:17:39 +0900 Subject: [PATCH] add ibc ratelimit --- app/app.go | 48 ++++++++++++++++++++++++++++++++++++++++-------- go.mod | 5 +++-- go.sum | 6 ++++-- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app/app.go b/app/app.go index 8c5b87ad..7126cff6 100644 --- a/app/app.go +++ b/app/app.go @@ -73,12 +73,16 @@ import ( "github.com/cosmos/cosmos-sdk/x/group" groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper" groupmodule "github.com/cosmos/cosmos-sdk/x/group/module" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/gogoproto/proto" packetforward "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward" packetforwardkeeper "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/keeper" packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/types" + ratelimit "github.com/cosmos/ibc-apps/modules/rate-limiting/v8" + ratelimitkeeper "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/keeper" + ratelimittypes "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/types" "github.com/cosmos/ibc-go/modules/capability" capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" @@ -282,6 +286,7 @@ type InitiaApp struct { OracleKeeper *oraclekeeper.Keeper // x/oracle keeper used for the slinky oracle MarketMapKeeper *marketmapkeeper.Keeper ForwardingKeeper *forwardingkeeper.Keeper + RatelimitKeeper *ratelimitkeeper.Keeper // other slinky oracle services OracleClient oracleclient.OracleClient @@ -351,7 +356,7 @@ func NewInitiaApp( icacontrollertypes.StoreKey, ibcfeetypes.StoreKey, ibcpermtypes.StoreKey, movetypes.StoreKey, auctiontypes.StoreKey, ophosttypes.StoreKey, oracletypes.StoreKey, packetforwardtypes.StoreKey, ibchookstypes.StoreKey, - forwardingtypes.StoreKey, marketmaptypes.StoreKey, + forwardingtypes.StoreKey, marketmaptypes.StoreKey, ratelimittypes.StoreKey, ) tkeys := storetypes.NewTransientStoreKeys(forwardingtypes.TransientStoreKey) memKeys := storetypes.NewMemoryStoreKeys(capabilitytypes.MemStoreKey) @@ -601,8 +606,9 @@ func NewInitiaApp( var transferStack porttypes.IBCModule { packetForwardKeeper := &packetforwardkeeper.Keeper{} + rateLimitKeeper := &ratelimitkeeper.Keeper{} - // Create Transfer Keepers + // create Transfer Keepers transferKeeper := ibctransferkeeper.NewKeeper( appCodec, keys[ibctransfertypes.StoreKey], @@ -635,8 +641,8 @@ func NewInitiaApp( app.IBCKeeper.ChannelKeeper, app.DistrKeeper, app.BankKeeper, - // ics4wrapper: transfer -> packet forward -> fee - app.IBCFeeKeeper, + // ics4wrapper: transfer -> packet forward -> rate limit + rateLimitKeeper, authorityAddr, ) app.PacketForwardKeeper = packetForwardKeeper @@ -649,9 +655,29 @@ func NewInitiaApp( packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp, ) + // create the rate limit keeper + *rateLimitKeeper = *ratelimitkeeper.NewKeeper( + appCodec, + runtime.NewKVStoreService(keys[ratelimittypes.StoreKey]), + paramtypes.Subspace{}, // empty params + authorityAddr, + app.BankKeeper, + app.IBCKeeper.ChannelKeeper, + // ics4wrapper: transfer -> packet forward -> rate limit -> fee + app.IBCFeeKeeper, + ) + app.RatelimitKeeper = rateLimitKeeper + + // rate limit middleware + transferStack = ratelimit.NewIBCMiddleware( + *app.RatelimitKeeper, + // receive: rate limit -> packet forward -> forwarding -> transfer + transferStack, + ) + // create move middleware for transfer transferStack = ibchooks.NewIBCMiddleware( - // receive: move -> packet forward -> forwarding -> transfer + // receive: move -> rate limit -> packet forward -> forwarding -> transfer transferStack, ibchooks.NewICS4Middleware( nil, /* ics4wrapper: not used */ @@ -662,14 +688,14 @@ func NewInitiaApp( // create ibcfee middleware for transfer transferStack = ibcfee.NewIBCMiddleware( - // receive: fee -> move -> packet forward -> forwarding -> transfer + // receive: fee -> move -> rate limit -> packet forward -> forwarding -> transfer transferStack, *app.IBCFeeKeeper, ) // create perm middleware for transfer transferStack = ibcperm.NewIBCMiddleware( - // receive: perm -> fee -> move -> packet forward -> forwarding -> transfer + // receive: perm -> fee -> move -> rate limit -> packet forward -> forwarding -> transfer transferStack, // ics4wrapper: not used nil, @@ -740,6 +766,8 @@ func NewInitiaApp( app.MsgServiceRouter(), authorityAddr, ) + icaHostKeeper.WithQueryRouter(app.BaseApp.GRPCQueryRouter()) + // icaHostKeeper.WithICS4Wrapper() app.ICAHostKeeper = &icaHostKeeper icaControllerKeeper := icacontrollerkeeper.NewKeeper( @@ -752,6 +780,7 @@ func NewInitiaApp( app.MsgServiceRouter(), authorityAddr, ) + // icaControllerKeeper.WithICS4Wrapper() app.ICAControllerKeeper = &icaControllerKeeper icaAuthKeeper := icaauthkeeper.NewKeeper( @@ -903,6 +932,7 @@ func NewInitiaApp( packetforward.NewAppModule(app.PacketForwardKeeper, nil), ibchooks.NewAppModule(appCodec, *app.IBCHooksKeeper), forwarding.NewAppModule(app.ForwardingKeeper), + ratelimit.NewAppModule(appCodec, *app.RatelimitKeeper), ) // BasicModuleManager defines the module BasicManager is in charge of setting up basic, @@ -939,6 +969,7 @@ func NewInitiaApp( ibcexported.ModuleName, oracletypes.ModuleName, marketmaptypes.ModuleName, + ratelimittypes.ModuleName, ) app.ModuleManager.SetOrderEndBlockers( @@ -952,6 +983,7 @@ func NewInitiaApp( oracletypes.ModuleName, marketmaptypes.ModuleName, forwardingtypes.ModuleName, + ratelimittypes.ModuleName, ) // NOTE: The genutils module must occur after staking so that pools are @@ -968,7 +1000,7 @@ func NewInitiaApp( ibcnfttransfertypes.ModuleName, icatypes.ModuleName, icaauthtypes.ModuleName, ibcfeetypes.ModuleName, ibcpermtypes.ModuleName, consensusparamtypes.ModuleName, auctiontypes.ModuleName, ophosttypes.ModuleName, oracletypes.ModuleName, marketmaptypes.ModuleName, packetforwardtypes.ModuleName, ibchookstypes.ModuleName, - forwardingtypes.ModuleName, + forwardingtypes.ModuleName, ratelimittypes.ModuleName, } app.ModuleManager.SetOrderInitGenesis(genesisModuleOrder...) app.ModuleManager.SetOrderExportGenesis(genesisModuleOrder...) diff --git a/go.mod b/go.mod index dedd2671..adf1b89e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/initia-labs/initia -go 1.22 +go 1.22.0 toolchain go1.22.2 @@ -30,8 +30,9 @@ require ( github.com/cosmos/go-bip39 v1.0.0 github.com/cosmos/gogoproto v1.5.0 github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8 v8.0.2 + github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0 github.com/cosmos/ibc-go/modules/capability v1.0.0 - github.com/cosmos/ibc-go/v8 v8.2.1 + github.com/cosmos/ibc-go/v8 v8.4.0 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 github.com/gogo/protobuf v1.3.3 github.com/golang/mock v1.6.0 diff --git a/go.sum b/go.sum index 6efcc019..3cba5fc4 100644 --- a/go.sum +++ b/go.sum @@ -373,10 +373,12 @@ github.com/cosmos/iavl v1.1.4 h1:Z0cVVjeQqOUp78/nWt/uhQy83vYluWlAMGQ4zbH9G34= github.com/cosmos/iavl v1.1.4/go.mod h1:vCYmRQUJU1wwj0oRD3wMEtOM9sJNDP+GFMaXmIxZ/rU= github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8 v8.0.2 h1:dyLNlDElY6+5zW/BT/dO/3Ad9FpQblfh+9dQpYQodbA= github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8 v8.0.2/go.mod h1:82hPO/tRawbuFad2gPwChvpZ0JEIoNi91LwVneAYCeM= +github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0 h1:AQO9NIAP3RFqvBCj7IqM/V1LCxmuvcvGUdu0RIEz/c0= +github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0/go.mod h1:/ZpKJSW/SKPkFS7jTqkPVn7kOHUUfRNzu+8aS7YOL8o= github.com/cosmos/ibc-go/modules/capability v1.0.0 h1:r/l++byFtn7jHYa09zlAdSeevo8ci1mVZNO9+V0xsLE= github.com/cosmos/ibc-go/modules/capability v1.0.0/go.mod h1:D81ZxzjZAe0ZO5ambnvn1qedsFQ8lOwtqicG6liLBco= -github.com/cosmos/ibc-go/v8 v8.2.1 h1:MTsnZZjxvGD4Fv5pYyx5UkELafSX0rlPt6IfsE2BpTQ= -github.com/cosmos/ibc-go/v8 v8.2.1/go.mod h1:wj3qx75iC/XNnsMqbPDCIGs0G6Y3E/lo3bdqCyoCy+8= +github.com/cosmos/ibc-go/v8 v8.4.0 h1:K2PfX0AZ+1XKZytHGEMuSjQXG/MZshPb83RSTQt2+cE= +github.com/cosmos/ibc-go/v8 v8.4.0/go.mod h1:zh6x1osR0hNvEcFrC/lhGD08sMfQmr9wHVvZ/mRWMCs= github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM= github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0= github.com/cosmos/interchain-security/v5 v5.0.0 h1:iwHu1nFbXuYfa13isEgm6hkHU+2t/t56YjcfyP3PnQA=