Skip to content

Commit

Permalink
fix relayer cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Aug 14, 2019
1 parent 33d1760 commit bf762ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion x/ibc/04-channel/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (obj CLIObject) SeqRecv(ctx context.CLIContext) (res uint64, proof merkle.P
return
}

func (obj CLIObject) PacketCommit(ctx context.CLIContext, index uint64) (res Packet, proof merkle.Proof, err error) {
func (obj CLIObject) Packet(ctx context.CLIContext, index uint64) (res Packet, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.PacketCommitKey(index), &res)
return
}
Expand Down
51 changes: 41 additions & 10 deletions x/ibc/04-channel/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"io/ioutil"
"strconv"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -224,7 +225,6 @@ func GetCmdRelay(storeKey string, cdc *codec.Codec) *cobra.Command {
Args: cobra.ExactArgs(4),
// Args: []string{connid1, chanid1, chanfilepath1, connid2, chanid2, chanfilepath2}
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
ctx1 := context.NewCLIContext().
WithCodec(cdc).
WithNodeURI(viper.GetString(FlagNode1)).
Expand All @@ -239,13 +239,33 @@ func GetCmdRelay(storeKey string, cdc *codec.Codec) *cobra.Command {

obj1 := object(ctx1, cdc, storeKey, ibc.Version, conn1id, chan1id)
obj2 := object(ctx2, cdc, storeKey, ibc.Version, conn2id, chan2id)

return relayLoop(cdc, ctx1, ctx2, obj1, obj2, conn1id, chan1id, conn2id, chan2id)
},
}

return cmd
}

func relay(ctxFrom, ctxTo context.CLIContext, objFrom, objTo channel.Object) error {
func relayLoop(cdc *codec.Codec,
ctx1, ctx2 context.CLIContext,
obj1, obj2 channel.CLIObject,
conn1id, chan1id, conn2id, chan2id string,
) error {
for {
// TODO: relay() should be goroutine and return error by channel
err := relay(cdc, ctx1, ctx2, obj1, obj2, conn2id, chan2id)
// TODO: relayBetween() should retry several times before halt
if err != nil {
return err
}
time.Sleep(1 * time.Second)
}
}

func relay(cdc *codec.Codec, ctxFrom, ctxTo context.CLIContext, objFrom, objTo channel.CLIObject, connidTo, chanidTo string) error {
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))

seq, _, err := objTo.SeqRecv(ctxTo)
if err != nil {
return err
Expand All @@ -256,14 +276,25 @@ func relay(ctxFrom, ctxTo context.CLIContext, objFrom, objTo channel.Object) err
return err
}

if seq == sent {
return nil
}

packet, proof, err := query(ctxFrom, objFrom.Packet(seq))
if err != nil {
return err
for i := seq; i <= sent; i++ {
packet, proof, err := objFrom.Packet(ctxFrom, seq)
if err != nil {
return err
}

msg := channel.MsgReceive{
ConnectionID: connidTo,
ChannelID: chanidTo,
Packet: packet,
Proofs: []commitment.Proof{proof},
Signer: ctxTo.GetFromAddress(),
}

err = utils.GenerateOrBroadcastMsgs(ctxTo, txBldr, []sdk.Msg{msg})
if err != nil {
return err
}
}

msg := channel.MsgReceive{}
return nil
}

0 comments on commit bf762ba

Please sign in to comment.