2023-07-27 13:45:35 +03:00
|
|
|
// Copyright 2014 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 miner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2024-02-19 15:59:40 +02:00
|
|
|
"github.com/holiman/uint256"
|
2023-07-27 13:45:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// txWithMinerFee wraps a transaction with its gas price or effective miner gasTipCap
|
|
|
|
type txWithMinerFee struct {
|
|
|
|
tx *txpool.LazyTransaction
|
|
|
|
from common.Address
|
2024-02-19 15:59:40 +02:00
|
|
|
fees *uint256.Int
|
2023-07-27 13:45:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTxWithMinerFee creates a wrapped transaction, calculating the effective
|
|
|
|
// miner gasTipCap if a base fee is provided.
|
|
|
|
// Returns error in case of a negative effective miner gasTipCap.
|
2024-02-19 15:59:40 +02:00
|
|
|
func newTxWithMinerFee(tx *txpool.LazyTransaction, from common.Address, baseFee *uint256.Int) (*txWithMinerFee, error) {
|
|
|
|
tip := new(uint256.Int).Set(tx.GasTipCap)
|
2023-07-27 13:45:35 +03:00
|
|
|
if baseFee != nil {
|
|
|
|
if tx.GasFeeCap.Cmp(baseFee) < 0 {
|
|
|
|
return nil, types.ErrGasFeeCapTooLow
|
|
|
|
}
|
2024-02-19 15:59:40 +02:00
|
|
|
tip = new(uint256.Int).Sub(tx.GasFeeCap, baseFee)
|
|
|
|
if tip.Gt(tx.GasTipCap) {
|
|
|
|
tip = tx.GasTipCap
|
|
|
|
}
|
2023-07-27 13:45:35 +03:00
|
|
|
}
|
|
|
|
return &txWithMinerFee{
|
|
|
|
tx: tx,
|
|
|
|
from: from,
|
|
|
|
fees: tip,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txByPriceAndTime implements both the sort and the heap interface, making it useful
|
|
|
|
// for all at once sorting as well as individually adding and removing elements.
|
|
|
|
type txByPriceAndTime []*txWithMinerFee
|
|
|
|
|
|
|
|
func (s txByPriceAndTime) Len() int { return len(s) }
|
|
|
|
func (s txByPriceAndTime) Less(i, j int) bool {
|
|
|
|
// If the prices are equal, use the time the transaction was first seen for
|
|
|
|
// deterministic sorting
|
|
|
|
cmp := s[i].fees.Cmp(s[j].fees)
|
|
|
|
if cmp == 0 {
|
|
|
|
return s[i].tx.Time.Before(s[j].tx.Time)
|
|
|
|
}
|
|
|
|
return cmp > 0
|
|
|
|
}
|
|
|
|
func (s txByPriceAndTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
func (s *txByPriceAndTime) Push(x interface{}) {
|
|
|
|
*s = append(*s, x.(*txWithMinerFee))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *txByPriceAndTime) Pop() interface{} {
|
|
|
|
old := *s
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
|
|
|
old[n-1] = nil
|
|
|
|
*s = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
// transactionsByPriceAndNonce represents a set of transactions that can return
|
|
|
|
// transactions in a profit-maximizing sorted order, while supporting removing
|
|
|
|
// entire batches of transactions for non-executable accounts.
|
|
|
|
type transactionsByPriceAndNonce struct {
|
|
|
|
txs map[common.Address][]*txpool.LazyTransaction // Per account nonce-sorted list of transactions
|
|
|
|
heads txByPriceAndTime // Next transaction for each unique account (price heap)
|
|
|
|
signer types.Signer // Signer for the set of transactions
|
2024-02-19 15:59:40 +02:00
|
|
|
baseFee *uint256.Int // Current base fee
|
2023-07-27 13:45:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTransactionsByPriceAndNonce creates a transaction set that can retrieve
|
|
|
|
// price sorted transactions in a nonce-honouring way.
|
|
|
|
//
|
|
|
|
// Note, the input map is reowned so the caller should not interact any more with
|
|
|
|
// if after providing it to the constructor.
|
|
|
|
func newTransactionsByPriceAndNonce(signer types.Signer, txs map[common.Address][]*txpool.LazyTransaction, baseFee *big.Int) *transactionsByPriceAndNonce {
|
2024-02-19 15:59:40 +02:00
|
|
|
// Convert the basefee from header format to uint256 format
|
|
|
|
var baseFeeUint *uint256.Int
|
|
|
|
if baseFee != nil {
|
|
|
|
baseFeeUint = uint256.MustFromBig(baseFee)
|
|
|
|
}
|
2023-07-27 13:45:35 +03:00
|
|
|
// Initialize a price and received time based heap with the head transactions
|
|
|
|
heads := make(txByPriceAndTime, 0, len(txs))
|
|
|
|
for from, accTxs := range txs {
|
2024-02-19 15:59:40 +02:00
|
|
|
wrapped, err := newTxWithMinerFee(accTxs[0], from, baseFeeUint)
|
2023-07-27 13:45:35 +03:00
|
|
|
if err != nil {
|
|
|
|
delete(txs, from)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
heads = append(heads, wrapped)
|
|
|
|
txs[from] = accTxs[1:]
|
|
|
|
}
|
|
|
|
heap.Init(&heads)
|
|
|
|
|
|
|
|
// Assemble and return the transaction set
|
|
|
|
return &transactionsByPriceAndNonce{
|
|
|
|
txs: txs,
|
|
|
|
heads: heads,
|
|
|
|
signer: signer,
|
2024-02-19 15:59:40 +02:00
|
|
|
baseFee: baseFeeUint,
|
2023-07-27 13:45:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 17:46:08 +08:00
|
|
|
// Copy copys a new TransactionsPriceAndNonce with the same *transaction
|
|
|
|
func (t *transactionsByPriceAndNonce) Copy() *transactionsByPriceAndNonce {
|
|
|
|
heads := make([]*txWithMinerFee, len(t.heads))
|
|
|
|
copy(heads, t.heads)
|
|
|
|
txs := make(map[common.Address][]*txpool.LazyTransaction, len(t.txs))
|
|
|
|
for acc, txsTmp := range t.txs {
|
|
|
|
txs[acc] = txsTmp
|
|
|
|
}
|
2024-03-08 15:36:25 +08:00
|
|
|
var baseFee uint256.Int
|
2023-09-12 16:54:02 +08:00
|
|
|
if t.baseFee != nil {
|
2024-03-08 15:36:25 +08:00
|
|
|
baseFee = *t.baseFee
|
2023-09-12 16:54:02 +08:00
|
|
|
}
|
2023-08-23 17:46:08 +08:00
|
|
|
return &transactionsByPriceAndNonce{
|
|
|
|
heads: heads,
|
|
|
|
txs: txs,
|
|
|
|
signer: t.signer,
|
2024-03-08 15:36:25 +08:00
|
|
|
baseFee: &baseFee,
|
2023-08-23 17:46:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:45:35 +03:00
|
|
|
// Peek returns the next transaction by price.
|
2024-02-19 15:59:40 +02:00
|
|
|
func (t *transactionsByPriceAndNonce) Peek() (*txpool.LazyTransaction, *uint256.Int) {
|
2023-07-27 13:45:35 +03:00
|
|
|
if len(t.heads) == 0 {
|
2024-02-06 10:59:24 +02:00
|
|
|
return nil, nil
|
2023-07-27 13:45:35 +03:00
|
|
|
}
|
2024-02-06 10:59:24 +02:00
|
|
|
return t.heads[0].tx, t.heads[0].fees
|
2023-07-27 13:45:35 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:46:08 +08:00
|
|
|
// Peek returns the next transaction by price.
|
|
|
|
func (t *transactionsByPriceAndNonce) PeekWithUnwrap() *types.Transaction {
|
|
|
|
if len(t.heads) > 0 && t.heads[0].tx != nil && t.heads[0].tx.Resolve() != nil {
|
2024-02-02 15:43:33 +08:00
|
|
|
return t.heads[0].tx.Tx
|
2023-08-23 17:46:08 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:45:35 +03:00
|
|
|
// Shift replaces the current best head with the next one from the same account.
|
|
|
|
func (t *transactionsByPriceAndNonce) Shift() {
|
|
|
|
acc := t.heads[0].from
|
|
|
|
if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
|
|
|
|
if wrapped, err := newTxWithMinerFee(txs[0], acc, t.baseFee); err == nil {
|
|
|
|
t.heads[0], t.txs[acc] = wrapped, txs[1:]
|
|
|
|
heap.Fix(&t.heads, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
heap.Pop(&t.heads)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop removes the best transaction, *not* replacing it with the next one from
|
|
|
|
// the same account. This should be used when a transaction cannot be executed
|
|
|
|
// and hence all subsequent ones should be discarded from the same account.
|
|
|
|
func (t *transactionsByPriceAndNonce) Pop() {
|
|
|
|
heap.Pop(&t.heads)
|
|
|
|
}
|
2023-08-23 17:46:08 +08:00
|
|
|
|
2024-02-20 11:37:23 +02:00
|
|
|
// Empty returns if the price heap is empty. It can be used to check it simpler
|
|
|
|
// than calling peek and checking for nil return.
|
|
|
|
func (t *transactionsByPriceAndNonce) Empty() bool {
|
|
|
|
return len(t.heads) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear removes the entire content of the heap.
|
|
|
|
func (t *transactionsByPriceAndNonce) Clear() {
|
|
|
|
t.heads, t.txs = nil, nil
|
|
|
|
}
|
2024-03-08 15:36:25 +08:00
|
|
|
|
2023-08-23 17:46:08 +08:00
|
|
|
func (t *transactionsByPriceAndNonce) CurrentSize() int {
|
|
|
|
return len(t.heads)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward moves current transaction to be the one which is one index after tx
|
|
|
|
func (t *transactionsByPriceAndNonce) Forward(tx *types.Transaction) {
|
|
|
|
if tx == nil {
|
|
|
|
if len(t.heads) > 0 {
|
|
|
|
t.heads = t.heads[0:0]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//check whether target tx exists in t.heads
|
|
|
|
for _, head := range t.heads {
|
|
|
|
if head.tx != nil && head.tx.Resolve() != nil {
|
2024-02-02 15:43:33 +08:00
|
|
|
if tx == head.tx.Tx {
|
2023-08-23 17:46:08 +08:00
|
|
|
//shift t to the position one after tx
|
|
|
|
txTmp := t.PeekWithUnwrap()
|
|
|
|
for txTmp != tx {
|
|
|
|
t.Shift()
|
|
|
|
txTmp = t.PeekWithUnwrap()
|
|
|
|
}
|
|
|
|
t.Shift()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//get the sender address of tx
|
|
|
|
acc, _ := types.Sender(t.signer, tx)
|
|
|
|
//check whether target tx exists in t.txs
|
|
|
|
if txs, ok := t.txs[acc]; ok {
|
|
|
|
for _, txLazyTmp := range txs {
|
|
|
|
if txLazyTmp != nil && txLazyTmp.Resolve() != nil {
|
|
|
|
//found the same pointer in t.txs as tx and then shift t to the position one after tx
|
2024-02-02 15:43:33 +08:00
|
|
|
if tx == txLazyTmp.Tx {
|
2023-08-23 17:46:08 +08:00
|
|
|
txTmp := t.PeekWithUnwrap()
|
|
|
|
for txTmp != tx {
|
|
|
|
t.Shift()
|
|
|
|
txTmp = t.PeekWithUnwrap()
|
|
|
|
}
|
|
|
|
t.Shift()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|