From ca9142df3482543925d3ae397b7754eb7f3d7894 Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Mon, 1 Jul 2024 17:00:46 -0500 Subject: [PATCH] fix unit test conflicts and thread safety --- zetaclient/orchestrator/chain_activate.go | 8 +++ zetaclient/orchestrator/orchestrator.go | 4 ++ zetaclient/orchestrator/orchestrator_test.go | 53 +------------------- 3 files changed, 13 insertions(+), 52 deletions(-) diff --git a/zetaclient/orchestrator/chain_activate.go b/zetaclient/orchestrator/chain_activate.go index 617ec77c99..684179b711 100644 --- a/zetaclient/orchestrator/chain_activate.go +++ b/zetaclient/orchestrator/chain_activate.go @@ -50,6 +50,10 @@ func (oc *Orchestrator) ActivateDeactivateChains() { oc.logger.Std.Info().Msgf("ActivateDeactivateChains: deactivating chain %d", chainID) observer.Stop() + + // remove signer and observer from maps + oc.mu.Lock() + defer oc.mu.Unlock() delete(oc.signerMap, chainID) delete(oc.observerMap, chainID) } @@ -71,6 +75,10 @@ func (oc *Orchestrator) ActivateDeactivateChains() { } observer.Start() + + // add signer and observer to maps + oc.mu.Lock() + defer oc.mu.Unlock() oc.signerMap[chainID] = newSignerMap[chainID] oc.observerMap[chainID] = observer } diff --git a/zetaclient/orchestrator/orchestrator.go b/zetaclient/orchestrator/orchestrator.go index afade0a160..4995c36935 100644 --- a/zetaclient/orchestrator/orchestrator.go +++ b/zetaclient/orchestrator/orchestrator.go @@ -187,7 +187,9 @@ func (oc *Orchestrator) Stop() { // GetUpdatedSigner returns signer with updated chain parameters func (oc *Orchestrator) GetUpdatedSigner(chainID int64) (interfaces.ChainSigner, error) { + oc.mu.Lock() signer, found := oc.signerMap[chainID] + oc.mu.Unlock() if !found { return nil, fmt.Errorf("signer not found for chainID %d", chainID) } @@ -215,7 +217,9 @@ func (oc *Orchestrator) GetUpdatedSigner(chainID int64) (interfaces.ChainSigner, // GetUpdatedChainObserver returns chain observer with updated chain parameters func (oc *Orchestrator) GetUpdatedChainObserver(chainID int64) (interfaces.ChainObserver, error) { + oc.mu.Lock() observer, found := oc.observerMap[chainID] + oc.mu.Unlock() if !found { return nil, fmt.Errorf("chain observer not found for chainID %d", chainID) } diff --git a/zetaclient/orchestrator/orchestrator_test.go b/zetaclient/orchestrator/orchestrator_test.go index e9ffffa584..70ffae0806 100644 --- a/zetaclient/orchestrator/orchestrator_test.go +++ b/zetaclient/orchestrator/orchestrator_test.go @@ -56,11 +56,8 @@ func MockOrchestrator( return orchestrator } -<<<<<<< HEAD +// CreateTestAppContext creates a test app context for orchestrator testing func CreateTestAppContext( -======= -func CreateAppContext( ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f evmChain, btcChain chains.Chain, evmChainParams, btcChainParams *observertypes.ChainParams, ) *context.AppContext { @@ -72,42 +69,24 @@ func CreateAppContext( cfg.BitcoinConfig = config.BTCConfig{ RPCHost: "localhost", } -<<<<<<< HEAD // new app context appContext := context.NewAppContext(cfg) chainParamsMap := make(map[int64]*observertypes.ChainParams) chainParamsMap[evmChain.ChainId] = evmChainParams chainParamsMap[btcChain.ChainId] = btcChainParams -======= - // new zetacore context - appContext := context.New(cfg, zerolog.Nop()) - evmChainParamsMap := make(map[int64]*observertypes.ChainParams) - evmChainParamsMap[evmChain.ChainId] = evmChainParams ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f ccFlags := sample.CrosschainFlags() verificationFlags := sample.HeaderSupportedChains() // feed chain params appContext.Update( -<<<<<<< HEAD observertypes.Keygen{}, -======= - &observertypes.Keygen{}, - []chains.Chain{evmChain, btcChain}, - evmChainParamsMap, - btcChainParams, ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f "", []chains.Chain{evmChain, btcChain}, chainParamsMap, &chaincfg.RegressionNetParams, *ccFlags, verificationFlags, -<<<<<<< HEAD zerolog.Logger{}, -======= - true, ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f ) return appContext } @@ -131,26 +110,16 @@ func Test_GetUpdatedSigner(t *testing.T) { } t.Run("signer should not be found", func(t *testing.T) { -<<<<<<< HEAD appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams) -======= - orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams) - context := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f // BSC signer should not be found _, err := orchestrator.GetUpdatedSigner(chains.BscMainnet.ChainId) require.ErrorContains(t, err, "signer not found") }) t.Run("should be able to update connector and erc20 custody address", func(t *testing.T) { -<<<<<<< HEAD appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams) -======= - orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams) - context := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f // update signer with new connector and erc20 custody address signer, err := orchestrator.GetUpdatedSigner(evmChain.ChainId) require.NoError(t, err) @@ -207,27 +176,17 @@ func Test_GetUpdatedChainObserver(t *testing.T) { } t.Run("evm chain observer should not be found", func(t *testing.T) { -<<<<<<< HEAD appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams) -======= - orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams) - coreContext := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f // BSC chain observer should not be found _, err := orchestrator.GetUpdatedChainObserver(chains.BscMainnet.ChainId) require.ErrorContains(t, err, "chain observer not found") }) t.Run("chain params in evm chain observer should be updated successfully", func(t *testing.T) { -<<<<<<< HEAD appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams) -======= - orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams) - coreContext := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams) ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f // update evm chain observer with new chain params chainOb, err := orchestrator.GetUpdatedChainObserver(evmChain.ChainId) require.NoError(t, err) @@ -235,27 +194,17 @@ func Test_GetUpdatedChainObserver(t *testing.T) { require.True(t, observertypes.ChainParamsEqual(*evmChainParamsNew, chainOb.GetChainParams())) }) t.Run("btc chain observer should not be found", func(t *testing.T) { -<<<<<<< HEAD appCtx := CreateTestAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew) orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams) -======= - orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams) - coreContext := CreateAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew) ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f // BTC testnet chain observer should not be found _, err := orchestrator.GetUpdatedChainObserver(chains.BitcoinTestnet.ChainId) require.ErrorContains(t, err, "chain observer not found") }) t.Run("chain params in btc chain observer should be updated successfully", func(t *testing.T) { -<<<<<<< HEAD appCtx := CreateTestAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew) orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams) -======= - orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams) - coreContext := CreateAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew) ->>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f // update btc chain observer with new chain params chainOb, err := orchestrator.GetUpdatedChainObserver(btcChain.ChainId) require.NoError(t, err)