Skip to content

Commit

Permalink
Merge pull request #135 from neutron-org/feat/ibc-hooks
Browse files Browse the repository at this point in the history
feat/ibc-hooks #ntrn-344
  • Loading branch information
pr0n00gler authored Apr 4, 2023
2 parents 7984424 + de02587 commit 731f9a4
Show file tree
Hide file tree
Showing 19 changed files with 1,582 additions and 16 deletions.
38 changes: 33 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ import (
feeburnertypes "github.com/neutron-org/neutron/x/feeburner/types"
"github.com/neutron-org/neutron/x/feerefunder"
feekeeper "github.com/neutron-org/neutron/x/feerefunder/keeper"
ibchooks "github.com/neutron-org/neutron/x/ibc-hooks"
ibchookstypes "github.com/neutron-org/neutron/x/ibc-hooks/types"
"github.com/neutron-org/neutron/x/interchainqueries"
interchainqueriesmodulekeeper "github.com/neutron-org/neutron/x/interchainqueries/keeper"
interchainqueriesmoduletypes "github.com/neutron-org/neutron/x/interchainqueries/types"
Expand Down Expand Up @@ -211,6 +213,7 @@ var (
upgraderest.ProposalCancelRESTHandler,
),
),
ibchooks.AppModuleBasic{},
router.AppModuleBasic{},
)

Expand Down Expand Up @@ -289,6 +292,9 @@ type App struct {

RouterModule router.AppModule

HooksTransferIBCModule *ibchooks.IBCMiddleware
HooksICS4Wrapper ibchooks.ICS4Middleware

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -340,7 +346,7 @@ func New(
icahosttypes.StoreKey, capabilitytypes.StoreKey,
interchainqueriesmoduletypes.StoreKey, contractmanagermoduletypes.StoreKey, interchaintxstypes.StoreKey, wasm.StoreKey, feetypes.StoreKey,
feeburnertypes.StoreKey, adminmodulemoduletypes.StoreKey, ccvconsumertypes.StoreKey, tokenfactorytypes.StoreKey, routertypes.StoreKey,
crontypes.StoreKey,
crontypes.StoreKey, ibchookstypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, feetypes.MemStoreKey)
Expand Down Expand Up @@ -449,12 +455,25 @@ func New(
app.BankKeeper,
app.IBCKeeper.ChannelKeeper,
)
wasmHooks := ibchooks.NewWasmHooks(nil, sdk.GetConfig().GetBech32AccountAddrPrefix()) // The contract keeper needs to be set later
app.HooksICS4Wrapper = ibchooks.NewICS4Middleware(
app.RouterKeeper,
&wasmHooks,
)

// Create Transfer Keepers
app.TransferKeeper = wrapkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey], app.GetSubspace(ibctransfertypes.ModuleName),
app.RouterKeeper, app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper, app.FeeKeeper, app.ContractManagerKeeper,
appCodec,
keys[ibctransfertypes.StoreKey],
app.GetSubspace(ibctransfertypes.ModuleName),
app.HooksICS4Wrapper, // essentially still app.IBCKeeper.ChannelKeeper under the hood because no hook overrides
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
scopedTransferKeeper,
app.FeeKeeper,
app.ContractManagerKeeper,
)

app.RouterKeeper.SetTransferKeeper(app.TransferKeeper.Keeper)
Expand Down Expand Up @@ -568,6 +587,7 @@ func New(
supportedFeatures,
wasmOpts...,
)
wasmHooks.ContractKeeper = wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper)

app.CronKeeper.WasmMsgServer = wasmkeeper.NewMsgServerImpl(wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper))
cronModule := cron.NewAppModule(appCodec, &app.CronKeeper)
Expand All @@ -576,6 +596,9 @@ func New(
app.AdminmoduleKeeper.Router().AddRoute(wasm.RouterKey, wasm.NewWasmProposalHandler(app.WasmKeeper, enabledProposals))
}
transferIBCModule := transferSudo.NewIBCModule(app.TransferKeeper)
// receive call order: wasmHooks#OnRecvPacketOverride(transferIbcModule#OnRecvPacket())
ibcHooksMiddleware := ibchooks.NewIBCMiddleware(&transferIBCModule, &app.HooksICS4Wrapper)
app.HooksTransferIBCModule = &ibcHooksMiddleware

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
Expand All @@ -592,11 +615,12 @@ func New(
interchainQueriesModule := interchainqueries.NewAppModule(appCodec, app.InterchainQueriesKeeper, app.AccountKeeper, app.BankKeeper)
interchainTxsModule := interchaintxs.NewAppModule(appCodec, app.InterchainTxsKeeper, app.AccountKeeper, app.BankKeeper)
contractManagerModule := contractmanager.NewAppModule(appCodec, app.ContractManagerKeeper)
ibcHooksModule := ibchooks.NewAppModule(app.AccountKeeper)

app.RouterModule = router.NewAppModule(app.RouterKeeper)

ibcStack := router.NewIBCMiddleware(
transferIBCModule,
app.HooksTransferIBCModule,
app.RouterKeeper,
0,
routerkeeper.DefaultForwardTransferPacketTimeoutTimestamp,
Expand Down Expand Up @@ -644,6 +668,7 @@ func New(
feeBurnerModule,
contractManagerModule,
adminModule,
ibcHooksModule,
tokenfactory.NewAppModule(appCodec, *app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper),
cronModule,
)
Expand Down Expand Up @@ -676,6 +701,7 @@ func New(
feetypes.ModuleName,
feeburnertypes.ModuleName,
adminmodulemoduletypes.ModuleName,
ibchookstypes.ModuleName,
routertypes.ModuleName,
crontypes.ModuleName,
)
Expand Down Expand Up @@ -704,6 +730,7 @@ func New(
feetypes.ModuleName,
feeburnertypes.ModuleName,
adminmodulemoduletypes.ModuleName,
ibchookstypes.ModuleName,
routertypes.ModuleName,
crontypes.ModuleName,
)
Expand Down Expand Up @@ -737,6 +764,7 @@ func New(
feetypes.ModuleName,
feeburnertypes.ModuleName,
adminmodulemoduletypes.ModuleName,
ibchookstypes.ModuleName, // after auth keeper
routertypes.ModuleName,
crontypes.ModuleName,
)
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/CosmWasm/wasmvm v1.1.1
github.com/confio/ics23/go v0.9.0
github.com/cosmos/admin-module v0.0.0-00010101000000-000000000000
github.com/cosmos/cosmos-proto v1.0.0-alpha8
github.com/cosmos/cosmos-sdk v0.45.13
github.com/cosmos/ibc-go/v4 v4.3.0
github.com/cosmos/interchain-security v1.0.0-rc3
Expand All @@ -17,7 +18,6 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.14.0
github.com/regen-network/cosmos-proto v0.3.1
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
github.com/strangelove-ventures/packet-forward-middleware/v4 v4.0.5
Expand All @@ -44,7 +44,6 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/coinbase/rosetta-sdk-go v0.7.9 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/cosmos-proto v1.0.0-alpha8 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogoproto v1.4.3 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
Expand All @@ -62,7 +61,6 @@ require (
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
Expand Down Expand Up @@ -111,6 +109,7 @@ require (
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rakyll/statik v0.1.7 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/rs/zerolog v1.27.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
Expand All @@ -135,7 +134,7 @@ require (
google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
nhooyr.io/websocket v1.8.7 // indirect
)

replace (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,6 @@ github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW
github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand Down Expand Up @@ -3525,8 +3524,9 @@ mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jC
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
mvdan.cc/unparam v0.0.0-20200501210554-b37ab49443f7/go.mod h1:HGC5lll35J70Y5v7vCGb9oLhHoScFwkHDJm/05RdSTc=
mvdan.cc/unparam v0.0.0-20220706161116-678bad134442/go.mod h1:F/Cxw/6mVrNKqrR2YjFf5CaW0Bw4RL8RfbEf4GRggJk=
nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
pack.ag/amqp v0.11.2/go.mod h1:4/cbmt4EJXSKlG6LCfWHoqmN0uFdy5i/+YFz+fTfhV4=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
Expand Down
9 changes: 4 additions & 5 deletions testutil/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
appProvider "github.com/cosmos/interchain-security/app/provider"
e2e "github.com/cosmos/interchain-security/testutil/e2e"
"github.com/cosmos/interchain-security/testutil/e2e"
"github.com/cosmos/interchain-security/x/ccv/utils"
tmtypes "github.com/tendermint/tendermint/types"

Expand Down Expand Up @@ -413,13 +413,12 @@ func NewTransferPath(chainA, chainB, chainProvider *ibctesting.TestChain) *ibcte

// SetupTransferPath
func SetupTransferPath(path *ibctesting.Path) error {
ctx := path.EndpointA.Chain.GetContext()

channelSequence := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(ctx)
channelSequence := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(path.EndpointA.Chain.GetContext())
channelSequenceB := path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(path.EndpointB.Chain.GetContext())

// update port/channel ids
path.EndpointA.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence)
path.EndpointA.ChannelConfig.PortID = types.PortID
path.EndpointB.ChannelID = channeltypes.FormatChannelIdentifier(channelSequenceB)

if err := path.EndpointA.ChanOpenInit(); err != nil {
return err
Expand Down
127 changes: 127 additions & 0 deletions x/ibc-hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# IBC-hooks

Taken from [osmosis](https://github.com/osmosis-labs/osmosis) `v14.0.0-rc1` (commit `26e2fad8e7b3eb7c33965360b31a593b392d7d75`)

Removed `ibc_callback` functionality since we already have similar [sudo callback mechanism](https://docs.neutron.org/neutron/transfer/overview#ibc-transfer-results-handover) in Transfer module.

Module https://github.com/osmosis-labs/osmosis/tree/v14.0.0-rc1/x/ibc-hooks

## Wasm Hooks

The wasm hook is an IBC middleware which is used to allow ICS-20 token transfers to initiate contract calls.
This allows cross-chain contract calls, that involve token movement.
This is useful for a variety of usecases.
One of primary importance is cross-chain swaps, which is an extremely powerful primitive.

The mechanism enabling this is a `memo` field on every ICS20 transfer packet as of [IBC v3.4.0](https://medium.com/the-interchain-foundation/moving-beyond-simple-token-transfers-d42b2b1dc29b).
Wasm hooks is an IBC middleware that parses an ICS20 transfer, and if the `memo` field is of a particular form, executes a wasm contract call. We now detail the `memo` format for `wasm` contract calls, and the execution guarantees provided.

### Cosmwasm Contract Execution Format

Before we dive into the IBC metadata format, we show the cosmwasm execute message format, so the reader has a sense of what are the fields we need to be setting in.
The cosmwasm `MsgExecuteContract` is defined [here](https://github.com/CosmWasm/wasmd/blob/4fe2fbc8f322efdaf187e2e5c99ce32fd1df06f0/x/wasm/types/tx.pb.go#L340-L349
) as the following type:

```go
type MsgExecuteContract struct {
// Sender is the that actor that signed the messages
Sender string
// Contract is the address of the smart contract
Contract string
// Msg json encoded message to be passed to the contract
Msg RawContractMessage
// Funds coins that are transferred to the contract on execution
Funds sdk.Coins
}
```

So we detail where we want to get each of these fields from:

* Sender: We cannot trust the sender of an IBC packet, the counterparty chain has full ability to lie about it.
We cannot risk this sender being confused for a particular user or module address on Neutron.
So we replace the sender with an account to represent the sender prefixed by the channel and a wasm module prefix.
This is done by setting the sender to `Bech32(Hash("ibc-wasm-hook-intermediaryg" || channelID || sender))`, where the channelId is the channel id on the local chain.
* Contract: This field should be directly obtained from the ICS-20 packet metadata
* Msg: This field should be directly obtained from the ICS-20 packet metadata.
* Funds: This field is set to the amount of funds being sent over in the ICS 20 packet. One detail is that the denom in the packet is the counterparty chains representation of the denom, so we have to translate it to Neutron' representation.

So our constructed cosmwasm message that we execute will look like:

```go
msg := MsgExecuteContract{
// Sender is the that actor that signed the messages
Sender: "ntrn-hash-of-channel-and-sender",
// Contract is the address of the smart contract
Contract: packet.data.memo["wasm"]["ContractAddress"],
// Msg json encoded message to be passed to the contract
Msg: packet.data.memo["wasm"]["Msg"],
// Funds coins that are transferred to the contract on execution
Funds: sdk.NewCoin{Denom: ibc.ConvertSenderDenomToLocalDenom(packet.data.Denom), Amount: packet.data.Amount}
}
```

### ICS20 packet structure

So given the details above, we propagate the implied ICS20 packet data structure.
ICS20 is JSON native, so we use JSON for the memo format.

```json
{
//... other ibc fields that we don't care about
"data":{
"denom": "denom on counterparty chain (e.g. uatom)", // will be transformed to the local denom (ibc/...)
"amount": "1000",
"sender": "addr on counterparty chain", // will be transformed
"receiver": "contract addr or blank",
"memo": {
"wasm": {
"contract": "ntrnContractAddr",
"msg": {
"raw_message_fields": "raw_message_data",
}
}
}
}
}
```

An ICS20 packet is formatted correctly for wasmhooks iff the following all hold:

* `memo` is not blank
* `memo` is valid JSON
* `memo` has at least one key, with value `"wasm"`
* `memo["wasm"]` has exactly two entries, `"contract"` and `"msg"`
* `memo["wasm"]["msg"]` is a valid JSON object
* `receiver == "" || receiver == memo["wasm"]["contract"]`

We consider an ICS20 packet as directed towards wasmhooks iff all of the following hold:

* `memo` is not blank
* `memo` is valid JSON
* `memo` has at least one key, with name `"wasm"`

If an ICS20 packet is not directed towards wasmhooks, wasmhooks doesn't do anything.
If an ICS20 packet is directed towards wasmhooks, and is formatted incorrectly, then wasmhooks returns an error.

### Execution flow

Pre wasm hooks:

* Ensure the incoming IBC packet is cryptographically valid
* Ensure the incoming IBC packet is not timed out.

In Wasm hooks, pre packet execution:

* Ensure the packet is correctly formatted (as defined above)
* Edit the receiver to be the hardcoded IBC module account

In wasm hooks, post packet execution:

* Construct wasm message as defined before
* Execute wasm message
* if wasm message has error, return ErrAck
* otherwise continue through middleware

# Testing strategy

See go tests.
4 changes: 4 additions & 0 deletions x/ibc-hooks/bytecode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Description
This is a place for bytecode to test ibc-hooks.

- echo.wasm is an echo contract compiled from https://github.com/neutron-org/neutron-dev-contracts/
Binary file added x/ibc-hooks/bytecode/echo.wasm
Binary file not shown.
Loading

0 comments on commit 731f9a4

Please sign in to comment.