From d8694070c74e4014486ae768e35ebed6eb739003 Mon Sep 17 00:00:00 2001 From: mossid Date: Tue, 3 Apr 2018 18:19:47 +0200 Subject: [PATCH] use tmlibs/log --- x/ibc/commands/README.md | 7 +++---- x/ibc/commands/relay.go | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/x/ibc/commands/README.md b/x/ibc/commands/README.md index 30d19cdcecc4..b779542f3d1e 100644 --- a/x/ibc/commands/README.md +++ b/x/ibc/commands/README.md @@ -133,11 +133,10 @@ Committed at block 1022. Hash: E16019DCC4AA08CA70AFCFBC96028ABCC51B6AD0 ## Relay IBC packets ```console -> basecli relay --name key2 --from-chain-id $ID1 --from-chain-node $NODE1 --to-chain-id $ID2 --to-chain-node $ID2 --chain-id $ID2 +> basecli relay --name key2 --from-chain-id $ID1 --from-chain-node $NODE1 --to-chain-id $ID2 --to-chain-node $NODE2 --chain-id $ID2 Password to sign with 'key2': -IBC packet #0 detected -Relayed IBC packet #0 - +I[04-03|16:18:59.984] IBC packet detected number=0 +I[04-03|16:19:00.869] Relayed IBC packet number=0 > basecli account $ADDR2 --node $NODE2 { "address": "DC26002735D3AA9573707CFA6D77C12349E49868", diff --git a/x/ibc/commands/relay.go b/x/ibc/commands/relay.go index ecdcaed396b3..45e53d6428cd 100644 --- a/x/ibc/commands/relay.go +++ b/x/ibc/commands/relay.go @@ -1,12 +1,14 @@ package commands import ( - "fmt" + "os" "time" "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/tendermint/tmlibs/log" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/builder" @@ -30,6 +32,8 @@ type relayCommander struct { decoder sdk.AccountDecoder mainStore string ibcStore string + + logger log.Logger } func IBCRelayCmd(cdc *wire.Codec) *cobra.Command { @@ -38,6 +42,8 @@ func IBCRelayCmd(cdc *wire.Codec) *cobra.Command { decoder: authcmd.GetAccountDecoder(cdc), ibcStore: "ibc", mainStore: "main", + + logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)), } cmd := &cobra.Command{ @@ -105,7 +111,7 @@ OUTER: lengthKey := ibc.EgressLengthKey(toChainID) egressLengthbz, err := query(fromChainNode, lengthKey, c.ibcStore) if err != nil { - fmt.Printf("Error querying outgoing packet list length: '%s'\n", err) + c.logger.Error("Error querying outgoing packet list length", "err", err) continue OUTER } var egressLength int64 @@ -115,26 +121,29 @@ OUTER: panic(err) } if egressLength > processed { - fmt.Printf("IBC packet #%d detected\n", egressLength-1) + c.logger.Info("IBC packet detected", "number", egressLength-1) } + seq := c.getSequence(toChainNode) + for i := processed; i < egressLength; i++ { egressbz, err := query(fromChainNode, ibc.EgressKey(toChainID, i), c.ibcStore) if err != nil { - fmt.Printf("Error querying egress packet: '%s'\n", err) + c.logger.Error("Error querying egress packet", "err", err) continue OUTER } + viper.Set(client.FlagSequence, seq) + seq++ + err = c.broadcastTx(toChainNode, c.refine(egressbz, i, passphrase)) if err != nil { - fmt.Printf("Error broadcasting ingress packet: '%s'\n", err) + c.logger.Error("Error broadcasting ingress packet", "err", err) continue OUTER } - fmt.Printf("Relayed IBC packet #%d\n", i) + c.logger.Info("Relayed IBC packet", "number", i) } - - processed = egressLength } } @@ -149,8 +158,6 @@ func query(node string, key []byte, storeName string) (res []byte, err error) { func (c relayCommander) broadcastTx(node string, tx []byte) error { orig := viper.GetString(client.FlagNode) viper.Set(client.FlagNode, node) - seq := c.getSequence(node) - viper.Set(client.FlagSequence, seq) _, err := builder.BroadcastTx(tx) viper.Set(client.FlagNode, orig) return err @@ -170,6 +177,10 @@ func (c relayCommander) getSequence(node string) int64 { return account.GetSequence() } +func setSequence(seq int64) { + viper.Set(client.FlagSequence, seq) +} + func (c relayCommander) refine(bz []byte, sequence int64, passphrase string) []byte { var packet ibc.IBCPacket if err := c.cdc.UnmarshalBinary(bz, &packet); err != nil {