2016-07-01 18:59:55 +03: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/>.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"sort"
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-07-01 18:59:55 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-05-16 22:07:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-07-01 18:59:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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]
|
|
|
|
*h = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2016-08-25 19:04:40 +03:00
|
|
|
// txSortedMap is a nonce->transaction hash map with a heap based index to allow
|
|
|
|
// iterating over the contents in a nonce-incrementing way.
|
|
|
|
type txSortedMap struct {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// newTxSortedMap creates a new nonce-sorted transaction map.
|
2016-08-25 19:04:40 +03:00
|
|
|
func newTxSortedMap() *txSortedMap {
|
|
|
|
return &txSortedMap{
|
|
|
|
items: make(map[uint64]*types.Transaction),
|
2017-05-16 22:07:27 +03:00
|
|
|
index: new(nonceHeap),
|
2016-08-25 19:04:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves the current transactions associated with the given nonce.
|
|
|
|
func (m *txSortedMap) Get(nonce uint64) *types.Transaction {
|
|
|
|
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.
|
|
|
|
func (m *txSortedMap) Put(tx *types.Transaction) {
|
|
|
|
nonce := tx.Nonce()
|
|
|
|
if m.items[nonce] == nil {
|
|
|
|
heap.Push(m.index, nonce)
|
|
|
|
}
|
|
|
|
m.items[nonce], m.cache = tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (m *txSortedMap) Forward(threshold uint64) types.Transactions {
|
|
|
|
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
|
|
|
|
if m.cache != nil {
|
|
|
|
m.cache = m.cache[len(removed):]
|
|
|
|
}
|
|
|
|
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 22:42:32 +03: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()
|
2016-08-25 19:04:40 +03:00
|
|
|
func (m *txSortedMap) Filter(filter func(*types.Transaction) bool) types.Transactions {
|
2020-07-14 22:42:32 +03:00
|
|
|
removed := m.filter(filter)
|
|
|
|
// If transactions were removed, the heap and cache are ruined
|
|
|
|
if len(removed) > 0 {
|
|
|
|
m.reheap()
|
|
|
|
}
|
|
|
|
return removed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *txSortedMap) reheap() {
|
|
|
|
*m.index = make([]uint64, 0, len(m.items))
|
|
|
|
for nonce := range m.items {
|
|
|
|
*m.index = append(*m.index, nonce)
|
|
|
|
}
|
|
|
|
heap.Init(m.index)
|
|
|
|
m.cache = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
func (m *txSortedMap) filter(filter func(*types.Transaction) bool) types.Transactions {
|
2016-08-25 19:04:40 +03: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 {
|
|
|
|
m.cache = nil
|
|
|
|
}
|
|
|
|
return removed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cap places a hard limit on the number of items, returning all transactions
|
|
|
|
// exceeding that limit.
|
|
|
|
func (m *txSortedMap) Cap(threshold int) types.Transactions {
|
|
|
|
// 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
|
|
|
|
if m.cache != nil {
|
|
|
|
m.cache = m.cache[:len(m.cache)-len(drops)]
|
|
|
|
}
|
|
|
|
return drops
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a transaction from the maintained map, returning whether the
|
|
|
|
// transaction was found.
|
|
|
|
func (m *txSortedMap) Remove(nonce uint64) bool {
|
|
|
|
// 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)
|
|
|
|
m.cache = nil
|
|
|
|
|
|
|
|
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!
|
|
|
|
func (m *txSortedMap) Ready(start uint64) types.Transactions {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
m.cache = nil
|
|
|
|
|
|
|
|
return ready
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the transaction map.
|
|
|
|
func (m *txSortedMap) Len() int {
|
|
|
|
return len(m.items)
|
|
|
|
}
|
|
|
|
|
2020-07-14 22:42:32 +03:00
|
|
|
func (m *txSortedMap) flatten() types.Transactions {
|
2016-08-25 19:04:40 +03: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 22:42:32 +03: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.
|
|
|
|
func (m *txSortedMap) Flatten() types.Transactions {
|
2016-08-25 19:04:40 +03:00
|
|
|
// Copy the cache to prevent accidental modifications
|
2020-07-14 22:42:32 +03:00
|
|
|
cache := m.flatten()
|
|
|
|
txs := make(types.Transactions, len(cache))
|
|
|
|
copy(txs, cache)
|
2016-08-25 19:04:40 +03:00
|
|
|
return txs
|
|
|
|
}
|
|
|
|
|
2020-07-14 22:42:32 +03:00
|
|
|
// LastElement returns the last element of a flattened list, thus, the
|
|
|
|
// transaction with the highest nonce
|
|
|
|
func (m *txSortedMap) LastElement() *types.Transaction {
|
|
|
|
cache := m.flatten()
|
|
|
|
return cache[len(cache)-1]
|
|
|
|
}
|
|
|
|
|
2016-07-01 18:59:55 +03:00
|
|
|
// txList is a "list" of transactions belonging to an account, sorted by account
|
|
|
|
// 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 20:44:35 +03:00
|
|
|
// executable/future queue, with minor behavioral changes.
|
2016-07-01 18:59:55 +03:00
|
|
|
type txList struct {
|
2017-05-30 00:31:37 +03:00
|
|
|
strict bool // Whether nonces are strictly continuous or not
|
|
|
|
txs *txSortedMap // Heap indexed sorted hash map of the transactions
|
|
|
|
|
2020-07-09 15:02:03 +03: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)
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTxList create a new transaction list for maintaining nonce-indexable fast,
|
|
|
|
// gapped, sortable transaction lists.
|
|
|
|
func newTxList(strict bool) *txList {
|
|
|
|
return &txList{
|
2020-07-09 15:02:03 +03:00
|
|
|
strict: strict,
|
|
|
|
txs: newTxSortedMap(),
|
|
|
|
costcap: new(big.Int),
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Overlaps returns whether the transaction specified has the same nonce as one
|
|
|
|
// already contained within the list.
|
|
|
|
func (l *txList) Overlaps(tx *types.Transaction) bool {
|
|
|
|
return l.txs.Get(tx.Nonce()) != nil
|
|
|
|
}
|
|
|
|
|
2016-08-17 16:53:15 +03: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 18:59:55 +03:00
|
|
|
//
|
2017-05-30 00:31:37 +03:00
|
|
|
// If the new transaction is accepted into the list, the lists' cost and gas
|
|
|
|
// thresholds are also potentially updated.
|
2017-05-26 13:40:47 +03:00
|
|
|
func (l *txList) Add(tx *types.Transaction, priceBump uint64) (bool, *types.Transaction) {
|
2016-08-25 19:04:40 +03:00
|
|
|
// If there's an older better transaction, abort
|
|
|
|
old := l.txs.Get(tx.Nonce())
|
2017-05-16 22:07:27 +03:00
|
|
|
if old != nil {
|
2020-07-14 22:42:32 +03:00
|
|
|
// threshold = oldGP * (100 + priceBump) / 100
|
|
|
|
a := big.NewInt(100 + int64(priceBump))
|
|
|
|
a = a.Mul(a, old.GasPrice())
|
|
|
|
b := big.NewInt(100)
|
|
|
|
threshold := a.Div(a, b)
|
2017-10-30 14:05:00 +03:00
|
|
|
// Have to ensure that the new gas price is higher than the old gas
|
|
|
|
// price as well as checking the percentage threshold to ensure that
|
|
|
|
// this is accurate for low (Wei-level) gas price replacements
|
2020-06-30 12:59:06 +03:00
|
|
|
if old.GasPriceCmp(tx) >= 0 || tx.GasPriceIntCmp(threshold) < 0 {
|
2017-05-16 22:07:27 +03:00
|
|
|
return false, nil
|
|
|
|
}
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
2016-08-25 19:04:40 +03:00
|
|
|
// Otherwise overwrite the old transaction with the current one
|
|
|
|
l.txs.Put(tx)
|
2020-07-09 15:02:03 +03:00
|
|
|
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
|
2016-07-01 18:59:55 +03:00
|
|
|
l.costcap = cost
|
|
|
|
}
|
2017-11-13 14:47:27 +03:00
|
|
|
if gas := tx.Gas(); l.gascap < gas {
|
2017-05-30 00:31:37 +03:00
|
|
|
l.gascap = gas
|
|
|
|
}
|
2016-07-01 18:59:55 +03: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.
|
|
|
|
func (l *txList) Forward(threshold uint64) types.Transactions {
|
2016-08-25 19:04:40 +03:00
|
|
|
return l.txs.Forward(threshold)
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
|
2017-05-30 00:31:37 +03: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 18:59:55 +03:00
|
|
|
//
|
2017-05-30 00:31:37 +03: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.
|
2020-07-09 15:02:03 +03:00
|
|
|
func (l *txList) Filter(costLimit *big.Int, gasLimit uint64) (types.Transactions, types.Transactions) {
|
2016-08-17 16:53:15 +03:00
|
|
|
// If all transactions are below the threshold, short circuit
|
2020-07-09 15:02:03 +03:00
|
|
|
if l.costcap.Cmp(costLimit) <= 0 && l.gascap <= gasLimit {
|
2016-07-01 18:59:55 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-09 15:02:03 +03:00
|
|
|
l.costcap = new(big.Int).Set(costLimit) // Lower the caps to the thresholds
|
2017-11-13 14:47:27 +03:00
|
|
|
l.gascap = gasLimit
|
2016-07-01 18:59:55 +03:00
|
|
|
|
2016-08-25 19:04:40 +03:00
|
|
|
// Filter out all the transactions above the account's funds
|
2020-07-14 22:42:32 +03:00
|
|
|
removed := l.txs.Filter(func(tx *types.Transaction) bool {
|
|
|
|
return tx.Gas() > gasLimit || tx.Cost().Cmp(costLimit) > 0
|
|
|
|
})
|
2016-08-25 19:04:40 +03:00
|
|
|
|
2020-07-14 22:42:32 +03:00
|
|
|
if len(removed) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-09 15:02:03 +03:00
|
|
|
var invalids types.Transactions
|
2020-07-14 22:42:32 +03:00
|
|
|
// If the list was strict, filter anything above the lowest nonce
|
|
|
|
if l.strict {
|
2016-08-25 19:04:40 +03:00
|
|
|
lowest := uint64(math.MaxUint64)
|
|
|
|
for _, tx := range removed {
|
|
|
|
if nonce := tx.Nonce(); lowest > nonce {
|
|
|
|
lowest = nonce
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 22:42:32 +03:00
|
|
|
invalids = l.txs.filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest })
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
2020-07-14 22:42:32 +03:00
|
|
|
l.txs.reheap()
|
2016-07-01 18:59:55 +03:00
|
|
|
return removed, invalids
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cap places a hard limit on the number of items, returning all transactions
|
2016-08-17 16:53:15 +03:00
|
|
|
// exceeding that limit.
|
2016-07-01 18:59:55 +03:00
|
|
|
func (l *txList) Cap(threshold int) types.Transactions {
|
2016-08-25 19:04:40 +03:00
|
|
|
return l.txs.Cap(threshold)
|
2016-07-01 18:59:55 +03: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).
|
|
|
|
func (l *txList) Remove(tx *types.Transaction) (bool, types.Transactions) {
|
2016-08-25 19:04:40 +03:00
|
|
|
// Remove the transaction from the set
|
2016-07-01 18:59:55 +03:00
|
|
|
nonce := tx.Nonce()
|
2016-08-25 19:04:40 +03:00
|
|
|
if removed := l.txs.Remove(nonce); !removed {
|
|
|
|
return false, nil
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
2016-08-25 19:04:40 +03:00
|
|
|
// In strict mode, filter out non-executable transactions
|
|
|
|
if l.strict {
|
|
|
|
return true, l.txs.Filter(func(tx *types.Transaction) bool { return tx.Nonce() > nonce })
|
|
|
|
}
|
|
|
|
return true, nil
|
2016-07-01 18:59:55 +03: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 16:53:15 +03:00
|
|
|
// Note, all transactions with nonces lower than start will also be returned to
|
2016-07-01 18:59:55 +03:00
|
|
|
// prevent getting into and invalid state. This is not something that should ever
|
|
|
|
// happen but better to be self correcting than failing!
|
|
|
|
func (l *txList) Ready(start uint64) types.Transactions {
|
2016-08-25 19:04:40 +03:00
|
|
|
return l.txs.Ready(start)
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the transaction list.
|
|
|
|
func (l *txList) Len() int {
|
2016-08-25 19:04:40 +03:00
|
|
|
return l.txs.Len()
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns whether the list of transactions is empty or not.
|
|
|
|
func (l *txList) Empty() bool {
|
2016-08-25 19:04:40 +03:00
|
|
|
return l.Len() == 0
|
2016-07-01 18:59:55 +03: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.
|
|
|
|
func (l *txList) Flatten() types.Transactions {
|
2016-08-25 19:04:40 +03:00
|
|
|
return l.txs.Flatten()
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2020-07-14 22:42:32 +03:00
|
|
|
// LastElement returns the last element of a flattened list, thus, the
|
|
|
|
// transaction with the highest nonce
|
|
|
|
func (l *txList) LastElement() *types.Transaction {
|
|
|
|
return l.txs.LastElement()
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// priceHeap is a heap.Interface implementation over transactions for retrieving
|
|
|
|
// price-sorted transactions to discard when the pool fills up.
|
|
|
|
type priceHeap []*types.Transaction
|
|
|
|
|
2018-04-12 12:17:52 +03:00
|
|
|
func (h priceHeap) Len() int { return len(h) }
|
|
|
|
func (h priceHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
|
|
|
|
|
|
|
func (h priceHeap) Less(i, j int) bool {
|
|
|
|
// Sort primarily by price, returning the cheaper one
|
2020-06-30 12:59:06 +03:00
|
|
|
switch h[i].GasPriceCmp(h[j]) {
|
2018-04-12 12:17:52 +03:00
|
|
|
case -1:
|
|
|
|
return true
|
|
|
|
case 1:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// If the prices match, stabilize via nonces (high nonce is worse)
|
|
|
|
return h[i].Nonce() > h[j].Nonce()
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
|
|
|
|
func (h *priceHeap) Push(x interface{}) {
|
|
|
|
*h = append(*h, x.(*types.Transaction))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *priceHeap) Pop() interface{} {
|
|
|
|
old := *h
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
|
|
|
*h = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
// txPricedList is a price-sorted heap to allow operating on transactions pool
|
|
|
|
// contents in a price-incrementing way.
|
|
|
|
type txPricedList struct {
|
2018-05-23 15:55:42 +03:00
|
|
|
all *txLookup // Pointer to the map of all transactions
|
|
|
|
items *priceHeap // Heap of prices of all the stored transactions
|
|
|
|
stales int // Number of stale price points to (re-heap trigger)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTxPricedList creates a new price-sorted transaction heap.
|
2018-05-23 15:55:42 +03:00
|
|
|
func newTxPricedList(all *txLookup) *txPricedList {
|
2017-05-16 22:07:27 +03:00
|
|
|
return &txPricedList{
|
|
|
|
all: all,
|
|
|
|
items: new(priceHeap),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put inserts a new transaction into the heap.
|
|
|
|
func (l *txPricedList) Put(tx *types.Transaction) {
|
|
|
|
heap.Push(l.items, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-06-10 14:21:02 +03:00
|
|
|
func (l *txPricedList) Removed(count int) {
|
2017-05-16 22:07:27 +03:00
|
|
|
// Bump the stale counter, but exit if still too low (< 25%)
|
2019-06-10 14:21:02 +03:00
|
|
|
l.stales += count
|
2017-05-16 22:07:27 +03:00
|
|
|
if l.stales <= len(*l.items)/4 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Seems we've reached a critical number of stale transactions, reheap
|
2018-05-23 15:55:42 +03:00
|
|
|
reheap := make(priceHeap, 0, l.all.Count())
|
2017-05-16 22:07:27 +03:00
|
|
|
|
|
|
|
l.stales, l.items = 0, &reheap
|
2018-05-23 15:55:42 +03:00
|
|
|
l.all.Range(func(hash common.Hash, tx *types.Transaction) bool {
|
2017-10-25 12:18:44 +03:00
|
|
|
*l.items = append(*l.items, tx)
|
2018-05-23 15:55:42 +03:00
|
|
|
return true
|
|
|
|
})
|
2017-05-16 22:07:27 +03:00
|
|
|
heap.Init(l.items)
|
|
|
|
}
|
|
|
|
|
2017-07-05 16:51:55 +03:00
|
|
|
// Cap finds all the transactions below the given price threshold, drops them
|
2018-07-30 14:10:48 +03:00
|
|
|
// from the priced list and returns them for further removal from the entire pool.
|
2017-06-30 23:43:26 +03:00
|
|
|
func (l *txPricedList) Cap(threshold *big.Int, local *accountSet) types.Transactions {
|
2017-05-16 22:07:27 +03:00
|
|
|
drop := make(types.Transactions, 0, 128) // Remote underpriced transactions to drop
|
|
|
|
save := make(types.Transactions, 0, 64) // Local underpriced transactions to keep
|
|
|
|
|
|
|
|
for len(*l.items) > 0 {
|
|
|
|
// Discard stale transactions if found during cleanup
|
|
|
|
tx := heap.Pop(l.items).(*types.Transaction)
|
2018-05-23 15:55:42 +03:00
|
|
|
if l.all.Get(tx.Hash()) == nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
l.stales--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Stop the discards if we've reached the threshold
|
2020-06-30 12:59:06 +03:00
|
|
|
if tx.GasPriceIntCmp(threshold) >= 0 {
|
2017-09-04 12:48:36 +03:00
|
|
|
save = append(save, tx)
|
2017-05-16 22:07:27 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Non stale transaction found, discard unless local
|
2017-07-05 16:51:55 +03:00
|
|
|
if local.containsTx(tx) {
|
2017-05-16 22:07:27 +03:00
|
|
|
save = append(save, tx)
|
|
|
|
} else {
|
|
|
|
drop = append(drop, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tx := range save {
|
|
|
|
heap.Push(l.items, tx)
|
|
|
|
}
|
|
|
|
return drop
|
|
|
|
}
|
|
|
|
|
|
|
|
// Underpriced checks whether a transaction is cheaper than (or as cheap as) the
|
|
|
|
// lowest priced transaction currently being tracked.
|
2017-06-30 23:43:26 +03:00
|
|
|
func (l *txPricedList) Underpriced(tx *types.Transaction, local *accountSet) bool {
|
2017-05-16 22:07:27 +03:00
|
|
|
// Local transactions cannot be underpriced
|
2017-07-05 16:51:55 +03:00
|
|
|
if local.containsTx(tx) {
|
2017-05-16 22:07:27 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Discard stale price points if found at the heap start
|
|
|
|
for len(*l.items) > 0 {
|
|
|
|
head := []*types.Transaction(*l.items)[0]
|
2018-05-23 15:55:42 +03:00
|
|
|
if l.all.Get(head.Hash()) == nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
l.stales--
|
|
|
|
heap.Pop(l.items)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Check if the transaction is underpriced or not
|
|
|
|
if len(*l.items) == 0 {
|
|
|
|
log.Error("Pricing query for empty pool") // This cannot happen, print to catch programming errors
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cheapest := []*types.Transaction(*l.items)[0]
|
2020-06-30 12:59:06 +03:00
|
|
|
return cheapest.GasPriceCmp(tx) >= 0
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Discard finds a number of most underpriced transactions, removes them from the
|
2017-07-06 11:51:59 +03:00
|
|
|
// priced list and returns them for further removal from the entire pool.
|
2020-01-10 12:40:03 +03:00
|
|
|
func (l *txPricedList) Discard(slots int, local *accountSet) types.Transactions {
|
2020-07-14 22:42:32 +03:00
|
|
|
// If we have some local accountset, those will not be discarded
|
|
|
|
if !local.empty() {
|
|
|
|
// In case the list is filled to the brim with 'local' txs, we do this
|
|
|
|
// little check to avoid unpacking / repacking the heap later on, which
|
|
|
|
// is very expensive
|
|
|
|
discardable := 0
|
|
|
|
for _, tx := range *l.items {
|
|
|
|
if !local.containsTx(tx) {
|
|
|
|
discardable++
|
|
|
|
}
|
|
|
|
if discardable >= slots {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if slots > discardable {
|
|
|
|
slots = discardable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if slots == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
drop := make(types.Transactions, 0, slots) // Remote underpriced transactions to drop
|
|
|
|
save := make(types.Transactions, 0, len(*l.items)-slots) // Local underpriced transactions to keep
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
for len(*l.items) > 0 && slots > 0 {
|
2017-05-16 22:07:27 +03:00
|
|
|
// Discard stale transactions if found during cleanup
|
|
|
|
tx := heap.Pop(l.items).(*types.Transaction)
|
2018-05-23 15:55:42 +03:00
|
|
|
if l.all.Get(tx.Hash()) == nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
l.stales--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Non stale transaction found, discard unless local
|
2017-07-05 16:51:55 +03:00
|
|
|
if local.containsTx(tx) {
|
2017-05-16 22:07:27 +03:00
|
|
|
save = append(save, tx)
|
|
|
|
} else {
|
|
|
|
drop = append(drop, tx)
|
2020-01-10 12:40:03 +03:00
|
|
|
slots -= numSlots(tx)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tx := range save {
|
|
|
|
heap.Push(l.items, tx)
|
|
|
|
}
|
|
|
|
return drop
|
|
|
|
}
|