From 2cdff62c54bd19b1317321954f4485923310c4d2 Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Tue, 4 Jun 2024 23:36:54 -0500 Subject: [PATCH] moved logger to base package --- cmd/zetaclientd/main.go | 64 ------------ cmd/zetaclientd/start.go | 9 +- cmd/zetaclientd/utils.go | 26 ++--- zetaclient/chains/base/logger.go | 87 ++++++++++++++++- zetaclient/chains/base/logger_test.go | 97 +++++++++++++++++++ zetaclient/chains/base/observer.go | 8 +- zetaclient/chains/base/observer_test.go | 25 +++-- zetaclient/chains/base/signer.go | 49 ++++++++++ .../chains/bitcoin/observer/live_test.go | 4 +- .../chains/bitcoin/observer/observer.go | 8 +- .../chains/bitcoin/observer/observer_test.go | 6 +- zetaclient/chains/bitcoin/signer/signer.go | 8 +- .../chains/bitcoin/signer/signer_test.go | 8 +- zetaclient/chains/evm/observer/observer.go | 8 +- .../chains/evm/observer/observer_test.go | 4 +- zetaclient/chains/evm/signer/signer.go | 12 +-- zetaclient/chains/evm/signer/signer_test.go | 6 +- zetaclient/common/logger.go | 20 ---- zetaclient/context/zetacore_context_test.go | 8 +- 19 files changed, 306 insertions(+), 151 deletions(-) create mode 100644 zetaclient/chains/base/logger_test.go delete mode 100644 zetaclient/common/logger.go diff --git a/cmd/zetaclientd/main.go b/cmd/zetaclientd/main.go index fdac54a7d0..99aaf55c10 100644 --- a/cmd/zetaclientd/main.go +++ b/cmd/zetaclientd/main.go @@ -3,24 +3,15 @@ package main import ( "math/rand" "os" - "path/filepath" "time" ecdsakeygen "github.com/binance-chain/tss-lib/ecdsa/keygen" "github.com/cosmos/cosmos-sdk/server" svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" "github.com/cosmos/cosmos-sdk/types" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "github.com/zeta-chain/zetacore/app" "github.com/zeta-chain/zetacore/cmd" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" - "github.com/zeta-chain/zetacore/zetaclient/config" -) - -const ( - ComplianceLogFile = "compliance.log" ) var ( @@ -53,58 +44,3 @@ func SetupConfigForTest() { rand.Seed(time.Now().UnixNano()) } - -func InitLogger(cfg config.Config) (clientcommon.ClientLogger, error) { - // open compliance log file - file, err := OpenComplianceLogFile(cfg) - if err != nil { - return clientcommon.DefaultLoggers(), err - } - - var logger zerolog.Logger - var loggerCompliance zerolog.Logger - switch cfg.LogFormat { - case "json": - logger = zerolog.New(os.Stdout).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() - loggerCompliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() - case "text": - logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}). - Level(zerolog.Level(cfg.LogLevel)). - With(). - Timestamp(). - Logger() - loggerCompliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() - default: - logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}) - loggerCompliance = zerolog.New(file).With().Timestamp().Logger() - } - - if cfg.LogSampler { - logger = logger.Sample(&zerolog.BasicSampler{N: 5}) - } - log.Logger = logger // set global logger - - return clientcommon.ClientLogger{ - Std: log.Logger, - Compliance: loggerCompliance, - }, nil -} - -func OpenComplianceLogFile(cfg config.Config) (*os.File, error) { - // use zetacore home as default - logPath := cfg.ZetaCoreHome - if cfg.ComplianceConfig.LogPath != "" { - logPath = cfg.ComplianceConfig.LogPath - } - - // clean file name - name := filepath.Join(logPath, ComplianceLogFile) - name, err := filepath.Abs(name) - if err != nil { - return nil, err - } - name = filepath.Clean(name) - - // open (or create) compliance log file - return os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) -} diff --git a/cmd/zetaclientd/start.go b/cmd/zetaclientd/start.go index 9c964ea26e..c815b616c0 100644 --- a/cmd/zetaclientd/start.go +++ b/cmd/zetaclientd/start.go @@ -24,6 +24,7 @@ import ( "github.com/zeta-chain/zetacore/pkg/authz" "github.com/zeta-chain/zetacore/pkg/constant" observerTypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/metrics" @@ -61,7 +62,7 @@ func start(_ *cobra.Command, _ []string) error { if err != nil { return err } - loggers, err := InitLogger(cfg) + logger, err := base.InitLogger(cfg) if err != nil { log.Error().Err(err).Msg("InitLogger failed") return err @@ -76,7 +77,7 @@ func start(_ *cobra.Command, _ []string) error { } } - masterLogger := loggers.Std + masterLogger := logger.Std startLogger := masterLogger.With().Str("module", "startup").Logger() // Wait until zetacore is up @@ -267,7 +268,7 @@ func start(_ *cobra.Command, _ []string) error { } // CreateSignerMap: This creates a map of all signers for each chain . Each signer is responsible for signing transactions for a particular chain - signerMap, err := CreateSignerMap(appContext, tss, loggers, telemetryServer) + signerMap, err := CreateSignerMap(appContext, tss, logger, telemetryServer) if err != nil { log.Error().Err(err).Msg("CreateSignerMap") return err @@ -281,7 +282,7 @@ func start(_ *cobra.Command, _ []string) error { dbpath := filepath.Join(userDir, ".zetaclient/chainobserver") // Creates a map of all chain observers for each chain. Each chain observer is responsible for observing events on the chain and processing them. - observerMap, err := CreateChainObserverMap(appContext, zetacoreClient, tss, dbpath, loggers, telemetryServer) + observerMap, err := CreateChainObserverMap(appContext, zetacoreClient, tss, dbpath, logger, telemetryServer) if err != nil { startLogger.Err(err).Msg("CreateChainObserverMap") return err diff --git a/cmd/zetaclientd/utils.go b/cmd/zetaclientd/utils.go index 7c37c00380..521c6dc858 100644 --- a/cmd/zetaclientd/utils.go +++ b/cmd/zetaclientd/utils.go @@ -5,12 +5,12 @@ import ( ethcommon "github.com/ethereum/go-ethereum/common" "github.com/zeta-chain/zetacore/zetaclient/authz" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" btcobserver "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/observer" btcsigner "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/signer" evmobserver "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer" evmsigner "github.com/zeta-chain/zetacore/zetaclient/chains/evm/signer" "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/keys" @@ -57,7 +57,7 @@ func CreateZetacoreClient( func CreateSignerMap( appContext *context.AppContext, tss interfaces.TSSSigner, - loggers clientcommon.ClientLogger, + logger base.Logger, ts *metrics.TelemetryServer, ) (map[int64]interfaces.ChainSigner, error) { coreContext := appContext.ZetacoreContext() @@ -70,7 +70,7 @@ func CreateSignerMap( } evmChainParams, found := coreContext.GetEVMChainParams(evmConfig.Chain.ChainId) if !found { - loggers.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String()) + logger.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String()) continue } mpiAddress := ethcommon.HexToAddress(evmChainParams.ConnectorContractAddress) @@ -84,10 +84,10 @@ func CreateSignerMap( mpiAddress, erc20CustodyAddress, coreContext, - loggers, + logger, ts) if err != nil { - loggers.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String()) + logger.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String()) continue } signerMap[evmConfig.Chain.ChainId] = signer @@ -95,9 +95,9 @@ func CreateSignerMap( // BTC signer btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig() if enabled { - signer, err := btcsigner.NewSigner(btcConfig, tss, loggers, ts, coreContext) + signer, err := btcsigner.NewSigner(btcConfig, tss, logger, ts, coreContext) if err != nil { - loggers.Std.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String()) + logger.Std.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String()) } else { signerMap[btcChain.ChainId] = signer } @@ -112,7 +112,7 @@ func CreateChainObserverMap( zetacoreClient *zetacore.Client, tss interfaces.TSSSigner, dbpath string, - loggers clientcommon.ClientLogger, + logger base.Logger, ts *metrics.TelemetryServer, ) (map[int64]interfaces.ChainObserver, error) { observerMap := make(map[int64]interfaces.ChainObserver) @@ -123,12 +123,12 @@ func CreateChainObserverMap( } _, found := appContext.ZetacoreContext().GetEVMChainParams(evmConfig.Chain.ChainId) if !found { - loggers.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String()) + logger.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String()) continue } - co, err := evmobserver.NewObserver(appContext, zetacoreClient, tss, dbpath, loggers, evmConfig, ts) + co, err := evmobserver.NewObserver(appContext, zetacoreClient, tss, dbpath, logger, evmConfig, ts) if err != nil { - loggers.Std.Error().Err(err).Msgf("NewObserver error for evm chain %s", evmConfig.Chain.String()) + logger.Std.Error().Err(err).Msgf("NewObserver error for evm chain %s", evmConfig.Chain.String()) continue } observerMap[evmConfig.Chain.ChainId] = co @@ -136,9 +136,9 @@ func CreateChainObserverMap( // BTC observer btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig() if enabled { - co, err := btcobserver.NewObserver(appContext, btcChain, zetacoreClient, tss, dbpath, loggers, btcConfig, ts) + co, err := btcobserver.NewObserver(appContext, btcChain, zetacoreClient, tss, dbpath, logger, btcConfig, ts) if err != nil { - loggers.Std.Error().Err(err).Msgf("NewObserver error for bitcoin chain %s", btcChain.String()) + logger.Std.Error().Err(err).Msgf("NewObserver error for bitcoin chain %s", btcChain.String()) } else { observerMap[btcChain.ChainId] = co diff --git a/zetaclient/chains/base/logger.go b/zetaclient/chains/base/logger.go index c675450d2d..a6810cc984 100644 --- a/zetaclient/chains/base/logger.go +++ b/zetaclient/chains/base/logger.go @@ -1,6 +1,33 @@ package base -import "github.com/rs/zerolog" +import ( + "os" + "path/filepath" + "time" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + + "github.com/zeta-chain/zetacore/zetaclient/config" +) + +const ( + ComplianceLogFile = "compliance.log" +) + +// Logger contains the base loggers +type Logger struct { + Std zerolog.Logger + Compliance zerolog.Logger +} + +// DefaultLoggers creates default base loggers for tests +func DefaultLogger() Logger { + return Logger{ + Std: log.Logger, + Compliance: log.Logger, + } +} // ObserverLogger is the base logger for chain observers type ObserverLogger struct { @@ -19,3 +46,61 @@ type ObserverLogger struct { // the logger for the compliance check Compliance zerolog.Logger } + +// InitLogger initializes the base loggers +func InitLogger(cfg config.Config) (Logger, error) { + // open compliance log file + file, err := openComplianceLogFile(cfg) + if err != nil { + return DefaultLogger(), err + } + + // create loggers based on configured level and format + var std zerolog.Logger + var compliance zerolog.Logger + switch cfg.LogFormat { + case "json": + std = zerolog.New(os.Stdout).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() + compliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() + case "text": + std = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}). + Level(zerolog.Level(cfg.LogLevel)). + With(). + Timestamp(). + Logger() + compliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger() + default: + std = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}) + compliance = zerolog.New(file).With().Timestamp().Logger() + } + + if cfg.LogSampler { + std = std.Sample(&zerolog.BasicSampler{N: 5}) + } + log.Logger = std // set global logger + + return Logger{ + Std: std, + Compliance: compliance, + }, nil +} + +// openComplianceLogFile opens the compliance log file +func openComplianceLogFile(cfg config.Config) (*os.File, error) { + // use zetacore home as default + logPath := cfg.ZetaCoreHome + if cfg.ComplianceConfig.LogPath != "" { + logPath = cfg.ComplianceConfig.LogPath + } + + // clean file name + name := filepath.Join(logPath, ComplianceLogFile) + name, err := filepath.Abs(name) + if err != nil { + return nil, err + } + name = filepath.Clean(name) + + // open (or create) compliance log file + return os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) +} diff --git a/zetaclient/chains/base/logger_test.go b/zetaclient/chains/base/logger_test.go new file mode 100644 index 0000000000..07c2859b0e --- /dev/null +++ b/zetaclient/chains/base/logger_test.go @@ -0,0 +1,97 @@ +package base_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/zeta-chain/zetacore/zetaclient/chains/base" + "github.com/zeta-chain/zetacore/zetaclient/config" +) + +func TestInitLogger(t *testing.T) { + // test cases + tests := []struct { + name string + t *testing.T + cfg config.Config + fail bool + }{ + { + name: "should be able to initialize json formatted logger", + cfg: config.Config{ + LogFormat: "json", + LogLevel: 1, // zerolog.InfoLevel, + ComplianceConfig: config.ComplianceConfig{ + LogPath: createTempDir(t), + }, + }, + fail: false, + }, + { + name: "should be able to initialize plain text logger", + cfg: config.Config{ + LogFormat: "text", + LogLevel: 2, // zerolog.WarnLevel, + ComplianceConfig: config.ComplianceConfig{ + LogPath: createTempDir(t), + }, + }, + fail: false, + }, + { + name: "should be able to initialize default formatted logger", + cfg: config.Config{ + LogFormat: "unknown", + LogLevel: 3, // zerolog.ErrorLevel, + ComplianceConfig: config.ComplianceConfig{ + LogPath: createTempDir(t), + }, + }, + fail: false, + }, + { + name: "should be able to initialize sampled logger", + cfg: config.Config{ + LogFormat: "json", + LogLevel: 4, // zerolog.DebugLevel, + LogSampler: true, + ComplianceConfig: config.ComplianceConfig{ + LogPath: createTempDir(t), + }, + }, + }, + { + name: "should fail on invalid compliance log path", + cfg: config.Config{ + LogFormat: "json", + LogLevel: 1, // zerolog.InfoLevel, + ComplianceConfig: config.ComplianceConfig{ + LogPath: "/invalid/123path", + }, + }, + fail: true, + }, + } + + // run tests + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // init logger + logger, err := base.InitLogger(tt.cfg) + + // check if error is expected + if tt.fail { + require.Error(t, err) + return + } + + // check if logger is initialized + require.NoError(t, err) + + // should be able to print log + logger.Std.Info().Msg("print standard log") + logger.Compliance.Info().Msg("print compliance log") + }) + } +} diff --git a/zetaclient/chains/base/observer.go b/zetaclient/chains/base/observer.go index 1865560fd5..e36a2aa924 100644 --- a/zetaclient/chains/base/observer.go +++ b/zetaclient/chains/base/observer.go @@ -9,14 +9,14 @@ import ( lru "github.com/hashicorp/golang-lru" "github.com/pkg/errors" "github.com/rs/zerolog" - "github.com/zeta-chain/zetacore/pkg/chains" - "github.com/zeta-chain/zetacore/zetaclient/context" - "github.com/zeta-chain/zetacore/zetaclient/metrics" "gorm.io/driver/sqlite" "gorm.io/gorm" + "github.com/zeta-chain/zetacore/pkg/chains" observertypes "github.com/zeta-chain/zetacore/x/observer/types" "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" + "github.com/zeta-chain/zetacore/zetaclient/context" + "github.com/zeta-chain/zetacore/zetaclient/metrics" clienttypes "github.com/zeta-chain/zetacore/zetaclient/types" ) @@ -25,7 +25,7 @@ const ( DefaultBlockCacheSize = 1000 ) -// Observer is the base chain observer +// Observer is the base chain observer for external chains type Observer struct { // the external chain chain chains.Chain diff --git a/zetaclient/chains/base/observer_test.go b/zetaclient/chains/base/observer_test.go index 5f3b0ec966..007dca4199 100644 --- a/zetaclient/chains/base/observer_test.go +++ b/zetaclient/chains/base/observer_test.go @@ -6,6 +6,7 @@ import ( lru "github.com/hashicorp/golang-lru" "github.com/stretchr/testify/require" + "github.com/zeta-chain/zetacore/pkg/chains" "github.com/zeta-chain/zetacore/testutil/sample" observertypes "github.com/zeta-chain/zetacore/x/observer/types" @@ -16,10 +17,9 @@ import ( "github.com/zeta-chain/zetacore/zetaclient/testutils/mocks" ) -// create a temporary path for database -func tempDbPath(t *testing.T) string { - // Create a temporary file to get a unique name - tempPath, err := os.MkdirTemp("", "tempdb-") +// create a temporary directory for testing +func createTempDir(t *testing.T) string { + tempPath, err := os.MkdirTemp("", "tempdir-") require.NoError(t, err) return tempPath } @@ -33,7 +33,7 @@ func createObserver(t *testing.T) *base.Observer { zetacoreClient := mocks.NewMockZetacoreClient() tss := mocks.NewTSSMainnet() blockCacheSize := base.DefaultBlockCacheSize - dbPath := tempDbPath(t) + dbPath := createTempDir(t) // create observer ob, err := base.NewObserver(chain, chainParams, zetacoreContext, zetacoreClient, tss, blockCacheSize, dbPath, nil) @@ -50,7 +50,7 @@ func TestNewObserver(t *testing.T) { zetacoreClient := mocks.NewMockZetacoreClient() tss := mocks.NewTSSMainnet() blockCacheSize := base.DefaultBlockCacheSize - dbPath := tempDbPath(t) + dbPath := createTempDir(t) // test cases tests := []struct { @@ -105,7 +105,16 @@ func TestNewObserver(t *testing.T) { // run tests for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ob, err := base.NewObserver(tt.chain, tt.chainParams, tt.zetacoreContext, tt.zetacoreClient, tt.tss, tt.blockCacheSize, tt.dbPath, nil) + ob, err := base.NewObserver( + tt.chain, + tt.chainParams, + tt.zetacoreContext, + tt.zetacoreClient, + tt.tss, + tt.blockCacheSize, + tt.dbPath, + nil, + ) if tt.fail { require.ErrorContains(t, err, tt.message) require.Nil(t, ob) @@ -173,7 +182,7 @@ func TestObserverSetters(t *testing.T) { func TestOpenDB(t *testing.T) { ob := createObserver(t) - dbPath := tempDbPath(t) + dbPath := createTempDir(t) t.Run("should be able to open db", func(t *testing.T) { err := ob.OpenDB(dbPath) diff --git a/zetaclient/chains/base/signer.go b/zetaclient/chains/base/signer.go index 22f1111448..1e768d21ea 100644 --- a/zetaclient/chains/base/signer.go +++ b/zetaclient/chains/base/signer.go @@ -1 +1,50 @@ package base + +import ( + "github.com/rs/zerolog" + + "github.com/zeta-chain/zetacore/pkg/chains" + "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" + "github.com/zeta-chain/zetacore/zetaclient/context" + "github.com/zeta-chain/zetacore/zetaclient/metrics" +) + +// Signer is the base chain signer for external chains +type Signer struct { + // the external chain + chain chains.Chain + + // zetacore context + zetacoreContext *context.ZetacoreContext + + // tss signer + tss interfaces.TSSSigner + + // telemetry server + ts *metrics.TelemetryServer + + // the standard logger + logger zerolog.Logger + + // the compliance logger + loggerCompliance zerolog.Logger +} + +// NewSigner creates a new base signer +func NewSigner( + chain chains.Chain, + zetacoreContext *context.ZetacoreContext, + tss interfaces.TSSSigner, + logger zerolog.Logger, + loggerCompliance zerolog.Logger, + ts *metrics.TelemetryServer, +) *Signer { + return &Signer{ + chain: chain, + zetacoreContext: zetacoreContext, + tss: tss, + logger: logger, + loggerCompliance: loggerCompliance, + ts: ts, + } +} diff --git a/zetaclient/chains/bitcoin/observer/live_test.go b/zetaclient/chains/bitcoin/observer/live_test.go index 51bedbf1ef..dd1053f620 100644 --- a/zetaclient/chains/bitcoin/observer/live_test.go +++ b/zetaclient/chains/bitcoin/observer/live_test.go @@ -21,8 +21,8 @@ import ( "github.com/stretchr/testify/suite" "github.com/zeta-chain/zetacore/pkg/chains" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" clientcontext "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/testutils" @@ -49,7 +49,7 @@ func (suite *BitcoinObserverTestSuite) SetupTest() { } appContext := clientcontext.NewAppContext(&clientcontext.ZetacoreContext{}, config.Config{}) client, err := NewObserver(appContext, chains.BitcoinRegtest, nil, tss, tempSQLiteDbPath, - clientcommon.DefaultLoggers(), config.BTCConfig{}, nil) + base.DefaultLogger(), config.BTCConfig{}, nil) suite.Require().NoError(err) suite.rpcClient, err = getRPCClient(18332) suite.Require().NoError(err) diff --git a/zetaclient/chains/bitcoin/observer/observer.go b/zetaclient/chains/bitcoin/observer/observer.go index 91f0d695f4..dcb81a47f3 100644 --- a/zetaclient/chains/bitcoin/observer/observer.go +++ b/zetaclient/chains/bitcoin/observer/observer.go @@ -28,9 +28,9 @@ import ( "github.com/zeta-chain/zetacore/pkg/chains" "github.com/zeta-chain/zetacore/pkg/proofs" observertypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin" "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/metrics" @@ -138,7 +138,7 @@ func NewObserver( zetacoreClient interfaces.ZetacoreClient, tss interfaces.TSSSigner, dbpath string, - loggers clientcommon.ClientLogger, + logger base.Logger, btcCfg config.BTCConfig, ts *metrics.TelemetryServer, ) (*Observer, error) { @@ -158,14 +158,14 @@ func NewObserver( ob.Mu = &sync.Mutex{} - chainLogger := loggers.Std.With().Str("chain", chain.ChainName.String()).Logger() + chainLogger := logger.Std.With().Str("chain", chain.ChainName.String()).Logger() ob.logger = Logger{ Chain: chainLogger, Inbound: chainLogger.With().Str("module", "WatchInbound").Logger(), Outbound: chainLogger.With().Str("module", "WatchOutbound").Logger(), UTXOS: chainLogger.With().Str("module", "WatchUTXOS").Logger(), GasPrice: chainLogger.With().Str("module", "WatchGasPrice").Logger(), - Compliance: loggers.Compliance, + Compliance: logger.Compliance, } ob.zetacoreClient = zetacoreClient diff --git a/zetaclient/chains/bitcoin/observer/observer_test.go b/zetaclient/chains/bitcoin/observer/observer_test.go index dfba6b3dbc..2d2494dd16 100644 --- a/zetaclient/chains/bitcoin/observer/observer_test.go +++ b/zetaclient/chains/bitcoin/observer/observer_test.go @@ -14,7 +14,7 @@ import ( "github.com/zeta-chain/zetacore/pkg/chains" "github.com/zeta-chain/zetacore/testutil/sample" observertypes "github.com/zeta-chain/zetacore/x/observer/types" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/metrics" @@ -75,11 +75,11 @@ func TestNewBitcoinObserver(t *testing.T) { chain := chains.BitcoinMainnet zetacoreClient := mocks.NewMockZetacoreClient() tss := mocks.NewMockTSS(chains.BitcoinTestnet, sample.EthAddress().String(), "") - loggers := clientcommon.ClientLogger{} + logger := base.Logger{} btcCfg := cfg.BitcoinConfig ts := metrics.NewTelemetryServer() - client, err := NewObserver(appContext, chain, zetacoreClient, tss, tempSQLiteDbPath, loggers, btcCfg, ts) + client, err := NewObserver(appContext, chain, zetacoreClient, tss, tempSQLiteDbPath, logger, btcCfg, ts) require.ErrorContains(t, err, "btc chains params not initialized") require.Nil(t, client) }) diff --git a/zetaclient/chains/bitcoin/signer/signer.go b/zetaclient/chains/bitcoin/signer/signer.go index 0c51fc7204..1215b32bb3 100644 --- a/zetaclient/chains/bitcoin/signer/signer.go +++ b/zetaclient/chains/bitcoin/signer/signer.go @@ -21,10 +21,10 @@ import ( "github.com/zeta-chain/zetacore/pkg/coin" "github.com/zeta-chain/zetacore/x/crosschain/types" observertypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin" "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/observer" "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/compliance" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" @@ -56,7 +56,7 @@ type Signer struct { func NewSigner( cfg config.BTCConfig, tssSigner interfaces.TSSSigner, - loggers clientcommon.ClientLogger, + logger base.Logger, ts *metrics.TelemetryServer, coreContext *context.ZetacoreContext) (*Signer, error) { connCfg := &rpcclient.ConnConfig{ @@ -75,8 +75,8 @@ func NewSigner( return &Signer{ tssSigner: tssSigner, rpcClient: client, - logger: loggers.Std.With().Str("chain", "BTC").Str("module", "BTCSigner").Logger(), - loggerCompliance: loggers.Compliance, + logger: logger.Std.With().Str("chain", "BTC").Str("module", "BTCSigner").Logger(), + loggerCompliance: logger.Compliance, ts: ts, coreContext: coreContext, }, nil diff --git a/zetaclient/chains/bitcoin/signer/signer_test.go b/zetaclient/chains/bitcoin/signer/signer_test.go index 6d048c7bba..74628211a4 100644 --- a/zetaclient/chains/bitcoin/signer/signer_test.go +++ b/zetaclient/chains/bitcoin/signer/signer_test.go @@ -18,8 +18,8 @@ import ( . "gopkg.in/check.v1" "github.com/zeta-chain/zetacore/pkg/chains" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/metrics" @@ -52,7 +52,7 @@ func (s *BTCSignerSuite) SetUpTest(c *C) { s.btcSigner, err = NewSigner( config.BTCConfig{}, tss, - clientcommon.DefaultLoggers(), + base.DefaultLogger(), &metrics.TelemetryServer{}, context.NewZetacoreContext(cfg)) c.Assert(err, IsNil) @@ -233,7 +233,7 @@ func TestAddWithdrawTxOutputs(t *testing.T) { signer, err := NewSigner( config.BTCConfig{}, mocks.NewTSSMainnet(), - clientcommon.DefaultLoggers(), + base.DefaultLogger(), &metrics.TelemetryServer{}, nil, ) @@ -396,7 +396,7 @@ func TestNewBTCSigner(t *testing.T) { btcSigner, err := NewSigner( config.BTCConfig{}, tss, - clientcommon.DefaultLoggers(), + base.DefaultLogger(), &metrics.TelemetryServer{}, context.NewZetacoreContext(cfg)) require.NoError(t, err) diff --git a/zetaclient/chains/evm/observer/observer.go b/zetaclient/chains/evm/observer/observer.go index 4a38a62d12..272f0a06d9 100644 --- a/zetaclient/chains/evm/observer/observer.go +++ b/zetaclient/chains/evm/observer/observer.go @@ -30,9 +30,9 @@ import ( "github.com/zeta-chain/zetacore/pkg/chains" "github.com/zeta-chain/zetacore/pkg/proofs" observertypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/evm" "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" clientcontext "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/metrics" @@ -93,7 +93,7 @@ func NewObserver( zetacoreClient interfaces.ZetacoreClient, tss interfaces.TSSSigner, dbpath string, - loggers clientcommon.ClientLogger, + logger base.Logger, evmCfg config.EVMConfig, ts *metrics.TelemetryServer, ) (*Observer, error) { @@ -101,13 +101,13 @@ func NewObserver( ts: ts, } - chainLogger := loggers.Std.With().Str("chain", evmCfg.Chain.ChainName.String()).Logger() + chainLogger := logger.Std.With().Str("chain", evmCfg.Chain.ChainName.String()).Logger() ob.logger = Logger{ Chain: chainLogger, Inbound: chainLogger.With().Str("module", "WatchInbound").Logger(), Outbound: chainLogger.With().Str("module", "WatchOutbound").Logger(), GasPrice: chainLogger.With().Str("module", "WatchGasPrice").Logger(), - Compliance: loggers.Compliance, + Compliance: logger.Compliance, } ob.coreContext = appContext.ZetacoreContext() diff --git a/zetaclient/chains/evm/observer/observer_test.go b/zetaclient/chains/evm/observer/observer_test.go index bfc85e15e1..0601c083f2 100644 --- a/zetaclient/chains/evm/observer/observer_test.go +++ b/zetaclient/chains/evm/observer/observer_test.go @@ -15,9 +15,9 @@ import ( "github.com/zeta-chain/zetacore/testutil/sample" "github.com/zeta-chain/zetacore/x/crosschain/types" observertypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer" "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" - "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/keys" @@ -83,7 +83,7 @@ func MockEVMObserver( appCtx, evmCfg := getAppContext(chain, ¶ms) // create chain observer - client, err := observer.NewObserver(appCtx, zetacoreClient, tss, "", common.ClientLogger{}, evmCfg, nil) + client, err := observer.NewObserver(appCtx, zetacoreClient, tss, "", base.Logger{}, evmCfg, nil) require.NoError(t, err) client.WithEvmClient(evmClient) client.WithEvmJSONRPC(evmJSONRPC) diff --git a/zetaclient/chains/evm/signer/signer.go b/zetaclient/chains/evm/signer/signer.go index 8ab5608959..447ea9d924 100644 --- a/zetaclient/chains/evm/signer/signer.go +++ b/zetaclient/chains/evm/signer/signer.go @@ -27,10 +27,10 @@ import ( crosschainkeeper "github.com/zeta-chain/zetacore/x/crosschain/keeper" "github.com/zeta-chain/zetacore/x/crosschain/types" observertypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/evm" "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer" "github.com/zeta-chain/zetacore/zetaclient/chains/interfaces" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/compliance" clientcontext "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/metrics" @@ -47,7 +47,7 @@ type Signer struct { chain *chains.Chain tssSigner interfaces.TSSSigner ethSigner ethtypes.Signer - logger clientcommon.ClientLogger + logger base.Logger ts *metrics.TelemetryServer coreContext *clientcontext.ZetacoreContext @@ -69,7 +69,7 @@ func NewSigner( zetaConnectorAddress ethcommon.Address, erc20CustodyAddress ethcommon.Address, coreContext *clientcontext.ZetacoreContext, - loggers clientcommon.ClientLogger, + logger base.Logger, ts *metrics.TelemetryServer, ) (*Signer, error) { client, ethSigner, err := getEVMRPC(endpoint) @@ -95,9 +95,9 @@ func NewSigner( zetaConnectorAddress: zetaConnectorAddress, er20CustodyAddress: erc20CustodyAddress, coreContext: coreContext, - logger: clientcommon.ClientLogger{ - Std: loggers.Std.With().Str("chain", chain.ChainName.String()).Str("module", "EVMSigner").Logger(), - Compliance: loggers.Compliance, + logger: base.Logger{ + Std: logger.Std.With().Str("chain", chain.ChainName.String()).Str("module", "EVMSigner").Logger(), + Compliance: logger.Compliance, }, ts: ts, mu: &sync.Mutex{}, diff --git a/zetaclient/chains/evm/signer/signer_test.go b/zetaclient/chains/evm/signer/signer_test.go index 6039c44803..580486d709 100644 --- a/zetaclient/chains/evm/signer/signer_test.go +++ b/zetaclient/chains/evm/signer/signer_test.go @@ -12,8 +12,8 @@ import ( "github.com/zeta-chain/zetacore/pkg/constant" "github.com/zeta-chain/zetacore/testutil/sample" crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" + "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer" - "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/context" "github.com/zeta-chain/zetacore/zetaclient/keys" @@ -32,7 +32,7 @@ var ( func getNewEvmSigner() (*Signer, error) { mpiAddress := ConnectorAddress erc20CustodyAddress := ERC20CustodyAddress - logger := common.ClientLogger{} + logger := base.Logger{} ts := &metrics.TelemetryServer{} cfg := config.NewConfig() return NewSigner( @@ -49,7 +49,7 @@ func getNewEvmSigner() (*Signer, error) { } func getNewEvmChainObserver() (*observer.Observer, error) { - logger := common.ClientLogger{} + logger := base.Logger{} ts := &metrics.TelemetryServer{} cfg := config.NewConfig() tss := mocks.NewTSSMainnet() diff --git a/zetaclient/common/logger.go b/zetaclient/common/logger.go deleted file mode 100644 index ebe773abfc..0000000000 --- a/zetaclient/common/logger.go +++ /dev/null @@ -1,20 +0,0 @@ -package common - -import ( - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" -) - -// ClientLogger is a struct that contains the logger for a chain observer -type ClientLogger struct { - Std zerolog.Logger - Compliance zerolog.Logger -} - -// DefaultLoggers returns the default loggers for a chain observer -func DefaultLoggers() ClientLogger { - return ClientLogger{ - Std: log.Logger, - Compliance: log.Logger, - } -} diff --git a/zetaclient/context/zetacore_context_test.go b/zetaclient/context/zetacore_context_test.go index 0ee7d5bc8a..d8117e124b 100644 --- a/zetaclient/context/zetacore_context_test.go +++ b/zetaclient/context/zetacore_context_test.go @@ -4,13 +4,13 @@ import ( "testing" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/stretchr/testify/require" "github.com/zeta-chain/zetacore/pkg/chains" "github.com/zeta-chain/zetacore/testutil/sample" lightclienttypes "github.com/zeta-chain/zetacore/x/lightclient/types" observertypes "github.com/zeta-chain/zetacore/x/observer/types" - clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" context "github.com/zeta-chain/zetacore/zetaclient/context" ) @@ -179,7 +179,6 @@ func TestUpdateZetacoreContext(t *testing.T) { ChainId: 3, } tssPubKeyToUpdate := "tsspubkeytest" - loggers := clientcommon.DefaultLoggers() crosschainFlags := sample.CrosschainFlags() verificationFlags := sample.HeaderSupportedChains() @@ -193,7 +192,7 @@ func TestUpdateZetacoreContext(t *testing.T) { *crosschainFlags, verificationFlags, false, - loggers.Std, + log.Logger, ) // assert keygen updated @@ -285,7 +284,6 @@ func TestUpdateZetacoreContext(t *testing.T) { crosschainFlags := sample.CrosschainFlags() verificationFlags := sample.HeaderSupportedChains() require.NotNil(t, crosschainFlags) - loggers := clientcommon.DefaultLoggers() zetaContext.Update( &keyGenToUpdate, enabledChainsToUpdate, @@ -295,7 +293,7 @@ func TestUpdateZetacoreContext(t *testing.T) { *crosschainFlags, verificationFlags, false, - loggers.Std, + log.Logger, ) // assert keygen updated