Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger (ibc-go v7) #92

Merged
merged 47 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a2cde6f
Add logger
dongrie Jun 21, 2023
bd38968
Fix log
dongrie Jun 21, 2023
462040b
merge
dongrie Jul 6, 2023
539ab8c
Fix zap logger
dongrie Jul 10, 2023
85e3d29
Change Error, Info to Errorw, Infow
dongrie Jul 19, 2023
e2c09f4
merge
dongrie Jul 19, 2023
ee43d90
AddCallerSkip
dongrie Jul 19, 2023
34e3a9c
Remove logging from cmd/query
dongrie Jul 19, 2023
b3df4eb
Fix log message
dongrie Jul 25, 2023
88b1fe4
Add info
dongrie Jul 27, 2023
918b1bd
Fix parameter
dongrie Jul 27, 2023
bad47d0
Merge branch 'main' of github.com:dongrie/yui-relayer into add-logger
dongrie Aug 2, 2023
de504f9
Use config.yaml for init logger
dongrie Aug 3, 2023
c3340ea
Refactoring logger
dongrie Aug 3, 2023
93501c6
Fix InitLogger
dongrie Aug 4, 2023
c5f8dee
merge
dongrie Aug 8, 2023
131c851
Fix retry count
dongrie Aug 8, 2023
68e40a3
Add default logger
dongrie Aug 8, 2023
f0114e2
Disable logger
dongrie Aug 8, 2023
32b8911
CI Debug
dongrie Aug 8, 2023
af05024
Add chain logger
dongrie Aug 8, 2023
b877b1d
Fix retry count
dongrie Aug 8, 2023
aa71b40
CI Debug
dongrie Aug 8, 2023
6228960
Revert
dongrie Aug 8, 2023
d542e08
Fix logger
dongrie Aug 10, 2023
8f7817b
Replace zap to slog
dongrie Aug 15, 2023
e65498a
Add source
dongrie Aug 15, 2023
c1e80cf
Fix slog logger
dongrie Aug 16, 2023
3ac58b7
merge
dongrie Aug 25, 2023
c0f0ec7
Refactoring logger
dongrie Aug 25, 2023
f01128e
Change package name
dongrie Aug 25, 2023
6a1c67d
Fix cache key
dongrie Aug 25, 2023
4314601
Fix cache key
dongrie Aug 25, 2023
ead4963
Fix initLogger
dongrie Aug 25, 2023
d4f9777
Revert code
dongrie Aug 25, 2023
2511434
Merge tag 'v0.4.6' into add-logger
siburu Aug 31, 2023
10454ab
fix Get{Chain,Connection,Channel}Logger interface
siburu Sep 1, 2023
6f16b05
rename GetXxxLogger to GetXxxPairLogger and WithXxx to WithXxxPair
siburu Sep 1, 2023
c588ba6
fix logging
siburu Sep 1, 2023
c6dc00c
fix logging more
siburu Sep 4, 2023
5cb6b1b
use default config if config.yml not found
siburu Sep 4, 2023
4322e31
fix typo
siburu Sep 4, 2023
dc42584
fix logging in metrics
siburu Sep 4, 2023
14cf0c5
Merge pull request #2 from siburu/add-logger
dongrie Sep 5, 2023
05ad463
Use slog.Group
dongrie Sep 6, 2023
46c50e2
* Use slog.Level.UnmarshalText
dongrie Sep 6, 2023
853d291
merge
dongrie Sep 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func initConfig(ctx *config.Context, cmd *cobra.Command) error {
os.Exit(1)
}
}
} else {
defConfig := config.DefaultConfig()
ctx.Config = &defConfig
}
return nil
}
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hyperledger-labs/yui-relayer/config"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/hyperledger-labs/yui-relayer/log"
"github.com/hyperledger-labs/yui-relayer/metrics"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -80,6 +81,9 @@ func Execute(modules ...config.ModuleI) error {
if err := initConfig(ctx, rootCmd); err != nil {
return fmt.Errorf("failed to initialize the configuration: %v", err)
}
if err := initLogger(ctx); err != nil {
return err
}
if err := metrics.InitializeMetrics(metrics.ExporterNull{}); err != nil {
return fmt.Errorf("failed to initialize the metrics: %v", err)
}
Expand All @@ -101,6 +105,11 @@ func readStdin() (string, error) {
return strings.TrimSpace(str), err
}

func initLogger(ctx *config.Context) error {
c := ctx.Config.Global.LoggerConfig
return log.InitLogger(c.Level, c.Format, c.Output)
}

func noCommand(cmd *cobra.Command, args []string) error {
cmd.Help()
return errors.New("specified command does not exist")
Expand Down
16 changes: 14 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,27 @@ func DefaultConfig() Config {

// GlobalConfig describes any global relayer settings
type GlobalConfig struct {
Timeout string `yaml:"timeout" json:"timeout"`
LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"`
Timeout string `yaml:"timeout" json:"timeout"`
LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"`
LoggerConfig LoggerConfig `yaml:"logger" json:"logger"`
}

type LoggerConfig struct {
Level string `yaml:"level" json:"level"`
Format string `yaml:"format" json:"format"`
Output string `yaml:"output" json:"output"`
}

// newDefaultGlobalConfig returns a global config with defaults set
func newDefaultGlobalConfig() GlobalConfig {
return GlobalConfig{
Timeout: "10s",
LightCacheSize: 20,
LoggerConfig: LoggerConfig{
Level: "DEBUG",
Format: "json",
Output: "stderr",
},
}
}

Expand Down
10 changes: 10 additions & 0 deletions core/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
conntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/log"
)

// ProvableChain represents a chain that is supported by the relayer
Expand Down Expand Up @@ -195,3 +196,12 @@ func (qc queryContext) Context() context.Context {
func (qc queryContext) Height() ibcexported.Height {
return qc.height
}

func GetChainPairLogger(src, dst ChainInfo) *log.RelayLogger {
return log.GetLogger().
WithChainPair(
src.ChainID(),
dst.ChainID(),
).
WithModule("core.chain")
}
72 changes: 51 additions & 21 deletions core/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package core
import (
"context"
"fmt"
"log"
"time"

retry "github.com/avast/retry-go"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/hyperledger-labs/yui-relayer/log"
"golang.org/x/exp/slog"
)

// 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 := GetChannelPairLogger(src, dst)
var order chantypes.Order
if ordered {
order = chantypes.ORDERED
Expand All @@ -25,11 +27,15 @@ 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(
"failed to create channel step",
err,
)
return err
}

if !chanSteps.Ready() {
log.Println("Waiting for next channel step ...")
logger.Debug("Waiting for next channel step ...")
continue
}

Expand All @@ -39,9 +45,9 @@ 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:
log.Printf("★ 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)
logger.Info(
"★ Channel created",
)
return nil
// In the case of success, reset the failures counter
case chanSteps.Success():
Expand All @@ -50,12 +56,17 @@ 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++
log.Printf("retrying transaction...")
logger.Info("retrying transaction...")
time.Sleep(5 * time.Second)
if failures > 2 {
logger.Error(
"! Channel failed",
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)
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID,
)
}
}
}
Expand Down Expand Up @@ -171,20 +182,22 @@ func createChannelStep(src, dst *ProvableChain, ordering chantypes.Order) (*Rela
return out, nil
}

func logChannelStates(src, dst Chain, srcChan, dstChan *chantypes.QueryChannelResponse) {
log.Printf("- [%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,
)
func logChannelStates(src, dst *ProvableChain, srcChan, dstChan *chantypes.QueryChannelResponse) {
logger := GetChannelPairLogger(src, dst)
logger.Info(
"channel states",
slog.Group("src",
slog.Uint64("proof_height", mustGetHeight(srcChan.ProofHeight)),
slog.String("state", srcChan.Channel.State.String()),
),
siburu marked this conversation as resolved.
Show resolved Hide resolved
slog.Group("dst",
slog.Uint64("proof_height", mustGetHeight(dstChan.ProofHeight)),
slog.String("state", dstChan.Channel.State.String()),
))
}

func checkChannelFinality(src, dst *ProvableChain, srcChannel, dstChannel *chantypes.Channel) (bool, error) {
logger := GetChannelPairLogger(src, dst)
sh, err := src.LatestHeight()
if err != nil {
return false, err
Expand All @@ -198,12 +211,29 @@ func checkChannelFinality(src, dst *ProvableChain, srcChannel, dstChannel *chant
return false, err
}
if srcChannel.State != srcChanLatest.Channel.State {
log.Printf("Source Channel: Finalized state [%s] <> Latest state [%s]", srcChannel.State, srcChanLatest.Channel.State)
logger.Debug("src channel state in transition", "from_state", srcChannel.State, "to_state", srcChanLatest.Channel.State)
return false, nil
}
if dstChannel.State != dstChanLatest.Channel.State {
log.Printf("Destination Channel: Finalized state [%s] <> Latest state [%s]", dstChannel.State, dstChanLatest.Channel.State)
logger.Debug("dst channel state in transition", "from_state", dstChannel.State, "to_state", dstChanLatest.Channel.State)
return false, nil
}
return true, nil
}

func GetChannelLogger(c Chain) *log.RelayLogger {
return log.GetLogger().
WithChannel(
c.ChainID(), c.Path().PortID, c.Path().ChannelID,
).
WithModule("core.channel")
}

func GetChannelPairLogger(src, dst Chain) *log.RelayLogger {
return log.GetLogger().
WithChannelPair(
src.ChainID(), src.Path().PortID, src.Path().ChannelID,
dst.ChainID(), dst.Path().PortID, dst.Path().ChannelID,
).
WithModule("core.channel")
}
52 changes: 46 additions & 6 deletions core/client.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
package core

import (
"log"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/hyperledger-labs/yui-relayer/log"
"golang.org/x/sync/errgroup"
)

func CreateClients(src, dst *ProvableChain) error {
logger := GetChainPairLogger(src, dst)
var (
clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
)

srcH, dstH, err := getHeadersForCreateClient(src, dst)
if err != nil {
logger.Error(
"failed to get headers for create client",
err,
)
return err
}

srcAddr, err := src.GetAddress()
if err != nil {
logger.Error(
"failed to get address for create client",
err,
)
return err
}
dstAddr, err := dst.GetAddress()
if err != nil {
logger.Error(
"failed to get address for create client",
err,
)
return err
}

{
msg, err := dst.CreateMsgCreateClient(src.Path().ClientID, dstH, srcAddr)
if err != nil {
logger.Error(
"failed to create client",
err,
)
return err
}
clients.Src = append(clients.Src, msg)
Expand All @@ -37,6 +53,10 @@ func CreateClients(src, dst *ProvableChain) error {
{
msg, err := src.CreateMsgCreateClient(dst.Path().ClientID, srcH, dstAddr)
if err != nil {
logger.Error(
"failed to create client",
err,
)
return err
}
clients.Dst = append(clients.Dst, msg)
Expand All @@ -46,24 +66,34 @@ 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() {
log.Printf("★ Clients created: [%s]client(%s) and [%s]client(%s)",
src.ChainID(), src.Path().ClientID, dst.ChainID(), dst.Path().ClientID)
logger.Info(
"★ Clients created",
)
}
}
return nil
}

func UpdateClients(src, dst *ProvableChain) error {
logger := GetClientPairLogger(src, dst)
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(
"failed to create sync headers for update client",
err,
)
return err
}
srcUpdateHeaders, dstUpdateHeaders, err := sh.SetupBothHeadersForUpdate(src, dst)
if err != nil {
logger.Error(
"failed to setup both headers for update client",
err,
)
return err
}
if len(dstUpdateHeaders) > 0 {
Expand All @@ -75,8 +105,9 @@ func UpdateClients(src, dst *ProvableChain) error {
// Send msgs to both chains
if clients.Ready() {
if clients.Send(src, dst); clients.Success() {
log.Printf("★ Clients updated: [%s]client(%s) and [%s]client(%s)",
src.ChainID(), src.Path().ClientID, dst.ChainID(), dst.Path().ClientID)
logger.Info(
"★ Clients updated",
)
}
}
return nil
Expand All @@ -98,3 +129,12 @@ func getHeadersForCreateClient(src, dst LightClient) (srch, dsth Header, err err
}
return srch, dsth, nil
}

func GetClientPairLogger(src, dst Chain) *log.RelayLogger {
return log.GetLogger().
WithClientPair(
src.ChainID(), src.Path().ClientID,
dst.ChainID(), dst.Path().ClientID,
).
WithModule("core.client")
}
4 changes: 4 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/gogoproto/proto"
"github.com/hyperledger-labs/yui-relayer/log"
"github.com/hyperledger-labs/yui-relayer/utils"
)

Expand Down Expand Up @@ -33,12 +34,15 @@ type ProverConfig interface {

// NewChainProverConfig returns a new config instance
func NewChainProverConfig(m codec.JSONCodec, chain ChainConfig, client ProverConfig) (*ChainProverConfig, error) {
logger := log.GetLogger().WithModule("core.config")
cbz, err := utils.MarshalJSONAny(m, chain)
if err != nil {
logger.Error("error marshalling chain config", err)
return nil, err
}
clbz, err := utils.MarshalJSONAny(m, client)
if err != nil {
logger.Error("error marshalling client config", err)
return nil, err
}
return &ChainProverConfig{
Expand Down
Loading
Loading