From dcb54c21289a042346f08ab54bd907524a7fdb80 Mon Sep 17 00:00:00 2001 From: chengzhinei Date: Mon, 9 Aug 2021 18:00:04 +0800 Subject: [PATCH] Merge PR: fix txpool lock (#951) * add defer unlock * fix defer unlock * fix defer unlock Co-authored-by: MengXiangJian <805442788@qq.com> --- app/rpc/namespaces/eth/tx_pool.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/rpc/namespaces/eth/tx_pool.go b/app/rpc/namespaces/eth/tx_pool.go index 217fc0ccdd..7aa81629d9 100644 --- a/app/rpc/namespaces/eth/tx_pool.go +++ b/app/rpc/namespaces/eth/tx_pool.go @@ -125,12 +125,11 @@ func broadcastTxByTxPool(api *PublicEthereumAPI, tx *evmtypes.MsgEthereumTx, txB } api.txPool.mu.Lock() + defer api.txPool.mu.Unlock() if err = api.txPool.CacheAndBroadcastTx(api, from, tx); err != nil { api.logger.Error("eth_sendRawTransaction txPool err:", err.Error()) - api.txPool.mu.Unlock() return common.Hash{}, err } - api.txPool.mu.Unlock() return common.HexToHash(strings.ToUpper(hex.EncodeToString(tmhash.Sum(txBytes)))), nil } @@ -295,22 +294,26 @@ func (pool *TxPool) delTxInDB(address common.Address, txNonce uint64) error { func (pool *TxPool) broadcastPeriod(api *PublicEthereumAPI) { for { time.Sleep(time.Second * time.Duration(viper.GetInt(BroadcastPeriodSecond))) - pool.mu.Lock() - for address, _ := range pool.addressTxsPool { - pCurrentNonce, err := api.GetTransactionCount(address, rpctypes.PendingBlockNumber) - if err != nil { - continue - } - currentNonce := uint64(*pCurrentNonce) - - pool.continueBroadcast(api, currentNonce, address) + pool.broadcastPeriodCore(api) + } +} +func (pool *TxPool) broadcastPeriodCore(api *PublicEthereumAPI) { + pool.mu.Lock() + defer pool.mu.Unlock() + for address, _ := range pool.addressTxsPool { + pCurrentNonce, err := api.GetTransactionCount(address, rpctypes.PendingBlockNumber) + if err != nil { + continue } - pool.mu.Unlock() + currentNonce := uint64(*pCurrentNonce) + + pool.continueBroadcast(api, currentNonce, address) } } func (pool *TxPool) broadcastOnce(api *PublicEthereumAPI) { pool.mu.Lock() + defer pool.mu.Unlock() for address, _ := range pool.addressTxsPool { pCurrentNonce, err := api.GetTransactionCount(address, rpctypes.PendingBlockNumber) if err != nil { @@ -320,5 +323,4 @@ func (pool *TxPool) broadcastOnce(api *PublicEthereumAPI) { err = pool.continueBroadcast(api, currentNonce, address) } - pool.mu.Unlock() }