From 0442dd695411080714e699cc6a6187eca971bced Mon Sep 17 00:00:00 2001 From: Evan Han Date: Thu, 3 Jun 2021 14:00:41 +0800 Subject: [PATCH] =?UTF-8?q?Merge=20PR=EF=BC=9Aconcurrent=20safe=20cdc&conf?= =?UTF-8?q?ig=20singleton=20(#897)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/rpc/namespaces/eth/simulation/impl.go | 33 ++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/app/rpc/namespaces/eth/simulation/impl.go b/app/rpc/namespaces/eth/simulation/impl.go index d6fd474bcc..9b219d5f68 100644 --- a/app/rpc/namespaces/eth/simulation/impl.go +++ b/app/rpc/namespaces/eth/simulation/impl.go @@ -2,6 +2,8 @@ package simulation import ( "encoding/binary" + "github.com/okex/exchain/x/evm" + "sync" "github.com/cosmos/cosmos-sdk/codec" store "github.com/cosmos/cosmos-sdk/store/types" @@ -19,7 +21,6 @@ import ( "github.com/okex/exchain/x/backend" "github.com/okex/exchain/x/dex" distr "github.com/okex/exchain/x/distribution" - "github.com/okex/exchain/x/evm" evmtypes "github.com/okex/exchain/x/evm/types" "github.com/okex/exchain/x/evm/watcher" "github.com/okex/exchain/x/farm" @@ -169,26 +170,28 @@ type InternalDba struct { ocProxy QueryOnChainProxy } -var gSimulateCdc *codec.Codec = nil -var gSimulateChainConfig []byte = nil +var ( + gSimulateCdc *codec.Codec + cdcOnce sync.Once + gSimulateChainConfig []byte + configOnce sync.Once +) func instanceOfCdc() *codec.Codec { - if gSimulateCdc != nil { - return gSimulateCdc - } - module := evm.AppModuleBasic{} - cdc := codec.New() - module.RegisterCodec(cdc) - gSimulateCdc = cdc + cdcOnce.Do(func() { + module := evm.AppModuleBasic{} + cdc := codec.New() + module.RegisterCodec(cdc) + gSimulateCdc = cdc + }) return gSimulateCdc } func instanceOfChainConfig() []byte { - if gSimulateChainConfig != nil { - return gSimulateChainConfig - } - cdc := instanceOfCdc() - gSimulateChainConfig = cdc.MustMarshalBinaryBare(evmtypes.DefaultChainConfig()) + configOnce.Do(func() { + cdc := instanceOfCdc() + gSimulateChainConfig = cdc.MustMarshalBinaryBare(evmtypes.DefaultChainConfig()) + }) return gSimulateChainConfig }