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

refactor: create base signer and signer structs to group common data of EVM/Bitcoin signer and observer #2344

Merged
merged 13 commits into from
Jun 19, 2024
Merged
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* [2269](https://github.com/zeta-chain/node/pull/2269) - refactor MsgUpdateCrosschainFlags into MsgEnableCCTX, MsgDisableCCTX and MsgUpdateGasPriceIncreaseFlags
* [2306](https://github.com/zeta-chain/node/pull/2306) - refactor zetaclient outbound transaction signing logic
* [2296](https://github.com/zeta-chain/node/pull/2296) - move `testdata` package to `testutil` to organize test-related utilities
* [2344](https://github.com/zeta-chain/node/pull/2344) - group common data of EVM/Bitcoin signer and observer using base structs

### Tests

Expand Down
64 changes: 0 additions & 64 deletions cmd/zetaclientd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
9 changes: 5 additions & 4 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand 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
Expand All @@ -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
Expand Down
26 changes: 13 additions & 13 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -84,20 +84,20 @@ 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
}
// 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
}
Expand All @@ -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)
Expand All @@ -123,22 +123,22 @@ 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
}
// 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
Expand Down
109 changes: 109 additions & 0 deletions zetaclient/chains/base/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package base

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 contains the loggers for chain observers
type ObserverLogger struct {
// the parent logger for the chain observer
Chain zerolog.Logger

// the logger for inbound transactions
Inbound zerolog.Logger

// the logger for outbound transactions
Outbound zerolog.Logger

// the logger for the chain's gas price
GasPrice zerolog.Logger

// the logger for block headers
Headers zerolog.Logger

// 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

Check warning on line 103 in zetaclient/chains/base/logger.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/base/logger.go#L103

Added line #L103 was not covered by tests
}
name = filepath.Clean(name)

// open (or create) compliance log file
return os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
}
Loading
Loading