Skip to content

Commit

Permalink
Merge pull request #6 from confio/priv_stargate_extract_relay
Browse files Browse the repository at this point in the history
Extract relay.go
  • Loading branch information
alpe authored Jan 18, 2021
2 parents e1fb3d7 + c619549 commit d757176
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 160 deletions.
12 changes: 12 additions & 0 deletions x/wasm/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
"github.com/cosmos/cosmos-sdk/x/ibc/core/03-connection/types"
channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types"
porttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/05-port/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
"math"
)

var _ porttypes.IBCModule = IBCHandler{}

type IBCHandler struct {
keeper Keeper
channelKeeper wasmTypes.ChannelKeeper
Expand All @@ -21,6 +24,7 @@ func NewIBCHandler(keeper Keeper) IBCHandler {
return IBCHandler{keeper: keeper, channelKeeper: keeper.ChannelKeeper}
}

// OnChanOpenInit implements the IBCModule interface
func (i IBCHandler) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
Expand Down Expand Up @@ -57,6 +61,7 @@ func (i IBCHandler) OnChanOpenInit(
return nil
}

// OnChanOpenTry implements the IBCModule interface
func (i IBCHandler) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
Expand Down Expand Up @@ -100,6 +105,7 @@ func (i IBCHandler) OnChanOpenTry(
return nil
}

// OnChanOpenAck implements the IBCModule interface
func (i IBCHandler) OnChanOpenAck(
ctx sdk.Context,
portID, channelID string,
Expand All @@ -123,6 +129,7 @@ func (i IBCHandler) OnChanOpenAck(
})
}

// OnChanOpenConfirm implements the IBCModule interface
func (i IBCHandler) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string) error {
contractAddr, err := ContractFromPortID(portID)
if err != nil {
Expand All @@ -141,11 +148,13 @@ func (i IBCHandler) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string)
})
}

// OnChanCloseInit implements the IBCModule interface
func (i IBCHandler) OnChanCloseInit(ctx sdk.Context, portID, channelID string) error {
// we can let contracts close channels so we can play back this to the contract
panic("not implemented")
}

// OnChanCloseConfirm implements the IBCModule interface
func (i IBCHandler) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string) error {
// counterparty has closed the channel

Expand All @@ -158,6 +167,7 @@ func (i IBCHandler) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string
panic("not implemented")
}

// OnRecvPacket implements the IBCModule interface
func (i IBCHandler) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
Expand Down Expand Up @@ -189,6 +199,7 @@ func (i IBCHandler) OnRecvPacket(
}, msgBz, nil
}

// OnAcknowledgementPacket implements the IBCModule interface
func (i IBCHandler) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte) (*sdk.Result, error) {
contractAddr, err := ContractFromPortID(packet.SourcePort)
if err != nil {
Expand Down Expand Up @@ -228,6 +239,7 @@ func (i IBCHandler) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes

}

// OnTimeoutPacket implements the IBCModule interface
func (i IBCHandler) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet) (*sdk.Result, error) {
contractAddr, err := ContractFromPortID(packet.DestinationPort)
if err != nil {
Expand Down
160 changes: 0 additions & 160 deletions x/wasm/internal/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,163 +91,3 @@ type IBCContractCallbacks interface {
}

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

func (k Keeper) OnOpenChannel(ctx sdk.Context, contractAddr sdk.AccAddress, channel cosmwasm.IBCChannel) error {
_, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
}

params := types.NewEnv(ctx, contractAddr)
querier := QueryHandler{
Ctx: ctx,
Plugins: k.queryPlugins,
}

gas := gasForContract(ctx)
mock, ok := MockContracts[contractAddr.String()]
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.OnIBCChannelOpen(codeInfo.CodeHash, params, channel, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
}
if !res.Success { // todo: would it make more sense to let the contract return an error instead?
return sdkerrors.Wrap(types.ErrInvalid, res.Reason)
}
return nil
}

func (k Keeper) OnRecvPacket(ctx sdk.Context, contractAddr sdk.AccAddress, packet cosmwasm.IBCPacket) ([]byte, error) {
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return nil, err
}

params := types.NewEnv(ctx, contractAddr)
querier := QueryHandler{
Ctx: ctx,
Plugins: k.queryPlugins,
}

gas := gasForContract(ctx)
mock, ok := MockContracts[contractAddr.String()]
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.OnIBCPacketReceive(codeInfo.CodeHash, params, packet, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return nil, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
}

// emit all events from this contract itself
events := types.ParseEvents(res.Attributes, contractAddr)
ctx.EventManager().EmitEvents(events)

if err := k.messenger.DispatchV2(ctx, contractAddr, contractInfo.IBCPortID, res.Messages...); err != nil {
return nil, err
}
return res.Acknowledgement, nil
}

func (k Keeper) OnAckPacket(ctx sdk.Context, contractAddr sdk.AccAddress, acknowledgement cosmwasm.IBCAcknowledgement) error {
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
}

params := types.NewEnv(ctx, contractAddr)
querier := QueryHandler{
Ctx: ctx,
Plugins: k.queryPlugins,
}

gas := gasForContract(ctx)
mock, ok := MockContracts[contractAddr.String()] // hack for testing without wasmer
if !ok {
panic("not supported")
}
res, gasUsed, execErr := mock.OnIBCPacketAcknowledgement(codeInfo.CodeHash, params, acknowledgement, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
}

// emit all events from this contract itself
events := types.ParseEvents(res.Attributes, contractAddr)
ctx.EventManager().EmitEvents(events)

if err := k.messenger.DispatchV2(ctx, contractAddr, contractInfo.IBCPortID, res.Messages...); err != nil {
return err
}
return nil
}

func (k Keeper) OnTimeoutPacket(ctx sdk.Context, contractAddr sdk.AccAddress, packet cosmwasm.IBCPacket) error {
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
}

params := types.NewEnv(ctx, contractAddr)
querier := QueryHandler{
Ctx: ctx,
Plugins: k.queryPlugins,
}

gas := gasForContract(ctx)
mock, ok := MockContracts[contractAddr.String()]
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.OnIBCPacketTimeout(codeInfo.CodeHash, params, packet, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
}

// emit all events from this contract itself
events := types.ParseEvents(res.Attributes, contractAddr)
ctx.EventManager().EmitEvents(events)

if err := k.messenger.DispatchV2(ctx, contractAddr, contractInfo.IBCPortID, res.Messages...); err != nil {
return err
}
return nil
}

func (k Keeper) OnConnectChannel(ctx sdk.Context, contractAddr sdk.AccAddress, channel cosmwasm.IBCChannel) error {
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
}

params := types.NewEnv(ctx, contractAddr)
querier := QueryHandler{
Ctx: ctx,
Plugins: k.queryPlugins,
}

gas := gasForContract(ctx)
mock, ok := MockContracts[contractAddr.String()]
if !ok { // hack for testing without wasmer
panic("not supported")
}
res, gasUsed, execErr := mock.OnIBCChannelConnect(codeInfo.CodeHash, params, channel, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
consumeGas(ctx, gasUsed)
if execErr != nil {
return sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
}

// emit all events from this contract itself
events := types.ParseEvents(res.Attributes, contractAddr)
ctx.EventManager().EmitEvents(events)

if err := k.messenger.DispatchV2(ctx, contractAddr, contractInfo.IBCPortID, res.Messages...); err != nil {
return err
}
return nil
}
Loading

0 comments on commit d757176

Please sign in to comment.