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

Refactor ibc/mock/bank into ICS 20 #5264

Merged
merged 11 commits into from
Oct 30, 2019
1 change: 1 addition & 0 deletions x/ibc/20-transfer/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type (
ConnectionKeeper = types.ConnectionKeeper
SupplyKeeper = types.SupplyKeeper
MsgTransfer = types.MsgTransfer
MsgRecvPacket = types.MsgRecvPacket
PacketData = types.PacketData
PacketDataAlias = types.PacketDataAlias
)
62 changes: 62 additions & 0 deletions x/ibc/20-transfer/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package cli

import (
"fmt"
"io/ioutil"
"os"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/20-transfer/types"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
)

// IBC transfer flags
Expand All @@ -26,6 +34,7 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
}
txCmd.AddCommand(
GetTransferTxCmd(cdc),
GetMsgRecvPacketCmd(cdc),
)

return txCmd
Expand Down Expand Up @@ -68,3 +77,56 @@ func GetTransferTxCmd(cdc *codec.Codec) *cobra.Command {
cmd.Flags().Bool(FlagSource, false, "Pass flag for sending token from the source chain")
return cmd
}

// GetMsgRecvPacketCmd returns the command to create a MsgRecvTransferPacket transaction
func GetMsgRecvPacketCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "recv-packet [/path/to/packet-data.json] [/path/to/proof.json] [height]",
Short: "Creates and sends a SendPacket message",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc).WithBroadcastMode(flags.BroadcastBlock)

var packet channelexported.PacketI
if err := cdc.UnmarshalJSON([]byte(args[0]), &packet); err != nil {
fmt.Fprintf(os.Stderr, "failed to unmarshall input into struct, checking for file...\n")
contents, err := ioutil.ReadFile(args[0])
if err != nil {
return fmt.Errorf("error opening packet file: %v", err)
}

if err := cdc.UnmarshalJSON(contents, packet); err != nil {
return fmt.Errorf("error unmarshalling packet file: %v", err)
}
}

var proof commitment.Proof
if err := cdc.UnmarshalJSON([]byte(args[1]), &proof); err != nil {
fmt.Fprintf(os.Stderr, "failed to unmarshall input into struct, checking for file...\n")
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return fmt.Errorf("error opening proofs file: %v", err)
}
if err := cdc.UnmarshalJSON(contents, &proof); err != nil {
return fmt.Errorf("error unmarshalling proofs file: %v", err)
}
}

height, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return fmt.Errorf("error height: %v", err)
}

msg := types.NewMsgRecvPacket(packet, []commitment.Proof{proof}, height, cliCtx.GetFromAddress())
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd = client.PostCommands(cmd)[0]
return cmd
}
10 changes: 10 additions & 0 deletions x/ibc/20-transfer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import (
"github.com/cosmos/cosmos-sdk/x/ibc/20-transfer/types"
)

// handleMsgRecvPacket defines the sdk.Handler for MsgRecvPacket
func HandleMsgRecvPacket(ctx sdk.Context, k Keeper, msg MsgRecvPacket) (res sdk.Result) {
err := k.ReceivePacket(ctx, msg.Packet, msg.Proofs[0], msg.Height)
if err != nil {
return sdk.ResultFromError(err)
}

return sdk.Result{Events: ctx.EventManager().Events()}
}

// HandleMsgTransfer defines the sdk.Handler for MsgTransfer
func HandleMsgTransfer(ctx sdk.Context, k Keeper, msg MsgTransfer) (res sdk.Result) {
err := k.SendTransfer(ctx, msg.SourcePort, msg.SourceChannel, msg.Amount, msg.Sender, msg.Receiver, msg.Source)
Expand Down
18 changes: 18 additions & 0 deletions x/ibc/20-transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types"
"github.com/cosmos/cosmos-sdk/x/ibc/20-transfer/types"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
)

// SendTransfer handles transfer sending logic
Expand Down Expand Up @@ -50,6 +52,22 @@ func (k Keeper) SendTransfer(
return k.createOutgoingPacket(ctx, sequence, sourcePort, sourceChannel, destinationPort, destinationChannel, coins, sender, receiver, isSourceChain)
}

// ReceivePacket handles receiving packet
func (k Keeper) ReceivePacket(ctx sdk.Context, packet channelexported.PacketI, proof commitment.ProofI, height uint64) error {
_, err := k.channelKeeper.RecvPacket(ctx, packet, proof, height, nil, k.storeKey)
if err != nil {
return sdk.NewError(types.DefaultCodespace, types.CodeErrReceivePacket, "failed to receive packet")
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}

var data types.PacketData
err = data.UnmarshalJSON(packet.Data())
if err != nil {
return sdk.NewError(sdk.CodespaceType(types.DefaultCodespace), types.CodeInvalidPacketData, "invalid packet data")
cwgoes marked this conversation as resolved.
Show resolved Hide resolved
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}

return k.ReceiveTransfer(ctx, packet.SourcePort(), packet.SourceChannel(), packet.DestPort(), packet.DestChannel(), data)
}

// ReceiveTransfer handles transfer receiving logic
func (k Keeper) ReceiveTransfer(
ctx sdk.Context,
Expand Down
2 changes: 2 additions & 0 deletions x/ibc/20-transfer/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
CodeInvalidChannelOrder sdk.CodeType = 104
CodeInvalidPort sdk.CodeType = 105
CodeInvalidVersion sdk.CodeType = 106
CodeProofMissing sdk.CodeType = 107
CodeErrReceivePacket sdk.CodeType = 108
)

// ErrInvalidAddress implements sdk.Error
Expand Down
2 changes: 2 additions & 0 deletions x/ibc/20-transfer/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
connection "github.com/cosmos/cosmos-sdk/x/ibc/03-connection"
channel "github.com/cosmos/cosmos-sdk/x/ibc/04-channel"
channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
)

Expand All @@ -19,6 +20,7 @@ type ChannelKeeper interface {
GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channel.Channel, found bool)
GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool)
SendPacket(ctx sdk.Context, packet channelexported.PacketI, portCapability sdk.CapabilityKey) error
RecvPacket(ctx sdk.Context, packet channelexported.PacketI, proof commitment.ProofI, proofHeight uint64, acknowledgement []byte, portCapability sdk.CapabilityKey) (channelexported.PacketI, error)
}

// ClientKeeper defines the expected IBC client keeper
Expand Down
57 changes: 57 additions & 0 deletions x/ibc/20-transfer/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"

channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
ibctypes "github.com/cosmos/cosmos-sdk/x/ibc/types"
)

Expand Down Expand Up @@ -73,3 +76,57 @@ func (msg MsgTransfer) GetSignBytes() []byte {
func (msg MsgTransfer) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Sender}
}

type MsgRecvPacket struct {
Packet channelexported.PacketI `json:"packet" yaml:"packet"`
Proofs []commitment.Proof `json:"proofs" yaml:"proofs"`
Height uint64 `json:"height" yaml:"height"`
Signer sdk.AccAddress `json:"signer" yaml:"signer"`
}

// NewMsgRecvPacket creates a new MsgRecvPacket instance
func NewMsgRecvPacket(packet channelexported.PacketI, proofs []commitment.Proof, height uint64, signer sdk.AccAddress) MsgRecvPacket {
return MsgRecvPacket{
Packet: packet,
Proofs: proofs,
Height: height,
Signer: signer,
}
}

// Route implements sdk.Msg
func (MsgRecvPacket) Route() string {
return RouterKey
}

// Type implements sdk.Msg
func (MsgRecvPacket) Type() string {
return "recv_packet"
}

// ValidateBasic implements sdk.Msg
func (msg MsgRecvPacket) ValidateBasic() sdk.Error {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if msg.Proofs == nil {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
return sdk.NewError(DefaultCodespace, CodeProofMissing, "proof missing")
}

if msg.Signer.Empty() {
return sdk.NewError(DefaultCodespace, CodeInvalidAddress, "invalid signer")
}

if err := msg.Packet.ValidateBasic(); err != nil {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
return err
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

fedekunze marked this conversation as resolved.
Show resolved Hide resolved
return nil
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}

// GetSignBytes implements sdk.Msg
func (msg MsgRecvPacket) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}

// GetSigners implements sdk.Msg
func (msg MsgRecvPacket) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Signer}
}
2 changes: 0 additions & 2 deletions x/ibc/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
ibcclient "github.com/cosmos/cosmos-sdk/x/ibc/02-client"
connection "github.com/cosmos/cosmos-sdk/x/ibc/03-connection"
transfer "github.com/cosmos/cosmos-sdk/x/ibc/20-transfer"
mockbank "github.com/cosmos/cosmos-sdk/x/ibc/mock/bank"
"github.com/cosmos/cosmos-sdk/x/ibc/types"
)

Expand All @@ -26,7 +25,6 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
ibcclient.GetTxCmd(cdc, storeKey),
connection.GetTxCmd(cdc, storeKey),
transfer.GetTxCmd(cdc),
mockbank.GetTxCmd(cdc),
)
return ibcTxCmd
}
Expand Down
3 changes: 3 additions & 0 deletions x/ibc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func NewHandler(k Keeper) sdk.Handler {
case transfer.MsgTransfer:
return transfer.HandleMsgTransfer(ctx, k.TransferKeeper, msg)

case transfer.MsgRecvPacket:
return transfer.HandleMsgRecvPacket(ctx, k.TransferKeeper, msg)

default:
errMsg := fmt.Sprintf("unrecognized IBC message type: %T", msg)
return sdk.ErrUnknownRequest(errMsg).Result()
Expand Down
26 changes: 0 additions & 26 deletions x/ibc/mock/bank/alias.go

This file was deleted.

85 changes: 0 additions & 85 deletions x/ibc/mock/bank/client/cli/tx.go

This file was deleted.

Loading