Skip to content

Commit a2f23ca

Browse files
karalabefjl
authored andcommitted
cmd, core, eth, miner: remove txpool gas price limits (#14442)
1 parent e201581 commit a2f23ca

11 files changed

Lines changed: 619 additions & 178 deletions

File tree

‎cmd/utils/flags.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ var (
237237
GasPriceFlag = BigFlag{
238238
Name: "gasprice",
239239
Usage: "Minimal gas price to accept for mining a transactions",
240-
Value: big.NewInt(20 * params.Shannon),
240+
Value: eth.DefaultConfig.GasPrice,
241241
}
242242
ExtraDataFlag = cli.StringFlag{
243243
Name: "extradata",

‎core/events.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package core
1818

1919
import (
20-
"math/big"
21-
2220
"github.com/ethereum/go-ethereum/common"
2321
"github.com/ethereum/go-ethereum/core/types"
2422
)
@@ -67,8 +65,6 @@ type ChainUncleEvent struct {
6765

6866
type ChainHeadEvent struct{ Block *types.Block }
6967

70-
type GasPriceChanged struct{ Price *big.Int }
71-
7268
// Mining operation events
7369
type StartMining struct{}
7470
type TopMining struct{}

‎core/tx_list.go‎

Lines changed: 162 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"math/big"
2323
"sort"
2424

25+
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/core/types"
27+
"github.com/ethereum/go-ethereum/log"
2628
)
2729

2830
// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
@@ -53,11 +55,11 @@ type txSortedMap struct {
5355
cache types.Transactions // Cache of the transactions already sorted
5456
}
5557

56-
// newTxSortedMap creates a new sorted transaction map.
58+
// newTxSortedMap creates a new nonce-sorted transaction map.
5759
func newTxSortedMap() *txSortedMap {
5860
return &txSortedMap{
5961
items: make(map[uint64]*types.Transaction),
60-
index: &nonceHeap{},
62+
index: new(nonceHeap),
6163
}
6264
}
6365

@@ -233,6 +235,12 @@ func newTxList(strict bool) *txList {
233235
}
234236
}
235237

238+
// Overlaps returns whether the transaction specified has the same nonce as one
239+
// already contained within the list.
240+
func (l *txList) Overlaps(tx *types.Transaction) bool {
241+
return l.txs.Get(tx.Nonce()) != nil
242+
}
243+
236244
// Add tries to insert a new transaction into the list, returning whether the
237245
// transaction was accepted, and if yes, any previous transaction it replaced.
238246
//
@@ -241,8 +249,11 @@ func newTxList(strict bool) *txList {
241249
func (l *txList) Add(tx *types.Transaction) (bool, *types.Transaction) {
242250
// If there's an older better transaction, abort
243251
old := l.txs.Get(tx.Nonce())
244-
if old != nil && old.GasPrice().Cmp(tx.GasPrice()) >= 0 {
245-
return false, nil
252+
if old != nil {
253+
threshold := new(big.Int).Div(new(big.Int).Mul(old.GasPrice(), big.NewInt(100+minPriceBumpPercent)), big.NewInt(100))
254+
if threshold.Cmp(tx.GasPrice()) >= 0 {
255+
return false, nil
256+
}
246257
}
247258
// Otherwise overwrite the old transaction with the current one
248259
l.txs.Put(tx)
@@ -340,3 +351,150 @@ func (l *txList) Empty() bool {
340351
func (l *txList) Flatten() types.Transactions {
341352
return l.txs.Flatten()
342353
}
354+
355+
// priceHeap is a heap.Interface implementation over transactions for retrieving
356+
// price-sorted transactions to discard when the pool fills up.
357+
type priceHeap []*types.Transaction
358+
359+
func (h priceHeap) Len() int { return len(h) }
360+
func (h priceHeap) Less(i, j int) bool { return h[i].GasPrice().Cmp(h[j].GasPrice()) < 0 }
361+
func (h priceHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
362+
363+
func (h *priceHeap) Push(x interface{}) {
364+
*h = append(*h, x.(*types.Transaction))
365+
}
366+
367+
func (h *priceHeap) Pop() interface{} {
368+
old := *h
369+
n := len(old)
370+
x := old[n-1]
371+
*h = old[0 : n-1]
372+
return x
373+
}
374+
375+
// txPricedList is a price-sorted heap to allow operating on transactions pool
376+
// contents in a price-incrementing way.
377+
type txPricedList struct {
378+
all *map[common.Hash]*types.Transaction // Pointer to the map of all transactions
379+
items *priceHeap // Heap of prices of all the stored transactions
380+
stales int // Number of stale price points to (re-heap trigger)
381+
}
382+
383+
// newTxPricedList creates a new price-sorted transaction heap.
384+
func newTxPricedList(all *map[common.Hash]*types.Transaction) *txPricedList {
385+
return &txPricedList{
386+
all: all,
387+
items: new(priceHeap),
388+
}
389+
}
390+
391+
// Put inserts a new transaction into the heap.
392+
func (l *txPricedList) Put(tx *types.Transaction) {
393+
heap.Push(l.items, tx)
394+
}
395+
396+
// Removed notifies the prices transaction list that an old transaction dropped
397+
// from the pool. The list will just keep a counter of stale objects and update
398+
// the heap if a large enough ratio of transactions go stale.
399+
func (l *txPricedList) Removed() {
400+
// Bump the stale counter, but exit if still too low (< 25%)
401+
l.stales++
402+
if l.stales <= len(*l.items)/4 {
403+
return
404+
}
405+
// Seems we've reached a critical number of stale transactions, reheap
406+
reheap := make(priceHeap, 0, len(*l.all))
407+
408+
l.stales, l.items = 0, &reheap
409+
for _, tx := range *l.all {
410+
*l.items = append(*l.items, tx)
411+
}
412+
heap.Init(l.items)
413+
}
414+
415+
// Discard finds all the transactions below the given price threshold, drops them
416+
// from the priced list and returs them for further removal from the entire pool.
417+
func (l *txPricedList) Cap(threshold *big.Int, local *txSet) types.Transactions {
418+
drop := make(types.Transactions, 0, 128) // Remote underpriced transactions to drop
419+
save := make(types.Transactions, 0, 64) // Local underpriced transactions to keep
420+
421+
for len(*l.items) > 0 {
422+
// Discard stale transactions if found during cleanup
423+
tx := heap.Pop(l.items).(*types.Transaction)
424+
425+
hash := tx.Hash()
426+
if _, ok := (*l.all)[hash]; !ok {
427+
l.stales--
428+
continue
429+
}
430+
// Stop the discards if we've reached the threshold
431+
if tx.GasPrice().Cmp(threshold) >= 0 {
432+
break
433+
}
434+
// Non stale transaction found, discard unless local
435+
if local.contains(hash) {
436+
save = append(save, tx)
437+
} else {
438+
drop = append(drop, tx)
439+
}
440+
}
441+
for _, tx := range save {
442+
heap.Push(l.items, tx)
443+
}
444+
return drop
445+
}
446+
447+
// Underpriced checks whether a transaction is cheaper than (or as cheap as) the
448+
// lowest priced transaction currently being tracked.
449+
func (l *txPricedList) Underpriced(tx *types.Transaction, local *txSet) bool {
450+
// Local transactions cannot be underpriced
451+
if local.contains(tx.Hash()) {
452+
return false
453+
}
454+
// Discard stale price points if found at the heap start
455+
for len(*l.items) > 0 {
456+
head := []*types.Transaction(*l.items)[0]
457+
if _, ok := (*l.all)[head.Hash()]; !ok {
458+
l.stales--
459+
heap.Pop(l.items)
460+
continue
461+
}
462+
break
463+
}
464+
// Check if the transaction is underpriced or not
465+
if len(*l.items) == 0 {
466+
log.Error("Pricing query for empty pool") // This cannot happen, print to catch programming errors
467+
return false
468+
}
469+
cheapest := []*types.Transaction(*l.items)[0]
470+
return cheapest.GasPrice().Cmp(tx.GasPrice()) >= 0
471+
}
472+
473+
// Discard finds a number of most underpriced transactions, removes them from the
474+
// priced list and returs them for further removal from the entire pool.
475+
func (l *txPricedList) Discard(count int, local *txSet) types.Transactions {
476+
drop := make(types.Transactions, 0, count) // Remote underpriced transactions to drop
477+
save := make(types.Transactions, 0, 64) // Local underpriced transactions to keep
478+
479+
for len(*l.items) > 0 && count > 0 {
480+
// Discard stale transactions if found during cleanup
481+
tx := heap.Pop(l.items).(*types.Transaction)
482+
483+
hash := tx.Hash()
484+
if _, ok := (*l.all)[hash]; !ok {
485+
l.stales--
486+
continue
487+
}
488+
// Non stale transaction found, discard unless local
489+
if local.contains(hash) {
490+
save = append(save, tx)
491+
} else {
492+
drop = append(drop, tx)
493+
count--
494+
}
495+
}
496+
for _, tx := range save {
497+
heap.Push(l.items, tx)
498+
}
499+
return drop
500+
}

0 commit comments

Comments
 (0)