Skip to content

Commit

Permalink
refactor: create base signer and signer structs to group common data …
Browse files Browse the repository at this point in the history
…of EVM/Bitcoin signer and observer (#2344)

* save local new files to remote

* initiated base observer

* move base to chains folder

* moved logger to base package

* added base signer and logger

* added changelog entry

* added metrics server to base signer/observer

* added more descriptions for constants and base structs

---------

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
ws4charlie and lumtis authored Jun 19, 2024
1 parent d4a9f7d commit 2c76429
Show file tree
Hide file tree
Showing 22 changed files with 1,133 additions and 144 deletions.
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
}
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

0 comments on commit 2c76429

Please sign in to comment.