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: apply latest ibc-hook support #11

Merged
merged 2 commits into from
Mar 8, 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
66 changes: 52 additions & 14 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ import (

initiaapplanes "github.com/initia-labs/initia/app/lanes"
initiaappparams "github.com/initia-labs/initia/app/params"
ibchooks "github.com/initia-labs/initia/x/ibc-hooks"
ibchookskeeper "github.com/initia-labs/initia/x/ibc-hooks/keeper"
ibchookstypes "github.com/initia-labs/initia/x/ibc-hooks/types"
"github.com/initia-labs/initia/x/ibc/fetchprice"
fetchpricekeeper "github.com/initia-labs/initia/x/ibc/fetchprice/keeper"
fetchpricetypes "github.com/initia-labs/initia/x/ibc/fetchprice/types"
Expand Down Expand Up @@ -139,10 +142,9 @@ import (
// local imports
appante "github.com/initia-labs/miniwasm/app/ante"
apphook "github.com/initia-labs/miniwasm/app/hook"
wasmibcmiddleware "github.com/initia-labs/miniwasm/app/ibc-middleware"
ibcwasmhooks "github.com/initia-labs/miniwasm/app/ibc-hooks"
appkeepers "github.com/initia-labs/miniwasm/app/keepers"
applanes "github.com/initia-labs/miniwasm/app/lanes"

"github.com/initia-labs/miniwasm/x/bank"
bankkeeper "github.com/initia-labs/miniwasm/x/bank/keeper"
"github.com/initia-labs/miniwasm/x/tokenfactory"
Expand Down Expand Up @@ -229,6 +231,7 @@ type MinitiaApp struct {
OracleKeeper *oraclekeeper.Keeper // x/oracle keeper used for the slinky oracle
FetchPriceKeeper *fetchpricekeeper.Keeper
TokenFactoryKeeper *tokenfactorykeeper.Keeper
IBCHooksKeeper *ibchookskeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -284,6 +287,7 @@ func NewMinitiaApp(
ibcfeetypes.StoreKey, wasmtypes.StoreKey, opchildtypes.StoreKey,
auctiontypes.StoreKey, packetforwardtypes.StoreKey, icqtypes.StoreKey,
oracletypes.StoreKey, fetchpricetypes.StoreKey, tokenfactorytypes.StoreKey,
ibchookstypes.StoreKey,
)
tkeys := storetypes.NewTransientStoreKeys()
memKeys := storetypes.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -444,6 +448,13 @@ func NewMinitiaApp(
)
app.IBCFeeKeeper = &ibcFeeKeeper

app.IBCHooksKeeper = ibchookskeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[ibchookstypes.StoreKey]),
authorityAddr,
ac,
)

////////////////////////////
// Transfer configuration //
////////////////////////////
Expand All @@ -453,7 +464,6 @@ func NewMinitiaApp(
var transferStack porttypes.IBCModule
{
packetForwardKeeper := &packetforwardkeeper.Keeper{}
wasmMiddleware := &wasmibcmiddleware.IBCMiddleware{}

// Create Transfer Keepers
transferKeeper := ibctransferkeeper.NewKeeper(
Expand All @@ -480,8 +490,8 @@ func NewMinitiaApp(
app.IBCKeeper.ChannelKeeper,
communityPoolKeeper,
app.BankKeeper,
// ics4wrapper: transfer -> packet forward -> wasm
wasmMiddleware,
// ics4wrapper: transfer -> packet forward -> fee
app.IBCFeeKeeper,
authorityAddr,
)
app.PacketForwardKeeper = packetForwardKeeper
Expand All @@ -494,19 +504,21 @@ func NewMinitiaApp(
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp,
)

// create move middleware for transfer
*wasmMiddleware = wasmibcmiddleware.NewIBCMiddleware(
// create wasm middleware for transfer
hookMiddleware := ibchooks.NewIBCMiddleware(
// receive: wasm -> packet forward -> transfer
packetForwardMiddleware,
// ics4wrapper: transfer -> packet forward -> wasm -> fee
app.IBCFeeKeeper,
app.WasmKeeper,
ibchooks.NewICS4Middleware(
nil, /* ics4wrapper: not used */
ibcwasmhooks.NewWasmHooks(app.WasmKeeper, ac),
),
app.IBCHooksKeeper,
)

// create ibcfee middleware for transfer
transferStack = ibcfee.NewIBCMiddleware(
// receive: fee -> wasm -> packet forward -> transfer
wasmMiddleware,
hookMiddleware,
// ics4wrapper: transfer -> packet forward -> wasm -> fee -> channel
*app.IBCFeeKeeper,
)
Expand Down Expand Up @@ -563,8 +575,32 @@ func NewMinitiaApp(
// Wasm IBC Configuration //
//////////////////////////////

wasmIBCModule := wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper, app.IBCFeeKeeper)
wasmIBCStack := ibcfee.NewIBCMiddleware(wasmIBCModule, *app.IBCFeeKeeper)
var wasmIBCStack porttypes.IBCModule
{
wasmIBCModule := wasm.NewIBCHandler(
app.WasmKeeper,
app.IBCKeeper.ChannelKeeper,
// ics4wrapper: wasm -> fee
app.IBCFeeKeeper,
)

// create wasm middleware for wasm IBC stack
hookMiddleware := ibchooks.NewIBCMiddleware(
// receive: hook -> wasm
wasmIBCModule,
ibchooks.NewICS4Middleware(
nil, /* ics4wrapper: not used */
ibcwasmhooks.NewWasmHooks(app.WasmKeeper, ac),
),
app.IBCHooksKeeper,
)

wasmIBCStack = ibcfee.NewIBCMiddleware(
// receive: fee -> hook -> wasm
hookMiddleware,
*app.IBCFeeKeeper,
)
}

///////////////////////
// ICQ configuration //
Expand Down Expand Up @@ -718,6 +754,7 @@ func NewMinitiaApp(
consensus.NewAppModule(appCodec, *app.ConsensusParamsKeeper),
wasm.NewAppModule(appCodec, app.WasmKeeper, nil /* unused */, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), nil),
auction.NewAppModule(app.appCodec, *app.AuctionKeeper),
tokenfactory.NewAppModule(appCodec, *app.TokenFactoryKeeper, *app.AccountKeeper, *app.BankKeeper),
// ibc modules
ibc.NewAppModule(app.IBCKeeper),
ibctransfer.NewAppModule(*app.TransferKeeper),
Expand All @@ -729,9 +766,9 @@ func NewMinitiaApp(
packetforward.NewAppModule(app.PacketForwardKeeper, nil),
icq.NewAppModule(*app.ICQKeeper, nil),
fetchprice.NewAppModule(appCodec, *app.FetchPriceKeeper),
ibchooks.NewAppModule(appCodec, *app.IBCHooksKeeper),
// slinky modules
oracle.NewAppModule(appCodec, *app.OracleKeeper),
tokenfactory.NewAppModule(appCodec, *app.TokenFactoryKeeper, *app.AccountKeeper, *app.BankKeeper),
)

// BasicModuleManager defines the module BasicManager is in charge of setting up basic,
Expand Down Expand Up @@ -782,6 +819,7 @@ func NewMinitiaApp(
ibcfeetypes.ModuleName, auctiontypes.ModuleName,
wasmtypes.ModuleName, oracletypes.ModuleName, packetforwardtypes.ModuleName,
icqtypes.ModuleName, fetchpricetypes.ModuleName, tokenfactorytypes.ModuleName,
ibchookstypes.ModuleName,
}

app.ModuleManager.SetOrderInitGenesis(genesisModuleOrder...)
Expand Down
103 changes: 92 additions & 11 deletions app/ibc-middleware/README.md → app/ibc-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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).
The mechanism enabling this is a `memo` field on every ICS20 or ICS721 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
Expand All @@ -17,6 +17,17 @@ The cosmwasm `MsgExecuteContract` is defined [here](https://github.com/CosmWasm/
) as the following type:

```go
// HookData defines a wrapper for wasm execute message
// and async callback.
type HookData struct {
// Message is a wasm execute message which will be executed
// at `OnRecvPacket` of receiver chain.
Message *wasmtypes.MsgExecuteContract `json:"message,omitempty"`

// AsyncCallback is a contract address
AsyncCallback string `json:"async_callback,omitempty"`
}

type MsgExecuteContract struct {
// Sender is the actor that committed the message in the sender chain
Sender string
Expand Down Expand Up @@ -46,13 +57,14 @@ So our constructed cosmwasm message that we execute will look like:
```go
msg := MsgExecuteContract{
// Sender is the that actor that signed the messages
Sender: "osmo1-hash-of-channel-and-sender",
Sender: "init1-hash-of-channel-and-sender",
// Contract is the address of the smart contract
Contract: packet.data.memo["wasm"]["ContractAddress"],
Contract: packet.data.memo["wasm"]["contract"],
// Msg json encoded message to be passed to the contract
Msg: packet.data.memo["wasm"]["Msg"],
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
Expand All @@ -68,12 +80,15 @@ ICS20 is JSON native, so we use JSON for the memo format.
"amount": "1000",
"sender": "addr on counterparty chain", // will be transformed
"receiver": "contract addr or blank",
"memo": {
"memo": {
"wasm": {
"contract": "osmo1contractAddr",
"contract": "init1contractAddr",
"msg": {
"raw_message_fields": "raw_message_data",
}
},
"funds": [
{"denom": "ibc/denom", "amount": "100"}
]
}
}
}
Expand All @@ -85,8 +100,8 @@ 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
* `memo["wasm"]["message"]` has exactly two entries, `"contract"`, `"msg"` and `"fund"`
* `memo["wasm"]["message"]["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:
Expand Down Expand Up @@ -117,6 +132,72 @@ In wasm hooks, post packet execution:
* if wasm message has error, return ErrAck
* otherwise continue through middleware

## Testing strategy
## Ack callbacks

A contract that sends an IBC transfer, may need to listen for the ACK from that packet. To allow
contracts to listen on the ack of specific packets, we provide Ack callbacks.

### Design

The sender of an IBC transfer packet may specify a callback for when the ack of that packet is received in the memo
field of the transfer packet.

Crucially, _only_ the IBC packet sender can set the callback.

### Use case

The crosschain swaps implementation sends an IBC transfer. If the transfer were to fail, we want to allow the sender
to be able to retrieve their funds (which would otherwise be stuck in the contract). To do this, we allow users to
retrieve the funds after the timeout has passed, but without the ack information, we cannot guarantee that the send
hasn't failed (i.e.: returned an error ack notifying that the receiving change didn't accept it)

### Implementation

#### Callback information in memo

See go tests.
For the callback to be processed, the transfer packet's memo should contain the following in its JSON:

```json
{
"wasm": {
"async_callback": "init1contractAddr"
}
}
```

When an ack is received, it will notify the specified contract via a sudo message.

#### Interface for receiving the Acks and Timeouts

The contract that awaits the callback should implement the following interface for a sudo message:

```rust
#[cw_serde]
pub enum IBCLifecycleComplete {
#[serde(rename = "ibc_ack")]
IBCAck {
/// The source channel (miniwasm side) of the IBC packet
channel: String,
/// The sequence number that the packet was sent with
sequence: u64,
/// String encoded version of the ack as seen by OnAcknowledgementPacket(..)
ack: String,
/// Weather an ack is a success of failure according to the transfer spec
success: bool,
},
#[serde(rename = "ibc_timeout")]
IBCTimeout {
/// The source channel (miniwasm side) of the IBC packet
channel: String,
/// The sequence number that the packet was sent with
sequence: u64,
},
}

/// Message type for `sudo` entry_point
#[cw_serde]
pub enum SudoMsg {
#[serde(rename = "ibc_lifecycle_complete")]
IBCLifecycleComplete(IBCLifecycleComplete),
}
```
Loading
Loading