Skip to content

Commit

Permalink
Expose ibc v3 entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed May 16, 2022
1 parent fb175d0 commit 76a5053
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
19 changes: 11 additions & 8 deletions x/wasm/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (i IBCHandler) OnChanOpenInit(
},
},
}
err = i.keeper.OnOpenChannel(ctx, contractAddr, msg)
_, err = i.keeper.OnOpenChannel(ctx, contractAddr, msg)
if err != nil {
return err
}
Expand Down Expand Up @@ -103,11 +103,15 @@ func (i IBCHandler) OnChanOpenTry(
},
}

// DESIGN V3: Allow contracts to return a version (or default to counterpartyVersion if unset)
err = i.keeper.OnOpenChannel(ctx, contractAddr, msg)
// Allow contracts to return a version (or default to counterpartyVersion if unset)
version, err := i.keeper.OnOpenChannel(ctx, contractAddr, msg)
if err != nil {
return "", err
}
if version == "" {
version = counterpartyVersion
}

// Module may have already claimed capability in OnChanOpenInit in the case of crossing hellos
// (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry)
// If module can already authenticate the capability then module already owns it so we don't need to claim
Expand All @@ -119,9 +123,7 @@ func (i IBCHandler) OnChanOpenTry(
}
}

// In the future, we can negotiate (see design comment above), but for now, we only error if we disagee
// with the proposed version
return counterpartyVersion, nil
return version, nil
}

// OnChanOpenAck implements the IBCModule interface
Expand Down Expand Up @@ -240,7 +242,7 @@ func (i IBCHandler) OnRecvPacket(
if err != nil {
return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrapf(err, "contract port id").Error())
}
msg := wasmvmtypes.IBCPacketReceiveMsg{Packet: newIBCPacket(packet)}
msg := wasmvmtypes.IBCPacketReceiveMsg{Packet: newIBCPacket(packet), Relayer: relayer.String()}
ack, err := i.keeper.OnRecvPacket(ctx, contractAddr, msg)
if err != nil {
return channeltypes.NewErrorAcknowledgement(err.Error())
Expand Down Expand Up @@ -275,6 +277,7 @@ func (i IBCHandler) OnAcknowledgementPacket(
err = i.keeper.OnAckPacket(ctx, contractAddr, wasmvmtypes.IBCPacketAckMsg{
Acknowledgement: wasmvmtypes.IBCAcknowledgement{Data: acknowledgement},
OriginalPacket: newIBCPacket(packet),
Relayer: relayer.String(),
})
if err != nil {
return sdkerrors.Wrap(err, "on ack")
Expand All @@ -288,7 +291,7 @@ func (i IBCHandler) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet,
if err != nil {
return sdkerrors.Wrapf(err, "contract port id")
}
msg := wasmvmtypes.IBCPacketTimeoutMsg{Packet: newIBCPacket(packet)}
msg := wasmvmtypes.IBCPacketTimeoutMsg{Packet: newIBCPacket(packet), Relayer: relayer.String()}
err = i.keeper.OnTimeoutPacket(ctx, contractAddr, msg)
if err != nil {
return sdkerrors.Wrap(err, "on timeout")
Expand Down
16 changes: 10 additions & 6 deletions x/wasm/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,30 @@ func (k Keeper) OnOpenChannel(
ctx sdk.Context,
contractAddr sdk.AccAddress,
msg wasmvmtypes.IBCChannelOpenMsg,
) error {
) (string, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-open-channel")
version := ""

_, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
return "", err
}

env := types.NewEnv(ctx, contractAddr)
querier := k.newQueryHandler(ctx, contractAddr)

gas := k.runtimeGasForContract(ctx)
// TODO: we probably want to use the response somehow
_, gasUsed, execErr := k.wasmVM.IBCChannelOpen(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.IBCChannelOpen(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
return "", sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
}

if res != nil {
version = res.Version
}

return nil
return version, nil
}

// OnConnectChannel calls the contract to let it know the IBC channel was established.
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestOnOpenChannel(t *testing.T) {
CounterpartyVersion: "foo",
},
}
err := keepers.WasmKeeper.OnOpenChannel(ctx, spec.contractAddr, msg)
_, err := keepers.WasmKeeper.OnOpenChannel(ctx, spec.contractAddr, msg)

// then
if spec.expErr {
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/types/exported_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type IBCContractKeeper interface {
ctx sdk.Context,
contractAddr sdk.AccAddress,
msg wasmvmtypes.IBCChannelOpenMsg,
) error
) (string, error)
OnConnectChannel(
ctx sdk.Context,
contractAddr sdk.AccAddress,
Expand Down

0 comments on commit 76a5053

Please sign in to comment.