eth/fetcher: use mutex for accessing underpriced

This commit is contained in:
Marius van der Wijden 2023-09-13 13:20:17 +02:00
parent 49b077e9ce
commit 4a148a4a5e

View file

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
mrand "math/rand" mrand "math/rand"
"sort" "sort"
"sync"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -150,6 +151,7 @@ type TxFetcher struct {
drop chan *txDrop drop chan *txDrop
quit chan struct{} quit chan struct{}
underpricedMu sync.Mutex
underpriced map[common.Hash]int64 // Transactions discarded as too cheap (don't re-fetch) underpriced map[common.Hash]int64 // Transactions discarded as too cheap (don't re-fetch)
// Stage 1: Waiting lists for newly discovered transactions that might be // Stage 1: Waiting lists for newly discovered transactions that might be
@ -229,6 +231,9 @@ func (f *TxFetcher) Notify(peer string, hashes []common.Hash) error {
duplicate, underpriced int64 duplicate, underpriced int64
) )
isUnderpriced := func(hash common.Hash) bool { isUnderpriced := func(hash common.Hash) bool {
f.underpricedMu.Lock()
defer f.underpricedMu.Unlock()
prevTime, ok := f.underpriced[hash] prevTime, ok := f.underpriced[hash]
if ok && prevTime+maxTxUnderpricedTimeout < time.Now().Unix() { if ok && prevTime+maxTxUnderpricedTimeout < time.Now().Unix() {
delete(f.underpriced, hash) delete(f.underpriced, hash)
@ -311,6 +316,7 @@ func (f *TxFetcher) Enqueue(peer string, txs []*types.Transaction, direct bool)
// Avoid re-request this transaction when we receive another // Avoid re-request this transaction when we receive another
// announcement. // announcement.
if errors.Is(err, txpool.ErrUnderpriced) || errors.Is(err, txpool.ErrReplaceUnderpriced) { if errors.Is(err, txpool.ErrUnderpriced) || errors.Is(err, txpool.ErrReplaceUnderpriced) {
f.underpricedMu.Lock()
// If the set is to big, delete a pseudorandom element // If the set is to big, delete a pseudorandom element
for hash := range f.underpriced { for hash := range f.underpriced {
if len(f.underpriced) < maxTxUnderpricedSetSize { if len(f.underpriced) < maxTxUnderpricedSetSize {
@ -320,6 +326,7 @@ func (f *TxFetcher) Enqueue(peer string, txs []*types.Transaction, direct bool)
} }
// add the underpriced transaction to the set // add the underpriced transaction to the set
f.underpriced[batch[j].Hash()] = batch[j].Time().Unix() f.underpriced[batch[j].Hash()] = batch[j].Time().Unix()
f.underpricedMu.Unlock()
} }
// Track a few interesting failure types // Track a few interesting failure types
switch { switch {