2016-07-01 10:59:55 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2023-06-16 07:29:40 -05:00
|
|
|
package legacypool
|
2016-07-01 10:59:55 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"sort"
|
2021-08-31 03:33:39 -05:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2021-05-28 03:28:07 -05:00
|
|
|
"time"
|
2016-07-01 10:59:55 -05:00
|
|
|
|
2017-05-16 14:07:27 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-07-01 10:59:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
|
|
|
|
// retrieving sorted transactions from the possibly gapped future queue.
|
|
|
|
type nonceHeap []uint64
|
|
|
|
|
|
|
|
func (h nonceHeap) Len() int { return len(h) }
|
|
|
|
func (h nonceHeap) Less(i, j int) bool { return h[i] < h[j] }
|
|
|
|
func (h nonceHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
|
|
|
|
|
|
|
func (h *nonceHeap) Push(x interface{}) {
|
|
|
|
*h = append(*h, x.(uint64))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *nonceHeap) Pop() interface{} {
|
|
|
|
old := *h
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
2022-12-05 06:49:54 -06:00
|
|
|
old[n-1] = 0
|
2016-07-01 10:59:55 -05:00
|
|
|
*h = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
// sortedMap is a nonce->transaction hash map with a heap based index to allow
|
2016-08-25 11:04:40 -05:00
|
|
|
// iterating over the contents in a nonce-incrementing way.
|
2022-10-24 08:13:55 -05:00
|
|
|
type sortedMap struct {
|
2023-08-17 04:22:18 -05:00
|
|
|
items map[uint64]*types.Transaction // Hash map storing the transaction data
|
|
|
|
index *nonceHeap // Heap of nonces of all the stored transactions (non-strict mode)
|
|
|
|
cache types.Transactions // Cache of the transactions already sorted
|
|
|
|
cacheMu sync.Mutex // Mutex covering the cache
|
2016-08-25 11:04:40 -05:00
|
|
|
}
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
// newSortedMap creates a new nonce-sorted transaction map.
|
|
|
|
func newSortedMap() *sortedMap {
|
|
|
|
return &sortedMap{
|
2016-08-25 11:04:40 -05:00
|
|
|
items: make(map[uint64]*types.Transaction),
|
2017-05-16 14:07:27 -05:00
|
|
|
index: new(nonceHeap),
|
2016-08-25 11:04:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves the current transactions associated with the given nonce.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Get(nonce uint64) *types.Transaction {
|
2016-08-25 11:04:40 -05:00
|
|
|
return m.items[nonce]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put inserts a new transaction into the map, also updating the map's nonce
|
|
|
|
// index. If a transaction already exists with the same nonce, it's overwritten.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Put(tx *types.Transaction) {
|
2016-08-25 11:04:40 -05:00
|
|
|
nonce := tx.Nonce()
|
|
|
|
if m.items[nonce] == nil {
|
|
|
|
heap.Push(m.index, nonce)
|
|
|
|
}
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
2016-08-25 11:04:40 -05:00
|
|
|
m.items[nonce], m.cache = tx, nil
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Unlock()
|
2016-08-25 11:04:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Forward removes all transactions from the map with a nonce lower than the
|
|
|
|
// provided threshold. Every removed transaction is returned for any post-removal
|
|
|
|
// maintenance.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Forward(threshold uint64) types.Transactions {
|
2016-08-25 11:04:40 -05:00
|
|
|
var removed types.Transactions
|
|
|
|
|
|
|
|
// Pop off heap items until the threshold is reached
|
|
|
|
for m.index.Len() > 0 && (*m.index)[0] < threshold {
|
|
|
|
nonce := heap.Pop(m.index).(uint64)
|
|
|
|
removed = append(removed, m.items[nonce])
|
|
|
|
delete(m.items, nonce)
|
|
|
|
}
|
|
|
|
// If we had a cached order, shift the front
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
2016-08-25 11:04:40 -05:00
|
|
|
if m.cache != nil {
|
|
|
|
m.cache = m.cache[len(removed):]
|
|
|
|
}
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Unlock()
|
2016-08-25 11:04:40 -05:00
|
|
|
return removed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter iterates over the list of transactions and removes all of them for which
|
|
|
|
// the specified function evaluates to true.
|
2020-07-14 14:42:32 -05:00
|
|
|
// Filter, as opposed to 'filter', re-initialises the heap after the operation is done.
|
|
|
|
// If you want to do several consecutive filterings, it's therefore better to first
|
|
|
|
// do a .filter(func1) followed by .Filter(func2) or reheap()
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Filter(filter func(*types.Transaction) bool) types.Transactions {
|
2020-07-14 14:42:32 -05:00
|
|
|
removed := m.filter(filter)
|
|
|
|
// If transactions were removed, the heap and cache are ruined
|
|
|
|
if len(removed) > 0 {
|
|
|
|
m.reheap()
|
|
|
|
}
|
|
|
|
return removed
|
|
|
|
}
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) reheap() {
|
2020-07-14 14:42:32 -05:00
|
|
|
*m.index = make([]uint64, 0, len(m.items))
|
|
|
|
for nonce := range m.items {
|
|
|
|
*m.index = append(*m.index, nonce)
|
|
|
|
}
|
|
|
|
heap.Init(m.index)
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
2020-07-14 14:42:32 -05:00
|
|
|
m.cache = nil
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Unlock()
|
2020-07-14 14:42:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// filter is identical to Filter, but **does not** regenerate the heap. This method
|
|
|
|
// should only be used if followed immediately by a call to Filter or reheap()
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) filter(filter func(*types.Transaction) bool) types.Transactions {
|
2016-08-25 11:04:40 -05:00
|
|
|
var removed types.Transactions
|
|
|
|
|
|
|
|
// Collect all the transactions to filter out
|
|
|
|
for nonce, tx := range m.items {
|
|
|
|
if filter(tx) {
|
|
|
|
removed = append(removed, tx)
|
|
|
|
delete(m.items, nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(removed) > 0 {
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
2016-08-25 11:04:40 -05:00
|
|
|
m.cache = nil
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Unlock()
|
2016-08-25 11:04:40 -05:00
|
|
|
}
|
|
|
|
return removed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cap places a hard limit on the number of items, returning all transactions
|
|
|
|
// exceeding that limit.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Cap(threshold int) types.Transactions {
|
2016-08-25 11:04:40 -05:00
|
|
|
// Short circuit if the number of items is under the limit
|
|
|
|
if len(m.items) <= threshold {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Otherwise gather and drop the highest nonce'd transactions
|
|
|
|
var drops types.Transactions
|
|
|
|
|
|
|
|
sort.Sort(*m.index)
|
|
|
|
for size := len(m.items); size > threshold; size-- {
|
|
|
|
drops = append(drops, m.items[(*m.index)[size-1]])
|
|
|
|
delete(m.items, (*m.index)[size-1])
|
|
|
|
}
|
|
|
|
*m.index = (*m.index)[:threshold]
|
|
|
|
heap.Init(m.index)
|
|
|
|
|
|
|
|
// If we had a cache, shift the back
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
2016-08-25 11:04:40 -05:00
|
|
|
if m.cache != nil {
|
|
|
|
m.cache = m.cache[:len(m.cache)-len(drops)]
|
|
|
|
}
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Unlock()
|
2016-08-25 11:04:40 -05:00
|
|
|
return drops
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a transaction from the maintained map, returning whether the
|
|
|
|
// transaction was found.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Remove(nonce uint64) bool {
|
2016-08-25 11:04:40 -05:00
|
|
|
// Short circuit if no transaction is present
|
|
|
|
_, ok := m.items[nonce]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Otherwise delete the transaction and fix the heap index
|
|
|
|
for i := 0; i < m.index.Len(); i++ {
|
|
|
|
if (*m.index)[i] == nonce {
|
|
|
|
heap.Remove(m.index, i)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(m.items, nonce)
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
2016-08-25 11:04:40 -05:00
|
|
|
m.cache = nil
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Unlock()
|
2016-08-25 11:04:40 -05:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ready retrieves a sequentially increasing list of transactions starting at the
|
|
|
|
// provided nonce that is ready for processing. The returned transactions will be
|
|
|
|
// removed from the list.
|
|
|
|
//
|
|
|
|
// Note, all transactions with nonces lower than start will also be returned to
|
|
|
|
// prevent getting into and invalid state. This is not something that should ever
|
|
|
|
// happen but better to be self correcting than failing!
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Ready(start uint64) types.Transactions {
|
2016-08-25 11:04:40 -05:00
|
|
|
// Short circuit if no transactions are available
|
|
|
|
if m.index.Len() == 0 || (*m.index)[0] > start {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Otherwise start accumulating incremental transactions
|
|
|
|
var ready types.Transactions
|
|
|
|
for next := (*m.index)[0]; m.index.Len() > 0 && (*m.index)[0] == next; next++ {
|
|
|
|
ready = append(ready, m.items[next])
|
|
|
|
delete(m.items, next)
|
|
|
|
heap.Pop(m.index)
|
|
|
|
}
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
2016-08-25 11:04:40 -05:00
|
|
|
m.cache = nil
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Unlock()
|
2016-08-25 11:04:40 -05:00
|
|
|
|
|
|
|
return ready
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the transaction map.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Len() int {
|
2016-08-25 11:04:40 -05:00
|
|
|
return len(m.items)
|
|
|
|
}
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) flatten() types.Transactions {
|
2023-08-17 04:22:18 -05:00
|
|
|
m.cacheMu.Lock()
|
|
|
|
defer m.cacheMu.Unlock()
|
2016-08-25 11:04:40 -05:00
|
|
|
// If the sorting was not cached yet, create and cache it
|
|
|
|
if m.cache == nil {
|
|
|
|
m.cache = make(types.Transactions, 0, len(m.items))
|
|
|
|
for _, tx := range m.items {
|
|
|
|
m.cache = append(m.cache, tx)
|
|
|
|
}
|
|
|
|
sort.Sort(types.TxByNonce(m.cache))
|
|
|
|
}
|
2020-07-14 14:42:32 -05:00
|
|
|
return m.cache
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flatten creates a nonce-sorted slice of transactions based on the loosely
|
|
|
|
// sorted internal representation. The result of the sorting is cached in case
|
|
|
|
// it's requested again before any modifications are made to the contents.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) Flatten() types.Transactions {
|
2020-07-14 14:42:32 -05:00
|
|
|
cache := m.flatten()
|
2023-08-17 04:22:18 -05:00
|
|
|
// Copy the cache to prevent accidental modification
|
2020-07-14 14:42:32 -05:00
|
|
|
txs := make(types.Transactions, len(cache))
|
|
|
|
copy(txs, cache)
|
2016-08-25 11:04:40 -05:00
|
|
|
return txs
|
|
|
|
}
|
|
|
|
|
2020-07-14 14:42:32 -05:00
|
|
|
// LastElement returns the last element of a flattened list, thus, the
|
|
|
|
// transaction with the highest nonce
|
2022-10-24 08:13:55 -05:00
|
|
|
func (m *sortedMap) LastElement() *types.Transaction {
|
2020-07-14 14:42:32 -05:00
|
|
|
cache := m.flatten()
|
|
|
|
return cache[len(cache)-1]
|
|
|
|
}
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
// list is a "list" of transactions belonging to an account, sorted by account
|
2016-07-01 10:59:55 -05:00
|
|
|
// nonce. The same type can be used both for storing contiguous transactions for
|
|
|
|
// the executable/pending queue; and for storing gapped transactions for the non-
|
2017-01-06 11:44:35 -06:00
|
|
|
// executable/future queue, with minor behavioral changes.
|
2022-10-24 08:13:55 -05:00
|
|
|
type list struct {
|
|
|
|
strict bool // Whether nonces are strictly continuous or not
|
|
|
|
txs *sortedMap // Heap indexed sorted hash map of the transactions
|
2017-05-29 16:31:37 -05:00
|
|
|
|
2023-03-10 11:30:26 -06:00
|
|
|
costcap *big.Int // Price of the highest costing transaction (reset only if exceeds balance)
|
|
|
|
gascap uint64 // Gas limit of the highest spending transaction (reset only if exceeds block limit)
|
|
|
|
totalcost *big.Int // Total cost of all transactions in the list
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
// newList create a new transaction list for maintaining nonce-indexable fast,
|
2016-07-01 10:59:55 -05:00
|
|
|
// gapped, sortable transaction lists.
|
2022-10-24 08:13:55 -05:00
|
|
|
func newList(strict bool) *list {
|
|
|
|
return &list{
|
2023-03-10 11:30:26 -06:00
|
|
|
strict: strict,
|
|
|
|
txs: newSortedMap(),
|
|
|
|
costcap: new(big.Int),
|
|
|
|
totalcost: new(big.Int),
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 03:59:32 -05:00
|
|
|
// Contains returns whether the list contains a transaction
|
|
|
|
// with the provided nonce.
|
|
|
|
func (l *list) Contains(nonce uint64) bool {
|
|
|
|
return l.txs.Get(nonce) != nil
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
|
|
|
|
2016-08-17 08:53:15 -05:00
|
|
|
// Add tries to insert a new transaction into the list, returning whether the
|
|
|
|
// transaction was accepted, and if yes, any previous transaction it replaced.
|
2016-07-01 10:59:55 -05:00
|
|
|
//
|
2017-05-29 16:31:37 -05:00
|
|
|
// If the new transaction is accepted into the list, the lists' cost and gas
|
|
|
|
// thresholds are also potentially updated.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transaction) {
|
2016-08-25 11:04:40 -05:00
|
|
|
// If there's an older better transaction, abort
|
|
|
|
old := l.txs.Get(tx.Nonce())
|
2017-05-16 14:07:27 -05:00
|
|
|
if old != nil {
|
2021-06-08 05:05:41 -05:00
|
|
|
if old.GasFeeCapCmp(tx) >= 0 || old.GasTipCapCmp(tx) >= 0 {
|
2021-05-28 03:28:07 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// thresholdFeeCap = oldFC * (100 + priceBump) / 100
|
2020-07-14 14:42:32 -05:00
|
|
|
a := big.NewInt(100 + int64(priceBump))
|
2021-06-08 05:05:41 -05:00
|
|
|
aFeeCap := new(big.Int).Mul(a, old.GasFeeCap())
|
|
|
|
aTip := a.Mul(a, old.GasTipCap())
|
2021-05-28 03:28:07 -05:00
|
|
|
|
|
|
|
// thresholdTip = oldTip * (100 + priceBump) / 100
|
2020-07-14 14:42:32 -05:00
|
|
|
b := big.NewInt(100)
|
2021-05-28 03:28:07 -05:00
|
|
|
thresholdFeeCap := aFeeCap.Div(aFeeCap, b)
|
|
|
|
thresholdTip := aTip.Div(aTip, b)
|
|
|
|
|
2021-10-28 07:59:51 -05:00
|
|
|
// We have to ensure that both the new fee cap and tip are higher than the
|
2021-05-28 03:28:07 -05:00
|
|
|
// old ones as well as checking the percentage threshold to ensure that
|
2021-10-28 07:59:51 -05:00
|
|
|
// this is accurate for low (Wei-level) gas price replacements.
|
2021-06-08 05:05:41 -05:00
|
|
|
if tx.GasFeeCapIntCmp(thresholdFeeCap) < 0 || tx.GasTipCapIntCmp(thresholdTip) < 0 {
|
2017-05-16 14:07:27 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
2023-03-10 11:30:26 -06:00
|
|
|
// Old is being replaced, subtract old cost
|
|
|
|
l.subTotalCost([]*types.Transaction{old})
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
2023-03-10 11:30:26 -06:00
|
|
|
// Add new tx cost to totalcost
|
|
|
|
l.totalcost.Add(l.totalcost, tx.Cost())
|
2016-08-25 11:04:40 -05:00
|
|
|
// Otherwise overwrite the old transaction with the current one
|
|
|
|
l.txs.Put(tx)
|
2020-07-09 07:02:03 -05:00
|
|
|
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
|
2016-07-01 10:59:55 -05:00
|
|
|
l.costcap = cost
|
|
|
|
}
|
2017-11-13 05:47:27 -06:00
|
|
|
if gas := tx.Gas(); l.gascap < gas {
|
2017-05-29 16:31:37 -05:00
|
|
|
l.gascap = gas
|
|
|
|
}
|
2016-07-01 10:59:55 -05:00
|
|
|
return true, old
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward removes all transactions from the list with a nonce lower than the
|
|
|
|
// provided threshold. Every removed transaction is returned for any post-removal
|
|
|
|
// maintenance.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Forward(threshold uint64) types.Transactions {
|
2023-03-10 11:30:26 -06:00
|
|
|
txs := l.txs.Forward(threshold)
|
|
|
|
l.subTotalCost(txs)
|
|
|
|
return txs
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
|
2017-05-29 16:31:37 -05:00
|
|
|
// Filter removes all transactions from the list with a cost or gas limit higher
|
|
|
|
// than the provided thresholds. Every removed transaction is returned for any
|
|
|
|
// post-removal maintenance. Strict-mode invalidated transactions are also
|
|
|
|
// returned.
|
2016-07-01 10:59:55 -05:00
|
|
|
//
|
2017-05-29 16:31:37 -05:00
|
|
|
// This method uses the cached costcap and gascap to quickly decide if there's even
|
|
|
|
// a point in calculating all the costs or if the balance covers all. If the threshold
|
|
|
|
// is lower than the costgas cap, the caps will be reset to a new high after removing
|
|
|
|
// the newly invalidated transactions.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Filter(costLimit *big.Int, gasLimit uint64) (types.Transactions, types.Transactions) {
|
2016-08-17 08:53:15 -05:00
|
|
|
// If all transactions are below the threshold, short circuit
|
2020-07-09 07:02:03 -05:00
|
|
|
if l.costcap.Cmp(costLimit) <= 0 && l.gascap <= gasLimit {
|
2016-07-01 10:59:55 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-09 07:02:03 -05:00
|
|
|
l.costcap = new(big.Int).Set(costLimit) // Lower the caps to the thresholds
|
2017-11-13 05:47:27 -06:00
|
|
|
l.gascap = gasLimit
|
2016-07-01 10:59:55 -05:00
|
|
|
|
2016-08-25 11:04:40 -05:00
|
|
|
// Filter out all the transactions above the account's funds
|
2020-07-14 14:42:32 -05:00
|
|
|
removed := l.txs.Filter(func(tx *types.Transaction) bool {
|
|
|
|
return tx.Gas() > gasLimit || tx.Cost().Cmp(costLimit) > 0
|
|
|
|
})
|
2016-08-25 11:04:40 -05:00
|
|
|
|
2020-07-14 14:42:32 -05:00
|
|
|
if len(removed) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-09 07:02:03 -05:00
|
|
|
var invalids types.Transactions
|
2020-07-14 14:42:32 -05:00
|
|
|
// If the list was strict, filter anything above the lowest nonce
|
|
|
|
if l.strict {
|
2016-08-25 11:04:40 -05:00
|
|
|
lowest := uint64(math.MaxUint64)
|
|
|
|
for _, tx := range removed {
|
|
|
|
if nonce := tx.Nonce(); lowest > nonce {
|
|
|
|
lowest = nonce
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 14:42:32 -05:00
|
|
|
invalids = l.txs.filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest })
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
2023-03-10 11:30:26 -06:00
|
|
|
// Reset total cost
|
|
|
|
l.subTotalCost(removed)
|
|
|
|
l.subTotalCost(invalids)
|
2020-07-14 14:42:32 -05:00
|
|
|
l.txs.reheap()
|
2016-07-01 10:59:55 -05:00
|
|
|
return removed, invalids
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cap places a hard limit on the number of items, returning all transactions
|
2016-08-17 08:53:15 -05:00
|
|
|
// exceeding that limit.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Cap(threshold int) types.Transactions {
|
2023-03-10 11:30:26 -06:00
|
|
|
txs := l.txs.Cap(threshold)
|
|
|
|
l.subTotalCost(txs)
|
|
|
|
return txs
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a transaction from the maintained list, returning whether the
|
|
|
|
// transaction was found, and also returning any transaction invalidated due to
|
|
|
|
// the deletion (strict mode only).
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Remove(tx *types.Transaction) (bool, types.Transactions) {
|
2016-08-25 11:04:40 -05:00
|
|
|
// Remove the transaction from the set
|
2016-07-01 10:59:55 -05:00
|
|
|
nonce := tx.Nonce()
|
2016-08-25 11:04:40 -05:00
|
|
|
if removed := l.txs.Remove(nonce); !removed {
|
|
|
|
return false, nil
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
2023-03-10 11:30:26 -06:00
|
|
|
l.subTotalCost([]*types.Transaction{tx})
|
2016-08-25 11:04:40 -05:00
|
|
|
// In strict mode, filter out non-executable transactions
|
|
|
|
if l.strict {
|
2023-03-10 11:30:26 -06:00
|
|
|
txs := l.txs.Filter(func(tx *types.Transaction) bool { return tx.Nonce() > nonce })
|
|
|
|
l.subTotalCost(txs)
|
|
|
|
return true, txs
|
2016-08-25 11:04:40 -05:00
|
|
|
}
|
|
|
|
return true, nil
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ready retrieves a sequentially increasing list of transactions starting at the
|
|
|
|
// provided nonce that is ready for processing. The returned transactions will be
|
|
|
|
// removed from the list.
|
|
|
|
//
|
2016-08-17 08:53:15 -05:00
|
|
|
// Note, all transactions with nonces lower than start will also be returned to
|
2016-07-01 10:59:55 -05:00
|
|
|
// prevent getting into and invalid state. This is not something that should ever
|
|
|
|
// happen but better to be self correcting than failing!
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Ready(start uint64) types.Transactions {
|
2023-03-10 11:30:26 -06:00
|
|
|
txs := l.txs.Ready(start)
|
|
|
|
l.subTotalCost(txs)
|
|
|
|
return txs
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the transaction list.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Len() int {
|
2016-08-25 11:04:40 -05:00
|
|
|
return l.txs.Len()
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns whether the list of transactions is empty or not.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Empty() bool {
|
2016-08-25 11:04:40 -05:00
|
|
|
return l.Len() == 0
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flatten creates a nonce-sorted slice of transactions based on the loosely
|
|
|
|
// sorted internal representation. The result of the sorting is cached in case
|
|
|
|
// it's requested again before any modifications are made to the contents.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) Flatten() types.Transactions {
|
2016-08-25 11:04:40 -05:00
|
|
|
return l.txs.Flatten()
|
2016-07-01 10:59:55 -05:00
|
|
|
}
|
2017-05-16 14:07:27 -05:00
|
|
|
|
2020-07-14 14:42:32 -05:00
|
|
|
// LastElement returns the last element of a flattened list, thus, the
|
|
|
|
// transaction with the highest nonce
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *list) LastElement() *types.Transaction {
|
2020-07-14 14:42:32 -05:00
|
|
|
return l.txs.LastElement()
|
|
|
|
}
|
|
|
|
|
2023-03-10 11:30:26 -06:00
|
|
|
// subTotalCost subtracts the cost of the given transactions from the
|
|
|
|
// total cost of all transactions.
|
|
|
|
func (l *list) subTotalCost(txs []*types.Transaction) {
|
|
|
|
for _, tx := range txs {
|
|
|
|
l.totalcost.Sub(l.totalcost, tx.Cost())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 14:07:27 -05:00
|
|
|
// priceHeap is a heap.Interface implementation over transactions for retrieving
|
2021-05-28 03:28:07 -05:00
|
|
|
// price-sorted transactions to discard when the pool fills up. If baseFee is set
|
|
|
|
// then the heap is sorted based on the effective tip based on the given base fee.
|
2021-06-08 05:05:41 -05:00
|
|
|
// If baseFee is nil then the sorting is based on gasFeeCap.
|
2021-05-28 03:28:07 -05:00
|
|
|
type priceHeap struct {
|
|
|
|
baseFee *big.Int // heap should always be re-sorted after baseFee is changed
|
|
|
|
list []*types.Transaction
|
|
|
|
}
|
2017-05-16 14:07:27 -05:00
|
|
|
|
2021-05-28 03:28:07 -05:00
|
|
|
func (h *priceHeap) Len() int { return len(h.list) }
|
|
|
|
func (h *priceHeap) Swap(i, j int) { h.list[i], h.list[j] = h.list[j], h.list[i] }
|
2018-04-12 04:17:52 -05:00
|
|
|
|
2021-05-28 03:28:07 -05:00
|
|
|
func (h *priceHeap) Less(i, j int) bool {
|
|
|
|
switch h.cmp(h.list[i], h.list[j]) {
|
2018-04-12 04:17:52 -05:00
|
|
|
case -1:
|
|
|
|
return true
|
|
|
|
case 1:
|
|
|
|
return false
|
2021-05-28 03:28:07 -05:00
|
|
|
default:
|
|
|
|
return h.list[i].Nonce() > h.list[j].Nonce()
|
2018-04-12 04:17:52 -05:00
|
|
|
}
|
2021-05-28 03:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *priceHeap) cmp(a, b *types.Transaction) int {
|
|
|
|
if h.baseFee != nil {
|
|
|
|
// Compare effective tips if baseFee is specified
|
2021-06-08 05:05:41 -05:00
|
|
|
if c := a.EffectiveGasTipCmp(b, h.baseFee); c != 0 {
|
2021-05-28 03:28:07 -05:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compare fee caps if baseFee is not specified or effective tips are equal
|
2021-06-08 05:05:41 -05:00
|
|
|
if c := a.GasFeeCapCmp(b); c != 0 {
|
2021-05-28 03:28:07 -05:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
// Compare tips if effective tips and fee caps are equal
|
2021-06-08 05:05:41 -05:00
|
|
|
return a.GasTipCapCmp(b)
|
2018-04-12 04:17:52 -05:00
|
|
|
}
|
2017-05-16 14:07:27 -05:00
|
|
|
|
|
|
|
func (h *priceHeap) Push(x interface{}) {
|
2021-05-28 03:28:07 -05:00
|
|
|
tx := x.(*types.Transaction)
|
|
|
|
h.list = append(h.list, tx)
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *priceHeap) Pop() interface{} {
|
2021-05-28 03:28:07 -05:00
|
|
|
old := h.list
|
2017-05-16 14:07:27 -05:00
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
2020-09-28 07:24:01 -05:00
|
|
|
old[n-1] = nil
|
2021-05-28 03:28:07 -05:00
|
|
|
h.list = old[0 : n-1]
|
2017-05-16 14:07:27 -05:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
// pricedList is a price-sorted heap to allow operating on transactions pool
|
2022-10-11 02:37:00 -05:00
|
|
|
// contents in a price-incrementing way. It's built upon the all transactions
|
2020-12-11 03:44:57 -06:00
|
|
|
// in txpool but only interested in the remote part. It means only remote transactions
|
|
|
|
// will be considered for tracking, sorting, eviction, etc.
|
2021-05-28 03:28:07 -05:00
|
|
|
//
|
|
|
|
// Two heaps are used for sorting: the urgent heap (based on effective tip in the next
|
2021-06-08 05:05:41 -05:00
|
|
|
// block) and the floating heap (based on gasFeeCap). Always the bigger heap is chosen for
|
2021-05-28 03:28:07 -05:00
|
|
|
// eviction. Transactions evicted from the urgent heap are first demoted into the floating heap.
|
|
|
|
// In some cases (during a congestion, when blocks are full) the urgent heap can provide
|
|
|
|
// better candidates for inclusion while in other cases (at the top of the baseFee peak)
|
|
|
|
// the floating heap is better. When baseFee is decreasing they behave similarly.
|
2022-10-24 08:13:55 -05:00
|
|
|
type pricedList struct {
|
2021-09-07 09:15:11 -05:00
|
|
|
// Number of stale price points to (re-heap trigger).
|
2023-03-20 03:12:24 -05:00
|
|
|
stales atomic.Int64
|
2021-09-07 09:15:11 -05:00
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
all *lookup // Pointer to the map of all transactions
|
2021-08-31 03:33:39 -05:00
|
|
|
urgent, floating priceHeap // Heaps of prices of all the stored **remote** transactions
|
|
|
|
reheapMu sync.Mutex // Mutex asserts that only one routine is reheaping the list
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 03:28:07 -05:00
|
|
|
const (
|
|
|
|
// urgentRatio : floatingRatio is the capacity ratio of the two queues
|
|
|
|
urgentRatio = 4
|
|
|
|
floatingRatio = 1
|
|
|
|
)
|
|
|
|
|
2022-10-24 08:13:55 -05:00
|
|
|
// newPricedList creates a new price-sorted transaction heap.
|
|
|
|
func newPricedList(all *lookup) *pricedList {
|
|
|
|
return &pricedList{
|
2021-05-28 03:28:07 -05:00
|
|
|
all: all,
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put inserts a new transaction into the heap.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *pricedList) Put(tx *types.Transaction, local bool) {
|
2020-12-11 03:44:57 -06:00
|
|
|
if local {
|
|
|
|
return
|
|
|
|
}
|
2021-05-28 03:28:07 -05:00
|
|
|
// Insert every new transaction to the urgent heap first; Discard will balance the heaps
|
|
|
|
heap.Push(&l.urgent, tx)
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removed notifies the prices transaction list that an old transaction dropped
|
|
|
|
// from the pool. The list will just keep a counter of stale objects and update
|
|
|
|
// the heap if a large enough ratio of transactions go stale.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *pricedList) Removed(count int) {
|
2017-05-16 14:07:27 -05:00
|
|
|
// Bump the stale counter, but exit if still too low (< 25%)
|
2023-03-20 03:12:24 -05:00
|
|
|
stales := l.stales.Add(int64(count))
|
2021-08-31 03:33:39 -05:00
|
|
|
if int(stales) <= (len(l.urgent.list)+len(l.floating.list))/4 {
|
2017-05-16 14:07:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Seems we've reached a critical number of stale transactions, reheap
|
2020-12-11 03:44:57 -06:00
|
|
|
l.Reheap()
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Underpriced checks whether a transaction is cheaper than (or as cheap as) the
|
2020-12-11 03:44:57 -06:00
|
|
|
// lowest priced (remote) transaction currently being tracked.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *pricedList) Underpriced(tx *types.Transaction) bool {
|
2021-05-28 03:28:07 -05:00
|
|
|
// Note: with two queues, being underpriced is defined as being worse than the worst item
|
|
|
|
// in all non-empty queues if there is any. If both queues are empty then nothing is underpriced.
|
|
|
|
return (l.underpricedFor(&l.urgent, tx) || len(l.urgent.list) == 0) &&
|
|
|
|
(l.underpricedFor(&l.floating, tx) || len(l.floating.list) == 0) &&
|
|
|
|
(len(l.urgent.list) != 0 || len(l.floating.list) != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// underpricedFor checks whether a transaction is cheaper than (or as cheap as) the
|
|
|
|
// lowest priced (remote) transaction in the given heap.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *pricedList) underpricedFor(h *priceHeap, tx *types.Transaction) bool {
|
2017-05-16 14:07:27 -05:00
|
|
|
// Discard stale price points if found at the heap start
|
2021-05-28 03:28:07 -05:00
|
|
|
for len(h.list) > 0 {
|
|
|
|
head := h.list[0]
|
2020-12-11 03:44:57 -06:00
|
|
|
if l.all.GetRemote(head.Hash()) == nil { // Removed or migrated
|
2023-03-20 03:12:24 -05:00
|
|
|
l.stales.Add(-1)
|
2021-05-28 03:28:07 -05:00
|
|
|
heap.Pop(h)
|
2017-05-16 14:07:27 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Check if the transaction is underpriced or not
|
2021-05-28 03:28:07 -05:00
|
|
|
if len(h.list) == 0 {
|
2020-12-11 03:44:57 -06:00
|
|
|
return false // There is no remote transaction at all.
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
2020-12-11 03:44:57 -06:00
|
|
|
// If the remote transaction is even cheaper than the
|
|
|
|
// cheapest one tracked locally, reject it.
|
2021-05-28 03:28:07 -05:00
|
|
|
return h.cmp(h.list[0], tx) >= 0
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Discard finds a number of most underpriced transactions, removes them from the
|
2017-07-06 03:51:59 -05:00
|
|
|
// priced list and returns them for further removal from the entire pool.
|
2023-03-10 11:30:26 -06:00
|
|
|
// If noPending is set to true, we will only consider the floating list
|
2020-12-11 03:44:57 -06:00
|
|
|
//
|
|
|
|
// Note local transaction won't be considered for eviction.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *pricedList) Discard(slots int, force bool) (types.Transactions, bool) {
|
2020-12-11 03:44:57 -06:00
|
|
|
drop := make(types.Transactions, 0, slots) // Remote underpriced transactions to drop
|
2021-05-28 03:28:07 -05:00
|
|
|
for slots > 0 {
|
2023-06-19 00:52:40 -05:00
|
|
|
if len(l.urgent.list)*floatingRatio > len(l.floating.list)*urgentRatio {
|
2021-05-28 03:28:07 -05:00
|
|
|
// Discard stale transactions if found during cleanup
|
|
|
|
tx := heap.Pop(&l.urgent).(*types.Transaction)
|
|
|
|
if l.all.GetRemote(tx.Hash()) == nil { // Removed or migrated
|
2023-03-20 03:12:24 -05:00
|
|
|
l.stales.Add(-1)
|
2021-05-28 03:28:07 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Non stale transaction found, move to floating heap
|
|
|
|
heap.Push(&l.floating, tx)
|
|
|
|
} else {
|
|
|
|
if len(l.floating.list) == 0 {
|
|
|
|
// Stop if both heaps are empty
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Discard stale transactions if found during cleanup
|
|
|
|
tx := heap.Pop(&l.floating).(*types.Transaction)
|
|
|
|
if l.all.GetRemote(tx.Hash()) == nil { // Removed or migrated
|
2023-03-20 03:12:24 -05:00
|
|
|
l.stales.Add(-1)
|
2021-05-28 03:28:07 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Non stale transaction found, discard it
|
|
|
|
drop = append(drop, tx)
|
|
|
|
slots -= numSlots(tx)
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
2020-12-11 03:44:57 -06:00
|
|
|
}
|
|
|
|
// If we still can't make enough room for the new transaction
|
|
|
|
if slots > 0 && !force {
|
|
|
|
for _, tx := range drop {
|
2021-05-28 03:28:07 -05:00
|
|
|
heap.Push(&l.urgent, tx)
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
2020-12-11 03:44:57 -06:00
|
|
|
return nil, false
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|
2020-12-11 03:44:57 -06:00
|
|
|
return drop, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reheap forcibly rebuilds the heap based on the current remote transaction set.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *pricedList) Reheap() {
|
2021-08-31 03:33:39 -05:00
|
|
|
l.reheapMu.Lock()
|
|
|
|
defer l.reheapMu.Unlock()
|
2021-05-28 03:28:07 -05:00
|
|
|
start := time.Now()
|
2023-03-20 03:12:24 -05:00
|
|
|
l.stales.Store(0)
|
2021-05-28 03:28:07 -05:00
|
|
|
l.urgent.list = make([]*types.Transaction, 0, l.all.RemoteCount())
|
2020-12-11 03:44:57 -06:00
|
|
|
l.all.Range(func(hash common.Hash, tx *types.Transaction, local bool) bool {
|
2021-05-28 03:28:07 -05:00
|
|
|
l.urgent.list = append(l.urgent.list, tx)
|
2020-12-11 03:44:57 -06:00
|
|
|
return true
|
|
|
|
}, false, true) // Only iterate remotes
|
2021-05-28 03:28:07 -05:00
|
|
|
heap.Init(&l.urgent)
|
|
|
|
|
|
|
|
// balance out the two heaps by moving the worse half of transactions into the
|
|
|
|
// floating heap
|
|
|
|
// Note: Discard would also do this before the first eviction but Reheap can do
|
|
|
|
// is more efficiently. Also, Underpriced would work suboptimally the first time
|
|
|
|
// if the floating queue was empty.
|
|
|
|
floatingCount := len(l.urgent.list) * floatingRatio / (urgentRatio + floatingRatio)
|
|
|
|
l.floating.list = make([]*types.Transaction, floatingCount)
|
|
|
|
for i := 0; i < floatingCount; i++ {
|
|
|
|
l.floating.list[i] = heap.Pop(&l.urgent).(*types.Transaction)
|
|
|
|
}
|
|
|
|
heap.Init(&l.floating)
|
|
|
|
reheapTimer.Update(time.Since(start))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBaseFee updates the base fee and triggers a re-heap. Note that Removed is not
|
|
|
|
// necessary to call right before SetBaseFee when processing a new block.
|
2022-10-24 08:13:55 -05:00
|
|
|
func (l *pricedList) SetBaseFee(baseFee *big.Int) {
|
2021-05-28 03:28:07 -05:00
|
|
|
l.urgent.baseFee = baseFee
|
|
|
|
l.Reheap()
|
2017-05-16 14:07:27 -05:00
|
|
|
}
|