Skip to content

Commit

Permalink
Change Error, Info to Errorw, Infow
Browse files Browse the repository at this point in the history
Signed-off-by: Dongri Jin <[email protected]>
  • Loading branch information
dongrie committed Jul 19, 2023
1 parent 539ab8c commit 85e3d29
Show file tree
Hide file tree
Showing 11 changed files with 739 additions and 271 deletions.
32 changes: 23 additions & 9 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/hyperledger-labs/yui-relayer/helpers"
"github.com/hyperledger-labs/yui-relayer/logger"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

// queryCmd represents the chain command
Expand Down Expand Up @@ -180,8 +179,8 @@ func queryBalanceCmd(ctx *config.Context) *cobra.Command {
}

func queryUnrelayedPackets(ctx *config.Context) *cobra.Command {
logger := logger.ZapLogger()
defer logger.Sync()
zapLogger := logger.GetLogger()
defer zapLogger.Zap.Sync()
cmd := &cobra.Command{
Use: "unrelayed-packets [path]",
Short: "Query for the packet sequence numbers that remain to be relayed on a given path",
Expand Down Expand Up @@ -209,11 +208,11 @@ func queryUnrelayedPackets(ctx *config.Context) *cobra.Command {
}
out, err := json.Marshal(sp)
if err != nil {
logger.Error("failed to marshal sequences", zap.Any("sp", sp), zap.Error(err))
queryErrorw(zapLogger, "failed to marshal sequences", err)
return err
}

logger.Info("unrelayed sequences", zap.String("sequences", string(out)))
fmt.Println(string(out))
return nil
},
}
Expand All @@ -222,8 +221,8 @@ func queryUnrelayedPackets(ctx *config.Context) *cobra.Command {
}

func queryUnrelayedAcknowledgements(ctx *config.Context) *cobra.Command {
logger := logger.ZapLogger()
defer logger.Sync()
zapLogger := logger.GetLogger()
defer zapLogger.Zap.Sync()
cmd := &cobra.Command{
Use: "unrelayed-acknowledgements [path]",
Short: "Query for the packet sequence numbers that remain to be relayed on a given path",
Expand Down Expand Up @@ -253,14 +252,29 @@ func queryUnrelayedAcknowledgements(ctx *config.Context) *cobra.Command {

out, err := json.Marshal(sp)
if err != nil {
logger.Error("failed to marshal sequences", zap.Any("sp", sp), zap.Error(err))
queryErrorw(zapLogger, "failed to marshal sequences", err)
return err
}

logger.Info("unrelayed acknowledgements", zap.String("acknowledgements", string(out)))
queryInfow(zapLogger, "unrelayed acknowledgements", fmt.Sprintf("acknowledgements %s", string(out)))
return nil
},
}

return cmd
}

func queryErrorw(zapLogger *logger.ZapLogger, msg string, err error) {
zapLogger.Errorw(
msg,
err,
"core.query",
)
}

func queryInfow(zapLogger *logger.ZapLogger, msg string, info string) {
zapLogger.Infow(
msg,
info,
)
}
82 changes: 55 additions & 27 deletions core/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
retry "github.com/avast/retry-go"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/hyperledger-labs/yui-relayer/logger"
"go.uber.org/zap"
)

// CreateChannel runs the channel creation messages on timeout until they pass
// TODO: add max retries or something to this function
func CreateChannel(src, dst *ProvableChain, ordered bool, to time.Duration) error {
logger := logger.ZapLogger()
defer logger.Sync()
zapLogger := logger.GetLogger()
defer zapLogger.Zap.Sync()
var order chantypes.Order
if ordered {
order = chantypes.ORDERED
Expand All @@ -27,10 +26,12 @@ func CreateChannel(src, dst *ProvableChain, ordered bool, to time.Duration) erro
for ; true; <-ticker.C {
chanSteps, err := createChannelStep(src, dst, order)
if err != nil {
logger.Error(fmt.Sprintf("failed to create channel step: [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID),
zap.Error(err))
channelErrorw(
zapLogger,
"failed to create channel step",
src, dst,
err,
)
return err
}

Expand All @@ -44,9 +45,11 @@ func CreateChannel(src, dst *ProvableChain, ordered bool, to time.Duration) erro
// In the case of success and this being the last transaction
// debug logging, log created connection and break
case chanSteps.Success() && chanSteps.Last:
logger.Info(fmt.Sprintf("★ Channel created: [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID))
channnelInfowChannel(
zapLogger,
"★ Channel created",
src, dst,
)
return nil
// In the case of success, reset the failures counter
case chanSteps.Success():
Expand All @@ -55,15 +58,19 @@ func CreateChannel(src, dst *ProvableChain, ordered bool, to time.Duration) erro
// In the case of failure, increment the failures counter and exit if this is the 3rd failure
case !chanSteps.Success():
failures++
logger.Info("retrying transaction...")
zapLogger.Zap.Info("retrying transaction...")
time.Sleep(5 * time.Second)
if failures > 2 {
logger.Error(fmt.Sprintf("! Channel failed: [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID))
channelErrorw(
zapLogger,
"! Channel failed",
src, dst,
err,
)
return fmt.Errorf("! Channel failed: [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ClientID, src.Path().ChannelID,
dst.ChainID(), dst.Path().ClientID, dst.Path().ChannelID)
dst.ChainID(), dst.Path().ClientID, dst.Path().ChannelID,
)
}
}
}
Expand Down Expand Up @@ -174,16 +181,37 @@ func createChannelStep(src, dst *ProvableChain, ordering chantypes.Order) (*Rela
}

func logChannelStates(src, dst Chain, srcChan, dstChan *chantypes.QueryChannelResponse) {
logger := logger.ZapLogger()
defer logger.Sync()
logger.Info(fmt.Sprintf("- [%s]@{%d}chan(%s)-{%s} : [%s]@{%d}chan(%s)-{%s}",
src.ChainID(),
mustGetHeight(srcChan.ProofHeight),
src.Path().ChannelID,
srcChan.Channel.State,
dst.ChainID(),
mustGetHeight(dstChan.ProofHeight),
dst.Path().ChannelID,
dstChan.Channel.State,
))
zapLogger := logger.GetLogger()
defer zapLogger.Zap.Sync()
zapLogger.InfowChannel(
"channel states",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID,
fmt.Sprintf(
"src channel height: [%d] state: %s | dst channel height: [%d] state: %s",
mustGetHeight(srcChan.ProofHeight),
srcChan.Channel.State,
mustGetHeight(dstChan.ProofHeight),
dstChan.Channel.State,
),
)
}

func channelErrorw(zapLogger *logger.ZapLogger, msg string, src, dst *ProvableChain, err error) {
zapLogger.ErrorwChannel(
msg,
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID,
err,
"core.channel",
)
}

func channnelInfowChannel(zapLogger *logger.ZapLogger, msg string, src, dst *ProvableChain) {
zapLogger.InfowChannel(
msg,
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID,
"",
)
}
109 changes: 76 additions & 33 deletions core/client.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
package core

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/hyperledger-labs/yui-relayer/logger"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)

func CreateClients(src, dst *ProvableChain) error {
logger := logger.ZapLogger()
defer logger.Sync()
zapLogger := logger.GetLogger()
defer zapLogger.Zap.Sync()

var (
clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
)

srcH, dstH, err := getHeadersForCreateClient(src, dst)
if err != nil {
logger.Error(fmt.Sprintf("failed to get headers for create client [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID),
zap.Error(err))
clientErrorwChannel(
zapLogger,
"failed to get headers for create client",
src, dst,
err,
)
return err
}

srcAddr, err := src.GetAddress()
if err != nil {
logger.Error(fmt.Sprintf("failed to get address for create client [src: %s]", src.ChainID()), zap.Error(err))
clientErrorwChannel(
zapLogger,
"failed to get address for create client",
src, dst,
err,
)
return err
}
dstAddr, err := dst.GetAddress()
if err != nil {
logger.Error(fmt.Sprintf("failed to get address for create client [dst: %s]", dst.ChainID()), zap.Error(err))
clientErrorwChannel(
zapLogger,
"failed to get address for create client",
src, dst,
err,
)
return err
}

{
msg, err := dst.CreateMsgCreateClient(src.Path().ClientID, dstH, srcAddr)
if err != nil {
logger.Error(fmt.Sprintf("failed to create client on dst chain [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID),
zap.Error(err))
clientErrorwChannel(
zapLogger,
"failed to create client",
src, dst,
err,
)
return err
}
clients.Src = append(clients.Src, msg)
Expand All @@ -51,10 +63,12 @@ func CreateClients(src, dst *ProvableChain) error {
{
msg, err := src.CreateMsgCreateClient(dst.Path().ClientID, srcH, dstAddr)
if err != nil {
logger.Error(fmt.Sprintf("failed to create client on src chain [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID),
zap.Error(err))
clientErrorwChannel(
zapLogger,
"failed to create client",
src, dst,
err,
)
return err
}
clients.Dst = append(clients.Dst, msg)
Expand All @@ -64,34 +78,41 @@ func CreateClients(src, dst *ProvableChain) error {
if clients.Ready() {
// TODO: Add retry here for out of gas or other errors
if clients.Send(src, dst); clients.Success() {
logger.Info(fmt.Sprintf("★ Clients created: [%s]client(%s) and [%s]client(%s)",
src.ChainID(), src.Path().ClientID, dst.ChainID(), dst.Path().ClientID))
clientInfowChannel(
zapLogger,
"★ Clients created",
src, dst,
)
}
}
return nil
}

func UpdateClients(src, dst *ProvableChain) error {
logger := logger.ZapLogger()
defer logger.Sync()
zapLogger := logger.GetLogger()
defer zapLogger.Zap.Sync()
var (
clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
)
// First, update the light clients to the latest header and return the header
sh, err := NewSyncHeaders(src, dst)
if err != nil {
logger.Error(fmt.Sprintf("failed to create sync headers for update client [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID),
zap.Error(err))
clientErrorwChannel(
zapLogger,
"failed to create sync headers for update client",
src, dst,
err,
)
return err
}
srcUpdateHeaders, dstUpdateHeaders, err := sh.SetupBothHeadersForUpdate(src, dst)
if err != nil {
logger.Error(fmt.Sprintf("failed to setup both headers for update client [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID),
zap.Error(err))
clientErrorwChannel(
zapLogger,
"failed to setup both headers for update client",
src, dst,
err,
)
return err
}
if len(dstUpdateHeaders) > 0 {
Expand All @@ -103,8 +124,11 @@ func UpdateClients(src, dst *ProvableChain) error {
// Send msgs to both chains
if clients.Ready() {
if clients.Send(src, dst); clients.Success() {
logger.Info(fmt.Sprintf("★ Clients updated: [%s]client(%s) and [%s]client(%s)",
src.ChainID(), src.Path().ClientID, dst.ChainID(), dst.Path().ClientID))
clientInfowChannel(
zapLogger,
"★ Clients updated",
src, dst,
)
}
}
return nil
Expand All @@ -126,3 +150,22 @@ func getHeadersForCreateClient(src, dst LightClient) (srch, dsth Header, err err
}
return srch, dsth, nil
}

func clientErrorwChannel(zapLogger *logger.ZapLogger, msg string, src, dst *ProvableChain, err error) {
zapLogger.ErrorwChannel(
msg,
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID,
err,
"core.client",
)
}

func clientInfowChannel(zapLogger *logger.ZapLogger, msg string, src, dst *ProvableChain) {
zapLogger.InfowChannel(
msg,
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID,
"",
)
}
Loading

0 comments on commit 85e3d29

Please sign in to comment.