Skip to content

[WIP] eth: register tx source peer #31974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion eth/fetcher/tx_fetcher.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import (
"math"
mrand "math/rand"
"sort"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
@@ -199,6 +200,11 @@ 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
// should be protected by mutex
txFromPeerMutex sync.RWMutex

// 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 +241,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 +360,11 @@ 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:
// protect against concurrent access to the map
f.txFromPeerMutex.Lock()
f.txFromPeer[batch[j].Hash()] = peer
f.txFromPeerMutex.Unlock()

case errors.Is(err, txpool.ErrAlreadyKnown):
duplicate++
@@ -1034,3 +1045,10 @@ func rotateStrings(slice []string, n int) {
slice[i] = orig[(i+n)%len(orig)]
}
}

func (f *TxFetcher) TxFromPeer(hash common.Hash) (string, bool) {
f.txFromPeerMutex.RLock()
defer f.txFromPeerMutex.RUnlock()
provenance, ok := f.txFromPeer[hash]
return provenance, ok
}