From 7f2d25c593485b82802a6ef1c2f1c9646dd1cdfa Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 21 May 2017 22:40:48 -0400 Subject: [PATCH] [cmd] support SendTx to other chains via IBC --- cmd/commands/relay.go | 5 +++++ cmd/commands/tx.go | 20 +++++++++++++++++++- plugins/ibc/ibc.go | 4 +++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/commands/relay.go b/cmd/commands/relay.go index 2c7f812a03..5a2cacfc96 100644 --- a/cmd/commands/relay.go +++ b/cmd/commands/relay.go @@ -90,6 +90,11 @@ OUTER: logger.Error("Error parsing sequence number from query", "query.Value", query.Value, "error", err.Error()) continue OUTER } + seq -= 1 // seq is the packet count. -1 because 0-indexed + + if nextSeq <= int(seq) { + logger.Info("Got new packets", "last-sequence", nextSeq-1, "new-sequence", seq) + } // get all packets since the last one we relayed for ; nextSeq <= int(seq); nextSeq++ { diff --git a/cmd/commands/tx.go b/cmd/commands/tx.go index 946c290a17..2cde963b99 100644 --- a/cmd/commands/tx.go +++ b/cmd/commands/tx.go @@ -3,6 +3,7 @@ package commands import ( "encoding/hex" "fmt" + "strings" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -81,12 +82,29 @@ func init() { func sendTxCmd(cmd *cobra.Command, args []string) error { + var toHex string + var chainPrefix string + spl := strings.Split(toFlag, "/") + switch len(spl) { + case 1: + toHex = spl[0] + case 2: + chainPrefix = spl[0] + toHex = spl[1] + default: + return errors.Errorf("To address has too many slashes") + } + // convert destination address to bytes - to, err := hex.DecodeString(StripHex(toFlag)) + to, err := hex.DecodeString(StripHex(toHex)) if err != nil { return errors.Errorf("To address is invalid hex: %v\n", err) } + if chainPrefix != "" { + to = []byte(chainPrefix + "/" + string(to)) + } + // load the priv key privKey, err := LoadKey(fromFlag) if err != nil { diff --git a/plugins/ibc/ibc.go b/plugins/ibc/ibc.go index bea7a43dfb..eb8a27f1b0 100644 --- a/plugins/ibc/ibc.go +++ b/plugins/ibc/ibc.go @@ -69,7 +69,9 @@ func NewPacket(src, dst string, seq uint64, payload Payload) Packet { } } -// GetSequenceNumber gets the sequence number for packets being sent from the src chain to the dst chain +// GetSequenceNumber gets the sequence number for packets being sent from the src chain to the dst chain. +// The sequence number counts how many packets have been sent. +// The next packet must include the latest sequence number. func GetSequenceNumber(store types.KVStore, src, dst string) uint64 { sequenceKey := toKey(_IBC, _EGRESS, src, dst) seqBytes := store.Get(sequenceKey)