From b2e6162da7491170b9b027c262f7fa42739ed6b9 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Sun, 20 Apr 2025 20:35:55 +0200 Subject: [PATCH] eth: track provenance of mempool transactions Signed-off-by: Csaba Kiraly --- eth/fetcher/tx_fetcher.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/eth/fetcher/tx_fetcher.go b/eth/fetcher/tx_fetcher.go index 98a1c6e9a6..8a192510eb 100644 --- a/eth/fetcher/tx_fetcher.go +++ b/eth/fetcher/tx_fetcher.go @@ -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 +}