Skip to content

Commit

Permalink
Refactor ibc/mock/bank into ICS 20 (#5264)
Browse files Browse the repository at this point in the history
* Most of code port from mock module to ICS 20

* A few minor fixes

* Apply suggestions from code review

Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>

* Fix suggestions from autolinter

* Apply suggestions from code review

Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Fix order of messages

* Add invalid height error code, check non-nil proof

* Fix linter error

* Return the underlying error

* Tendermint starts at height 1

* Apply suggestions from code review
  • Loading branch information
cwgoes authored and fedekunze committed Oct 30, 2019
1 parent af6bed9 commit 82d9124
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 428 deletions.
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 err
}

var data types.PacketData
err = data.UnmarshalJSON(packet.Data())
if err != nil {
return sdk.NewError(types.DefaultCodespace, types.CodeInvalidPacketData, "invalid packet data")
}

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
CodeInvalidHeight 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
63 changes: 63 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,63 @@ 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 {
if msg.Height < 1 {
return sdk.NewError(DefaultCodespace, CodeInvalidHeight, "invalid height")
}

if msg.Proofs == nil {
return sdk.NewError(DefaultCodespace, CodeProofMissing, "proof missing")
}

for _, proof := range msg.Proofs {
if proof.Proof == nil {
return sdk.NewError(DefaultCodespace, CodeProofMissing, "proof missing")
}
}

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

return msg.Packet.ValidateBasic()
}

// 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

0 comments on commit 82d9124

Please sign in to comment.