Skip to content

Commit

Permalink
added some unit tests on context updater and chain activator
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Jul 1, 2024
1 parent cef707a commit bf0468f
Show file tree
Hide file tree
Showing 28 changed files with 735 additions and 358 deletions.
2 changes: 1 addition & 1 deletion cmd/zetaclientd-supervisor/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (w *serializedWriter) Write(p []byte) (n int, err error) {
return w.upstream.Write(p)
}

func getLogger(cfg config.Config, out io.Writer) zerolog.Logger {
func getLogger(cfg *config.Config, out io.Writer) zerolog.Logger {
var logger zerolog.Logger
switch cfg.LogFormat {
case "json":
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/p2p_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func RunDiagnostics(
startLogger zerolog.Logger,
peers p2p.AddrList,
hotkeyPk cryptotypes.PrivKey,
cfg config.Config,
cfg *config.Config,
) error {
startLogger.Warn().Msg("P2P Diagnostic mode enabled")
startLogger.Warn().Msgf("seed peer: %s", peers)
Expand Down
18 changes: 11 additions & 7 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,16 @@ func start(_ *cobra.Command, _ []string) error {

// Defensive check: Make sure the tss address is set to the current TSS address and not the newly generated one
tss.CurrentPubkey = currentTss.TssPubkey
if tss.EVMAddress() == (ethcommon.Address{}) || tss.BTCAddress() == "" {
startLogger.Error().Msg("TSS address is not set in zetacore")
startLogger.Info().Msgf("Current TSS PubKey: %s", tss.CurrentPubkey)
if tss.EVMAddress() == (ethcommon.Address{}) {
return errors.New("Current TSS ETH address is empty")
}
startLogger.Info().
Msgf("Current TSS address \n ETH : %s \n BTC : %s \n PubKey : %s ", tss.EVMAddress(), tss.BTCAddress(), tss.CurrentPubkey)
if len(appContext.GetEnabledExternalChains()) == 0 {
startLogger.Error().Msgf("No external chains enabled in the zetacore %s ", cfg.String())
startLogger.Info().Msgf("Current TSS ETH address: %s", tss.EVMAddress())
if tss.BitcoinNetParams != nil {
if tss.BTCAddress() == "" {
return errors.New("Current TSS BTC address is empty")
}
startLogger.Info().Msgf("Current TSS BTC address: %s", tss.BTCAddress())
}

// Stop zetaclient if this node is not an active observer
Expand Down Expand Up @@ -289,7 +292,8 @@ func start(_ *cobra.Command, _ []string) error {
// defer zetaSupplyChecker.Stop()
//}

// Orchestrator wraps the zetacore client and adds the observers and signer maps to it . This is the high level object used for CCTX interactions
// Orchestrator wraps the app context, zetacore client, TSS and metrics server.
// This is the high level actor that monitors zetacore changes and coordinates CCTXs interactions.
orch := orchestrator.NewOrchestrator(
appContext,
zetacoreClient,
Expand Down
6 changes: 3 additions & 3 deletions cmd/zetaclientd/start_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/config"
)

func waitForZetaCore(config config.Config, logger zerolog.Logger) {
func waitForZetaCore(config *config.Config, logger zerolog.Logger) {
// wait until zetacore is up
logger.Debug().Msg("Waiting for zetacore to open 9090 port...")
for {
Expand Down Expand Up @@ -55,8 +55,8 @@ func validatePeer(seedPeer string) error {
// maskCfg sensitive fields are masked, currently only the EVM endpoints and bitcoin credentials,
//
// other fields can be added.
func maskCfg(cfg config.Config) string {
maskedCfg := cfg
func maskCfg(cfg *config.Config) string {
maskedCfg := *cfg

maskedCfg.BitcoinConfig = config.BTCConfig{
RPCUsername: cfg.BitcoinConfig.RPCUsername,
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/chains/base/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ObserverLogger struct {
}

// InitLogger initializes the base loggers
func InitLogger(cfg config.Config) (Logger, error) {
func InitLogger(cfg *config.Config) (Logger, error) {
// open compliance log file
file, err := openComplianceLogFile(cfg)
if err != nil {
Expand Down Expand Up @@ -89,7 +89,7 @@ func InitLogger(cfg config.Config) (Logger, error) {
}

// openComplianceLogFile opens the compliance log file
func openComplianceLogFile(cfg config.Config) (*os.File, error) {
func openComplianceLogFile(cfg *config.Config) (*os.File, error) {
// use zetacore home as default
logPath := cfg.ZetaCoreHome
if cfg.ComplianceConfig.LogPath != "" {
Expand Down
12 changes: 6 additions & 6 deletions zetaclient/chains/base/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func TestInitLogger(t *testing.T) {
tests := []struct {
name string
t *testing.T
cfg config.Config
cfg *config.Config
fail bool
}{
{
name: "should be able to initialize json formatted logger",
cfg: config.Config{
cfg: &config.Config{
LogFormat: "json",
LogLevel: 1, // zerolog.InfoLevel,
ComplianceConfig: config.ComplianceConfig{
Expand All @@ -31,7 +31,7 @@ func TestInitLogger(t *testing.T) {
},
{
name: "should be able to initialize plain text logger",
cfg: config.Config{
cfg: &config.Config{
LogFormat: "text",
LogLevel: 2, // zerolog.WarnLevel,
ComplianceConfig: config.ComplianceConfig{
Expand All @@ -42,7 +42,7 @@ func TestInitLogger(t *testing.T) {
},
{
name: "should be able to initialize default formatted logger",
cfg: config.Config{
cfg: &config.Config{
LogFormat: "unknown",
LogLevel: 3, // zerolog.ErrorLevel,
ComplianceConfig: config.ComplianceConfig{
Expand All @@ -53,7 +53,7 @@ func TestInitLogger(t *testing.T) {
},
{
name: "should be able to initialize sampled logger",
cfg: config.Config{
cfg: &config.Config{
LogFormat: "json",
LogLevel: 4, // zerolog.DebugLevel,
LogSampler: true,
Expand All @@ -64,7 +64,7 @@ func TestInitLogger(t *testing.T) {
},
{
name: "should fail on invalid compliance log path",
cfg: config.Config{
cfg: &config.Config{
LogFormat: "json",
LogLevel: 1, // zerolog.InfoLevel,
ComplianceConfig: config.ComplianceConfig{
Expand Down
6 changes: 3 additions & 3 deletions zetaclient/chains/evm/observer/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func Test_BuildInboundVoteMsgForZetaSentEvent(t *testing.T) {
event := testutils.ParseReceiptZetaSent(receipt, connector)

// create test compliance config
cfg := config.Config{
cfg := &config.Config{
ComplianceConfig: config.ComplianceConfig{},
}

Expand Down Expand Up @@ -323,7 +323,7 @@ func Test_BuildInboundVoteMsgForDepositedEvent(t *testing.T) {
sender := ethcommon.HexToAddress(tx.From)

// create test compliance config
cfg := config.Config{
cfg := &config.Config{
ComplianceConfig: config.ComplianceConfig{},
}

Expand Down Expand Up @@ -376,7 +376,7 @@ func Test_BuildInboundVoteMsgForTokenSentToTSS(t *testing.T) {

// create test compliance config
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, 1, mocks.MockChainParams(1, 1))
cfg := config.Config{
cfg := &config.Config{
ComplianceConfig: config.ComplianceConfig{},
}

Expand Down
3 changes: 1 addition & 2 deletions zetaclient/chains/evm/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ func getAppContext(
// feed chain params
appCtx.Update(
observertypes.Keygen{},
"",
[]chains.Chain{evmChain},
newChainParams,
&chaincfg.RegressionNetParams,
"",
*sample.CrosschainFlags(),
sample.HeaderSupportedChains(),
true,
zerolog.Logger{},
)
// create app context
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/evm/observer/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Test_IsOutboundProcessed(t *testing.T) {
ob.SetTxNReceipt(nonce, receipt, outbound)

// modify compliance config to restrict sender address
cfg := config.Config{
cfg := &config.Config{
ComplianceConfig: config.ComplianceConfig{},
}
cfg.ComplianceConfig.RestrictedAddresses = []string{cctx.InboundParams.Sender}
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/compliance/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var restrictedAddressBook = map[string]bool{}

// LoadComplianceConfig loads compliance config from zetaclient config
func LoadComplianceConfig(cfg config.Config) {
func LoadComplianceConfig(cfg *config.Config) {
restrictedAddressBook = cfg.GetRestrictedAddressBook()
}

Expand Down
2 changes: 1 addition & 1 deletion zetaclient/compliance/compliance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCctxRestricted(t *testing.T) {
cctx := testutils.LoadCctxByNonce(t, chain.ChainId, 6270)

// create config
cfg := config.Config{
cfg := &config.Config{
ComplianceConfig: config.ComplianceConfig{},
}

Expand Down
10 changes: 5 additions & 5 deletions zetaclient/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,32 @@ func Save(config *Config, path string) error {
}

// Load loads ZetaClient config from a filepath
func Load(path string) (Config, error) {
func Load(path string) (*Config, error) {
// retrieve file
file := filepath.Join(path, folder, filename)
file, err := filepath.Abs(file)
if err != nil {
return Config{}, err
return nil, err
}
file = filepath.Clean(file)

// read config
cfg := NewConfig()
input, err := os.ReadFile(file)
if err != nil {
return Config{}, err
return nil, err
}
err = json.Unmarshal(input, &cfg)
if err != nil {
return Config{}, err
return nil, err
}

// read keyring backend and use test by default
if cfg.KeyringBackend == KeyringBackendUndefined {
cfg.KeyringBackend = KeyringBackendTest
}
if cfg.KeyringBackend != KeyringBackendFile && cfg.KeyringBackend != KeyringBackendTest {
return Config{}, fmt.Errorf("invalid keyring backend %s", cfg.KeyringBackend)
return nil, fmt.Errorf("invalid keyring backend %s", cfg.KeyringBackend)
}

// fields sanitization
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ type Config struct {
ComplianceConfig ComplianceConfig `json:"ComplianceConfig"`
}

func NewConfig() Config {
return Config{
func NewConfig() *Config {
return &Config{
cfgLock: &sync.RWMutex{},
EVMChainConfigs: make(map[int64]EVMConfig),
}
Expand Down
Loading

0 comments on commit bf0468f

Please sign in to comment.