Skip to content

Commit

Permalink
use tmlibs/log
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Apr 3, 2018
1 parent 606f516 commit d869407
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
7 changes: 3 additions & 4 deletions x/ibc/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 21 additions & 10 deletions x/ibc/commands/relay.go
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -30,6 +32,8 @@ type relayCommander struct {
decoder sdk.AccountDecoder
mainStore string
ibcStore string

logger log.Logger
}

func IBCRelayCmd(cdc *wire.Codec) *cobra.Command {
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}

Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit d869407

Please sign in to comment.