Skip to content

Commit e48dd40

Browse files
committed
core/txpool: add back sorting by time
1 parent 12a6af4 commit e48dd40

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

core/txpool/blobpool/blobpool.go

+1
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,7 @@ func (p *BlobPool) Pending(filter txpool.PendingFilter) txpool.Pending {
15191519
heads = append(heads, &txpool.TxTips{
15201520
From: addr,
15211521
Tips: lazyTx.Fees,
1522+
Time: lazyTx.Time.UnixNano(),
15221523
})
15231524
}
15241525
tail = append(tail, lazyTx)

core/txpool/legacypool/legacypool.go

+1
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) txpool.Pending {
581581
heads = append(heads, &txpool.TxTips{
582582
From: addr,
583583
Tips: lazyTx.Fees,
584+
Time: lazyTx.Time.UnixNano(),
584585
})
585586
}
586587
tail = append(tail, lazyTx)

core/txpool/pending.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type PendingFilter struct {
6262
type TxTips struct {
6363
From common.Address // sender
6464
Tips uint256.Int // miner-fees earned by this transaction.
65+
Time int64 // Time when the transaction was first seen
6566
}
6667

6768
type TipList []*TxTips
@@ -71,7 +72,12 @@ func (f TipList) Len() int {
7172
}
7273

7374
func (f TipList) Less(i, j int) bool {
74-
return f[i].Tips.Gt(&f[j].Tips)
75+
76+
cmp := f[i].Tips.Cmp(&f[j].Tips)
77+
if cmp == 0 {
78+
return f[i].Time < f[j].Time
79+
}
80+
return cmp > 0
7581
}
7682

7783
func (f TipList) Swap(i, j int) {
@@ -113,6 +119,7 @@ func (ps *pendingSet) Shift() {
113119
acc := ps.Heads[0].From
114120
if txs, ok := ps.Tails[acc]; ok && len(txs) > 1 {
115121
ps.Heads[0].Tips = txs[1].Fees
122+
ps.Heads[0].Time = txs[1].Time.UnixNano()
116123
ps.Tails[acc] = txs[1:]
117124
heap.Fix(&ps.Heads, 0)
118125
return

core/txpool/pending_test.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package txpool
1818

1919
import (
20+
"container/heap"
2021
"math/rand"
2122
"testing"
2223

@@ -48,6 +49,7 @@ func initLists() (heads TipList, tails map[common.Address][]*LazyTransaction) {
4849
heads = append(heads, &TxTips{
4950
From: addr,
5051
Tips: lazyTx.Fees,
52+
Time: 0,
5153
})
5254
}
5355
tail = append(tail, lazyTx)
@@ -126,17 +128,24 @@ func TestPendingSortAndPop(t *testing.T) {
126128
// Tests that if multiple transactions have the same price, the ones seen earlier
127129
// are prioritized to avoid network spam attacks aiming for a specific ordering.
128130
func TestSortingByTime(t *testing.T) {
129-
t.Skip("The TipList does not take time into account. Should it? ")
130-
//var heads TipList
131-
//for i := 0; i < 25; i++ {
132-
// addr := common.Address{byte(i)}
133-
// heads = append(heads, &TxTips{
134-
// From: addr,
135-
// Tips: uint256.NewInt(uint64(100)),
136-
// })
137-
//}
138-
//// un-sort the heads
139-
//rand.Shuffle(len(heads), func(i, j int) {
140-
// heads[i], heads[j] = heads[j], heads[i]
141-
//})
131+
var heads TipList
132+
for i := 0; i < 25; i++ {
133+
addr := common.Address{byte(i)}
134+
heads = append(heads, &TxTips{
135+
From: addr,
136+
Tips: *(uint256.NewInt(uint64(100))),
137+
Time: int64(i),
138+
})
139+
}
140+
// un-sort the heads
141+
rand.Shuffle(len(heads), func(i, j int) {
142+
heads[i], heads[j] = heads[j], heads[i]
143+
})
144+
heap.Init(&heads)
145+
for want := int64(0); want < 25; want++ {
146+
obj := heap.Pop(&heads).(*TxTips)
147+
if have := obj.Time; have != want {
148+
t.Fatalf("have %d want %d", have, want)
149+
}
150+
}
142151
}

eth/handler_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func (p *testTxPool) Pending(filter txpool.PendingFilter) txpool.Pending {
137137
heads = append(heads, &txpool.TxTips{
138138
From: addr,
139139
Tips: *tip,
140+
Time: ltx.Time.UnixNano(),
140141
})
141142
}
142143
}

miner/worker.go

+1
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ func (w *worker) mainLoop() {
585585
heads = append(heads, &txpool.TxTips{
586586
From: acc,
587587
Tips: *fees,
588+
Time: lazyTx.Time.UnixNano(),
588589
})
589590
}
590591
txs[acc] = append(txs[acc], lazyTx)

0 commit comments

Comments
 (0)