Skip to content

Commit

Permalink
Polish contract callback methods naming
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Aug 20, 2020
1 parent 19a76a4 commit 92f9b28
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 85 deletions.
32 changes: 16 additions & 16 deletions x/wasm/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (i IBCHandler) OnChanOpenInit(ctx sdk.Context, order channeltypes.Order, co
return sdkerrors.Wrapf(err, "contract port id")
}

err = i.keeper.AcceptChannel(ctx, contractAddr, order, version, connectionHops, cosmwasm.IBCInfo{
PortID: portID,
ChannelID: channelID,
err = i.keeper.OnOpenChannel(ctx, contractAddr, order, version, connectionHops, cosmwasm.IBCInfo{
Port: portID,
Channel: channelID,
})
if err != nil {
return err
Expand All @@ -50,9 +50,9 @@ func (i IBCHandler) OnChanOpenTry(ctx sdk.Context, order channeltypes.Order, con
return sdkerrors.Wrapf(err, "contract port id")
}

err = i.keeper.AcceptChannel(ctx, contractAddr, order, version, connectionHops, cosmwasm.IBCInfo{
PortID: portID,
ChannelID: channelID,
err = i.keeper.OnOpenChannel(ctx, contractAddr, order, version, connectionHops, cosmwasm.IBCInfo{
Port: portID,
Channel: channelID,
})
if err != nil {
return err
Expand All @@ -73,9 +73,9 @@ func (i IBCHandler) OnChanOpenAck(ctx sdk.Context, portID, channelID string, cou
if !ok {
return sdkerrors.Wrap(types.ErrInvalidCounterparty, "not found")
}
return i.keeper.OnOpenChannel(ctx, contractAddr, channelInfo.Counterparty, counterpartyVersion, cosmwasm.IBCInfo{
PortID: portID,
ChannelID: channelID,
return i.keeper.OnConnectChannel(ctx, contractAddr, channelInfo.Counterparty, counterpartyVersion, cosmwasm.IBCInfo{
Port: portID,
Channel: channelID,
})
}

Expand All @@ -88,9 +88,9 @@ func (i IBCHandler) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string)
if !ok {
return sdkerrors.Wrap(types.ErrInvalidCounterparty, "not found")
}
return i.keeper.OnOpenChannel(ctx, contractAddr, channelInfo.Counterparty, channelInfo.Version, cosmwasm.IBCInfo{
PortID: portID,
ChannelID: channelID,
return i.keeper.OnConnectChannel(ctx, contractAddr, channelInfo.Counterparty, channelInfo.Version, cosmwasm.IBCInfo{
Port: portID,
Channel: channelID,
})
}

Expand All @@ -106,7 +106,7 @@ func (i IBCHandler) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string
//if err != nil {
// return sdkerrors.Wrapf(err, "contract port id")
//}
//return i.keeper.OnChannelClose(ctx, contractAddr, cosmwasm.IBCInfo{PortID: portID, ChannelID: channelID})
//return i.keeper.OnChannelClose(ctx, contractAddr, cosmwasm.IBCInfo{Port: portID, Channel: channelID})
// any events to send?
panic("not implemented")
}
Expand Down Expand Up @@ -202,8 +202,8 @@ func (i IBCHandler) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet)

func ibcInfoFromPacket(packet channeltypes.Packet) cosmwasm.IBCInfo {
return cosmwasm.IBCInfo{
PortID: packet.DestinationPort,
ChannelID: packet.DestinationChannel,
Packet: cosmwasm.NewIBCPacketInfo(packet.Sequence, packet.SourcePort, packet.SourceChannel, packet.TimeoutHeight, packet.TimeoutTimestamp),
Port: packet.DestinationPort,
Channel: packet.DestinationChannel,
Packet: cosmwasm.NewIBCPacketInfo(packet.Sequence, packet.SourcePort, packet.SourceChannel, packet.TimeoutHeight, packet.TimeoutTimestamp),
}
}
50 changes: 25 additions & 25 deletions x/wasm/internal/keeper/cosmwasm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

type OnReceiveIBCResponse struct {
Messages []sdk.Msg `json:"messages"`

Acknowledgement []byte
// log message to return over abci interface
Log []wasmTypes.LogAttribute `json:"log"`
type IBCPacketReceiveResponse struct {
// Acknowledgement contains the data to acknowledge the ibc packet execution
Acknowledgement []byte `json:"acknowledgement"`
// Messages comes directly from the contract and is it's request for action
Messages []sdk.Msg `json:"messages,omitempty"`
// Log contains event attributes to expose over abci interface
Log []wasmTypes.LogAttribute `json:"log,omitempty"`
}

type OnAcknowledgeIBCResponse struct {
Messages []sdk.Msg `json:"messages"`

// log message to return over abci interface
Log []wasmTypes.LogAttribute `json:"log"`
type IBCPacketAcknowledgementResponse struct {
Messages []sdk.Msg `json:"messages"`
Log []wasmTypes.LogAttribute `json:"log"`
}
type OnTimeoutIBCResponse struct {
Messages []sdk.Msg `json:"messages"`

// log message to return over abci interface
Log []wasmTypes.LogAttribute `json:"log"`
type IBCPacketTimeoutResponse struct {
Messages []sdk.Msg `json:"messages"`
Log []wasmTypes.LogAttribute `json:"log"`
}

// OnConnectIBCResponse response to a channel open event
type OnConnectIBCResponse struct {
Messages []sdk.Msg `json:"messages"`
type IBCChannelOpenResponse struct {
// Result contains a boolean if the channel would be accepted
Result bool `json:"result"`
// Reason optional description why it was not accepted
Reason string `json:"reason"`
}

// log message to return over abci interface
Log []wasmTypes.LogAttribute `json:"log"`
type IBCChannelConnectResponse struct {
Messages []sdk.Msg `json:"messages"`
Log []wasmTypes.LogAttribute `json:"log"`
}

// AcceptChannelResponse is a frame for flow control in wasmd.
type AcceptChannelResponse struct {
Result bool `json:"result"`
Reason string `json:"reason"`
RestrictCounterpartyVersions string `json:"accepted_counterparty_version"` // todo: return only 1
type IBCChannelCloseResponse struct {
Messages []sdk.Msg `json:"messages"`
Log []wasmTypes.LogAttribute `json:"log"`
}
18 changes: 10 additions & 8 deletions x/wasm/internal/keeper/cosmwasm/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type Env struct {
Block wasmtypes.BlockInfo `json:"block"`
Message wasmtypes.MessageInfo `json:"message"`
Contract wasmtypes.ContractInfo `json:"contract"`
IBC *IBCInfo
// optional IBC meta data only set when called in IBC context
IBC *IBCInfo `json:"ibc,omitempty"`
}

func NewEnv(ctx sdk.Context, creator sdk.AccAddress, deposit sdk.Coins, contractAddr sdk.AccAddress) Env {
Expand Down Expand Up @@ -39,17 +40,18 @@ func NewEnv(ctx sdk.Context, creator sdk.AccAddress, deposit sdk.Coins, contract
}

type IBCInfo struct {
// PortID of the contract
PortID string
// ChannelID to the contract
ChannelID string
Packet *IBCPacketInfo `json:"packet,omitempty"`
// Port of the contract
Port string `json:"port"`
// Channel to the contract
Channel string `json:"channel"`
// Optional packet meta data when called on IBC packet functions
Packet *IBCPacketInfo `json:"packet,omitempty"`
}

func (i IBCInfo) AsPacket(data []byte) channeltypes.Packet {
r := channeltypes.Packet{
DestinationPort: i.PortID,
DestinationChannel: i.ChannelID,
DestinationPort: i.Port,
DestinationChannel: i.Channel,
}
if i.Packet == nil {
return r
Expand Down
43 changes: 25 additions & 18 deletions x/wasm/internal/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,30 @@ func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability
return k.ScopedKeeper.ClaimCapability(ctx, cap, name)
}

// IBCContractCallbacks the contract methods that should be implemeted in rust without `ctx sdk.Context`
// IBCContractCallbacks defines the methods for go-cosmwasm to interact with the wasm contract.
// A mock contract would implement the interface to fully simulate a wasm contract's behaviour.
type IBCContractCallbacks interface {
// todo: Rename methods #214

// IBC packet lifecycle
OnReceive(hash []byte, params cosmwasm.Env, msg []byte, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.OnReceiveIBCResponse, uint64, error)
OnAcknowledgement(hash []byte, params cosmwasm.Env, originalData []byte, acknowledgement []byte, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.OnAcknowledgeIBCResponse, uint64, error)
OnTimeout(hash []byte, params cosmwasm.Env, msg []byte, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.OnTimeoutIBCResponse, uint64, error)
// IBC channel livecycle
AcceptChannel(hash []byte, params cosmwasm.Env, order channeltypes.Order, version string, connectionHops []string, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.AcceptChannelResponse, uint64, error)
OnConnect(hash []byte, params cosmwasm.Env, counterpartyPortID string, counterpartyChannelID string, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.OnConnectIBCResponse, uint64, error)
//OnClose(ctx sdk.Context, hash []byte, params cosmwasm.Env, counterpartyPortID string, counterpartyChannelID string, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.OnCloseIBCResponse, uint64, error)
// Package livecycle

// OnIBCPacketReceive handles an incoming IBC package
OnIBCPacketReceive(hash []byte, params cosmwasm.Env, msg []byte, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.IBCPacketReceiveResponse, uint64, error)
// OnIBCPacketAcknowledgement handles a IBC package execution on the counterparty chain
OnIBCPacketAcknowledgement(hash []byte, params cosmwasm.Env, originalData []byte, acknowledgement []byte, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.IBCPacketAcknowledgementResponse, uint64, error)
// OnIBCPacketTimeout reverts state when the IBC package execution does not come in time
OnIBCPacketTimeout(hash []byte, params cosmwasm.Env, msg []byte, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.IBCPacketTimeoutResponse, uint64, error)
// channel livecycle

// OnIBCChannelOpen does the protocol version negotiation during channel handshake phase
OnIBCChannelOpen(hash []byte, params cosmwasm.Env, order channeltypes.Order, version string, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.IBCChannelOpenResponse, uint64, error)
// OnIBCChannelConnect callback when a IBC channel is established
OnIBCChannelConnect(hash []byte, params cosmwasm.Env, counterpartyPortID, counterpartyChannelID string, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.IBCChannelConnectResponse, uint64, error)
// OnIBCChannelConnect callback when a IBC channel is closed
OnIBCChannelClose(ctx sdk.Context, hash []byte, params cosmwasm.Env, counterpartyPortID, counterpartyChannelID string, store prefix.Store, api wasm.GoAPI, querier QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasm.IBCChannelCloseResponse, uint64, error)
}

var MockContracts = make(map[string]IBCContractCallbacks, 0)

func (k Keeper) AcceptChannel(ctx sdk.Context, contractAddr sdk.AccAddress, order channeltypes.Order, version string, connectionHops []string, ibcInfo cosmwasm.IBCInfo) error {
func (k Keeper) OnOpenChannel(ctx sdk.Context, contractAddr sdk.AccAddress, order channeltypes.Order, version string, connectionHops []string, ibcInfo cosmwasm.IBCInfo) error {
codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
Expand All @@ -99,7 +106,7 @@ func (k Keeper) AcceptChannel(ctx sdk.Context, contractAddr sdk.AccAddress, orde
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.AcceptChannel(codeInfo.CodeHash, params, order, version, connectionHops, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
res, gasUsed, execErr := mock.OnIBCChannelOpen(codeInfo.CodeHash, params, order, version, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down Expand Up @@ -130,7 +137,7 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, contractAddr sdk.AccAddress, paylo
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.OnReceive(codeInfo.CodeHash, params, payloadData, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
res, gasUsed, execErr := mock.OnIBCPacketReceive(codeInfo.CodeHash, params, payloadData, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return nil, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down Expand Up @@ -169,7 +176,7 @@ func (k Keeper) OnAckPacket(ctx sdk.Context, contractAddr sdk.AccAddress, payloa
if !ok {
panic("not supported")
}
res, gasUsed, execErr := mock.OnAcknowledgement(codeInfo.CodeHash, params, payloadData, acknowledgement, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
res, gasUsed, execErr := mock.OnIBCPacketAcknowledgement(codeInfo.CodeHash, params, payloadData, acknowledgement, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down Expand Up @@ -208,7 +215,7 @@ func (k Keeper) OnTimeoutPacket(ctx sdk.Context, contractAddr sdk.AccAddress, pa
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.OnTimeout(codeInfo.CodeHash, params, payloadData, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
res, gasUsed, execErr := mock.OnIBCPacketTimeout(codeInfo.CodeHash, params, payloadData, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand All @@ -227,7 +234,7 @@ func (k Keeper) OnTimeoutPacket(ctx sdk.Context, contractAddr sdk.AccAddress, pa
return nil
}

func (k Keeper) OnOpenChannel(ctx sdk.Context, contractAddr sdk.AccAddress, counterparty channeltypes.Counterparty, version string, ibcInfo cosmwasm.IBCInfo) error {
func (k Keeper) OnConnectChannel(ctx sdk.Context, contractAddr sdk.AccAddress, counterparty channeltypes.Counterparty, version string, ibcInfo cosmwasm.IBCInfo) error {
codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
Expand All @@ -247,7 +254,7 @@ func (k Keeper) OnOpenChannel(ctx sdk.Context, contractAddr sdk.AccAddress, coun
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.OnConnect(codeInfo.CodeHash, params, counterparty.PortId, counterparty.ChannelId, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
res, gasUsed, execErr := mock.OnIBCChannelConnect(codeInfo.CodeHash, params, counterparty.PortId, counterparty.ChannelId, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down
Loading

0 comments on commit 92f9b28

Please sign in to comment.