github.com/ethereum/go-ethereum@v1.16.1/core/txpool/subpool.go (about)

     1  // Copyright 2023 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package txpool
    18  
    19  import (
    20  	"math/big"
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/event"
    27  	"github.com/holiman/uint256"
    28  )
    29  
    30  // LazyTransaction contains a small subset of the transaction properties that is
    31  // enough for the miner and other APIs to handle large batches of transactions;
    32  // and supports pulling up the entire transaction when really needed.
    33  type LazyTransaction struct {
    34  	Pool LazyResolver       // Transaction resolver to pull the real transaction up
    35  	Hash common.Hash        // Transaction hash to pull up if needed
    36  	Tx   *types.Transaction // Transaction if already resolved
    37  
    38  	Time      time.Time    // Time when the transaction was first seen
    39  	GasFeeCap *uint256.Int // Maximum fee per gas the transaction may consume
    40  	GasTipCap *uint256.Int // Maximum miner tip per gas the transaction can pay
    41  
    42  	Gas     uint64 // Amount of gas required by the transaction
    43  	BlobGas uint64 // Amount of blob gas required by the transaction
    44  }
    45  
    46  // Resolve retrieves the full transaction belonging to a lazy handle if it is still
    47  // maintained by the transaction pool.
    48  //
    49  // Note, the method will *not* cache the retrieved transaction if the original
    50  // pool has not cached it. The idea being, that if the tx was too big to insert
    51  // originally, silently saving it will cause more trouble down the line (and
    52  // indeed seems to have caused a memory bloat in the original implementation
    53  // which did just that).
    54  func (ltx *LazyTransaction) Resolve() *types.Transaction {
    55  	if ltx.Tx != nil {
    56  		return ltx.Tx
    57  	}
    58  	return ltx.Pool.Get(ltx.Hash)
    59  }
    60  
    61  // LazyResolver is a minimal interface needed for a transaction pool to satisfy
    62  // resolving lazy transactions. It's mostly a helper to avoid the entire sub-
    63  // pool being injected into the lazy transaction.
    64  type LazyResolver interface {
    65  	// Get returns a transaction if it is contained in the pool, or nil otherwise.
    66  	Get(hash common.Hash) *types.Transaction
    67  }
    68  
    69  // PendingFilter is a collection of filter rules to allow retrieving a subset
    70  // of transactions for announcement or mining.
    71  //
    72  // Note, the entries here are not arbitrary useful filters, rather each one has
    73  // a very specific call site in mind and each one can be evaluated very cheaply
    74  // by the pool implementations. Only add new ones that satisfy those constraints.
    75  type PendingFilter struct {
    76  	MinTip  *uint256.Int // Minimum miner tip required to include a transaction
    77  	BaseFee *uint256.Int // Minimum 1559 basefee needed to include a transaction
    78  	BlobFee *uint256.Int // Minimum 4844 blobfee needed to include a blob transaction
    79  
    80  	OnlyPlainTxs bool // Return only plain EVM transactions (peer-join announces, block space filling)
    81  	OnlyBlobTxs  bool // Return only blob transactions (block blob-space filling)
    82  }
    83  
    84  // TxMetadata denotes the metadata of a transaction.
    85  type TxMetadata struct {
    86  	Type uint8  // The type of the transaction
    87  	Size uint64 // The length of the 'rlp encoding' of a transaction
    88  }
    89  
    90  // SubPool represents a specialized transaction pool that lives on its own (e.g.
    91  // blob pool). Since independent of how many specialized pools we have, they do
    92  // need to be updated in lockstep and assemble into one coherent view for block
    93  // production, this interface defines the common methods that allow the primary
    94  // transaction pool to manage the subpools.
    95  type SubPool interface {
    96  	// Filter is a selector used to decide whether a transaction would be added
    97  	// to this particular subpool.
    98  	Filter(tx *types.Transaction) bool
    99  
   100  	// Init sets the base parameters of the subpool, allowing it to load any saved
   101  	// transactions from disk and also permitting internal maintenance routines to
   102  	// start up.
   103  	//
   104  	// These should not be passed as a constructor argument - nor should the pools
   105  	// start by themselves - in order to keep multiple subpools in lockstep with
   106  	// one another.
   107  	Init(gasTip uint64, head *types.Header, reserver Reserver) error
   108  
   109  	// Close terminates any background processing threads and releases any held
   110  	// resources.
   111  	Close() error
   112  
   113  	// Reset retrieves the current state of the blockchain and ensures the content
   114  	// of the transaction pool is valid with regard to the chain state.
   115  	Reset(oldHead, newHead *types.Header)
   116  
   117  	// SetGasTip updates the minimum price required by the subpool for a new
   118  	// transaction, and drops all transactions below this threshold.
   119  	SetGasTip(tip *big.Int)
   120  
   121  	// Has returns an indicator whether subpool has a transaction cached with the
   122  	// given hash.
   123  	Has(hash common.Hash) bool
   124  
   125  	// Get returns a transaction if it is contained in the pool, or nil otherwise.
   126  	Get(hash common.Hash) *types.Transaction
   127  
   128  	// GetRLP returns a RLP-encoded transaction if it is contained in the pool.
   129  	GetRLP(hash common.Hash) []byte
   130  
   131  	// GetMetadata returns the transaction type and transaction size with the
   132  	// given transaction hash.
   133  	GetMetadata(hash common.Hash) *TxMetadata
   134  
   135  	// ValidateTxBasics checks whether a transaction is valid according to the consensus
   136  	// rules, but does not check state-dependent validation such as sufficient balance.
   137  	// This check is meant as a static check which can be performed without holding the
   138  	// pool mutex.
   139  	ValidateTxBasics(tx *types.Transaction) error
   140  
   141  	// Add enqueues a batch of transactions into the pool if they are valid. Due
   142  	// to the large transaction churn, add may postpone fully integrating the tx
   143  	// to a later point to batch multiple ones together.
   144  	Add(txs []*types.Transaction, sync bool) []error
   145  
   146  	// Pending retrieves all currently processable transactions, grouped by origin
   147  	// account and sorted by nonce.
   148  	//
   149  	// The transactions can also be pre-filtered by the dynamic fee components to
   150  	// reduce allocations and load on downstream subsystems.
   151  	Pending(filter PendingFilter) map[common.Address][]*LazyTransaction
   152  
   153  	// SubscribeTransactions subscribes to new transaction events. The subscriber
   154  	// can decide whether to receive notifications only for newly seen transactions
   155  	// or also for reorged out ones.
   156  	SubscribeTransactions(ch chan<- core.NewTxsEvent, reorgs bool) event.Subscription
   157  
   158  	// Nonce returns the next nonce of an account, with all transactions executable
   159  	// by the pool already applied on top.
   160  	Nonce(addr common.Address) uint64
   161  
   162  	// Stats retrieves the current pool stats, namely the number of pending and the
   163  	// number of queued (non-executable) transactions.
   164  	Stats() (int, int)
   165  
   166  	// Content retrieves the data content of the transaction pool, returning all the
   167  	// pending as well as queued transactions, grouped by account and sorted by nonce.
   168  	Content() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction)
   169  
   170  	// ContentFrom retrieves the data content of the transaction pool, returning the
   171  	// pending as well as queued transactions of this address, grouped by nonce.
   172  	ContentFrom(addr common.Address) ([]*types.Transaction, []*types.Transaction)
   173  
   174  	// Status returns the known status (unknown/pending/queued) of a transaction
   175  	// identified by their hashes.
   176  	Status(hash common.Hash) TxStatus
   177  
   178  	// Clear removes all tracked transactions from the pool
   179  	Clear()
   180  }