eth/fetcher: protext txFromPeer against concurrent access

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-04-30 12:01:29 +02:00
parent b2e6162da7
commit 041ba30ae3
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E

View file

@ -22,6 +22,7 @@ import (
"math" "math"
mrand "math/rand" mrand "math/rand"
"sort" "sort"
"sync"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -201,6 +202,8 @@ type TxFetcher struct {
// txFromPeer stores where we received a transaction from // txFromPeer stores where we received a transaction from
txFromPeer map[common.Hash]string txFromPeer map[common.Hash]string
// should be protected by mutex
txFromPeerMutex sync.Mutex
// Callbacks // Callbacks
hasTx func(common.Hash) bool // Retrieves a tx from the local txpool hasTx func(common.Hash) bool // Retrieves a tx from the local txpool
@ -358,7 +361,10 @@ func (f *TxFetcher) Enqueue(peer string, txs []*types.Transaction, direct bool)
// Track a few interesting failure types // Track a few interesting failure types
switch { switch {
case err == nil: case err == nil:
// protect against concurrent access to the map
f.txFromPeerMutex.Lock()
f.txFromPeer[batch[j].Hash()] = peer f.txFromPeer[batch[j].Hash()] = peer
f.txFromPeerMutex.Unlock()
case errors.Is(err, txpool.ErrAlreadyKnown): case errors.Is(err, txpool.ErrAlreadyKnown):
duplicate++ duplicate++