Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ibc ratelimit #239

Merged
merged 1 commit into from
Aug 1, 2024
Merged
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
48 changes: 40 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand All @@ -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 */
Expand All @@ -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,
Expand Down Expand Up @@ -740,6 +766,8 @@ func NewInitiaApp(
app.MsgServiceRouter(),
authorityAddr,
)
icaHostKeeper.WithQueryRouter(app.BaseApp.GRPCQueryRouter())
// icaHostKeeper.WithICS4Wrapper()
app.ICAHostKeeper = &icaHostKeeper

icaControllerKeeper := icacontrollerkeeper.NewKeeper(
Expand All @@ -752,6 +780,7 @@ func NewInitiaApp(
app.MsgServiceRouter(),
authorityAddr,
)
// icaControllerKeeper.WithICS4Wrapper()
app.ICAControllerKeeper = &icaControllerKeeper

icaAuthKeeper := icaauthkeeper.NewKeeper(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -939,6 +969,7 @@ func NewInitiaApp(
ibcexported.ModuleName,
oracletypes.ModuleName,
marketmaptypes.ModuleName,
ratelimittypes.ModuleName,
)

app.ModuleManager.SetOrderEndBlockers(
Expand All @@ -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
Expand All @@ -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...)
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/initia-labs/initia

go 1.22
go 1.22.0

toolchain go1.22.2

Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading