Commit 2db5d275 authored by hezhengjun's avatar hezhengjun

fix data race for ut

parent efe9dcbe
...@@ -98,9 +98,11 @@ func (ethRelayer *Relayer4Ethereum) RestorePrivateKeys(passPhase string) (err er ...@@ -98,9 +98,11 @@ func (ethRelayer *Relayer4Ethereum) RestorePrivateKeys(passPhase string) (err er
} }
} }
ethRelayer.rwLock.RLock()
if nil != ethRelayer.privateKey4Chain33 { if nil != ethRelayer.privateKey4Chain33 {
ethRelayer.unlockchan <- start ethRelayer.unlockchan <- start
} }
ethRelayer.rwLock.RUnlock()
return nil return nil
} }
...@@ -133,7 +135,9 @@ func (ethRelayer *Relayer4Ethereum) ImportChain33PrivateKey(passphrase, privateK ...@@ -133,7 +135,9 @@ func (ethRelayer *Relayer4Ethereum) ImportChain33PrivateKey(passphrase, privateK
return err return err
} }
ethRelayer.rwLock.Lock()
ethRelayer.privateKey4Chain33 = priKey ethRelayer.privateKey4Chain33 = priKey
ethRelayer.rwLock.Unlock()
ethRelayer.unlockchan <- start ethRelayer.unlockchan <- start
addr, err := pubKeyToAddress4Bty(priKey.PubKey().Bytes()) addr, err := pubKeyToAddress4Bty(priKey.PubKey().Bytes())
if nil != err { if nil != err {
......
...@@ -130,11 +130,12 @@ func (ethRelayer *Relayer4Ethereum) recoverDeployPara() (err error) { ...@@ -130,11 +130,12 @@ func (ethRelayer *Relayer4Ethereum) recoverDeployPara() (err error) {
return err return err
} }
deployerAddr := crypto.PubkeyToAddress(deployPrivateKey.PublicKey) deployerAddr := crypto.PubkeyToAddress(deployPrivateKey.PublicKey)
ethRelayer.rwLock.Lock()
ethRelayer.operatorInfo = &ethtxs.OperatorInfo{ ethRelayer.operatorInfo = &ethtxs.OperatorInfo{
PrivateKey: deployPrivateKey, PrivateKey: deployPrivateKey,
Address: deployerAddr, Address: deployerAddr,
} }
ethRelayer.rwLock.Unlock()
return nil return nil
} }
...@@ -189,6 +190,7 @@ func (ethRelayer *Relayer4Ethereum) DeployContrcts() (bridgeRegistry string, err ...@@ -189,6 +190,7 @@ func (ethRelayer *Relayer4Ethereum) DeployContrcts() (bridgeRegistry string, err
if err != nil { if err != nil {
return bridgeRegistry, err return bridgeRegistry, err
} }
ethRelayer.rwLock.Lock()
ethRelayer.operatorInfo = &ethtxs.OperatorInfo{ ethRelayer.operatorInfo = &ethtxs.OperatorInfo{
PrivateKey: deployPrivateKey, PrivateKey: deployPrivateKey,
Address: deployerAddr, Address: deployerAddr,
...@@ -200,6 +202,7 @@ func (ethRelayer *Relayer4Ethereum) DeployContrcts() (bridgeRegistry string, err ...@@ -200,6 +202,7 @@ func (ethRelayer *Relayer4Ethereum) DeployContrcts() (bridgeRegistry string, err
_ = ethRelayer.setBridgeRegistryAddr(bridgeRegistry) _ = ethRelayer.setBridgeRegistryAddr(bridgeRegistry)
//设置注册合约地址,同时设置启动中继服务的信号 //设置注册合约地址,同时设置启动中继服务的信号
ethRelayer.bridgeRegistryAddr = x2EthDeployInfo.BridgeRegistry.Address ethRelayer.bridgeRegistryAddr = x2EthDeployInfo.BridgeRegistry.Address
ethRelayer.rwLock.Unlock()
ethRelayer.unlockchan <- start ethRelayer.unlockchan <- start
relayerLog.Info("deploy", "the BridgeRegistry address is", bridgeRegistry) relayerLog.Info("deploy", "the BridgeRegistry address is", bridgeRegistry)
...@@ -244,16 +247,22 @@ func (ethRelayer *Relayer4Ethereum) IsProphecyPending(claimID [32]byte) (bool, e ...@@ -244,16 +247,22 @@ func (ethRelayer *Relayer4Ethereum) IsProphecyPending(claimID [32]byte) (bool, e
} }
func (ethRelayer *Relayer4Ethereum) CreateBridgeToken(symbol string) (string, error) { func (ethRelayer *Relayer4Ethereum) CreateBridgeToken(symbol string) (string, error) {
ethRelayer.rwLock.RLock()
defer ethRelayer.rwLock.RUnlock()
return ethtxs.CreateBridgeToken(symbol, ethRelayer.clientSpec, ethRelayer.operatorInfo, ethRelayer.x2EthDeployInfo, ethRelayer.x2EthContracts) return ethtxs.CreateBridgeToken(symbol, ethRelayer.clientSpec, ethRelayer.operatorInfo, ethRelayer.x2EthDeployInfo, ethRelayer.x2EthContracts)
} }
func (ethRelayer *Relayer4Ethereum) CreateERC20Token(symbol string) (string, error) { func (ethRelayer *Relayer4Ethereum) CreateERC20Token(symbol string) (string, error) {
ethRelayer.rwLock.RLock()
defer ethRelayer.rwLock.RUnlock()
return ethtxs.CreateERC20Token(symbol, ethRelayer.clientSpec, ethRelayer.operatorInfo) return ethtxs.CreateERC20Token(symbol, ethRelayer.clientSpec, ethRelayer.operatorInfo)
} }
func (ethRelayer *Relayer4Ethereum) MintERC20Token(tokenAddr, ownerAddr, amount string) (string, error) { func (ethRelayer *Relayer4Ethereum) MintERC20Token(tokenAddr, ownerAddr, amount string) (string, error) {
bn := big.NewInt(1) bn := big.NewInt(1)
bn, _ = bn.SetString(x2ethTypes.TrimZeroAndDot(amount), 10) bn, _ = bn.SetString(x2ethTypes.TrimZeroAndDot(amount), 10)
ethRelayer.rwLock.RLock()
defer ethRelayer.rwLock.RUnlock()
return ethtxs.MintERC20Token(tokenAddr, ownerAddr, bn, ethRelayer.clientSpec, ethRelayer.operatorInfo) return ethtxs.MintERC20Token(tokenAddr, ownerAddr, bn, ethRelayer.clientSpec, ethRelayer.operatorInfo)
} }
...@@ -315,10 +324,12 @@ func (ethRelayer *Relayer4Ethereum) proc() { ...@@ -315,10 +324,12 @@ func (ethRelayer *Relayer4Ethereum) proc() {
if nilAddr != ethRelayer.bridgeRegistryAddr { if nilAddr != ethRelayer.bridgeRegistryAddr {
relayerLog.Info("proc", "Going to recover corresponding solidity contract handler with bridgeRegistryAddr", ethRelayer.bridgeRegistryAddr.String()) relayerLog.Info("proc", "Going to recover corresponding solidity contract handler with bridgeRegistryAddr", ethRelayer.bridgeRegistryAddr.String())
var err error var err error
ethRelayer.rwLock.Lock()
ethRelayer.x2EthContracts, ethRelayer.x2EthDeployInfo, err = ethtxs.RecoverContractHandler(ethRelayer.clientSpec, ethRelayer.bridgeRegistryAddr, ethRelayer.bridgeRegistryAddr) ethRelayer.x2EthContracts, ethRelayer.x2EthDeployInfo, err = ethtxs.RecoverContractHandler(ethRelayer.clientSpec, ethRelayer.bridgeRegistryAddr, ethRelayer.bridgeRegistryAddr)
if nil != err { if nil != err {
panic("Failed to recover corresponding solidity contract handler due to:" + err.Error()) panic("Failed to recover corresponding solidity contract handler due to:" + err.Error())
} }
ethRelayer.rwLock.Unlock()
relayerLog.Info("^-^ ^-^ Succeed to recover corresponding solidity contract handler") relayerLog.Info("^-^ ^-^ Succeed to recover corresponding solidity contract handler")
if nil != ethRelayer.recoverDeployPara() { if nil != ethRelayer.recoverDeployPara() {
panic("Failed to recoverDeployPara") panic("Failed to recoverDeployPara")
...@@ -331,7 +342,10 @@ func (ethRelayer *Relayer4Ethereum) proc() { ...@@ -331,7 +342,10 @@ func (ethRelayer *Relayer4Ethereum) proc() {
continueFailCount := int32(0) continueFailCount := int32(0)
for range ethRelayer.unlockchan { for range ethRelayer.unlockchan {
relayerLog.Info("Received ethRelayer.unlockchan") relayerLog.Info("Received ethRelayer.unlockchan")
if nil != ethRelayer.privateKey4Chain33 && nilAddr != ethRelayer.bridgeRegistryAddr { ethRelayer.rwLock.RLock()
privateKey4Chain33 := ethRelayer.privateKey4Chain33
ethRelayer.rwLock.RUnlock()
if nil != privateKey4Chain33 && nilAddr != ethRelayer.bridgeRegistryAddr {
relayerLog.Info("Ethereum relayer starts to run...") relayerLog.Info("Ethereum relayer starts to run...")
ethRelayer.prePareSubscribeEvent() ethRelayer.prePareSubscribeEvent()
//向bridgeBank订阅事件 //向bridgeBank订阅事件
...@@ -605,8 +619,10 @@ func (ethRelayer *Relayer4Ethereum) IsValidatorActive(addr string) (bool, error) ...@@ -605,8 +619,10 @@ func (ethRelayer *Relayer4Ethereum) IsValidatorActive(addr string) (bool, error)
if !re.MatchString(addr) { if !re.MatchString(addr) {
return false, errors.New("this address is not an ethereum address") return false, errors.New("this address is not an ethereum address")
} }
ethRelayer.rwLock.RLock()
return ethtxs.IsActiveValidator(common.HexToAddress(addr), ethRelayer.x2EthContracts.Valset) active, err := ethtxs.IsActiveValidator(common.HexToAddress(addr), ethRelayer.x2EthContracts.Valset)
ethRelayer.rwLock.RUnlock()
return active, err
} }
func (ethRelayer *Relayer4Ethereum) ShowOperator() (string, error) { func (ethRelayer *Relayer4Ethereum) ShowOperator() (string, error) {
...@@ -663,7 +679,10 @@ func (ethRelayer *Relayer4Ethereum) handleLogLockEvent(clientChainID *big.Int, c ...@@ -663,7 +679,10 @@ func (ethRelayer *Relayer4Ethereum) handleLogLockEvent(clientChainID *big.Int, c
} }
// Initiate the relay // Initiate the relay
txhash, err := ethtxs.RelayLockToChain33(ethRelayer.privateKey4Chain33, prophecyClaim, rpcURL) ethRelayer.rwLock.RLock()
privateKey4Chain33 := ethRelayer.privateKey4Chain33
ethRelayer.rwLock.RUnlock()
txhash, err := ethtxs.RelayLockToChain33(privateKey4Chain33, prophecyClaim, rpcURL)
if err != nil { if err != nil {
relayerLog.Error("handleLogLockEvent", "Failed to RelayLockToChain33 due to:", err.Error()) relayerLog.Error("handleLogLockEvent", "Failed to RelayLockToChain33 due to:", err.Error())
return err return err
...@@ -718,7 +737,10 @@ func (ethRelayer *Relayer4Ethereum) handleLogBurnEvent(clientChainID *big.Int, c ...@@ -718,7 +737,10 @@ func (ethRelayer *Relayer4Ethereum) handleLogBurnEvent(clientChainID *big.Int, c
} }
// Initiate the relay // Initiate the relay
txhash, err := ethtxs.RelayBurnToChain33(ethRelayer.privateKey4Chain33, prophecyClaim, rpcURL) ethRelayer.rwLock.RLock()
privateKey4Chain33 := ethRelayer.privateKey4Chain33
ethRelayer.rwLock.RUnlock()
txhash, err := ethtxs.RelayBurnToChain33(privateKey4Chain33, prophecyClaim, rpcURL)
if err != nil { if err != nil {
relayerLog.Error("handleLogLockEvent", "Failed to RelayLockToChain33 due to:", err.Error()) relayerLog.Error("handleLogLockEvent", "Failed to RelayLockToChain33 due to:", err.Error())
return err return err
......
...@@ -449,21 +449,29 @@ func Test_RestorePrivateKeys(t *testing.T) { ...@@ -449,21 +449,29 @@ func Test_RestorePrivateKeys(t *testing.T) {
for range ethRelayer.unlockchan { for range ethRelayer.unlockchan {
} }
}() }()
ethRelayer.rwLock.RLock()
temp := ethRelayer.privateKey4Chain33 temp := ethRelayer.privateKey4Chain33
ethRelayer.rwLock.RUnlock()
err = ethRelayer.RestorePrivateKeys("123") err = ethRelayer.RestorePrivateKeys("123")
ethRelayer.rwLock.RLock()
assert.NotEqual(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes())) assert.NotEqual(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes()))
ethRelayer.rwLock.RUnlock()
require.Nil(t, err) require.Nil(t, err)
err = ethRelayer.RestorePrivateKeys(passphrase) err = ethRelayer.RestorePrivateKeys(passphrase)
ethRelayer.rwLock.RLock()
assert.Equal(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes())) assert.Equal(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes()))
ethRelayer.rwLock.RUnlock()
require.Nil(t, err) require.Nil(t, err)
err = ethRelayer.StoreAccountWithNewPassphase("new123", passphrase) err = ethRelayer.StoreAccountWithNewPassphase("new123", passphrase)
require.Nil(t, err) require.Nil(t, err)
err = ethRelayer.RestorePrivateKeys("new123") err = ethRelayer.RestorePrivateKeys("new123")
ethRelayer.rwLock.RLock()
assert.Equal(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes())) assert.Equal(t, hex.EncodeToString(temp.Bytes()), hex.EncodeToString(ethRelayer.privateKey4Chain33.Bytes()))
ethRelayer.rwLock.RUnlock()
require.Nil(t, err) require.Nil(t, err)
time.Sleep(time.Second) time.Sleep(time.Second)
...@@ -508,6 +516,7 @@ func newEthRelayer(para *ethtxs.DeployPara, sim *ethinterface.SimExtend, x2EthCo ...@@ -508,6 +516,7 @@ func newEthRelayer(para *ethtxs.DeployPara, sim *ethinterface.SimExtend, x2EthCo
deployPrivateKey, _ := crypto.ToECDSA(common.FromHex(relayer.deployInfo.DeployerPrivateKey)) deployPrivateKey, _ := crypto.ToECDSA(common.FromHex(relayer.deployInfo.DeployerPrivateKey))
deployerAddr := crypto.PubkeyToAddress(deployPrivateKey.PublicKey) deployerAddr := crypto.PubkeyToAddress(deployPrivateKey.PublicKey)
relayer.rwLock.Lock()
relayer.operatorInfo = &ethtxs.OperatorInfo{ relayer.operatorInfo = &ethtxs.OperatorInfo{
PrivateKey: deployPrivateKey, PrivateKey: deployPrivateKey,
Address: deployerAddr, Address: deployerAddr,
...@@ -515,6 +524,8 @@ func newEthRelayer(para *ethtxs.DeployPara, sim *ethinterface.SimExtend, x2EthCo ...@@ -515,6 +524,8 @@ func newEthRelayer(para *ethtxs.DeployPara, sim *ethinterface.SimExtend, x2EthCo
relayer.deployPara = para relayer.deployPara = para
relayer.x2EthContracts = x2EthContracts relayer.x2EthContracts = x2EthContracts
relayer.x2EthDeployInfo = x2EthDeployInfo relayer.x2EthDeployInfo = x2EthDeployInfo
relayer.rwLock.Unlock()
go relayer.proc() go relayer.proc()
return relayer return relayer
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment