From 82a37c9c88b63865a95bf3701c720f5f8e684e4a Mon Sep 17 00:00:00 2001 From: skosito Date: Wed, 28 Feb 2024 20:11:58 +0100 Subject: [PATCH 01/16] Fix keygen not found case --- cmd/zetaclientd/keygen_tss.go | 5 ++++- cmd/zetaclientd/start.go | 8 ++++++-- zetaclient/core_context/zeta_core_context.go | 8 ++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/zetaclientd/keygen_tss.go b/cmd/zetaclientd/keygen_tss.go index 16f79581b5..726fda11ee 100644 --- a/cmd/zetaclientd/keygen_tss.go +++ b/cmd/zetaclientd/keygen_tss.go @@ -74,7 +74,10 @@ func GenerateTss( // This loop will try keygen at the keygen block and then wait for keygen to be successfully reported by all nodes before breaking out of the loop. // If keygen is unsuccessful, it will reset the triedKeygenAtBlock flag and try again at a new keygen block. - keyGen := appContext.ZetaCoreContext().GetKeygen() + keyGen, found := appContext.ZetaCoreContext().GetKeygen() + if !found { + return nil, fmt.Errorf("keygen not found") + } if keyGen.Status == observertypes.KeygenStatus_KeyGenSuccess { return tss, nil } diff --git a/cmd/zetaclientd/start.go b/cmd/zetaclientd/start.go index 8458acb802..6881120c15 100644 --- a/cmd/zetaclientd/start.go +++ b/cmd/zetaclientd/start.go @@ -190,8 +190,12 @@ func start(_ *cobra.Command, _ []string) error { // For existing keygen, this should directly proceed to the next step ticker := time.NewTicker(time.Second * 1) for range ticker.C { - if appContext.ZetaCoreContext().GetKeygen().Status != observerTypes.KeygenStatus_KeyGenSuccess { - startLogger.Info().Msgf("Waiting for TSS Keygen to be a success, current status %s", appContext.ZetaCoreContext().GetKeygen().Status) + keyGen, found := appContext.ZetaCoreContext().GetKeygen() + if !found { + return fmt.Errorf("keygen not found") + } + if keyGen.Status != observerTypes.KeygenStatus_KeyGenSuccess { + startLogger.Info().Msgf("Waiting for TSS Keygen to be a success, current status %s", keyGen.Status) continue } break diff --git a/zetaclient/core_context/zeta_core_context.go b/zetaclient/core_context/zeta_core_context.go index 27a88b8951..0c5794f4cd 100644 --- a/zetaclient/core_context/zeta_core_context.go +++ b/zetaclient/core_context/zeta_core_context.go @@ -42,9 +42,13 @@ func NewZetaCoreContext(cfg *config.Config) *ZetaCoreContext { } } -func (c *ZetaCoreContext) GetKeygen() observertypes.Keygen { +func (c *ZetaCoreContext) GetKeygen() (observertypes.Keygen, bool) { c.coreContextLock.RLock() defer c.coreContextLock.RUnlock() + + if c.keygen == nil { + return observertypes.Keygen{}, false + } copiedPubkeys := make([]string, len(c.keygen.GranteePubkeys)) copy(copiedPubkeys, c.keygen.GranteePubkeys) @@ -52,7 +56,7 @@ func (c *ZetaCoreContext) GetKeygen() observertypes.Keygen { Status: c.keygen.Status, GranteePubkeys: copiedPubkeys, BlockNumber: c.keygen.BlockNumber, - } + }, true } func (c *ZetaCoreContext) GetCurrentTssPubkey() string { From 5aca34596e6ec038acf231fbe503e7603793b7fd Mon Sep 17 00:00:00 2001 From: skosito Date: Wed, 28 Feb 2024 20:12:10 +0100 Subject: [PATCH 02/16] Add test for empty core context --- .../core_context/zeta_core_context_test.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 zetaclient/core_context/zeta_core_context_test.go diff --git a/zetaclient/core_context/zeta_core_context_test.go b/zetaclient/core_context/zeta_core_context_test.go new file mode 100644 index 0000000000..a0705f3538 --- /dev/null +++ b/zetaclient/core_context/zeta_core_context_test.go @@ -0,0 +1,40 @@ +package corecontext + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/zeta-chain/zetacore/common" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/config" +) + +func TestNewZetaCoreContext(t *testing.T) { + t.Run("should create new zeta core context with empty config", func(t *testing.T) { + testCfg := config.NewConfig() + + zetaContext := NewZetaCoreContext(testCfg) + + require.NotNil(t, zetaContext) + keyGen, keyGenFound := zetaContext.GetKeygen() + // assert keygen + require.False(t, keyGenFound) + require.Equal(t, observertypes.Keygen{}, keyGen) + + // assert enabled chains + require.Empty(t, len(zetaContext.GetEnabledChains())) + + // assert current tss pubkey + require.Equal(t, "", zetaContext.GetCurrentTssPubkey()) + + // assert btc chain params + chain, btcChainParams, btcChainParamsFound := zetaContext.GetBTCChainParams() + require.Equal(t, common.Chain{}, chain) + require.False(t, btcChainParamsFound) + require.Equal(t, &observertypes.ChainParams{}, btcChainParams) + + // assert evm chain params + allEVMChainParams := zetaContext.GetAllEVMChainParams() + require.Empty(t, allEVMChainParams) + }) +} From 51d7cf4aba42a40c2714abbd486dee0bf138ae55 Mon Sep 17 00:00:00 2001 From: skosito Date: Thu, 29 Feb 2024 14:50:04 +0100 Subject: [PATCH 03/16] Add tests for creating new core context with config chain params --- .../core_context/zeta_core_context_test.go | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/zetaclient/core_context/zeta_core_context_test.go b/zetaclient/core_context/zeta_core_context_test.go index a0705f3538..fdf57ce881 100644 --- a/zetaclient/core_context/zeta_core_context_test.go +++ b/zetaclient/core_context/zeta_core_context_test.go @@ -14,10 +14,10 @@ func TestNewZetaCoreContext(t *testing.T) { testCfg := config.NewConfig() zetaContext := NewZetaCoreContext(testCfg) - require.NotNil(t, zetaContext) - keyGen, keyGenFound := zetaContext.GetKeygen() + // assert keygen + keyGen, keyGenFound := zetaContext.GetKeygen() require.False(t, keyGenFound) require.Equal(t, observertypes.Keygen{}, keyGen) @@ -37,4 +37,65 @@ func TestNewZetaCoreContext(t *testing.T) { allEVMChainParams := zetaContext.GetAllEVMChainParams() require.Empty(t, allEVMChainParams) }) + + t.Run("should create new zeta core context with config containing evm chain params", func(t *testing.T) { + testCfg := config.NewConfig() + testCfg.EVMChainConfigs = map[int64]*config.EVMConfig{ + 1: { + Chain: common.Chain{ + ChainName: 1, + ChainId: 1, + }, + }, + 2: { + Chain: common.Chain{ + ChainName: 2, + ChainId: 2, + }, + }, + } + zetaContext := NewZetaCoreContext(testCfg) + require.NotNil(t, zetaContext) + + // assert evm chain params + allEVMChainParams := zetaContext.GetAllEVMChainParams() + require.Equal(t, 2, len(allEVMChainParams)) + require.Equal(t, &observertypes.ChainParams{}, allEVMChainParams[1]) + require.Equal(t, &observertypes.ChainParams{}, allEVMChainParams[2]) + + evmChainParams1, found := zetaContext.GetEVMChainParams(1) + require.True(t, found) + require.Equal(t, &observertypes.ChainParams{}, evmChainParams1) + + evmChainParams2, found := zetaContext.GetEVMChainParams(2) + require.True(t, found) + require.Equal(t, &observertypes.ChainParams{}, evmChainParams2) + }) + + t.Run("should create new zeta core context with config containing btc config", func(t *testing.T) { + testCfg := config.NewConfig() + testCfg.BitcoinConfig = &config.BTCConfig{ + RPCUsername: "test username", + RPCPassword: "test password", + RPCHost: "test host", + RPCParams: "test params", + } + zetaContext := NewZetaCoreContext(testCfg) + require.NotNil(t, zetaContext) + + // assert btc chain params panic because chain params are not yet updated + assertPanic(t, func() { + zetaContext.GetBTCChainParams() + }, "BTCChain is missing for chainID 0") + }) +} + +func assertPanic(t *testing.T, f func(), errorLog string) { + defer func() { + r := recover() + if r != nil { + require.Contains(t, r, errorLog) + } + }() + f() } From 52de4392c3f94b150b547dd26c4e852a005ed401 Mon Sep 17 00:00:00 2001 From: skosito Date: Thu, 29 Feb 2024 15:15:48 +0100 Subject: [PATCH 04/16] Add update core context unit tests --- .../core_context/zeta_core_context_test.go | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git a/zetaclient/core_context/zeta_core_context_test.go b/zetaclient/core_context/zeta_core_context_test.go index fdf57ce881..2620ac5445 100644 --- a/zetaclient/core_context/zeta_core_context_test.go +++ b/zetaclient/core_context/zeta_core_context_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" "github.com/zeta-chain/zetacore/common" observertypes "github.com/zeta-chain/zetacore/x/observer/types" + clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" ) @@ -90,6 +91,168 @@ func TestNewZetaCoreContext(t *testing.T) { }) } +func TestUpdateZetaCoreContext(t *testing.T) { + t.Run("should update core context after being created from empty config", func(t *testing.T) { + testCfg := config.NewConfig() + + zetaContext := NewZetaCoreContext(testCfg) + require.NotNil(t, zetaContext) + + keyGenToUpdate := observertypes.Keygen{ + Status: observertypes.KeygenStatus_KeyGenSuccess, + GranteePubkeys: []string{"testpubkey1"}, + } + enabledChainsToUpdate := []common.Chain{ + { + ChainName: 1, + ChainId: 1, + }, + { + ChainName: 2, + ChainId: 2, + }, + } + evmChainParamsToUpdate := map[int64]*observertypes.ChainParams{ + 1: { + ChainId: 1, + }, + 2: { + ChainId: 2, + }, + } + btcChainParamsToUpdate := &observertypes.ChainParams{ + ChainId: 3, + } + tssPubKeyToUpdate := "tsspubkeytest" + loggers := clientcommon.DefaultLoggers() + zetaContext.Update( + &keyGenToUpdate, + enabledChainsToUpdate, + evmChainParamsToUpdate, + btcChainParamsToUpdate, + tssPubKeyToUpdate, + false, + loggers.Std, + ) + + // assert keygen updated + keyGen, keyGenFound := zetaContext.GetKeygen() + require.True(t, keyGenFound) + require.Equal(t, keyGenToUpdate, keyGen) + + // assert enabled chains updated + require.Equal(t, enabledChainsToUpdate, zetaContext.GetEnabledChains()) + + // assert current tss pubkey updated + require.Equal(t, tssPubKeyToUpdate, zetaContext.GetCurrentTssPubkey()) + + // assert btc chain params still empty because they were not specified in config + chain, btcChainParams, btcChainParamsFound := zetaContext.GetBTCChainParams() + require.Equal(t, common.Chain{}, chain) + require.False(t, btcChainParamsFound) + require.Equal(t, &observertypes.ChainParams{}, btcChainParams) + + // assert evm chain params still empty because they were not specified in config + allEVMChainParams := zetaContext.GetAllEVMChainParams() + require.Empty(t, allEVMChainParams) + }) + + t.Run("should update core context after being created from config with evm and btc chain params", func(t *testing.T) { + testCfg := config.NewConfig() + testCfg.EVMChainConfigs = map[int64]*config.EVMConfig{ + 1: { + Chain: common.Chain{ + ChainName: 1, + ChainId: 1, + }, + }, + 2: { + Chain: common.Chain{ + ChainName: 2, + ChainId: 2, + }, + }, + } + testCfg.BitcoinConfig = &config.BTCConfig{ + RPCUsername: "test username", + RPCPassword: "test password", + RPCHost: "test host", + RPCParams: "test params", + } + + zetaContext := NewZetaCoreContext(testCfg) + require.NotNil(t, zetaContext) + + keyGenToUpdate := observertypes.Keygen{ + Status: observertypes.KeygenStatus_KeyGenSuccess, + GranteePubkeys: []string{"testpubkey1"}, + } + enabledChainsToUpdate := []common.Chain{ + { + ChainName: 1, + ChainId: 1, + }, + { + ChainName: 2, + ChainId: 2, + }, + } + evmChainParamsToUpdate := map[int64]*observertypes.ChainParams{ + 1: { + ChainId: 1, + }, + 2: { + ChainId: 2, + }, + } + + testBtcChain := common.BtcTestNetChain() + btcChainParamsToUpdate := &observertypes.ChainParams{ + ChainId: testBtcChain.ChainId, + } + tssPubKeyToUpdate := "tsspubkeytest" + loggers := clientcommon.DefaultLoggers() + zetaContext.Update( + &keyGenToUpdate, + enabledChainsToUpdate, + evmChainParamsToUpdate, + btcChainParamsToUpdate, + tssPubKeyToUpdate, + false, + loggers.Std, + ) + + // assert keygen updated + keyGen, keyGenFound := zetaContext.GetKeygen() + require.True(t, keyGenFound) + require.Equal(t, keyGenToUpdate, keyGen) + + // assert enabled chains updated + require.Equal(t, enabledChainsToUpdate, zetaContext.GetEnabledChains()) + + // assert current tss pubkey updated + require.Equal(t, tssPubKeyToUpdate, zetaContext.GetCurrentTssPubkey()) + + // assert btc chain params + chain, btcChainParams, btcChainParamsFound := zetaContext.GetBTCChainParams() + require.Equal(t, testBtcChain, chain) + require.True(t, btcChainParamsFound) + require.Equal(t, btcChainParamsToUpdate, btcChainParams) + + // assert evm chain params + allEVMChainParams := zetaContext.GetAllEVMChainParams() + require.Equal(t, evmChainParamsToUpdate, allEVMChainParams) + + evmChainParams1, found := zetaContext.GetEVMChainParams(1) + require.True(t, found) + require.Equal(t, evmChainParamsToUpdate[1], evmChainParams1) + + evmChainParams2, found := zetaContext.GetEVMChainParams(2) + require.True(t, found) + require.Equal(t, evmChainParamsToUpdate[2], evmChainParams2) + }) +} + func assertPanic(t *testing.T, f func(), errorLog string) { defer func() { r := recover() From 5086f8c7d43796d6aeb16cb29df6b1a2083db7ca Mon Sep 17 00:00:00 2001 From: skosito Date: Mon, 4 Mar 2024 15:27:48 +0100 Subject: [PATCH 05/16] Cleanup pointers from config --- cmd/zetaclientd/debug.go | 2 +- cmd/zetaclientd/main.go | 6 +- cmd/zetaclientd/p2p_diagnostics.go | 2 +- cmd/zetaclientd/start_utils.go | 15 ++-- cmd/zetaclientd/utils.go | 8 +-- zetaclient/app_context/app_context.go | 15 ++-- zetaclient/bitcoin/bitcoin_client_rpc_test.go | 2 +- zetaclient/common/utils_test.go | 4 +- zetaclient/config/config.go | 12 ++-- zetaclient/config/config_chain.go | 4 +- zetaclient/config/types.go | 69 ++++--------------- zetaclient/core_context/zeta_core_context.go | 5 +- .../core_context/zeta_core_context_test.go | 8 +-- zetaclient/evm/inbounds.go | 4 +- zetaclient/evm/inbounds_test.go | 4 +- zetaclient/keys/keys.go | 2 +- zetaclient/keys/keys_test.go | 4 +- .../supplychecker/zeta_supply_checker.go | 2 +- zetaclient/testutils/utils.go | 4 +- zetaclient/tss/tss_signer.go | 2 +- 20 files changed, 64 insertions(+), 110 deletions(-) diff --git a/cmd/zetaclientd/debug.go b/cmd/zetaclientd/debug.go index 8dcb6e666a..4d7eba1903 100644 --- a/cmd/zetaclientd/debug.go +++ b/cmd/zetaclientd/debug.go @@ -103,7 +103,7 @@ func DebugCmd() *cobra.Command { ob.WithLogger(chainLogger) client := ðclient.Client{} coinType := common.CoinType_Cmd - for chain, evmConfig := range cfg.GetAllEVMConfigs() { + for chain, evmConfig := range cfg.EVMChainConfigs { if chainID == chain { client, err = ethclient.Dial(evmConfig.Endpoint) if err != nil { diff --git a/cmd/zetaclientd/main.go b/cmd/zetaclientd/main.go index 767b1fd78e..07654a0375 100644 --- a/cmd/zetaclientd/main.go +++ b/cmd/zetaclientd/main.go @@ -60,7 +60,7 @@ func SetupConfigForTest() { } -func InitLogger(cfg *config.Config) (clientcommon.ClientLogger, error) { +func InitLogger(cfg config.Config) (clientcommon.ClientLogger, error) { // open compliance log file file, err := OpenComplianceLogFile(cfg) if err != nil { @@ -92,10 +92,10 @@ func InitLogger(cfg *config.Config) (clientcommon.ClientLogger, error) { }, nil } -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 != nil && cfg.ComplianceConfig.LogPath != "" { + if cfg.ComplianceConfig.LogPath != "" { logPath = cfg.ComplianceConfig.LogPath } diff --git a/cmd/zetaclientd/p2p_diagnostics.go b/cmd/zetaclientd/p2p_diagnostics.go index 38bc269bc6..72a1580224 100644 --- a/cmd/zetaclientd/p2p_diagnostics.go +++ b/cmd/zetaclientd/p2p_diagnostics.go @@ -26,7 +26,7 @@ import ( "github.com/zeta-chain/zetacore/zetaclient/config" ) -func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, bridgePk cryptotypes.PrivKey, cfg *config.Config) error { +func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, bridgePk cryptotypes.PrivKey, cfg config.Config) error { startLogger.Warn().Msg("P2P Diagnostic mode enabled") startLogger.Warn().Msgf("seed peer: %s", peers) diff --git a/cmd/zetaclientd/start_utils.go b/cmd/zetaclientd/start_utils.go index 042f2fb20d..fe87b1d4f6 100644 --- a/cmd/zetaclientd/start_utils.go +++ b/cmd/zetaclientd/start_utils.go @@ -13,12 +13,12 @@ import ( "google.golang.org/grpc" ) -func waitForZetaCore(configData *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 { _, err := grpc.Dial( - fmt.Sprintf("%s:9090", configData.ZetaCoreURL), + fmt.Sprintf("%s:9090", config.ZetaCoreURL), grpc.WithInsecure(), ) if err != nil { @@ -54,20 +54,19 @@ 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 { +func maskCfg(cfg config.Config) string { maskedCfg := config.NewConfig() - // Deep copy since cfg contains some references - *maskedCfg = *cfg - maskedCfg.BitcoinConfig = &config.BTCConfig{ + maskedCfg = cfg + maskedCfg.BitcoinConfig = config.BTCConfig{ RPCUsername: cfg.BitcoinConfig.RPCUsername, RPCPassword: cfg.BitcoinConfig.RPCPassword, RPCHost: cfg.BitcoinConfig.RPCHost, RPCParams: cfg.BitcoinConfig.RPCParams, } - maskedCfg.EVMChainConfigs = map[int64]*config.EVMConfig{} + maskedCfg.EVMChainConfigs = map[int64]config.EVMConfig{} for key, val := range cfg.EVMChainConfigs { - maskedCfg.EVMChainConfigs[key] = &config.EVMConfig{ + maskedCfg.EVMChainConfigs[key] = config.EVMConfig{ Chain: val.Chain, Endpoint: val.Endpoint, } diff --git a/cmd/zetaclientd/utils.go b/cmd/zetaclientd/utils.go index 82a16dcb7f..6aa8379fa2 100644 --- a/cmd/zetaclientd/utils.go +++ b/cmd/zetaclientd/utils.go @@ -22,7 +22,7 @@ func CreateAuthzSigner(granter string, grantee sdk.AccAddress) { authz.SetupAuthZSignerList(granter, grantee) } -func CreateZetaBridge(cfg *config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetabridge.ZetaCoreBridge, error) { +func CreateZetaBridge(cfg config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetabridge.ZetaCoreBridge, error) { hotKey := cfg.AuthzHotkey if cfg.HsmMode { hotKey = cfg.HsmHotKey @@ -58,7 +58,7 @@ func CreateSignerMap( ) (map[common.Chain]interfaces.ChainSigner, error) { signerMap := make(map[common.Chain]interfaces.ChainSigner) // EVM signers - for _, evmConfig := range appContext.Config().GetAllEVMConfigs() { + for _, evmConfig := range appContext.Config().EVMChainConfigs { if evmConfig.Chain.IsZetaChain() { continue } @@ -100,11 +100,11 @@ func CreateChainClientMap( ) (map[common.Chain]interfaces.ChainClient, error) { clientMap := make(map[common.Chain]interfaces.ChainClient) // EVM clients - for _, evmConfig := range appContext.Config().GetAllEVMConfigs() { + for _, evmConfig := range appContext.Config().EVMChainConfigs { if evmConfig.Chain.IsZetaChain() { continue } - co, err := evm.NewEVMChainClient(appContext, bridge, tss, dbpath, loggers, *evmConfig, ts) + co, err := evm.NewEVMChainClient(appContext, bridge, tss, dbpath, loggers, evmConfig, ts) if err != nil { loggers.Std.Error().Err(err).Msgf("NewEVMChainClient error for chain %s", evmConfig.Chain.String()) continue diff --git a/zetaclient/app_context/app_context.go b/zetaclient/app_context/app_context.go index deab613618..38637b3846 100644 --- a/zetaclient/app_context/app_context.go +++ b/zetaclient/app_context/app_context.go @@ -9,13 +9,13 @@ import ( // AppContext contains global app structs like config, core context and logger type AppContext struct { coreContext *corecontext.ZetaCoreContext - config *config.Config + config config.Config } // NewAppContext creates and returns new AppContext func NewAppContext( coreContext *corecontext.ZetaCoreContext, - config *config.Config, + config config.Config, ) *AppContext { return &AppContext{ coreContext: coreContext, @@ -23,22 +23,21 @@ func NewAppContext( } } -func (a *AppContext) Config() *config.Config { +func (a AppContext) Config() config.Config { return a.config } -func (a *AppContext) ZetaCoreContext() *corecontext.ZetaCoreContext { +func (a AppContext) ZetaCoreContext() *corecontext.ZetaCoreContext { return a.coreContext } // GetBTCChainAndConfig returns btc chain and config if enabled -func (a *AppContext) GetBTCChainAndConfig() (common.Chain, config.BTCConfig, bool) { - btcConfig, configEnabled := a.Config().GetBTCConfig() +func (a AppContext) GetBTCChainAndConfig() (common.Chain, config.BTCConfig, bool) { btcChain, _, paramsEnabled := a.ZetaCoreContext().GetBTCChainParams() - if !configEnabled || !paramsEnabled { + if !a.config.BTCEnabled() || !paramsEnabled { return common.Chain{}, config.BTCConfig{}, false } - return btcChain, btcConfig, true + return btcChain, a.config.BitcoinConfig, true } diff --git a/zetaclient/bitcoin/bitcoin_client_rpc_test.go b/zetaclient/bitcoin/bitcoin_client_rpc_test.go index d40a08152c..7f8c060dce 100644 --- a/zetaclient/bitcoin/bitcoin_client_rpc_test.go +++ b/zetaclient/bitcoin/bitcoin_client_rpc_test.go @@ -46,7 +46,7 @@ func (suite *BitcoinClientTestSuite) SetupTest() { tss := interfaces.TestSigner{ PrivKey: privateKey, } - appContext := appcontext.NewAppContext(&corecontext.ZetaCoreContext{}, &config.Config{}) + appContext := appcontext.NewAppContext(&corecontext.ZetaCoreContext{}, config.Config{}) client, err := NewBitcoinClient(appContext, common.BtcRegtestChain(), nil, tss, "/tmp", clientcommon.DefaultLoggers(), nil) suite.Require().NoError(err) suite.BitcoinChainClient = client diff --git a/zetaclient/common/utils_test.go b/zetaclient/common/utils_test.go index 41b3f0c4cf..b7fa29b9bf 100644 --- a/zetaclient/common/utils_test.go +++ b/zetaclient/common/utils_test.go @@ -18,8 +18,8 @@ func TestCctxRestricted(t *testing.T) { require.NoError(t, err) // create config - cfg := &config.Config{ - ComplianceConfig: &config.ComplianceConfig{}, + cfg := config.Config{ + ComplianceConfig: config.ComplianceConfig{}, } t.Run("should return true if sender is restricted", func(t *testing.T) { diff --git a/zetaclient/config/config.go b/zetaclient/config/config.go index 08a9d4c002..4cb0e6b21b 100644 --- a/zetaclient/config/config.go +++ b/zetaclient/config/config.go @@ -36,12 +36,12 @@ 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 nil, err + return Config{}, err } file = filepath.Clean(file) @@ -49,11 +49,11 @@ func Load(path string) (*Config, error) { cfg := NewConfig() input, err := os.ReadFile(file) if err != nil { - return nil, err + return Config{}, err } err = json.Unmarshal(input, &cfg) if err != nil { - return nil, err + return Config{}, err } // read keyring backend and use test by default @@ -61,7 +61,7 @@ func Load(path string) (*Config, error) { cfg.KeyringBackend = KeyringBackendTest } if cfg.KeyringBackend != KeyringBackendFile && cfg.KeyringBackend != KeyringBackendTest { - return nil, fmt.Errorf("invalid keyring backend %s", cfg.KeyringBackend) + return Config{}, fmt.Errorf("invalid keyring backend %s", cfg.KeyringBackend) } // fields sanitization @@ -75,7 +75,7 @@ func Load(path string) (*Config, error) { return cfg, nil } -func LoadComplianceConfig(cfg *Config) { +func LoadComplianceConfig(cfg Config) { restrictedAddressBook = cfg.GetRestrictedAddressBook() } diff --git a/zetaclient/config/config_chain.go b/zetaclient/config/config_chain.go index 2596c554a5..ea497a1821 100644 --- a/zetaclient/config/config_chain.go +++ b/zetaclient/config/config_chain.go @@ -38,14 +38,14 @@ func New() Config { } } -var bitcoinConfigRegnet = &BTCConfig{ +var bitcoinConfigRegnet = BTCConfig{ RPCUsername: "e2e", RPCPassword: "123", RPCHost: "bitcoin:18443", RPCParams: "regtest", } -var evmChainsConfigs = map[int64]*EVMConfig{ +var evmChainsConfigs = map[int64]EVMConfig{ common.EthChain().ChainId: { Chain: common.EthChain(), }, diff --git a/zetaclient/config/types.go b/zetaclient/config/types.go index a85f9f269e..2995705290 100644 --- a/zetaclient/config/types.go +++ b/zetaclient/config/types.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "strings" - "sync" ethcommon "github.com/ethereum/go-ethereum/common" "github.com/zeta-chain/zetacore/common" @@ -51,8 +50,6 @@ type ComplianceConfig struct { // TODO: use snake case for json fields // https://github.com/zeta-chain/node/issues/1020 type Config struct { - cfgLock *sync.RWMutex `json:"-"` - Peer string `json:"Peer"` PublicIP string `json:"PublicIP"` LogFormat string `json:"LogFormat"` @@ -73,22 +70,22 @@ type Config struct { HsmMode bool `json:"HsmMode"` HsmHotKey string `json:"HsmHotKey"` - EVMChainConfigs map[int64]*EVMConfig `json:"EVMChainConfigs"` - BitcoinConfig *BTCConfig `json:"BitcoinConfig"` + EVMChainConfigs map[int64]EVMConfig `json:"EVMChainConfigs"` + BitcoinConfig BTCConfig `json:"BitcoinConfig"` // compliance config - ComplianceConfig *ComplianceConfig `json:"ComplianceConfig"` + ComplianceConfig ComplianceConfig `json:"ComplianceConfig"` } -func NewConfig() *Config { - return &Config{ - cfgLock: &sync.RWMutex{}, - } +func NewConfig() Config { + return Config{} +} + +func (c Config) BTCEnabled() bool { + return c.BitcoinConfig != BTCConfig{} } -func (c *Config) String() string { - c.cfgLock.RLock() - defer c.cfgLock.RUnlock() +func (c Config) String() string { s, err := json.MarshalIndent(c, "", "\t") if err != nil { return "" @@ -96,56 +93,18 @@ func (c *Config) String() string { return string(s) } -func (c *Config) GetEVMConfig(chainID int64) (EVMConfig, bool) { - c.cfgLock.RLock() - defer c.cfgLock.RUnlock() - evmCfg, found := c.EVMChainConfigs[chainID] - return *evmCfg, found -} - -func (c *Config) GetAllEVMConfigs() map[int64]*EVMConfig { - c.cfgLock.RLock() - defer c.cfgLock.RUnlock() - - // deep copy evm configs - copied := make(map[int64]*EVMConfig, len(c.EVMChainConfigs)) - for chainID, evmConfig := range c.EVMChainConfigs { - copied[chainID] = &EVMConfig{} - *copied[chainID] = *evmConfig - } - return copied -} - -func (c *Config) GetBTCConfig() (BTCConfig, bool) { - c.cfgLock.RLock() - defer c.cfgLock.RUnlock() - - if c.BitcoinConfig == nil { // bitcoin is not enabled - return BTCConfig{}, false - } - return *c.BitcoinConfig, true -} - // GetRestrictedAddressBook returns a map of restricted addresses // Note: the restricted address book contains both ETH and BTC addresses -func (c *Config) GetRestrictedAddressBook() map[string]bool { +func (c Config) GetRestrictedAddressBook() map[string]bool { restrictedAddresses := make(map[string]bool) - if c.ComplianceConfig != nil { - for _, address := range c.ComplianceConfig.RestrictedAddresses { - if address != "" { - restrictedAddresses[strings.ToLower(address)] = true - } + for _, address := range c.ComplianceConfig.RestrictedAddresses { + if address != "" { + restrictedAddresses[strings.ToLower(address)] = true } } return restrictedAddresses } -func (c *Config) GetKeyringBackend() KeyringBackend { - c.cfgLock.RLock() - defer c.cfgLock.RUnlock() - return c.KeyringBackend -} - // ValidateChainParams performs some basic checks on core params func ValidateChainParams(chainParams *observertypes.ChainParams) error { if chainParams == nil { diff --git a/zetaclient/core_context/zeta_core_context.go b/zetaclient/core_context/zeta_core_context.go index 0c5794f4cd..94871b03a6 100644 --- a/zetaclient/core_context/zeta_core_context.go +++ b/zetaclient/core_context/zeta_core_context.go @@ -24,14 +24,13 @@ type ZetaCoreContext struct { // NewZetaCoreContext creates and returns new ZetaCoreContext // it is initializing chain params from provided config -func NewZetaCoreContext(cfg *config.Config) *ZetaCoreContext { +func NewZetaCoreContext(cfg config.Config) *ZetaCoreContext { evmChainParams := make(map[int64]*observertypes.ChainParams) for _, e := range cfg.EVMChainConfigs { evmChainParams[e.Chain.ChainId] = &observertypes.ChainParams{} } var bitcoinChainParams *observertypes.ChainParams - _, found := cfg.GetBTCConfig() - if found { + if cfg.BTCEnabled() { bitcoinChainParams = &observertypes.ChainParams{} } return &ZetaCoreContext{ diff --git a/zetaclient/core_context/zeta_core_context_test.go b/zetaclient/core_context/zeta_core_context_test.go index 2620ac5445..6b559ccca2 100644 --- a/zetaclient/core_context/zeta_core_context_test.go +++ b/zetaclient/core_context/zeta_core_context_test.go @@ -41,7 +41,7 @@ func TestNewZetaCoreContext(t *testing.T) { t.Run("should create new zeta core context with config containing evm chain params", func(t *testing.T) { testCfg := config.NewConfig() - testCfg.EVMChainConfigs = map[int64]*config.EVMConfig{ + testCfg.EVMChainConfigs = map[int64]config.EVMConfig{ 1: { Chain: common.Chain{ ChainName: 1, @@ -75,7 +75,7 @@ func TestNewZetaCoreContext(t *testing.T) { t.Run("should create new zeta core context with config containing btc config", func(t *testing.T) { testCfg := config.NewConfig() - testCfg.BitcoinConfig = &config.BTCConfig{ + testCfg.BitcoinConfig = config.BTCConfig{ RPCUsername: "test username", RPCPassword: "test password", RPCHost: "test host", @@ -159,7 +159,7 @@ func TestUpdateZetaCoreContext(t *testing.T) { t.Run("should update core context after being created from config with evm and btc chain params", func(t *testing.T) { testCfg := config.NewConfig() - testCfg.EVMChainConfigs = map[int64]*config.EVMConfig{ + testCfg.EVMChainConfigs = map[int64]config.EVMConfig{ 1: { Chain: common.Chain{ ChainName: 1, @@ -173,7 +173,7 @@ func TestUpdateZetaCoreContext(t *testing.T) { }, }, } - testCfg.BitcoinConfig = &config.BTCConfig{ + testCfg.BitcoinConfig = config.BTCConfig{ RPCUsername: "test username", RPCPassword: "test password", RPCHost: "test host", diff --git a/zetaclient/evm/inbounds.go b/zetaclient/evm/inbounds.go index ffb65fd5f4..2453d38f47 100644 --- a/zetaclient/evm/inbounds.go +++ b/zetaclient/evm/inbounds.go @@ -169,9 +169,7 @@ func (ob *ChainClient) CheckReceiptForCoinTypeERC20(txHash string, vote bool) (s err = ob.CheckEvmTxLog(&zetaDeposited.Raw, addrCustory, txHash, TopicsDeposited) if err == nil { msg = ob.GetInboundVoteMsgForDepositedEvent(zetaDeposited, sender) - if err == nil { - break - } + break } else { ob.logger.ExternalChainWatcher.Error().Err(err).Msg("CheckEvmTxLog error on ERC20CustodyDeposited event") } diff --git a/zetaclient/evm/inbounds_test.go b/zetaclient/evm/inbounds_test.go index 84b24dd200..a1579f08ff 100644 --- a/zetaclient/evm/inbounds_test.go +++ b/zetaclient/evm/inbounds_test.go @@ -63,8 +63,8 @@ func TestEthereum_GetInboundVoteMsgForZetaSentEvent(t *testing.T) { require.Equal(t, "0x477544c4b8c8be544b23328b21286125c89cd6bb5d1d6d388d91eea8ea1a6f1f", msg.Digest()) // create config - cfg := &config.Config{ - ComplianceConfig: &config.ComplianceConfig{}, + cfg := config.Config{ + ComplianceConfig: config.ComplianceConfig{}, } t.Run("should return nil msg if sender is restricted", func(t *testing.T) { diff --git a/zetaclient/keys/keys.go b/zetaclient/keys/keys.go index 974743098f..e7e40c2bdc 100644 --- a/zetaclient/keys/keys.go +++ b/zetaclient/keys/keys.go @@ -44,7 +44,7 @@ func GetGranteeKeyName(signerName string) string { } // GetKeyringKeybase return keyring and key info -func GetKeyringKeybase(cfg *config.Config, hotkeyPassword string) (ckeys.Keyring, string, error) { +func GetKeyringKeybase(cfg config.Config, hotkeyPassword string) (ckeys.Keyring, string, error) { granteeName := cfg.AuthzHotkey chainHomeFolder := cfg.ZetaCoreHome logger := log.Logger.With().Str("module", "GetKeyringKeybase").Logger() diff --git a/zetaclient/keys/keys_test.go b/zetaclient/keys/keys_test.go index 5cbdeec5d5..2b37f5a63c 100644 --- a/zetaclient/keys/keys_test.go +++ b/zetaclient/keys/keys_test.go @@ -63,7 +63,7 @@ func (*KeysSuite) setupKeysForTest(c *C) string { func (ks *KeysSuite) TestGetKeyringKeybase(c *C) { keyring.Debug = true - cfg := &config.Config{ + cfg := config.Config{ AuthzHotkey: "bob", ZetaCoreHome: "/Users/test/.zetacored/", } @@ -83,7 +83,7 @@ func (ks *KeysSuite) TestNewKeys(c *C) { c.Assert(err, IsNil) }() - cfg := &config.Config{ + cfg := config.Config{ AuthzHotkey: signerNameForTest, ZetaCoreHome: folder, } diff --git a/zetaclient/supplychecker/zeta_supply_checker.go b/zetaclient/supplychecker/zeta_supply_checker.go index 2a2e112c4a..c5dce419cd 100644 --- a/zetaclient/supplychecker/zeta_supply_checker.go +++ b/zetaclient/supplychecker/zeta_supply_checker.go @@ -49,7 +49,7 @@ func NewZetaSupplyChecker(appContext *appcontext.AppContext, zetaClient *zetabri coreContext: appContext.ZetaCoreContext(), zetaClient: zetaClient, } - for _, evmConfig := range appContext.Config().GetAllEVMConfigs() { + for _, evmConfig := range appContext.Config().EVMChainConfigs { if evmConfig.Chain.IsZetaChain() { continue } diff --git a/zetaclient/testutils/utils.go b/zetaclient/testutils/utils.go index 4f00f10bfd..2e26dec67d 100644 --- a/zetaclient/testutils/utils.go +++ b/zetaclient/testutils/utils.go @@ -56,8 +56,8 @@ func SaveBTCBlockTrimTx(blockVb *btcjson.GetBlockVerboseTxResult, filename strin return SaveObjectToJSONFile(blockVb, filename) } -func ComplianceConfigTest() *config.ComplianceConfig { - return &config.ComplianceConfig{ +func ComplianceConfigTest() config.ComplianceConfig { + return config.ComplianceConfig{ RestrictedAddresses: []string{RestrictedEVMAddressTest, RestrictedBtcAddressTest}, } } diff --git a/zetaclient/tss/tss_signer.go b/zetaclient/tss/tss_signer.go index f9abdb78ee..b95baadf1b 100644 --- a/zetaclient/tss/tss_signer.go +++ b/zetaclient/tss/tss_signer.go @@ -127,7 +127,7 @@ func NewTSS( return &newTss, nil } -func SetupTSSServer(peer p2p.AddrList, privkey tmcrypto.PrivKey, preParams *keygen.LocalPreParams, cfg *config.Config, tssPassword string) (*tss.TssServer, error) { +func SetupTSSServer(peer p2p.AddrList, privkey tmcrypto.PrivKey, preParams *keygen.LocalPreParams, cfg config.Config, tssPassword string) (*tss.TssServer, error) { bootstrapPeers := peer log.Info().Msgf("Peers AddrList %v", bootstrapPeers) From 4e2c07ce7736ce5b4961bc4bf6d2312f6618e9d8 Mon Sep 17 00:00:00 2001 From: skosito Date: Mon, 4 Mar 2024 16:24:07 +0100 Subject: [PATCH 06/16] Cleanup keygen core context pointer --- cmd/zetaclientd/keygen_tss.go | 5 +---- cmd/zetaclientd/start.go | 5 +---- zetaclient/core_context/zeta_core_context.go | 18 ++++-------------- .../core_context/zeta_core_context_test.go | 9 +++------ 4 files changed, 9 insertions(+), 28 deletions(-) diff --git a/cmd/zetaclientd/keygen_tss.go b/cmd/zetaclientd/keygen_tss.go index 726fda11ee..16f79581b5 100644 --- a/cmd/zetaclientd/keygen_tss.go +++ b/cmd/zetaclientd/keygen_tss.go @@ -74,10 +74,7 @@ func GenerateTss( // This loop will try keygen at the keygen block and then wait for keygen to be successfully reported by all nodes before breaking out of the loop. // If keygen is unsuccessful, it will reset the triedKeygenAtBlock flag and try again at a new keygen block. - keyGen, found := appContext.ZetaCoreContext().GetKeygen() - if !found { - return nil, fmt.Errorf("keygen not found") - } + keyGen := appContext.ZetaCoreContext().GetKeygen() if keyGen.Status == observertypes.KeygenStatus_KeyGenSuccess { return tss, nil } diff --git a/cmd/zetaclientd/start.go b/cmd/zetaclientd/start.go index 6881120c15..c66a516968 100644 --- a/cmd/zetaclientd/start.go +++ b/cmd/zetaclientd/start.go @@ -190,10 +190,7 @@ func start(_ *cobra.Command, _ []string) error { // For existing keygen, this should directly proceed to the next step ticker := time.NewTicker(time.Second * 1) for range ticker.C { - keyGen, found := appContext.ZetaCoreContext().GetKeygen() - if !found { - return fmt.Errorf("keygen not found") - } + keyGen := appContext.ZetaCoreContext().GetKeygen() if keyGen.Status != observerTypes.KeygenStatus_KeyGenSuccess { startLogger.Info().Msgf("Waiting for TSS Keygen to be a success, current status %s", keyGen.Status) continue diff --git a/zetaclient/core_context/zeta_core_context.go b/zetaclient/core_context/zeta_core_context.go index 94871b03a6..ee0c1dd171 100644 --- a/zetaclient/core_context/zeta_core_context.go +++ b/zetaclient/core_context/zeta_core_context.go @@ -15,7 +15,7 @@ import ( // these are initialized and updated at runtime at every height type ZetaCoreContext struct { coreContextLock *sync.RWMutex - keygen *observertypes.Keygen + keygen observertypes.Keygen chainsEnabled []common.Chain evmChainParams map[int64]*observertypes.ChainParams bitcoinChainParams *observertypes.ChainParams @@ -41,21 +41,11 @@ func NewZetaCoreContext(cfg config.Config) *ZetaCoreContext { } } -func (c *ZetaCoreContext) GetKeygen() (observertypes.Keygen, bool) { +func (c *ZetaCoreContext) GetKeygen() observertypes.Keygen { c.coreContextLock.RLock() defer c.coreContextLock.RUnlock() - if c.keygen == nil { - return observertypes.Keygen{}, false - } - copiedPubkeys := make([]string, len(c.keygen.GranteePubkeys)) - copy(copiedPubkeys, c.keygen.GranteePubkeys) - - return observertypes.Keygen{ - Status: c.keygen.Status, - GranteePubkeys: copiedPubkeys, - BlockNumber: c.keygen.BlockNumber, - }, true + return c.keygen } func (c *ZetaCoreContext) GetCurrentTssPubkey() string { @@ -148,7 +138,7 @@ func (c *ZetaCoreContext) Update( } } } - c.keygen = keygen + c.keygen = *keygen c.chainsEnabled = newChains // update chain params for bitcoin if it has config in file if c.bitcoinChainParams != nil && btcChainParams != nil { diff --git a/zetaclient/core_context/zeta_core_context_test.go b/zetaclient/core_context/zeta_core_context_test.go index 6b559ccca2..08060d1181 100644 --- a/zetaclient/core_context/zeta_core_context_test.go +++ b/zetaclient/core_context/zeta_core_context_test.go @@ -18,8 +18,7 @@ func TestNewZetaCoreContext(t *testing.T) { require.NotNil(t, zetaContext) // assert keygen - keyGen, keyGenFound := zetaContext.GetKeygen() - require.False(t, keyGenFound) + keyGen := zetaContext.GetKeygen() require.Equal(t, observertypes.Keygen{}, keyGen) // assert enabled chains @@ -136,8 +135,7 @@ func TestUpdateZetaCoreContext(t *testing.T) { ) // assert keygen updated - keyGen, keyGenFound := zetaContext.GetKeygen() - require.True(t, keyGenFound) + keyGen := zetaContext.GetKeygen() require.Equal(t, keyGenToUpdate, keyGen) // assert enabled chains updated @@ -223,8 +221,7 @@ func TestUpdateZetaCoreContext(t *testing.T) { ) // assert keygen updated - keyGen, keyGenFound := zetaContext.GetKeygen() - require.True(t, keyGenFound) + keyGen := zetaContext.GetKeygen() require.Equal(t, keyGenToUpdate, keyGen) // assert enabled chains updated From d581bdfd1cc1710b5a20153d17d2fbb0377233c6 Mon Sep 17 00:00:00 2001 From: skosito Date: Mon, 4 Mar 2024 16:58:44 +0100 Subject: [PATCH 07/16] Add to changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index 54903786f5..4f4fa74ab2 100644 --- a/changelog.md +++ b/changelog.md @@ -7,6 +7,7 @@ * [1511](https://github.com/zeta-chain/node/pull/1511) - move ballot voting logic from `crosschain` to `observer` * [1783](https://github.com/zeta-chain/node/pull/1783) - refactor zetaclient metrics naming and structure * [1774](https://github.com/zeta-chain/node/pull/1774) - split params and config in zetaclient +* [1831](https://github.com/zeta-chain/node/pull/1831) - split params and config in zetaclient follow up ### Features From e865ec1045958cc2d21c55e145e1fc1474c2fd10 Mon Sep 17 00:00:00 2001 From: skosito Date: Mon, 4 Mar 2024 16:58:54 +0100 Subject: [PATCH 08/16] Fix lint and masked cfg --- cmd/zetaclientd/start_utils.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/cmd/zetaclientd/start_utils.go b/cmd/zetaclientd/start_utils.go index fe87b1d4f6..f58cef65c9 100644 --- a/cmd/zetaclientd/start_utils.go +++ b/cmd/zetaclientd/start_utils.go @@ -55,25 +55,10 @@ func validatePeer(seedPeer string) error { // // other fields can be added. func maskCfg(cfg config.Config) string { - maskedCfg := config.NewConfig() - - maskedCfg = cfg - maskedCfg.BitcoinConfig = config.BTCConfig{ - RPCUsername: cfg.BitcoinConfig.RPCUsername, - RPCPassword: cfg.BitcoinConfig.RPCPassword, - RPCHost: cfg.BitcoinConfig.RPCHost, - RPCParams: cfg.BitcoinConfig.RPCParams, - } - maskedCfg.EVMChainConfigs = map[int64]config.EVMConfig{} - for key, val := range cfg.EVMChainConfigs { - maskedCfg.EVMChainConfigs[key] = config.EVMConfig{ - Chain: val.Chain, - Endpoint: val.Endpoint, - } - } + maskedCfg := cfg // Mask Sensitive data - for _, chain := range maskedCfg.EVMChainConfigs { + for key, chain := range maskedCfg.EVMChainConfigs { if chain.Endpoint == "" { continue } @@ -81,7 +66,10 @@ func maskCfg(cfg config.Config) string { if err != nil { continue } - chain.Endpoint = endpointURL.Hostname() + maskedCfg.EVMChainConfigs[key] = config.EVMConfig{ + Chain: chain.Chain, + Endpoint: endpointURL.Hostname(), + } } maskedCfg.BitcoinConfig.RPCUsername = "" From 07d59f0d9c4f9d5a00296317625d759dcdc845e2 Mon Sep 17 00:00:00 2001 From: skosito Date: Mon, 4 Mar 2024 17:19:18 +0100 Subject: [PATCH 09/16] Fix lint --- zetaclient/evm/inbounds.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zetaclient/evm/inbounds.go b/zetaclient/evm/inbounds.go index 2453d38f47..6ed9f3a426 100644 --- a/zetaclient/evm/inbounds.go +++ b/zetaclient/evm/inbounds.go @@ -170,9 +170,8 @@ func (ob *ChainClient) CheckReceiptForCoinTypeERC20(txHash string, vote bool) (s if err == nil { msg = ob.GetInboundVoteMsgForDepositedEvent(zetaDeposited, sender) break - } else { - ob.logger.ExternalChainWatcher.Error().Err(err).Msg("CheckEvmTxLog error on ERC20CustodyDeposited event") } + ob.logger.ExternalChainWatcher.Error().Err(err).Msg("CheckEvmTxLog error on ERC20CustodyDeposited event") } } if msg == nil { From f398f3c3ff3f8f17bb2cb3c6cc700f7ba184990a Mon Sep 17 00:00:00 2001 From: skosito Date: Mon, 4 Mar 2024 20:58:58 +0100 Subject: [PATCH 10/16] Cleanup --- cmd/zetaclientd/debug.go | 2 +- cmd/zetaclientd/start_utils.go | 14 ++++++++ cmd/zetaclientd/utils.go | 4 +-- zetaclient/app_context/app_context.go | 5 +-- zetaclient/config/types.go | 33 +++++++++++++++++-- zetaclient/core_context/zeta_core_context.go | 14 ++++++-- .../supplychecker/zeta_supply_checker.go | 2 +- 7 files changed, 63 insertions(+), 11 deletions(-) diff --git a/cmd/zetaclientd/debug.go b/cmd/zetaclientd/debug.go index 4d7eba1903..8dcb6e666a 100644 --- a/cmd/zetaclientd/debug.go +++ b/cmd/zetaclientd/debug.go @@ -103,7 +103,7 @@ func DebugCmd() *cobra.Command { ob.WithLogger(chainLogger) client := ðclient.Client{} coinType := common.CoinType_Cmd - for chain, evmConfig := range cfg.EVMChainConfigs { + for chain, evmConfig := range cfg.GetAllEVMConfigs() { if chainID == chain { client, err = ethclient.Dial(evmConfig.Endpoint) if err != nil { diff --git a/cmd/zetaclientd/start_utils.go b/cmd/zetaclientd/start_utils.go index f58cef65c9..06d50c20a3 100644 --- a/cmd/zetaclientd/start_utils.go +++ b/cmd/zetaclientd/start_utils.go @@ -57,6 +57,20 @@ func validatePeer(seedPeer string) error { func maskCfg(cfg config.Config) string { maskedCfg := cfg + maskedCfg.BitcoinConfig = config.BTCConfig{ + RPCUsername: cfg.BitcoinConfig.RPCUsername, + RPCPassword: cfg.BitcoinConfig.RPCPassword, + RPCHost: cfg.BitcoinConfig.RPCHost, + RPCParams: cfg.BitcoinConfig.RPCParams, + } + maskedCfg.EVMChainConfigs = map[int64]config.EVMConfig{} + for key, val := range cfg.EVMChainConfigs { + maskedCfg.EVMChainConfigs[key] = config.EVMConfig{ + Chain: val.Chain, + Endpoint: val.Endpoint, + } + } + // Mask Sensitive data for key, chain := range maskedCfg.EVMChainConfigs { if chain.Endpoint == "" { diff --git a/cmd/zetaclientd/utils.go b/cmd/zetaclientd/utils.go index 6aa8379fa2..42469ae4e9 100644 --- a/cmd/zetaclientd/utils.go +++ b/cmd/zetaclientd/utils.go @@ -58,7 +58,7 @@ func CreateSignerMap( ) (map[common.Chain]interfaces.ChainSigner, error) { signerMap := make(map[common.Chain]interfaces.ChainSigner) // EVM signers - for _, evmConfig := range appContext.Config().EVMChainConfigs { + for _, evmConfig := range appContext.Config().GetAllEVMConfigs() { if evmConfig.Chain.IsZetaChain() { continue } @@ -100,7 +100,7 @@ func CreateChainClientMap( ) (map[common.Chain]interfaces.ChainClient, error) { clientMap := make(map[common.Chain]interfaces.ChainClient) // EVM clients - for _, evmConfig := range appContext.Config().EVMChainConfigs { + for _, evmConfig := range appContext.Config().GetAllEVMConfigs() { if evmConfig.Chain.IsZetaChain() { continue } diff --git a/zetaclient/app_context/app_context.go b/zetaclient/app_context/app_context.go index 38637b3846..d73f48d9d0 100644 --- a/zetaclient/app_context/app_context.go +++ b/zetaclient/app_context/app_context.go @@ -33,11 +33,12 @@ func (a AppContext) ZetaCoreContext() *corecontext.ZetaCoreContext { // GetBTCChainAndConfig returns btc chain and config if enabled func (a AppContext) GetBTCChainAndConfig() (common.Chain, config.BTCConfig, bool) { + btcConfig, configEnabled := a.Config().GetBTCConfig() btcChain, _, paramsEnabled := a.ZetaCoreContext().GetBTCChainParams() - if !a.config.BTCEnabled() || !paramsEnabled { + if !configEnabled || !paramsEnabled { return common.Chain{}, config.BTCConfig{}, false } - return btcChain, a.config.BitcoinConfig, true + return btcChain, btcConfig, true } diff --git a/zetaclient/config/types.go b/zetaclient/config/types.go index 2995705290..ea714fa379 100644 --- a/zetaclient/config/types.go +++ b/zetaclient/config/types.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "strings" + "sync" ethcommon "github.com/ethereum/go-ethereum/common" "github.com/zeta-chain/zetacore/common" @@ -50,6 +51,8 @@ type ComplianceConfig struct { // TODO: use snake case for json fields // https://github.com/zeta-chain/node/issues/1020 type Config struct { + cfgLock *sync.RWMutex `json:"-"` + Peer string `json:"Peer"` PublicIP string `json:"PublicIP"` LogFormat string `json:"LogFormat"` @@ -78,11 +81,35 @@ type Config struct { } func NewConfig() Config { - return Config{} + return Config{ + cfgLock: &sync.RWMutex{}, + } } -func (c Config) BTCEnabled() bool { - return c.BitcoinConfig != BTCConfig{} +func (c Config) GetEVMConfig(chainID int64) (EVMConfig, bool) { + c.cfgLock.RLock() + defer c.cfgLock.RUnlock() + evmCfg, found := c.EVMChainConfigs[chainID] + return evmCfg, found +} + +func (c Config) GetAllEVMConfigs() map[int64]EVMConfig { + c.cfgLock.RLock() + defer c.cfgLock.RUnlock() + + // deep copy evm configs + copied := make(map[int64]EVMConfig, len(c.EVMChainConfigs)) + for chainID, evmConfig := range c.EVMChainConfigs { + copied[chainID] = evmConfig + } + return copied +} + +func (c Config) GetBTCConfig() (BTCConfig, bool) { + c.cfgLock.RLock() + defer c.cfgLock.RUnlock() + + return c.BitcoinConfig, c.BitcoinConfig != (BTCConfig{}) } func (c Config) String() string { diff --git a/zetaclient/core_context/zeta_core_context.go b/zetaclient/core_context/zeta_core_context.go index ee0c1dd171..9eb3de1f43 100644 --- a/zetaclient/core_context/zeta_core_context.go +++ b/zetaclient/core_context/zeta_core_context.go @@ -30,7 +30,8 @@ func NewZetaCoreContext(cfg config.Config) *ZetaCoreContext { evmChainParams[e.Chain.ChainId] = &observertypes.ChainParams{} } var bitcoinChainParams *observertypes.ChainParams - if cfg.BTCEnabled() { + _, found := cfg.GetBTCConfig() + if found { bitcoinChainParams = &observertypes.ChainParams{} } return &ZetaCoreContext{ @@ -45,7 +46,16 @@ func (c *ZetaCoreContext) GetKeygen() observertypes.Keygen { c.coreContextLock.RLock() defer c.coreContextLock.RUnlock() - return c.keygen + var copiedPubkeys []string + if c.keygen.GranteePubkeys != nil { + copiedPubkeys = make([]string, len(c.keygen.GranteePubkeys)) + copy(copiedPubkeys, c.keygen.GranteePubkeys) + } + return observertypes.Keygen{ + Status: c.keygen.Status, + GranteePubkeys: copiedPubkeys, + BlockNumber: c.keygen.BlockNumber, + } } func (c *ZetaCoreContext) GetCurrentTssPubkey() string { diff --git a/zetaclient/supplychecker/zeta_supply_checker.go b/zetaclient/supplychecker/zeta_supply_checker.go index c5dce419cd..2a2e112c4a 100644 --- a/zetaclient/supplychecker/zeta_supply_checker.go +++ b/zetaclient/supplychecker/zeta_supply_checker.go @@ -49,7 +49,7 @@ func NewZetaSupplyChecker(appContext *appcontext.AppContext, zetaClient *zetabri coreContext: appContext.ZetaCoreContext(), zetaClient: zetaClient, } - for _, evmConfig := range appContext.Config().EVMChainConfigs { + for _, evmConfig := range appContext.Config().GetAllEVMConfigs() { if evmConfig.Chain.IsZetaChain() { continue } From 3b42b0b2a1937475f45127dc8a96e399312202d5 Mon Sep 17 00:00:00 2001 From: skosito Date: Mon, 4 Mar 2024 22:20:09 +0100 Subject: [PATCH 11/16] Cleanup --- cmd/zetaclientd/start_utils.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cmd/zetaclientd/start_utils.go b/cmd/zetaclientd/start_utils.go index 06d50c20a3..e0aa59002e 100644 --- a/cmd/zetaclientd/start_utils.go +++ b/cmd/zetaclientd/start_utils.go @@ -72,7 +72,7 @@ func maskCfg(cfg config.Config) string { } // Mask Sensitive data - for key, chain := range maskedCfg.EVMChainConfigs { + for _, chain := range maskedCfg.EVMChainConfigs { if chain.Endpoint == "" { continue } @@ -80,10 +80,7 @@ func maskCfg(cfg config.Config) string { if err != nil { continue } - maskedCfg.EVMChainConfigs[key] = config.EVMConfig{ - Chain: chain.Chain, - Endpoint: endpointURL.Hostname(), - } + chain.Endpoint = endpointURL.Hostname() } maskedCfg.BitcoinConfig.RPCUsername = "" From 4ddbe538162dbdaef90c7d6793c431d107436d47 Mon Sep 17 00:00:00 2001 From: skosito Date: Tue, 5 Mar 2024 13:05:19 +0100 Subject: [PATCH 12/16] Revert --- zetaclient/config/types.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zetaclient/config/types.go b/zetaclient/config/types.go index ea714fa379..5384f56c1a 100644 --- a/zetaclient/config/types.go +++ b/zetaclient/config/types.go @@ -132,6 +132,12 @@ func (c Config) GetRestrictedAddressBook() map[string]bool { return restrictedAddresses } +func (c *Config) GetKeyringBackend() KeyringBackend { + c.cfgLock.RLock() + defer c.cfgLock.RUnlock() + return c.KeyringBackend +} + // ValidateChainParams performs some basic checks on core params func ValidateChainParams(chainParams *observertypes.ChainParams) error { if chainParams == nil { From 6cab8583ff37529b52dc25ae1f7cf1a817a1fbd9 Mon Sep 17 00:00:00 2001 From: skosito Date: Tue, 5 Mar 2024 13:08:36 +0100 Subject: [PATCH 13/16] Add todo --- zetaclient/config/types.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zetaclient/config/types.go b/zetaclient/config/types.go index 5384f56c1a..13e5d6a337 100644 --- a/zetaclient/config/types.go +++ b/zetaclient/config/types.go @@ -138,6 +138,7 @@ func (c *Config) GetKeyringBackend() KeyringBackend { return c.KeyringBackend } +// TODO: is this function duplicate of one from observertypes? // ValidateChainParams performs some basic checks on core params func ValidateChainParams(chainParams *observertypes.ChainParams) error { if chainParams == nil { From 34d22f771304c96d4205b3527b8b9d1f70eb02ba Mon Sep 17 00:00:00 2001 From: skosito Date: Tue, 5 Mar 2024 18:56:32 +0100 Subject: [PATCH 14/16] PR comments --- zetaclient/config/types.go | 2 +- zetaclient/core_context/zeta_core_context_test.go | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/zetaclient/config/types.go b/zetaclient/config/types.go index 13e5d6a337..3e8c3c3692 100644 --- a/zetaclient/config/types.go +++ b/zetaclient/config/types.go @@ -138,7 +138,7 @@ func (c *Config) GetKeyringBackend() KeyringBackend { return c.KeyringBackend } -// TODO: is this function duplicate of one from observertypes? +// TODO: remove this duplicated function https://github.com/zeta-chain/node/issues/1838 // ValidateChainParams performs some basic checks on core params func ValidateChainParams(chainParams *observertypes.ChainParams) error { if chainParams == nil { diff --git a/zetaclient/core_context/zeta_core_context_test.go b/zetaclient/core_context/zeta_core_context_test.go index 08060d1181..e7690f0c23 100644 --- a/zetaclient/core_context/zeta_core_context_test.go +++ b/zetaclient/core_context/zeta_core_context_test.go @@ -1,4 +1,4 @@ -package corecontext +package corecontext_test import ( "testing" @@ -8,13 +8,14 @@ import ( observertypes "github.com/zeta-chain/zetacore/x/observer/types" clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" "github.com/zeta-chain/zetacore/zetaclient/config" + corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context" ) func TestNewZetaCoreContext(t *testing.T) { t.Run("should create new zeta core context with empty config", func(t *testing.T) { testCfg := config.NewConfig() - zetaContext := NewZetaCoreContext(testCfg) + zetaContext := corecontext.NewZetaCoreContext(testCfg) require.NotNil(t, zetaContext) // assert keygen @@ -54,7 +55,7 @@ func TestNewZetaCoreContext(t *testing.T) { }, }, } - zetaContext := NewZetaCoreContext(testCfg) + zetaContext := corecontext.NewZetaCoreContext(testCfg) require.NotNil(t, zetaContext) // assert evm chain params @@ -80,7 +81,7 @@ func TestNewZetaCoreContext(t *testing.T) { RPCHost: "test host", RPCParams: "test params", } - zetaContext := NewZetaCoreContext(testCfg) + zetaContext := corecontext.NewZetaCoreContext(testCfg) require.NotNil(t, zetaContext) // assert btc chain params panic because chain params are not yet updated @@ -94,7 +95,7 @@ func TestUpdateZetaCoreContext(t *testing.T) { t.Run("should update core context after being created from empty config", func(t *testing.T) { testCfg := config.NewConfig() - zetaContext := NewZetaCoreContext(testCfg) + zetaContext := corecontext.NewZetaCoreContext(testCfg) require.NotNil(t, zetaContext) keyGenToUpdate := observertypes.Keygen{ @@ -178,7 +179,7 @@ func TestUpdateZetaCoreContext(t *testing.T) { RPCParams: "test params", } - zetaContext := NewZetaCoreContext(testCfg) + zetaContext := corecontext.NewZetaCoreContext(testCfg) require.NotNil(t, zetaContext) keyGenToUpdate := observertypes.Keygen{ From 2fcdfb2124f7469182e97a93ab1e5baf61903729 Mon Sep 17 00:00:00 2001 From: skosito Date: Wed, 6 Mar 2024 10:20:24 +0100 Subject: [PATCH 15/16] PR comments --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 4d57b83680..d92833326f 100644 --- a/changelog.md +++ b/changelog.md @@ -11,7 +11,7 @@ * [1511](https://github.com/zeta-chain/node/pull/1511) - move ballot voting logic from `crosschain` to `observer` * [1783](https://github.com/zeta-chain/node/pull/1783) - refactor zetaclient metrics naming and structure * [1774](https://github.com/zeta-chain/node/pull/1774) - split params and config in zetaclient -* [1831](https://github.com/zeta-chain/node/pull/1831) - split params and config in zetaclient follow up +* [1831](https://github.com/zeta-chain/node/pull/1831) - removing unnecessary pointers in context structure ### Features From 76ac1ef81701d4d79791b91b7713447c6f5cd7e2 Mon Sep 17 00:00:00 2001 From: skosito Date: Thu, 7 Mar 2024 21:06:19 +0100 Subject: [PATCH 16/16] Fixes after merge --- zetaclient/evm/evm_signer_test.go | 2 +- zetaclient/evm/inbounds_test.go | 8 ++++---- zetaclient/testutils/testdata.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/zetaclient/evm/evm_signer_test.go b/zetaclient/evm/evm_signer_test.go index e685128902..711db544d1 100644 --- a/zetaclient/evm/evm_signer_test.go +++ b/zetaclient/evm/evm_signer_test.go @@ -52,7 +52,7 @@ func getNewEvmChainClient() (*ChainClient, error) { tss := stub.NewTSSMainnet() evmcfg := config.EVMConfig{Chain: corecommon.BscMainnetChain(), Endpoint: "http://localhost:8545"} - cfg.EVMChainConfigs[corecommon.BscMainnetChain().ChainId] = &evmcfg + cfg.EVMChainConfigs[corecommon.BscMainnetChain().ChainId] = evmcfg coreCTX := corecontext.NewZetaCoreContext(cfg) appCTX := appcontext.NewAppContext(coreCTX, cfg) diff --git a/zetaclient/evm/inbounds_test.go b/zetaclient/evm/inbounds_test.go index f34575ee1d..472cdc65ae 100644 --- a/zetaclient/evm/inbounds_test.go +++ b/zetaclient/evm/inbounds_test.go @@ -256,8 +256,8 @@ func TestEVM_BuildInboundVoteMsgForDepositedEvent(t *testing.T) { sender := ethcommon.HexToAddress(tx.From) // create test compliance config - cfg := &config.Config{ - ComplianceConfig: &config.ComplianceConfig{}, + cfg := config.Config{ + ComplianceConfig: config.ComplianceConfig{}, } t.Run("should return vote msg for archived Deposited event", func(t *testing.T) { @@ -302,8 +302,8 @@ func TestEVM_BuildInboundVoteMsgForTokenSentToTSS(t *testing.T) { // create test compliance config ob := MockEVMClient(common.EthChain(), nil, 1, stub.MockChainParams(1, 1)) - cfg := &config.Config{ - ComplianceConfig: &config.ComplianceConfig{}, + cfg := config.Config{ + ComplianceConfig: config.ComplianceConfig{}, } t.Run("should return vote msg for archived gas token transfer to TSS", func(t *testing.T) { diff --git a/zetaclient/testutils/testdata.go b/zetaclient/testutils/testdata.go index 8918476217..0676824514 100644 --- a/zetaclient/testutils/testdata.go +++ b/zetaclient/testutils/testdata.go @@ -50,8 +50,8 @@ func LoadObjectFromJSONFile(obj interface{}, filename string) error { return decoder.Decode(&obj) } -func ComplianceConfigTest() *config.ComplianceConfig { - return &config.ComplianceConfig{ +func ComplianceConfigTest() config.ComplianceConfig { + return config.ComplianceConfig{ RestrictedAddresses: []string{RestrictedEVMAddressTest, RestrictedBtcAddressTest}, } }