Skip to content

Commit 8d41e88

Browse files
holimankaralabe
authored andcommitted
core: smaller txpool status locking (#20080)
* txpool: smaller lock portion * core/tx_pool: fix data race
1 parent 8bd64f4 commit 8d41e88

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

core/tx_pool.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -803,19 +803,22 @@ func (pool *TxPool) addTxsLocked(txs []*types.Transaction, local bool) ([]error,
803803
// Status returns the status (unknown/pending/queued) of a batch of transactions
804804
// identified by their hashes.
805805
func (pool *TxPool) Status(hashes []common.Hash) []TxStatus {
806-
pool.mu.RLock()
807-
defer pool.mu.RUnlock()
808-
809806
status := make([]TxStatus, len(hashes))
810807
for i, hash := range hashes {
811-
if tx := pool.all.Get(hash); tx != nil {
812-
from, _ := types.Sender(pool.signer, tx) // already validated
813-
if pool.pending[from] != nil && pool.pending[from].txs.items[tx.Nonce()] != nil {
814-
status[i] = TxStatusPending
815-
} else {
816-
status[i] = TxStatusQueued
817-
}
808+
tx := pool.Get(hash)
809+
if tx == nil {
810+
continue
811+
}
812+
from, _ := types.Sender(pool.signer, tx) // already validated
813+
pool.mu.RLock()
814+
if txList := pool.pending[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
815+
status[i] = TxStatusPending
816+
} else if txList := pool.queue[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
817+
status[i] = TxStatusQueued
818818
}
819+
// implicit else: the tx may have been included into a block between
820+
// checking pool.Get and obtaining the lock. In that case, TxStatusUnknown is correct
821+
pool.mu.RUnlock()
819822
}
820823
return status
821824
}

0 commit comments

Comments
 (0)