eth: track provenance of mempool transactions

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-04-20 20:35:55 +02:00
parent 2a1784baef
commit b2e6162da7
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E

View file

@ -199,6 +199,9 @@ type TxFetcher struct {
requests map[string]*txRequest // In-flight transaction retrievals
alternates map[common.Hash]map[string]struct{} // In-flight transaction alternate origins if retrieval fails
// txFromPeer stores where we received a transaction from
txFromPeer map[common.Hash]string
// Callbacks
hasTx func(common.Hash) bool // Retrieves a tx from the local txpool
addTxs func([]*types.Transaction) []error // Insert a batch of transactions into local txpool
@ -235,6 +238,7 @@ func NewTxFetcherForTests(
fetching: make(map[common.Hash]string),
requests: make(map[string]*txRequest),
alternates: make(map[common.Hash]map[string]struct{}),
txFromPeer: make(map[common.Hash]string),
underpriced: lru.NewCache[common.Hash, time.Time](maxTxUnderpricedSetSize),
hasTx: hasTx,
addTxs: addTxs,
@ -353,7 +357,8 @@ func (f *TxFetcher) Enqueue(peer string, txs []*types.Transaction, direct bool)
}
// Track a few interesting failure types
switch {
case err == nil: // Noop, but need to handle to not count these
case err == nil:
f.txFromPeer[batch[j].Hash()] = peer
case errors.Is(err, txpool.ErrAlreadyKnown):
duplicate++
@ -1034,3 +1039,8 @@ func rotateStrings(slice []string, n int) {
slice[i] = orig[(i+n)%len(orig)]
}
}
func (f *TxFetcher) TxFromPeer(hash common.Hash) (string, bool) {
provenance, ok := f.txFromPeer[hash]
return provenance, ok
}