Skip to content

Commit

Permalink
core: smaller txpool status locking (ethereum#20080)
Browse files Browse the repository at this point in the history
* txpool: smaller lock portion

* core/tx_pool: fix data race
  • Loading branch information
holiman authored and gzliudan committed May 13, 2024
1 parent 19fb610 commit 656f2a6
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,19 +940,22 @@ func (pool *TxPool) addTxsLocked(txs []*types.Transaction, local bool) ([]error,
// Status returns the status (unknown/pending/queued) of a batch of transactions
// identified by their hashes.
func (pool *TxPool) Status(hashes []common.Hash) []TxStatus {
pool.mu.RLock()
defer pool.mu.RUnlock()

status := make([]TxStatus, len(hashes))
for i, hash := range hashes {
if tx := pool.all.Get(hash); tx != nil {
from, _ := types.Sender(pool.signer, tx) // already validated
if pool.pending[from] != nil && pool.pending[from].txs.items[tx.Nonce()] != nil {
status[i] = TxStatusPending
} else {
status[i] = TxStatusQueued
}
tx := pool.Get(hash)
if tx == nil {
continue
}
from, _ := types.Sender(pool.signer, tx) // already validated
pool.mu.RLock()
if txList := pool.pending[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
status[i] = TxStatusPending
} else if txList := pool.queue[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
status[i] = TxStatusQueued
}
// implicit else: the tx may have been included into a block between
// checking pool.Get and obtaining the lock. In that case, TxStatusUnknown is correct
pool.mu.RUnlock()
}
return status
}
Expand Down

0 comments on commit 656f2a6

Please sign in to comment.