@@ -24,8 +24,8 @@ import (
24
24
"sort"
25
25
"time"
26
26
27
- mapset "github.com/deckarep/golang-set/v2"
28
27
"github.com/ethereum/go-ethereum/common"
28
+ "github.com/ethereum/go-ethereum/common/lru"
29
29
"github.com/ethereum/go-ethereum/common/mclock"
30
30
"github.com/ethereum/go-ethereum/core/txpool"
31
31
"github.com/ethereum/go-ethereum/core/types"
@@ -53,6 +53,9 @@ const (
53
53
// re-request them.
54
54
maxTxUnderpricedSetSize = 32768
55
55
56
+ // maxTxUnderpricedTimeout is the max time a transaction should be stuck in the underpriced set.
57
+ maxTxUnderpricedTimeout = int64 (5 * time .Minute )
58
+
56
59
// txArriveTimeout is the time allowance before an announced transaction is
57
60
// explicitly requested.
58
61
txArriveTimeout = 500 * time .Millisecond
@@ -148,7 +151,7 @@ type TxFetcher struct {
148
151
drop chan * txDrop
149
152
quit chan struct {}
150
153
151
- underpriced mapset. Set [common.Hash ] // Transactions discarded as too cheap (don't re-fetch)
154
+ underpriced * lru. Cache [common.Hash , int64 ] // Transactions discarded as too cheap (don't re-fetch)
152
155
153
156
// Stage 1: Waiting lists for newly discovered transactions that might be
154
157
// broadcast without needing explicit request/reply round trips.
@@ -202,7 +205,7 @@ func NewTxFetcherForTests(
202
205
fetching : make (map [common.Hash ]string ),
203
206
requests : make (map [string ]* txRequest ),
204
207
alternates : make (map [common.Hash ]map [string ]struct {}),
205
- underpriced : mapset . NewSet [common.Hash ]( ),
208
+ underpriced : lru. NewCache [common.Hash , int64 ]( maxTxUnderpricedSetSize ),
206
209
hasTx : hasTx ,
207
210
addTxs : addTxs ,
208
211
fetchTxs : fetchTxs ,
@@ -223,17 +226,16 @@ func (f *TxFetcher) Notify(peer string, hashes []common.Hash) error {
223
226
// still valuable to check here because it runs concurrent to the internal
224
227
// loop, so anything caught here is time saved internally.
225
228
var (
226
- unknowns = make ([]common.Hash , 0 , len (hashes ))
227
- duplicate , underpriced int64
229
+ unknowns = make ([]common.Hash , 0 , len (hashes ))
230
+ duplicate int64
231
+ underpriced int64
228
232
)
229
233
for _ , hash := range hashes {
230
234
switch {
231
235
case f .hasTx (hash ):
232
236
duplicate ++
233
-
234
- case f .underpriced .Contains (hash ):
237
+ case f .isKnownUnderpriced (hash ):
235
238
underpriced ++
236
-
237
239
default :
238
240
unknowns = append (unknowns , hash )
239
241
}
@@ -245,10 +247,7 @@ func (f *TxFetcher) Notify(peer string, hashes []common.Hash) error {
245
247
if len (unknowns ) == 0 {
246
248
return nil
247
249
}
248
- announce := & txAnnounce {
249
- origin : peer ,
250
- hashes : unknowns ,
251
- }
250
+ announce := & txAnnounce {origin : peer , hashes : unknowns }
252
251
select {
253
252
case f .notify <- announce :
254
253
return nil
@@ -257,6 +256,16 @@ func (f *TxFetcher) Notify(peer string, hashes []common.Hash) error {
257
256
}
258
257
}
259
258
259
+ // isKnownUnderpriced reports whether a transaction hash was recently found to be underpriced.
260
+ func (f * TxFetcher ) isKnownUnderpriced (hash common.Hash ) bool {
261
+ prevTime , ok := f .underpriced .Peek (hash )
262
+ if ok && prevTime + maxTxUnderpricedTimeout < time .Now ().Unix () {
263
+ f .underpriced .Remove (hash )
264
+ return false
265
+ }
266
+ return ok
267
+ }
268
+
260
269
// Enqueue imports a batch of received transaction into the transaction pool
261
270
// and the fetcher. This method may be called by both transaction broadcasts and
262
271
// direct request replies. The differentiation is important so the fetcher can
@@ -300,10 +309,7 @@ func (f *TxFetcher) Enqueue(peer string, txs []*types.Transaction, direct bool)
300
309
// Avoid re-request this transaction when we receive another
301
310
// announcement.
302
311
if errors .Is (err , txpool .ErrUnderpriced ) || errors .Is (err , txpool .ErrReplaceUnderpriced ) {
303
- for f .underpriced .Cardinality () >= maxTxUnderpricedSetSize {
304
- f .underpriced .Pop ()
305
- }
306
- f .underpriced .Add (batch [j ].Hash ())
312
+ f .underpriced .Add (batch [j ].Hash (), batch [j ].Time ().Unix ())
307
313
}
308
314
// Track a few interesting failure types
309
315
switch {
0 commit comments