Skip to content

Commit

Permalink
ibc rate-limit and hooks reversed
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnukumavat committed Jun 1, 2023
1 parent 4162dc5 commit d7afe04
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 169 deletions.
177 changes: 25 additions & 152 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
ibctesting "github.com/cosmos/interchain-security/legacy_ibc_testing/testing"
"github.com/tendermint/spm/cosmoscmd"

ccvconsumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"
"io"
"net/http"
"os"
Expand All @@ -16,23 +15,13 @@ import (
"strconv"
"strings"

ccvconsumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"

paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"

"github.com/gorilla/mux"
"github.com/spf13/cast"

ibchooks "github.com/osmosis-labs/osmosis/x/ibc-hooks"
ibchookskeeper "github.com/osmosis-labs/osmosis/x/ibc-hooks/keeper"
ibchookstypes "github.com/osmosis-labs/osmosis/x/ibc-hooks/types"

ibcratelimit "github.com/osmosis-labs/osmosis/v15/x/ibc-rate-limit"
"github.com/osmosis-labs/osmosis/v15/x/ibc-rate-limit/ibcratelimitmodule"
ibcratelimittypes "github.com/osmosis-labs/osmosis/v15/x/ibc-rate-limit/types"

packetforward "github.com/strangelove-ventures/packet-forward-middleware/v4/router"
packetforwardkeeper "github.com/strangelove-ventures/packet-forward-middleware/v4/router/keeper"
packetforwardtypes "github.com/strangelove-ventures/packet-forward-middleware/v4/router/types"

"github.com/rakyll/statik/fs"

ica "github.com/cosmos/ibc-go/v4/modules/apps/27-interchain-accounts"
Expand Down Expand Up @@ -291,9 +280,6 @@ var (
liquidity.AppModuleBasic{},
rewards.AppModuleBasic{},
ica.AppModuleBasic{},
ibchooks.AppModuleBasic{},
ibcratelimitmodule.AppModuleBasic{},
packetforward.AppModuleBasic{},
)
)

Expand Down Expand Up @@ -350,7 +336,6 @@ type App struct {
UpgradeKeeper upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
IbcKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IbcHooksKeeper *ibchookskeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper
EvidenceKeeper evidencekeeper.Keeper
IbcTransferKeeper ibctransferkeeper.Keeper
Expand Down Expand Up @@ -378,15 +363,7 @@ type App struct {
LiquidityKeeper liquiditykeeper.Keeper
Rewardskeeper rewardskeeper.Keeper

// IBC modules
// transfer module
RawIcs20TransferAppModule ibctransfer.AppModule
RateLimitingICS4Wrapper *ibcratelimit.ICS4Wrapper
TransferStack *ibchooks.IBCMiddleware
Ics20WasmHooks *ibchooks.WasmHooks
HooksICS4Wrapper ibchooks.ICS4Middleware
PacketForwardKeeper *packetforwardkeeper.Keeper
ConsumerKeeper ccvconsumerkeeper.Keeper
ConsumerKeeper ccvconsumerkeeper.Keeper

WasmKeeper wasm.Keeper
ContractKeeper *wasmkeeper.PermissionedKeeper
Expand Down Expand Up @@ -430,7 +407,6 @@ func New(
markettypes.StoreKey, bandoraclemoduletypes.StoreKey, lockertypes.StoreKey,
wasm.StoreKey, authzkeeper.StoreKey, auctiontypes.StoreKey, tokenminttypes.StoreKey,
rewardstypes.StoreKey, feegrant.StoreKey, liquiditytypes.StoreKey, esmtypes.ModuleName, lendtypes.StoreKey,
ibchookstypes.StoreKey, packetforwardtypes.StoreKey,
)
)

Expand Down Expand Up @@ -484,8 +460,6 @@ func New(
app.ParamsKeeper.Subspace(tokenminttypes.ModuleName)
app.ParamsKeeper.Subspace(liquiditytypes.ModuleName)
app.ParamsKeeper.Subspace(rewardstypes.ModuleName)
app.ParamsKeeper.Subspace(ibcratelimittypes.ModuleName)
app.ParamsKeeper.Subspace(packetforwardtypes.ModuleName).WithKeyTable(packetforwardtypes.ParamKeyTable())

// set the BaseApp's parameter store
baseApp.SetParamStore(
Expand Down Expand Up @@ -597,14 +571,6 @@ func New(
scopedIBCKeeper,
)

// Configure the hooks keeper
hooksKeeper := ibchookskeeper.NewKeeper(
app.keys[ibchookstypes.StoreKey],
)
app.IbcHooksKeeper = &hooksKeeper

app.WireICS20PreWasmKeeper(appCodec, baseApp, app.IbcHooksKeeper, scopedTransferKeeper)

app.ICAHostKeeper = icahostkeeper.NewKeeper(
appCodec, app.keys[icahosttypes.StoreKey],
app.GetSubspace(icahosttypes.SubModuleName),
Expand Down Expand Up @@ -748,6 +714,19 @@ func New(
app.BankKeeper,
)

// Create Transfer Keepers
app.IbcTransferKeeper = ibctransferkeeper.NewKeeper(
app.cdc,
app.keys[ibctransfertypes.StoreKey],
app.GetSubspace(ibctransfertypes.ModuleName),
app.IbcKeeper.ChannelKeeper,
app.IbcKeeper.ChannelKeeper,
&app.IbcKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
scopedTransferKeeper,
)

app.LockerKeeper = lockerkeeper.NewKeeper(
app.cdc,
app.keys[lockertypes.StoreKey],
Expand Down Expand Up @@ -816,11 +795,6 @@ func New(
wasmOpts...,
)

// Pass the contract keeper to all the structs (generally ICS4Wrappers for ibc middlewares) that need it
app.ContractKeeper = wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper)
app.RateLimitingICS4Wrapper.ContractKeeper = app.ContractKeeper
app.Ics20WasmHooks.ContractKeeper = app.ContractKeeper

// register the proposal types
govRouter := govtypes.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler).
Expand Down Expand Up @@ -849,17 +823,16 @@ func New(
)

var (
evidenceRouter = evidencetypes.NewRouter()
ibcRouter = ibcporttypes.NewRouter()
// transferModule = ibctransfer.NewAppModule(app.IbcTransferKeeper)
// transferIBCModule = ibctransfer.NewIBCModule(app.IbcTransferKeeper)
evidenceRouter = evidencetypes.NewRouter()
ibcRouter = ibcporttypes.NewRouter()
transferModule = ibctransfer.NewAppModule(app.IbcTransferKeeper)
transferIBCModule = ibctransfer.NewIBCModule(app.IbcTransferKeeper)
oracleModule = market.NewAppModule(app.cdc, app.MarketKeeper, app.BandoracleKeeper, app.AssetKeeper)
bandOracleIBCModule = bandoraclemodule.NewIBCModule(app.BandoracleKeeper)
)

appVersionGetter := MyAppVersionGetter{App: app}
// ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, app.TransferStack)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
ibcRouter.AddRoute(bandoraclemoduletypes.ModuleName, bandOracleIBCModule)
ibcRouter.AddRoute(wasm.ModuleName, wasm.NewIBCHandler(app.WasmKeeper, app.IbcKeeper.ChannelKeeper, appVersionGetter))
ibcRouter.AddRoute(icahosttypes.SubModuleName, icaHostIBCModule)
Expand Down Expand Up @@ -900,7 +873,7 @@ func New(
ibc.NewAppModule(app.IbcKeeper),
ica.NewAppModule(nil, &app.ICAHostKeeper),
params.NewAppModule(app.ParamsKeeper),
app.RawIcs20TransferAppModule,
transferModule,
asset.NewAppModule(app.cdc, app.AssetKeeper),
vault.NewAppModule(app.cdc, app.VaultKeeper),
oracleModule,
Expand All @@ -915,9 +888,6 @@ func New(
tokenmint.NewAppModule(app.cdc, app.TokenmintKeeper, app.AccountKeeper, app.BankKeeper),
liquidity.NewAppModule(app.cdc, app.LiquidityKeeper, app.AccountKeeper, app.BankKeeper, app.AssetKeeper),
rewards.NewAppModule(app.cdc, app.Rewardskeeper, app.AccountKeeper, app.BankKeeper),
ibcratelimitmodule.NewAppModule(*app.RateLimitingICS4Wrapper),
ibchooks.NewAppModule(app.AccountKeeper),
packetforward.NewAppModule(app.PacketForwardKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -958,9 +928,6 @@ func New(
liquiditytypes.ModuleName,
lendtypes.ModuleName,
esmtypes.ModuleName,
ibcratelimittypes.ModuleName,
ibchookstypes.ModuleName,
packetforwardtypes.ModuleName,
)

app.mm.SetOrderEndBlockers(
Expand Down Expand Up @@ -997,9 +964,6 @@ func New(
rewardstypes.ModuleName,
liquiditytypes.ModuleName,
esmtypes.ModuleName,
ibcratelimittypes.ModuleName,
ibchookstypes.ModuleName,
packetforwardtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand All @@ -1021,6 +985,7 @@ func New(
genutiltypes.ModuleName,
evidencetypes.ModuleName,
ibctransfertypes.ModuleName,
wasmtypes.ModuleName,
authz.ModuleName,
vestingtypes.ModuleName,
paramstypes.ModuleName,
Expand All @@ -1039,14 +1004,6 @@ func New(
liquiditytypes.ModuleName,
rewardstypes.ModuleName,
crisistypes.ModuleName,
ibcratelimittypes.ModuleName,

// wasm after ibc transfer
wasmtypes.ModuleName,

// ibc_hooks after auth keeper
ibchookstypes.ModuleName,
packetforwardtypes.ModuleName,
)

app.mm.RegisterInvariants(&app.CrisisKeeper)
Expand Down Expand Up @@ -1121,90 +1078,6 @@ func New(
return app
}

// WireICS20PreWasmKeeper Create the IBC Transfer Stack from bottom to top:
//
// * SendPacket. Originates from the transferKeeper and goes up the stack:
// transferKeeper.SendPacket -> ibc_rate_limit.SendPacket -> ibc_hooks.SendPacket -> channel.SendPacket
// * RecvPacket, message that originates from core IBC and goes down to app, the flow is the other way
// channel.RecvPacket -> ibc_hooks.OnRecvPacket -> ibc_rate_limit.OnRecvPacket -> forward.OnRecvPacket -> transfer.OnRecvPacket
//
// Note that the forward middleware is only integrated on the "reveive" direction. It can be safely skipped when sending.
// Note also that the forward middleware is called "router", but we are using the name "forward" for clarity
// This may later be renamed upstream: https://github.com/strangelove-ventures/packet-forward-middleware/issues/10
//
// After this, the wasm keeper is required to be set on both
// a.WasmHooks AND a.RateLimitingICS4Wrapper
func (a *App) WireICS20PreWasmKeeper(
appCodec codec.Codec,
bApp *baseapp.BaseApp,
hooksKeeper *ibchookskeeper.Keeper,
scopedTransferKeeper capabilitykeeper.ScopedKeeper,
) {
// Setup the ICS4Wrapper used by the hooks middleware
cmdxPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
wasmHooks := ibchooks.NewWasmHooks(hooksKeeper, nil, cmdxPrefix) // The contract keeper needs to be set later
a.Ics20WasmHooks = &wasmHooks
a.HooksICS4Wrapper = ibchooks.NewICS4Middleware(
a.IbcKeeper.ChannelKeeper,
a.Ics20WasmHooks,
)

// ChannelKeeper wrapper for rate limiting SendPacket(). The wasmKeeper needs to be added after it's created
rateLimitingICS4Wrapper := ibcratelimit.NewICS4Middleware(
a.HooksICS4Wrapper,
&a.AccountKeeper,
// wasm keeper we set later.
nil,
a.BankBaseKeeper,
a.GetSubspace(ibcratelimittypes.ModuleName),
)
a.RateLimitingICS4Wrapper = &rateLimitingICS4Wrapper

// Create Transfer Keepers
transferKeeper := ibctransferkeeper.NewKeeper(
appCodec,
a.keys[ibctransfertypes.StoreKey],
a.GetSubspace(ibctransfertypes.ModuleName),
// The ICS4Wrapper is replaced by the rateLimitingICS4Wrapper instead of the channel
a.RateLimitingICS4Wrapper,
a.IbcKeeper.ChannelKeeper,
&a.IbcKeeper.PortKeeper,
a.AccountKeeper,
a.BankKeeper,
scopedTransferKeeper,
)
a.IbcTransferKeeper = transferKeeper
a.RawIcs20TransferAppModule = ibctransfer.NewAppModule(a.IbcTransferKeeper)
// Packet Forward Middleware
// Initialize packet forward middleware router
a.PacketForwardKeeper = packetforwardkeeper.NewKeeper(
appCodec,
a.keys[packetforwardtypes.StoreKey],
a.GetSubspace(packetforwardtypes.ModuleName),
a.IbcTransferKeeper,
a.IbcKeeper.ChannelKeeper,
a.DistrKeeper,
a.BankKeeper,
// The ICS4Wrapper is replaced by the HooksICS4Wrapper instead of the channel so that sending can be overridden by the middleware
a.HooksICS4Wrapper,
)
a.PacketForwardKeeper.SetTransferKeeper(transferKeeper)
packetForwardMiddleware := packetforward.NewIBCMiddleware(
ibctransfer.NewIBCModule(a.IbcTransferKeeper),
a.PacketForwardKeeper,
0,
packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp,
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp,
)

// RateLimiting IBC Middleware
rateLimitingTransferModule := ibcratelimit.NewIBCModule(packetForwardMiddleware, a.RateLimitingICS4Wrapper)

// Hooks Middleware
hooksTransferModule := ibchooks.NewIBCMiddleware(&rateLimitingTransferModule, &a.HooksICS4Wrapper)
a.TransferStack = &hooksTransferModule
}

func (a *App) getAppVersion(ctx sdk.Context, portID, channelID string) (uint64, bool) {
return a.BaseApp.AppVersion(), true
}
Expand Down Expand Up @@ -1442,11 +1315,11 @@ func upgradeHandlers(upgradeInfo storetypes.UpgradeInfo, a *App, storeUpgrades *
switch {
case upgradeInfo.Name == tv11.UpgradeName && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{ibchookstypes.StoreKey},
Added: []string{},
}
case upgradeInfo.Name == mv11.UpgradeName && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{ibchookstypes.StoreKey},
Added: []string{},
}
}
return storeUpgrades
Expand Down
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ require (
require (
github.com/cosmos/ibc-go/v4 v4.4.2
github.com/golangci/golangci-lint v1.51.2
github.com/osmosis-labs/osmosis/v15 v15.0.0
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.6
github.com/rakyll/statik v0.1.7
github.com/spf13/pflag v1.0.5
github.com/strangelove-ventures/packet-forward-middleware/v4 v4.0.4
github.com/tendermint/spm v0.1.9
gopkg.in/yaml.v2 v2.4.0
mvdan.cc/gofumpt v0.4.0
Expand Down Expand Up @@ -169,7 +166,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/iancoleman/orderedmap v0.2.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jgautheron/goconst v1.5.1 // indirect
Expand Down Expand Up @@ -217,7 +213,6 @@ require (
github.com/nunnatsa/ginkgolinter v0.8.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/osmosis-labs/osmosis/osmoutils v0.0.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1801,8 +1801,6 @@ github.com/huin/goupnp v1.0.3-0.20220313090229-ca81a64b4204/go.mod h1:ZxNlw5WqJj
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/hydrogen18/memlistener v0.0.0-20141126152155-54553eb933fb/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE=
github.com/hydrogen18/memlistener v0.0.0-20200120041712-dcc25e7acd91/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE=
github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA=
github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -2391,13 +2389,6 @@ github.com/openzipkin/zipkin-go v0.2.5/go.mod h1:KpXfKdgRDnnhsxw4pNIH9Md5lyFqKUa
github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA=
github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
github.com/ory/dockertest/v3 v3.9.1/go.mod h1:42Ir9hmvaAPm0Mgibk6mBPi7SFvTXxEcnztDYOJ//uM=
github.com/osmosis-labs/osmosis/osmomath v0.0.4 h1:5cJfDi8mOuAzLfsQFoTGJvm/qca7Gj7wvDiVSTE1ibk=
github.com/osmosis-labs/osmosis/osmoutils v0.0.4 h1:W/lvzFsyt30GGi0+nD3Ktxlwe5Tl6ipKWilyyLjJ9Ro=
github.com/osmosis-labs/osmosis/osmoutils v0.0.4/go.mod h1:2MStAdC2TajbhgkAwT3SZPtMW/Bbvp0JYGPRle+5Qsc=
github.com/osmosis-labs/osmosis/v15 v15.0.0 h1:lwAha4K+6fg/XFnqwAKd7iqZ1NfvCGiOgkyZ/PWUro4=
github.com/osmosis-labs/osmosis/v15 v15.0.0/go.mod h1:oG4n68FcMHsy5+MkjmwDa8iQNoJekYqn7bkzsc/85jY=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.6 h1:PjfLL5rwwm44CeLnNQssrFgmj4BdeIS5DriKYhGz7IM=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.6/go.mod h1:2Aqs0L6JnMfo+P+It8q7hJsP1YB+Is5DJc4nRSiBF/U=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/copy v1.6.0 h1:IinKAryFFuPONZ7cm6T6E2QX/vcJwSnlaA5lfoaXIiQ=
github.com/otiai10/copy v1.6.0/go.mod h1:XWfuS3CrI0R6IE0FbgHsEazaXO8G0LpMp9o8tos0x4E=
Expand Down Expand Up @@ -2764,9 +2755,6 @@ github.com/stbenjam/no-sprintf-host-port v0.1.1 h1:tYugd/yrm1O0dV+ThCbaKZh195Dfm
github.com/stbenjam/no-sprintf-host-port v0.1.1/go.mod h1:TLhvtIvONRzdmkFiio4O8LHsN9N74I+PhRquPsxpL0I=
github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/strangelove-ventures/async-icq/v4 v4.0.0-rc0 h1:foE/5O2/XiqGsdTKx3R0BTfKgW9KnUYyQLZt0WFSesE=
github.com/strangelove-ventures/packet-forward-middleware/v4 v4.0.4 h1:8Tn4Gy/DAq7wzV1CxEGv80ujZ+nUvzgwwdCobO/Gj8Y=
github.com/strangelove-ventures/packet-forward-middleware/v4 v4.0.4/go.mod h1:AG8F5pdk3x1h7PlRvPoMem3623W+w8HJHrWYkVJ51kk=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
Expand Down

0 comments on commit d7afe04

Please sign in to comment.