gitee.com/liu-zhao234568/cntest@v1.0.0/core/tx_pool.go (about)

     1  // Copyright 2014 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 core
    18  
    19  import (
    20  	"errors"
    21  	"math"
    22  	"math/big"
    23  	"sort"
    24  	"sync"
    25  	"time"
    26  
    27  	"gitee.com/liu-zhao234568/cntest/common"
    28  	"gitee.com/liu-zhao234568/cntest/common/prque"
    29  	"gitee.com/liu-zhao234568/cntest/consensus/misc"
    30  	"gitee.com/liu-zhao234568/cntest/core/state"
    31  	"gitee.com/liu-zhao234568/cntest/core/types"
    32  	"gitee.com/liu-zhao234568/cntest/event"
    33  	"gitee.com/liu-zhao234568/cntest/log"
    34  	"gitee.com/liu-zhao234568/cntest/metrics"
    35  	"gitee.com/liu-zhao234568/cntest/params"
    36  )
    37  
    38  const (
    39  	// chainHeadChanSize is the size of channel listening to ChainHeadEvent.
    40  	chainHeadChanSize = 10
    41  
    42  	// txSlotSize is used to calculate how many data slots a single transaction
    43  	// takes up based on its size. The slots are used as DoS protection, ensuring
    44  	// that validating a new transaction remains a constant operation (in reality
    45  	// O(maxslots), where max slots are 4 currently).
    46  	txSlotSize = 32 * 1024
    47  
    48  	// txMaxSize is the maximum size a single transaction can have. This field has
    49  	// non-trivial consequences: larger transactions are significantly harder and
    50  	// more expensive to propagate; larger transactions also take more resources
    51  	// to validate whether they fit into the pool or not.
    52  	txMaxSize = 4 * txSlotSize // 128KB
    53  )
    54  
    55  var (
    56  	// ErrAlreadyKnown is returned if the transactions is already contained
    57  	// within the pool.
    58  	ErrAlreadyKnown = errors.New("already known")
    59  
    60  	// ErrInvalidSender is returned if the transaction contains an invalid signature.
    61  	ErrInvalidSender = errors.New("invalid sender")
    62  
    63  	// ErrUnderpriced is returned if a transaction's gas price is below the minimum
    64  	// configured for the transaction pool.
    65  	ErrUnderpriced = errors.New("transaction underpriced")
    66  
    67  	// ErrTxPoolOverflow is returned if the transaction pool is full and can't accpet
    68  	// another remote transaction.
    69  	ErrTxPoolOverflow = errors.New("txpool is full")
    70  
    71  	// ErrReplaceUnderpriced is returned if a transaction is attempted to be replaced
    72  	// with a different one without the required price bump.
    73  	ErrReplaceUnderpriced = errors.New("replacement transaction underpriced")
    74  
    75  	// ErrGasLimit is returned if a transaction's requested gas limit exceeds the
    76  	// maximum allowance of the current block.
    77  	ErrGasLimit = errors.New("exceeds block gas limit")
    78  
    79  	// ErrNegativeValue is a sanity error to ensure no one is able to specify a
    80  	// transaction with a negative value.
    81  	ErrNegativeValue = errors.New("negative value")
    82  
    83  	// ErrOversizedData is returned if the input data of a transaction is greater
    84  	// than some meaningful limit a user might use. This is not a consensus error
    85  	// making the transaction invalid, rather a DOS protection.
    86  	ErrOversizedData = errors.New("oversized data")
    87  )
    88  
    89  var (
    90  	evictionInterval    = time.Minute     // Time interval to check for evictable transactions
    91  	statsReportInterval = 8 * time.Second // Time interval to report transaction pool stats
    92  )
    93  
    94  var (
    95  	// Metrics for the pending pool
    96  	pendingDiscardMeter   = metrics.NewRegisteredMeter("txpool/pending/discard", nil)
    97  	pendingReplaceMeter   = metrics.NewRegisteredMeter("txpool/pending/replace", nil)
    98  	pendingRateLimitMeter = metrics.NewRegisteredMeter("txpool/pending/ratelimit", nil) // Dropped due to rate limiting
    99  	pendingNofundsMeter   = metrics.NewRegisteredMeter("txpool/pending/nofunds", nil)   // Dropped due to out-of-funds
   100  
   101  	// Metrics for the queued pool
   102  	queuedDiscardMeter   = metrics.NewRegisteredMeter("txpool/queued/discard", nil)
   103  	queuedReplaceMeter   = metrics.NewRegisteredMeter("txpool/queued/replace", nil)
   104  	queuedRateLimitMeter = metrics.NewRegisteredMeter("txpool/queued/ratelimit", nil) // Dropped due to rate limiting
   105  	queuedNofundsMeter   = metrics.NewRegisteredMeter("txpool/queued/nofunds", nil)   // Dropped due to out-of-funds
   106  	queuedEvictionMeter  = metrics.NewRegisteredMeter("txpool/queued/eviction", nil)  // Dropped due to lifetime
   107  
   108  	// General tx metrics
   109  	knownTxMeter       = metrics.NewRegisteredMeter("txpool/known", nil)
   110  	validTxMeter       = metrics.NewRegisteredMeter("txpool/valid", nil)
   111  	invalidTxMeter     = metrics.NewRegisteredMeter("txpool/invalid", nil)
   112  	underpricedTxMeter = metrics.NewRegisteredMeter("txpool/underpriced", nil)
   113  	overflowedTxMeter  = metrics.NewRegisteredMeter("txpool/overflowed", nil)
   114  
   115  	pendingGauge = metrics.NewRegisteredGauge("txpool/pending", nil)
   116  	queuedGauge  = metrics.NewRegisteredGauge("txpool/queued", nil)
   117  	localGauge   = metrics.NewRegisteredGauge("txpool/local", nil)
   118  	slotsGauge   = metrics.NewRegisteredGauge("txpool/slots", nil)
   119  
   120  	reheapTimer = metrics.NewRegisteredTimer("txpool/reheap", nil)
   121  )
   122  
   123  // TxStatus is the current status of a transaction as seen by the pool.
   124  type TxStatus uint
   125  
   126  const (
   127  	TxStatusUnknown TxStatus = iota
   128  	TxStatusQueued
   129  	TxStatusPending
   130  	TxStatusIncluded
   131  )
   132  
   133  // blockChain provides the state of blockchain and current gas limit to do
   134  // some pre checks in tx pool and event subscribers.
   135  type blockChain interface {
   136  	CurrentBlock() *types.Block
   137  	GetBlock(hash common.Hash, number uint64) *types.Block
   138  	StateAt(root common.Hash) (*state.StateDB, error)
   139  
   140  	SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription
   141  }
   142  
   143  // TxPoolConfig are the configuration parameters of the transaction pool.
   144  type TxPoolConfig struct {
   145  	Locals    []common.Address // Addresses that should be treated by default as local
   146  	NoLocals  bool             // Whether local transaction handling should be disabled
   147  	Journal   string           // Journal of local transactions to survive node restarts
   148  	Rejournal time.Duration    // Time interval to regenerate the local transaction journal
   149  
   150  	PriceLimit uint64 // Minimum gas price to enforce for acceptance into the pool
   151  	PriceBump  uint64 // Minimum price bump percentage to replace an already existing transaction (nonce)
   152  
   153  	AccountSlots uint64 // Number of executable transaction slots guaranteed per account
   154  	GlobalSlots  uint64 // Maximum number of executable transaction slots for all accounts
   155  	AccountQueue uint64 // Maximum number of non-executable transaction slots permitted per account
   156  	GlobalQueue  uint64 // Maximum number of non-executable transaction slots for all accounts
   157  
   158  	Lifetime time.Duration // Maximum amount of time non-executable transaction are queued
   159  }
   160  
   161  // DefaultTxPoolConfig contains the default configurations for the transaction
   162  // pool.
   163  var DefaultTxPoolConfig = TxPoolConfig{
   164  	Journal:   "transactions.rlp",
   165  	Rejournal: time.Hour,
   166  
   167  	PriceLimit: 1,
   168  	PriceBump:  10,
   169  
   170  	AccountSlots: 16,
   171  	GlobalSlots:  4096 + 1024, // urgent + floating queue capacity with 4:1 ratio
   172  	AccountQueue: 64,
   173  	GlobalQueue:  1024,
   174  
   175  	Lifetime: 3 * time.Hour,
   176  }
   177  
   178  // sanitize checks the provided user configurations and changes anything that's
   179  // unreasonable or unworkable.
   180  func (config *TxPoolConfig) sanitize() TxPoolConfig {
   181  	conf := *config
   182  	if conf.Rejournal < time.Second {
   183  		log.Warn("Sanitizing invalid txpool journal time", "provided", conf.Rejournal, "updated", time.Second)
   184  		conf.Rejournal = time.Second
   185  	}
   186  	if conf.PriceLimit < 1 {
   187  		log.Warn("Sanitizing invalid txpool price limit", "provided", conf.PriceLimit, "updated", DefaultTxPoolConfig.PriceLimit)
   188  		conf.PriceLimit = DefaultTxPoolConfig.PriceLimit
   189  	}
   190  	if conf.PriceBump < 1 {
   191  		log.Warn("Sanitizing invalid txpool price bump", "provided", conf.PriceBump, "updated", DefaultTxPoolConfig.PriceBump)
   192  		conf.PriceBump = DefaultTxPoolConfig.PriceBump
   193  	}
   194  	if conf.AccountSlots < 1 {
   195  		log.Warn("Sanitizing invalid txpool account slots", "provided", conf.AccountSlots, "updated", DefaultTxPoolConfig.AccountSlots)
   196  		conf.AccountSlots = DefaultTxPoolConfig.AccountSlots
   197  	}
   198  	if conf.GlobalSlots < 1 {
   199  		log.Warn("Sanitizing invalid txpool global slots", "provided", conf.GlobalSlots, "updated", DefaultTxPoolConfig.GlobalSlots)
   200  		conf.GlobalSlots = DefaultTxPoolConfig.GlobalSlots
   201  	}
   202  	if conf.AccountQueue < 1 {
   203  		log.Warn("Sanitizing invalid txpool account queue", "provided", conf.AccountQueue, "updated", DefaultTxPoolConfig.AccountQueue)
   204  		conf.AccountQueue = DefaultTxPoolConfig.AccountQueue
   205  	}
   206  	if conf.GlobalQueue < 1 {
   207  		log.Warn("Sanitizing invalid txpool global queue", "provided", conf.GlobalQueue, "updated", DefaultTxPoolConfig.GlobalQueue)
   208  		conf.GlobalQueue = DefaultTxPoolConfig.GlobalQueue
   209  	}
   210  	if conf.Lifetime < 1 {
   211  		log.Warn("Sanitizing invalid txpool lifetime", "provided", conf.Lifetime, "updated", DefaultTxPoolConfig.Lifetime)
   212  		conf.Lifetime = DefaultTxPoolConfig.Lifetime
   213  	}
   214  	return conf
   215  }
   216  
   217  // TxPool contains all currently known transactions. Transactions
   218  // enter the pool when they are received from the network or submitted
   219  // locally. They exit the pool when they are included in the blockchain.
   220  //
   221  // The pool separates processable transactions (which can be applied to the
   222  // current state) and future transactions. Transactions move between those
   223  // two states over time as they are received and processed.
   224  type TxPool struct {
   225  	config      TxPoolConfig
   226  	chainconfig *params.ChainConfig
   227  	chain       blockChain
   228  	gasPrice    *big.Int
   229  	txFeed      event.Feed
   230  	scope       event.SubscriptionScope
   231  	signer      types.Signer
   232  	mu          sync.RWMutex
   233  
   234  	istanbul bool // Fork indicator whether we are in the istanbul stage.
   235  	eip2718  bool // Fork indicator whether we are using EIP-2718 type transactions.
   236  	eip1559  bool // Fork indicator whether we are using EIP-1559 type transactions.
   237  
   238  	currentState  *state.StateDB // Current state in the blockchain head
   239  	pendingNonces *txNoncer      // Pending state tracking virtual nonces
   240  	currentMaxGas uint64         // Current gas limit for transaction caps
   241  
   242  	locals  *accountSet // Set of local transaction to exempt from eviction rules
   243  	journal *txJournal  // Journal of local transaction to back up to disk
   244  
   245  	pending map[common.Address]*txList   // All currently processable transactions
   246  	queue   map[common.Address]*txList   // Queued but non-processable transactions
   247  	beats   map[common.Address]time.Time // Last heartbeat from each known account
   248  	all     *txLookup                    // All transactions to allow lookups
   249  	priced  *txPricedList                // All transactions sorted by price
   250  
   251  	chainHeadCh     chan ChainHeadEvent
   252  	chainHeadSub    event.Subscription
   253  	reqResetCh      chan *txpoolResetRequest
   254  	reqPromoteCh    chan *accountSet
   255  	queueTxEventCh  chan *types.Transaction
   256  	reorgDoneCh     chan chan struct{}
   257  	reorgShutdownCh chan struct{}  // requests shutdown of scheduleReorgLoop
   258  	wg              sync.WaitGroup // tracks loop, scheduleReorgLoop
   259  }
   260  
   261  type txpoolResetRequest struct {
   262  	oldHead, newHead *types.Header
   263  }
   264  
   265  // NewTxPool creates a new transaction pool to gather, sort and filter inbound
   266  // transactions from the network.
   267  func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain blockChain) *TxPool {
   268  	// Sanitize the input to ensure no vulnerable gas prices are set
   269  	config = (&config).sanitize()
   270  
   271  	// Create the transaction pool with its initial settings
   272  	pool := &TxPool{
   273  		config:          config,
   274  		chainconfig:     chainconfig,
   275  		chain:           chain,
   276  		signer:          types.LatestSigner(chainconfig),
   277  		pending:         make(map[common.Address]*txList),
   278  		queue:           make(map[common.Address]*txList),
   279  		beats:           make(map[common.Address]time.Time),
   280  		all:             newTxLookup(),
   281  		chainHeadCh:     make(chan ChainHeadEvent, chainHeadChanSize),
   282  		reqResetCh:      make(chan *txpoolResetRequest),
   283  		reqPromoteCh:    make(chan *accountSet),
   284  		queueTxEventCh:  make(chan *types.Transaction),
   285  		reorgDoneCh:     make(chan chan struct{}),
   286  		reorgShutdownCh: make(chan struct{}),
   287  		gasPrice:        new(big.Int).SetUint64(config.PriceLimit),
   288  	}
   289  	pool.locals = newAccountSet(pool.signer)
   290  	for _, addr := range config.Locals {
   291  		log.Info("Setting new local account", "address", addr)
   292  		pool.locals.add(addr)
   293  	}
   294  	pool.priced = newTxPricedList(pool.all)
   295  	pool.reset(nil, chain.CurrentBlock().Header())
   296  
   297  	// Start the reorg loop early so it can handle requests generated during journal loading.
   298  	pool.wg.Add(1)
   299  	go pool.scheduleReorgLoop()
   300  
   301  	// If local transactions and journaling is enabled, load from disk
   302  	if !config.NoLocals && config.Journal != "" {
   303  		pool.journal = newTxJournal(config.Journal)
   304  
   305  		if err := pool.journal.load(pool.AddLocals); err != nil {
   306  			log.Warn("Failed to load transaction journal", "err", err)
   307  		}
   308  		if err := pool.journal.rotate(pool.local()); err != nil {
   309  			log.Warn("Failed to rotate transaction journal", "err", err)
   310  		}
   311  	}
   312  
   313  	// Subscribe events from blockchain and start the main event loop.
   314  	pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh)
   315  	pool.wg.Add(1)
   316  	go pool.loop()
   317  
   318  	return pool
   319  }
   320  
   321  // loop is the transaction pool's main event loop, waiting for and reacting to
   322  // outside blockchain events as well as for various reporting and transaction
   323  // eviction events.
   324  func (pool *TxPool) loop() {
   325  	defer pool.wg.Done()
   326  
   327  	var (
   328  		prevPending, prevQueued, prevStales int
   329  		// Start the stats reporting and transaction eviction tickers
   330  		report  = time.NewTicker(statsReportInterval)
   331  		evict   = time.NewTicker(evictionInterval)
   332  		journal = time.NewTicker(pool.config.Rejournal)
   333  		// Track the previous head headers for transaction reorgs
   334  		head = pool.chain.CurrentBlock()
   335  	)
   336  	defer report.Stop()
   337  	defer evict.Stop()
   338  	defer journal.Stop()
   339  
   340  	for {
   341  		select {
   342  		// Handle ChainHeadEvent
   343  		case ev := <-pool.chainHeadCh:
   344  			if ev.Block != nil {
   345  				pool.requestReset(head.Header(), ev.Block.Header())
   346  				head = ev.Block
   347  			}
   348  
   349  		// System shutdown.
   350  		case <-pool.chainHeadSub.Err():
   351  			close(pool.reorgShutdownCh)
   352  			return
   353  
   354  		// Handle stats reporting ticks
   355  		case <-report.C:
   356  			pool.mu.RLock()
   357  			pending, queued := pool.stats()
   358  			stales := pool.priced.stales
   359  			pool.mu.RUnlock()
   360  
   361  			if pending != prevPending || queued != prevQueued || stales != prevStales {
   362  				log.Debug("Transaction pool status report", "executable", pending, "queued", queued, "stales", stales)
   363  				prevPending, prevQueued, prevStales = pending, queued, stales
   364  			}
   365  
   366  		// Handle inactive account transaction eviction
   367  		case <-evict.C:
   368  			pool.mu.Lock()
   369  			for addr := range pool.queue {
   370  				// Skip local transactions from the eviction mechanism
   371  				if pool.locals.contains(addr) {
   372  					continue
   373  				}
   374  				// Any non-locals old enough should be removed
   375  				if time.Since(pool.beats[addr]) > pool.config.Lifetime {
   376  					list := pool.queue[addr].Flatten()
   377  					for _, tx := range list {
   378  						pool.removeTx(tx.Hash(), true)
   379  					}
   380  					queuedEvictionMeter.Mark(int64(len(list)))
   381  				}
   382  			}
   383  			pool.mu.Unlock()
   384  
   385  		// Handle local transaction journal rotation
   386  		case <-journal.C:
   387  			if pool.journal != nil {
   388  				pool.mu.Lock()
   389  				if err := pool.journal.rotate(pool.local()); err != nil {
   390  					log.Warn("Failed to rotate local tx journal", "err", err)
   391  				}
   392  				pool.mu.Unlock()
   393  			}
   394  		}
   395  	}
   396  }
   397  
   398  // Stop terminates the transaction pool.
   399  func (pool *TxPool) Stop() {
   400  	// Unsubscribe all subscriptions registered from txpool
   401  	pool.scope.Close()
   402  
   403  	// Unsubscribe subscriptions registered from blockchain
   404  	pool.chainHeadSub.Unsubscribe()
   405  	pool.wg.Wait()
   406  
   407  	if pool.journal != nil {
   408  		pool.journal.close()
   409  	}
   410  	log.Info("Transaction pool stopped")
   411  }
   412  
   413  // SubscribeNewTxsEvent registers a subscription of NewTxsEvent and
   414  // starts sending event to the given channel.
   415  func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- NewTxsEvent) event.Subscription {
   416  	return pool.scope.Track(pool.txFeed.Subscribe(ch))
   417  }
   418  
   419  // GasPrice returns the current gas price enforced by the transaction pool.
   420  func (pool *TxPool) GasPrice() *big.Int {
   421  	pool.mu.RLock()
   422  	defer pool.mu.RUnlock()
   423  
   424  	return new(big.Int).Set(pool.gasPrice)
   425  }
   426  
   427  // SetGasPrice updates the minimum price required by the transaction pool for a
   428  // new transaction, and drops all transactions below this threshold.
   429  func (pool *TxPool) SetGasPrice(price *big.Int) {
   430  	pool.mu.Lock()
   431  	defer pool.mu.Unlock()
   432  
   433  	old := pool.gasPrice
   434  	pool.gasPrice = price
   435  	// if the min miner fee increased, remove transactions below the new threshold
   436  	if price.Cmp(old) > 0 {
   437  		// pool.priced is sorted by GasFeeCap, so we have to iterate through pool.all instead
   438  		drop := pool.all.RemotesBelowTip(price)
   439  		for _, tx := range drop {
   440  			pool.removeTx(tx.Hash(), false)
   441  		}
   442  		pool.priced.Removed(len(drop))
   443  	}
   444  
   445  	log.Info("Transaction pool price threshold updated", "price", price)
   446  }
   447  
   448  // Nonce returns the next nonce of an account, with all transactions executable
   449  // by the pool already applied on top.
   450  func (pool *TxPool) Nonce(addr common.Address) uint64 {
   451  	pool.mu.RLock()
   452  	defer pool.mu.RUnlock()
   453  
   454  	return pool.pendingNonces.get(addr)
   455  }
   456  
   457  // Stats retrieves the current pool stats, namely the number of pending and the
   458  // number of queued (non-executable) transactions.
   459  func (pool *TxPool) Stats() (int, int) {
   460  	pool.mu.RLock()
   461  	defer pool.mu.RUnlock()
   462  
   463  	return pool.stats()
   464  }
   465  
   466  // stats retrieves the current pool stats, namely the number of pending and the
   467  // number of queued (non-executable) transactions.
   468  func (pool *TxPool) stats() (int, int) {
   469  	pending := 0
   470  	for _, list := range pool.pending {
   471  		pending += list.Len()
   472  	}
   473  	queued := 0
   474  	for _, list := range pool.queue {
   475  		queued += list.Len()
   476  	}
   477  	return pending, queued
   478  }
   479  
   480  // Content retrieves the data content of the transaction pool, returning all the
   481  // pending as well as queued transactions, grouped by account and sorted by nonce.
   482  func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   483  	pool.mu.Lock()
   484  	defer pool.mu.Unlock()
   485  
   486  	pending := make(map[common.Address]types.Transactions)
   487  	for addr, list := range pool.pending {
   488  		pending[addr] = list.Flatten()
   489  	}
   490  	queued := make(map[common.Address]types.Transactions)
   491  	for addr, list := range pool.queue {
   492  		queued[addr] = list.Flatten()
   493  	}
   494  	return pending, queued
   495  }
   496  
   497  // ContentFrom retrieves the data content of the transaction pool, returning the
   498  // pending as well as queued transactions of this address, grouped by nonce.
   499  func (pool *TxPool) ContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
   500  	pool.mu.RLock()
   501  	defer pool.mu.RUnlock()
   502  
   503  	var pending types.Transactions
   504  	if list, ok := pool.pending[addr]; ok {
   505  		pending = list.Flatten()
   506  	}
   507  	var queued types.Transactions
   508  	if list, ok := pool.queue[addr]; ok {
   509  		queued = list.Flatten()
   510  	}
   511  	return pending, queued
   512  }
   513  
   514  // Pending retrieves all currently processable transactions, grouped by origin
   515  // account and sorted by nonce. The returned transaction set is a copy and can be
   516  // freely modified by calling code.
   517  //
   518  // The enforceTips parameter can be used to do an extra filtering on the pending
   519  // transactions and only return those whose **effective** tip is large enough in
   520  // the next pending execution environment.
   521  func (pool *TxPool) Pending(enforceTips bool) (map[common.Address]types.Transactions, error) {
   522  	pool.mu.Lock()
   523  	defer pool.mu.Unlock()
   524  
   525  	pending := make(map[common.Address]types.Transactions)
   526  	for addr, list := range pool.pending {
   527  		txs := list.Flatten()
   528  
   529  		// If the miner requests tip enforcement, cap the lists now
   530  		if enforceTips && !pool.locals.contains(addr) {
   531  			for i, tx := range txs {
   532  				if tx.EffectiveGasTipIntCmp(pool.gasPrice, pool.priced.urgent.baseFee) < 0 {
   533  					txs = txs[:i]
   534  					break
   535  				}
   536  			}
   537  		}
   538  		if len(txs) > 0 {
   539  			pending[addr] = txs
   540  		}
   541  	}
   542  	return pending, nil
   543  }
   544  
   545  // Locals retrieves the accounts currently considered local by the pool.
   546  func (pool *TxPool) Locals() []common.Address {
   547  	pool.mu.Lock()
   548  	defer pool.mu.Unlock()
   549  
   550  	return pool.locals.flatten()
   551  }
   552  
   553  // local retrieves all currently known local transactions, grouped by origin
   554  // account and sorted by nonce. The returned transaction set is a copy and can be
   555  // freely modified by calling code.
   556  func (pool *TxPool) local() map[common.Address]types.Transactions {
   557  	txs := make(map[common.Address]types.Transactions)
   558  	for addr := range pool.locals.accounts {
   559  		if pending := pool.pending[addr]; pending != nil {
   560  			txs[addr] = append(txs[addr], pending.Flatten()...)
   561  		}
   562  		if queued := pool.queue[addr]; queued != nil {
   563  			txs[addr] = append(txs[addr], queued.Flatten()...)
   564  		}
   565  	}
   566  	return txs
   567  }
   568  
   569  // validateTx checks whether a transaction is valid according to the consensus
   570  // rules and adheres to some heuristic limits of the local node (price and size).
   571  func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
   572  	// Accept only legacy transactions until EIP-2718/2930 activates.
   573  	if !pool.eip2718 && tx.Type() != types.LegacyTxType {
   574  		return ErrTxTypeNotSupported
   575  	}
   576  	// Reject dynamic fee transactions until EIP-1559 activates.
   577  	if !pool.eip1559 && tx.Type() == types.DynamicFeeTxType {
   578  		return ErrTxTypeNotSupported
   579  	}
   580  	// Reject transactions over defined size to prevent DOS attacks
   581  	if uint64(tx.Size()) > txMaxSize {
   582  		return ErrOversizedData
   583  	}
   584  	// Transactions can't be negative. This may never happen using RLP decoded
   585  	// transactions but may occur if you create a transaction using the RPC.
   586  	if tx.Value().Sign() < 0 {
   587  		return ErrNegativeValue
   588  	}
   589  	// Ensure the transaction doesn't exceed the current block limit gas.
   590  	if pool.currentMaxGas < tx.Gas() {
   591  		return ErrGasLimit
   592  	}
   593  	// Sanity check for extremely large numbers
   594  	if tx.GasFeeCap().BitLen() > 256 {
   595  		return ErrFeeCapVeryHigh
   596  	}
   597  	if tx.GasTipCap().BitLen() > 256 {
   598  		return ErrTipVeryHigh
   599  	}
   600  	// Ensure gasFeeCap is greater than or equal to gasTipCap.
   601  	if tx.GasFeeCapIntCmp(tx.GasTipCap()) < 0 {
   602  		return ErrTipAboveFeeCap
   603  	}
   604  	// Make sure the transaction is signed properly.
   605  	from, err := types.Sender(pool.signer, tx)
   606  	if err != nil {
   607  		return ErrInvalidSender
   608  	}
   609  	// Drop non-local transactions under our own minimal accepted gas price or tip
   610  	if !local && tx.GasTipCapIntCmp(pool.gasPrice) < 0 {
   611  		return ErrUnderpriced
   612  	}
   613  	// Ensure the transaction adheres to nonce ordering
   614  	if pool.currentState.GetNonce(from) > tx.Nonce() {
   615  		return ErrNonceTooLow
   616  	}
   617  	// Transactor should have enough funds to cover the costs
   618  	// cost == V + GP * GL
   619  	if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
   620  		return ErrInsufficientFunds
   621  	}
   622  	// Ensure the transaction has more gas than the basic tx fee.
   623  	intrGas, err := IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, pool.istanbul)
   624  	if err != nil {
   625  		return err
   626  	}
   627  	if tx.Gas() < intrGas {
   628  		return ErrIntrinsicGas
   629  	}
   630  	return nil
   631  }
   632  
   633  // add validates a transaction and inserts it into the non-executable queue for later
   634  // pending promotion and execution. If the transaction is a replacement for an already
   635  // pending or queued one, it overwrites the previous transaction if its price is higher.
   636  //
   637  // If a newly added transaction is marked as local, its sending account will be
   638  // whitelisted, preventing any associated transaction from being dropped out of the pool
   639  // due to pricing constraints.
   640  func (pool *TxPool) add(tx *types.Transaction, local bool) (replaced bool, err error) {
   641  	// If the transaction is already known, discard it
   642  	hash := tx.Hash()
   643  	if pool.all.Get(hash) != nil {
   644  		log.Trace("Discarding already known transaction", "hash", hash)
   645  		knownTxMeter.Mark(1)
   646  		return false, ErrAlreadyKnown
   647  	}
   648  	// Make the local flag. If it's from local source or it's from the network but
   649  	// the sender is marked as local previously, treat it as the local transaction.
   650  	isLocal := local || pool.locals.containsTx(tx)
   651  
   652  	// If the transaction fails basic validation, discard it
   653  	if err := pool.validateTx(tx, isLocal); err != nil {
   654  		log.Trace("Discarding invalid transaction", "hash", hash, "err", err)
   655  		invalidTxMeter.Mark(1)
   656  		return false, err
   657  	}
   658  	// If the transaction pool is full, discard underpriced transactions
   659  	if uint64(pool.all.Slots()+numSlots(tx)) > pool.config.GlobalSlots+pool.config.GlobalQueue {
   660  		// If the new transaction is underpriced, don't accept it
   661  		if !isLocal && pool.priced.Underpriced(tx) {
   662  			log.Trace("Discarding underpriced transaction", "hash", hash, "gasTipCap", tx.GasTipCap(), "gasFeeCap", tx.GasFeeCap())
   663  			underpricedTxMeter.Mark(1)
   664  			return false, ErrUnderpriced
   665  		}
   666  		// New transaction is better than our worse ones, make room for it.
   667  		// If it's a local transaction, forcibly discard all available transactions.
   668  		// Otherwise if we can't make enough room for new one, abort the operation.
   669  		drop, success := pool.priced.Discard(pool.all.Slots()-int(pool.config.GlobalSlots+pool.config.GlobalQueue)+numSlots(tx), isLocal)
   670  
   671  		// Special case, we still can't make the room for the new remote one.
   672  		if !isLocal && !success {
   673  			log.Trace("Discarding overflown transaction", "hash", hash)
   674  			overflowedTxMeter.Mark(1)
   675  			return false, ErrTxPoolOverflow
   676  		}
   677  		// Kick out the underpriced remote transactions.
   678  		for _, tx := range drop {
   679  			log.Trace("Discarding freshly underpriced transaction", "hash", tx.Hash(), "gasTipCap", tx.GasTipCap(), "gasFeeCap", tx.GasFeeCap())
   680  			underpricedTxMeter.Mark(1)
   681  			pool.removeTx(tx.Hash(), false)
   682  		}
   683  	}
   684  	// Try to replace an existing transaction in the pending pool
   685  	from, _ := types.Sender(pool.signer, tx) // already validated
   686  	if list := pool.pending[from]; list != nil && list.Overlaps(tx) {
   687  		// Nonce already pending, check if required price bump is met
   688  		inserted, old := list.Add(tx, pool.config.PriceBump)
   689  		if !inserted {
   690  			pendingDiscardMeter.Mark(1)
   691  			return false, ErrReplaceUnderpriced
   692  		}
   693  		// New transaction is better, replace old one
   694  		if old != nil {
   695  			pool.all.Remove(old.Hash())
   696  			pool.priced.Removed(1)
   697  			pendingReplaceMeter.Mark(1)
   698  		}
   699  		pool.all.Add(tx, isLocal)
   700  		pool.priced.Put(tx, isLocal)
   701  		pool.journalTx(from, tx)
   702  		pool.queueTxEvent(tx)
   703  		log.Trace("Pooled new executable transaction", "hash", hash, "from", from, "to", tx.To())
   704  
   705  		// Successful promotion, bump the heartbeat
   706  		pool.beats[from] = time.Now()
   707  		return old != nil, nil
   708  	}
   709  	// New transaction isn't replacing a pending one, push into queue
   710  	replaced, err = pool.enqueueTx(hash, tx, isLocal, true)
   711  	if err != nil {
   712  		return false, err
   713  	}
   714  	// Mark local addresses and journal local transactions
   715  	if local && !pool.locals.contains(from) {
   716  		log.Info("Setting new local account", "address", from)
   717  		pool.locals.add(from)
   718  		pool.priced.Removed(pool.all.RemoteToLocals(pool.locals)) // Migrate the remotes if it's marked as local first time.
   719  	}
   720  	if isLocal {
   721  		localGauge.Inc(1)
   722  	}
   723  	pool.journalTx(from, tx)
   724  
   725  	log.Trace("Pooled new future transaction", "hash", hash, "from", from, "to", tx.To())
   726  	return replaced, nil
   727  }
   728  
   729  // enqueueTx inserts a new transaction into the non-executable transaction queue.
   730  //
   731  // Note, this method assumes the pool lock is held!
   732  func (pool *TxPool) enqueueTx(hash common.Hash, tx *types.Transaction, local bool, addAll bool) (bool, error) {
   733  	// Try to insert the transaction into the future queue
   734  	from, _ := types.Sender(pool.signer, tx) // already validated
   735  	if pool.queue[from] == nil {
   736  		pool.queue[from] = newTxList(false)
   737  	}
   738  	inserted, old := pool.queue[from].Add(tx, pool.config.PriceBump)
   739  	if !inserted {
   740  		// An older transaction was better, discard this
   741  		queuedDiscardMeter.Mark(1)
   742  		return false, ErrReplaceUnderpriced
   743  	}
   744  	// Discard any previous transaction and mark this
   745  	if old != nil {
   746  		pool.all.Remove(old.Hash())
   747  		pool.priced.Removed(1)
   748  		queuedReplaceMeter.Mark(1)
   749  	} else {
   750  		// Nothing was replaced, bump the queued counter
   751  		queuedGauge.Inc(1)
   752  	}
   753  	// If the transaction isn't in lookup set but it's expected to be there,
   754  	// show the error log.
   755  	if pool.all.Get(hash) == nil && !addAll {
   756  		log.Error("Missing transaction in lookup set, please report the issue", "hash", hash)
   757  	}
   758  	if addAll {
   759  		pool.all.Add(tx, local)
   760  		pool.priced.Put(tx, local)
   761  	}
   762  	// If we never record the heartbeat, do it right now.
   763  	if _, exist := pool.beats[from]; !exist {
   764  		pool.beats[from] = time.Now()
   765  	}
   766  	return old != nil, nil
   767  }
   768  
   769  // journalTx adds the specified transaction to the local disk journal if it is
   770  // deemed to have been sent from a local account.
   771  func (pool *TxPool) journalTx(from common.Address, tx *types.Transaction) {
   772  	// Only journal if it's enabled and the transaction is local
   773  	if pool.journal == nil || !pool.locals.contains(from) {
   774  		return
   775  	}
   776  	if err := pool.journal.insert(tx); err != nil {
   777  		log.Warn("Failed to journal local transaction", "err", err)
   778  	}
   779  }
   780  
   781  // promoteTx adds a transaction to the pending (processable) list of transactions
   782  // and returns whether it was inserted or an older was better.
   783  //
   784  // Note, this method assumes the pool lock is held!
   785  func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) bool {
   786  	// Try to insert the transaction into the pending queue
   787  	if pool.pending[addr] == nil {
   788  		pool.pending[addr] = newTxList(true)
   789  	}
   790  	list := pool.pending[addr]
   791  
   792  	inserted, old := list.Add(tx, pool.config.PriceBump)
   793  	if !inserted {
   794  		// An older transaction was better, discard this
   795  		pool.all.Remove(hash)
   796  		pool.priced.Removed(1)
   797  		pendingDiscardMeter.Mark(1)
   798  		return false
   799  	}
   800  	// Otherwise discard any previous transaction and mark this
   801  	if old != nil {
   802  		pool.all.Remove(old.Hash())
   803  		pool.priced.Removed(1)
   804  		pendingReplaceMeter.Mark(1)
   805  	} else {
   806  		// Nothing was replaced, bump the pending counter
   807  		pendingGauge.Inc(1)
   808  	}
   809  	// Set the potentially new pending nonce and notify any subsystems of the new tx
   810  	pool.pendingNonces.set(addr, tx.Nonce()+1)
   811  
   812  	// Successful promotion, bump the heartbeat
   813  	pool.beats[addr] = time.Now()
   814  	return true
   815  }
   816  
   817  // AddLocals enqueues a batch of transactions into the pool if they are valid, marking the
   818  // senders as a local ones, ensuring they go around the local pricing constraints.
   819  //
   820  // This method is used to add transactions from the RPC API and performs synchronous pool
   821  // reorganization and event propagation.
   822  func (pool *TxPool) AddLocals(txs []*types.Transaction) []error {
   823  	return pool.addTxs(txs, !pool.config.NoLocals, true)
   824  }
   825  
   826  // AddLocal enqueues a single local transaction into the pool if it is valid. This is
   827  // a convenience wrapper aroundd AddLocals.
   828  func (pool *TxPool) AddLocal(tx *types.Transaction) error {
   829  	errs := pool.AddLocals([]*types.Transaction{tx})
   830  	return errs[0]
   831  }
   832  
   833  // AddRemotes enqueues a batch of transactions into the pool if they are valid. If the
   834  // senders are not among the locally tracked ones, full pricing constraints will apply.
   835  //
   836  // This method is used to add transactions from the p2p network and does not wait for pool
   837  // reorganization and internal event propagation.
   838  func (pool *TxPool) AddRemotes(txs []*types.Transaction) []error {
   839  	return pool.addTxs(txs, false, false)
   840  }
   841  
   842  // This is like AddRemotes, but waits for pool reorganization. Tests use this method.
   843  func (pool *TxPool) AddRemotesSync(txs []*types.Transaction) []error {
   844  	return pool.addTxs(txs, false, true)
   845  }
   846  
   847  // This is like AddRemotes with a single transaction, but waits for pool reorganization. Tests use this method.
   848  func (pool *TxPool) addRemoteSync(tx *types.Transaction) error {
   849  	errs := pool.AddRemotesSync([]*types.Transaction{tx})
   850  	return errs[0]
   851  }
   852  
   853  // AddRemote enqueues a single transaction into the pool if it is valid. This is a convenience
   854  // wrapper around AddRemotes.
   855  //
   856  // Deprecated: use AddRemotes
   857  func (pool *TxPool) AddRemote(tx *types.Transaction) error {
   858  	errs := pool.AddRemotes([]*types.Transaction{tx})
   859  	return errs[0]
   860  }
   861  
   862  // addTxs attempts to queue a batch of transactions if they are valid.
   863  func (pool *TxPool) addTxs(txs []*types.Transaction, local, sync bool) []error {
   864  	// Filter out known ones without obtaining the pool lock or recovering signatures
   865  	var (
   866  		errs = make([]error, len(txs))
   867  		news = make([]*types.Transaction, 0, len(txs))
   868  	)
   869  	for i, tx := range txs {
   870  		// If the transaction is known, pre-set the error slot
   871  		if pool.all.Get(tx.Hash()) != nil {
   872  			errs[i] = ErrAlreadyKnown
   873  			knownTxMeter.Mark(1)
   874  			continue
   875  		}
   876  		// Exclude transactions with invalid signatures as soon as
   877  		// possible and cache senders in transactions before
   878  		// obtaining lock
   879  		_, err := types.Sender(pool.signer, tx)
   880  		if err != nil {
   881  			errs[i] = ErrInvalidSender
   882  			invalidTxMeter.Mark(1)
   883  			continue
   884  		}
   885  		// Accumulate all unknown transactions for deeper processing
   886  		news = append(news, tx)
   887  	}
   888  	if len(news) == 0 {
   889  		return errs
   890  	}
   891  
   892  	// Process all the new transaction and merge any errors into the original slice
   893  	pool.mu.Lock()
   894  	newErrs, dirtyAddrs := pool.addTxsLocked(news, local)
   895  	pool.mu.Unlock()
   896  
   897  	var nilSlot = 0
   898  	for _, err := range newErrs {
   899  		for errs[nilSlot] != nil {
   900  			nilSlot++
   901  		}
   902  		errs[nilSlot] = err
   903  		nilSlot++
   904  	}
   905  	// Reorg the pool internals if needed and return
   906  	done := pool.requestPromoteExecutables(dirtyAddrs)
   907  	if sync {
   908  		<-done
   909  	}
   910  	return errs
   911  }
   912  
   913  // addTxsLocked attempts to queue a batch of transactions if they are valid.
   914  // The transaction pool lock must be held.
   915  func (pool *TxPool) addTxsLocked(txs []*types.Transaction, local bool) ([]error, *accountSet) {
   916  	dirty := newAccountSet(pool.signer)
   917  	errs := make([]error, len(txs))
   918  	for i, tx := range txs {
   919  		replaced, err := pool.add(tx, local)
   920  		errs[i] = err
   921  		if err == nil && !replaced {
   922  			dirty.addTx(tx)
   923  		}
   924  	}
   925  	validTxMeter.Mark(int64(len(dirty.accounts)))
   926  	return errs, dirty
   927  }
   928  
   929  // Status returns the status (unknown/pending/queued) of a batch of transactions
   930  // identified by their hashes.
   931  func (pool *TxPool) Status(hashes []common.Hash) []TxStatus {
   932  	status := make([]TxStatus, len(hashes))
   933  	for i, hash := range hashes {
   934  		tx := pool.Get(hash)
   935  		if tx == nil {
   936  			continue
   937  		}
   938  		from, _ := types.Sender(pool.signer, tx) // already validated
   939  		pool.mu.RLock()
   940  		if txList := pool.pending[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
   941  			status[i] = TxStatusPending
   942  		} else if txList := pool.queue[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
   943  			status[i] = TxStatusQueued
   944  		}
   945  		// implicit else: the tx may have been included into a block between
   946  		// checking pool.Get and obtaining the lock. In that case, TxStatusUnknown is correct
   947  		pool.mu.RUnlock()
   948  	}
   949  	return status
   950  }
   951  
   952  // Get returns a transaction if it is contained in the pool and nil otherwise.
   953  func (pool *TxPool) Get(hash common.Hash) *types.Transaction {
   954  	return pool.all.Get(hash)
   955  }
   956  
   957  // Has returns an indicator whether txpool has a transaction cached with the
   958  // given hash.
   959  func (pool *TxPool) Has(hash common.Hash) bool {
   960  	return pool.all.Get(hash) != nil
   961  }
   962  
   963  // removeTx removes a single transaction from the queue, moving all subsequent
   964  // transactions back to the future queue.
   965  func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
   966  	// Fetch the transaction we wish to delete
   967  	tx := pool.all.Get(hash)
   968  	if tx == nil {
   969  		return
   970  	}
   971  	addr, _ := types.Sender(pool.signer, tx) // already validated during insertion
   972  
   973  	// Remove it from the list of known transactions
   974  	pool.all.Remove(hash)
   975  	if outofbound {
   976  		pool.priced.Removed(1)
   977  	}
   978  	if pool.locals.contains(addr) {
   979  		localGauge.Dec(1)
   980  	}
   981  	// Remove the transaction from the pending lists and reset the account nonce
   982  	if pending := pool.pending[addr]; pending != nil {
   983  		if removed, invalids := pending.Remove(tx); removed {
   984  			// If no more pending transactions are left, remove the list
   985  			if pending.Empty() {
   986  				delete(pool.pending, addr)
   987  			}
   988  			// Postpone any invalidated transactions
   989  			for _, tx := range invalids {
   990  				// Internal shuffle shouldn't touch the lookup set.
   991  				pool.enqueueTx(tx.Hash(), tx, false, false)
   992  			}
   993  			// Update the account nonce if needed
   994  			pool.pendingNonces.setIfLower(addr, tx.Nonce())
   995  			// Reduce the pending counter
   996  			pendingGauge.Dec(int64(1 + len(invalids)))
   997  			return
   998  		}
   999  	}
  1000  	// Transaction is in the future queue
  1001  	if future := pool.queue[addr]; future != nil {
  1002  		if removed, _ := future.Remove(tx); removed {
  1003  			// Reduce the queued counter
  1004  			queuedGauge.Dec(1)
  1005  		}
  1006  		if future.Empty() {
  1007  			delete(pool.queue, addr)
  1008  			delete(pool.beats, addr)
  1009  		}
  1010  	}
  1011  }
  1012  
  1013  // requestReset requests a pool reset to the new head block.
  1014  // The returned channel is closed when the reset has occurred.
  1015  func (pool *TxPool) requestReset(oldHead *types.Header, newHead *types.Header) chan struct{} {
  1016  	select {
  1017  	case pool.reqResetCh <- &txpoolResetRequest{oldHead, newHead}:
  1018  		return <-pool.reorgDoneCh
  1019  	case <-pool.reorgShutdownCh:
  1020  		return pool.reorgShutdownCh
  1021  	}
  1022  }
  1023  
  1024  // requestPromoteExecutables requests transaction promotion checks for the given addresses.
  1025  // The returned channel is closed when the promotion checks have occurred.
  1026  func (pool *TxPool) requestPromoteExecutables(set *accountSet) chan struct{} {
  1027  	select {
  1028  	case pool.reqPromoteCh <- set:
  1029  		return <-pool.reorgDoneCh
  1030  	case <-pool.reorgShutdownCh:
  1031  		return pool.reorgShutdownCh
  1032  	}
  1033  }
  1034  
  1035  // queueTxEvent enqueues a transaction event to be sent in the next reorg run.
  1036  func (pool *TxPool) queueTxEvent(tx *types.Transaction) {
  1037  	select {
  1038  	case pool.queueTxEventCh <- tx:
  1039  	case <-pool.reorgShutdownCh:
  1040  	}
  1041  }
  1042  
  1043  // scheduleReorgLoop schedules runs of reset and promoteExecutables. Code above should not
  1044  // call those methods directly, but request them being run using requestReset and
  1045  // requestPromoteExecutables instead.
  1046  func (pool *TxPool) scheduleReorgLoop() {
  1047  	defer pool.wg.Done()
  1048  
  1049  	var (
  1050  		curDone       chan struct{} // non-nil while runReorg is active
  1051  		nextDone      = make(chan struct{})
  1052  		launchNextRun bool
  1053  		reset         *txpoolResetRequest
  1054  		dirtyAccounts *accountSet
  1055  		queuedEvents  = make(map[common.Address]*txSortedMap)
  1056  	)
  1057  	for {
  1058  		// Launch next background reorg if needed
  1059  		if curDone == nil && launchNextRun {
  1060  			// Run the background reorg and announcements
  1061  			go pool.runReorg(nextDone, reset, dirtyAccounts, queuedEvents)
  1062  
  1063  			// Prepare everything for the next round of reorg
  1064  			curDone, nextDone = nextDone, make(chan struct{})
  1065  			launchNextRun = false
  1066  
  1067  			reset, dirtyAccounts = nil, nil
  1068  			queuedEvents = make(map[common.Address]*txSortedMap)
  1069  		}
  1070  
  1071  		select {
  1072  		case req := <-pool.reqResetCh:
  1073  			// Reset request: update head if request is already pending.
  1074  			if reset == nil {
  1075  				reset = req
  1076  			} else {
  1077  				reset.newHead = req.newHead
  1078  			}
  1079  			launchNextRun = true
  1080  			pool.reorgDoneCh <- nextDone
  1081  
  1082  		case req := <-pool.reqPromoteCh:
  1083  			// Promote request: update address set if request is already pending.
  1084  			if dirtyAccounts == nil {
  1085  				dirtyAccounts = req
  1086  			} else {
  1087  				dirtyAccounts.merge(req)
  1088  			}
  1089  			launchNextRun = true
  1090  			pool.reorgDoneCh <- nextDone
  1091  
  1092  		case tx := <-pool.queueTxEventCh:
  1093  			// Queue up the event, but don't schedule a reorg. It's up to the caller to
  1094  			// request one later if they want the events sent.
  1095  			addr, _ := types.Sender(pool.signer, tx)
  1096  			if _, ok := queuedEvents[addr]; !ok {
  1097  				queuedEvents[addr] = newTxSortedMap()
  1098  			}
  1099  			queuedEvents[addr].Put(tx)
  1100  
  1101  		case <-curDone:
  1102  			curDone = nil
  1103  
  1104  		case <-pool.reorgShutdownCh:
  1105  			// Wait for current run to finish.
  1106  			if curDone != nil {
  1107  				<-curDone
  1108  			}
  1109  			close(nextDone)
  1110  			return
  1111  		}
  1112  	}
  1113  }
  1114  
  1115  // runReorg runs reset and promoteExecutables on behalf of scheduleReorgLoop.
  1116  func (pool *TxPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirtyAccounts *accountSet, events map[common.Address]*txSortedMap) {
  1117  	defer close(done)
  1118  
  1119  	var promoteAddrs []common.Address
  1120  	if dirtyAccounts != nil && reset == nil {
  1121  		// Only dirty accounts need to be promoted, unless we're resetting.
  1122  		// For resets, all addresses in the tx queue will be promoted and
  1123  		// the flatten operation can be avoided.
  1124  		promoteAddrs = dirtyAccounts.flatten()
  1125  	}
  1126  	pool.mu.Lock()
  1127  	if reset != nil {
  1128  		// Reset from the old head to the new, rescheduling any reorged transactions
  1129  		pool.reset(reset.oldHead, reset.newHead)
  1130  
  1131  		// Nonces were reset, discard any events that became stale
  1132  		for addr := range events {
  1133  			events[addr].Forward(pool.pendingNonces.get(addr))
  1134  			if events[addr].Len() == 0 {
  1135  				delete(events, addr)
  1136  			}
  1137  		}
  1138  		// Reset needs promote for all addresses
  1139  		promoteAddrs = make([]common.Address, 0, len(pool.queue))
  1140  		for addr := range pool.queue {
  1141  			promoteAddrs = append(promoteAddrs, addr)
  1142  		}
  1143  	}
  1144  	// Check for pending transactions for every account that sent new ones
  1145  	promoted := pool.promoteExecutables(promoteAddrs)
  1146  
  1147  	// If a new block appeared, validate the pool of pending transactions. This will
  1148  	// remove any transaction that has been included in the block or was invalidated
  1149  	// because of another transaction (e.g. higher gas price).
  1150  	if reset != nil {
  1151  		pool.demoteUnexecutables()
  1152  		if reset.newHead != nil && pool.chainconfig.IsLondon(new(big.Int).Add(reset.newHead.Number, big.NewInt(1))) {
  1153  			pendingBaseFee := misc.CalcBaseFee(pool.chainconfig, reset.newHead)
  1154  			pool.priced.SetBaseFee(pendingBaseFee)
  1155  		}
  1156  	}
  1157  	// Ensure pool.queue and pool.pending sizes stay within the configured limits.
  1158  	pool.truncatePending()
  1159  	pool.truncateQueue()
  1160  
  1161  	// Update all accounts to the latest known pending nonce
  1162  	for addr, list := range pool.pending {
  1163  		highestPending := list.LastElement()
  1164  		pool.pendingNonces.set(addr, highestPending.Nonce()+1)
  1165  	}
  1166  	pool.mu.Unlock()
  1167  
  1168  	// Notify subsystems for newly added transactions
  1169  	for _, tx := range promoted {
  1170  		addr, _ := types.Sender(pool.signer, tx)
  1171  		if _, ok := events[addr]; !ok {
  1172  			events[addr] = newTxSortedMap()
  1173  		}
  1174  		events[addr].Put(tx)
  1175  	}
  1176  	if len(events) > 0 {
  1177  		var txs []*types.Transaction
  1178  		for _, set := range events {
  1179  			txs = append(txs, set.Flatten()...)
  1180  		}
  1181  		pool.txFeed.Send(NewTxsEvent{txs})
  1182  	}
  1183  }
  1184  
  1185  // reset retrieves the current state of the blockchain and ensures the content
  1186  // of the transaction pool is valid with regard to the chain state.
  1187  func (pool *TxPool) reset(oldHead, newHead *types.Header) {
  1188  	// If we're reorging an old state, reinject all dropped transactions
  1189  	var reinject types.Transactions
  1190  
  1191  	if oldHead != nil && oldHead.Hash() != newHead.ParentHash {
  1192  		// If the reorg is too deep, avoid doing it (will happen during fast sync)
  1193  		oldNum := oldHead.Number.Uint64()
  1194  		newNum := newHead.Number.Uint64()
  1195  
  1196  		if depth := uint64(math.Abs(float64(oldNum) - float64(newNum))); depth > 64 {
  1197  			log.Debug("Skipping deep transaction reorg", "depth", depth)
  1198  		} else {
  1199  			// Reorg seems shallow enough to pull in all transactions into memory
  1200  			var discarded, included types.Transactions
  1201  			var (
  1202  				rem = pool.chain.GetBlock(oldHead.Hash(), oldHead.Number.Uint64())
  1203  				add = pool.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64())
  1204  			)
  1205  			if rem == nil {
  1206  				// This can happen if a setHead is performed, where we simply discard the old
  1207  				// head from the chain.
  1208  				// If that is the case, we don't have the lost transactions any more, and
  1209  				// there's nothing to add
  1210  				if newNum >= oldNum {
  1211  					// If we reorged to a same or higher number, then it's not a case of setHead
  1212  					log.Warn("Transaction pool reset with missing oldhead",
  1213  						"old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
  1214  					return
  1215  				}
  1216  				// If the reorg ended up on a lower number, it's indicative of setHead being the cause
  1217  				log.Debug("Skipping transaction reset caused by setHead",
  1218  					"old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
  1219  				// We still need to update the current state s.th. the lost transactions can be readded by the user
  1220  			} else {
  1221  				for rem.NumberU64() > add.NumberU64() {
  1222  					discarded = append(discarded, rem.Transactions()...)
  1223  					if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
  1224  						log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
  1225  						return
  1226  					}
  1227  				}
  1228  				for add.NumberU64() > rem.NumberU64() {
  1229  					included = append(included, add.Transactions()...)
  1230  					if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
  1231  						log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
  1232  						return
  1233  					}
  1234  				}
  1235  				for rem.Hash() != add.Hash() {
  1236  					discarded = append(discarded, rem.Transactions()...)
  1237  					if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
  1238  						log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
  1239  						return
  1240  					}
  1241  					included = append(included, add.Transactions()...)
  1242  					if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
  1243  						log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
  1244  						return
  1245  					}
  1246  				}
  1247  				reinject = types.TxDifference(discarded, included)
  1248  			}
  1249  		}
  1250  	}
  1251  	// Initialize the internal state to the current head
  1252  	if newHead == nil {
  1253  		newHead = pool.chain.CurrentBlock().Header() // Special case during testing
  1254  	}
  1255  	statedb, err := pool.chain.StateAt(newHead.Root)
  1256  	if err != nil {
  1257  		log.Error("Failed to reset txpool state", "err", err)
  1258  		return
  1259  	}
  1260  	pool.currentState = statedb
  1261  	pool.pendingNonces = newTxNoncer(statedb)
  1262  	pool.currentMaxGas = newHead.GasLimit
  1263  
  1264  	// Inject any transactions discarded due to reorgs
  1265  	log.Debug("Reinjecting stale transactions", "count", len(reinject))
  1266  	senderCacher.recover(pool.signer, reinject)
  1267  	pool.addTxsLocked(reinject, false)
  1268  
  1269  	// Update all fork indicator by next pending block number.
  1270  	next := new(big.Int).Add(newHead.Number, big.NewInt(1))
  1271  	pool.istanbul = pool.chainconfig.IsIstanbul(next)
  1272  	pool.eip2718 = pool.chainconfig.IsBerlin(next)
  1273  	pool.eip1559 = pool.chainconfig.IsLondon(next)
  1274  }
  1275  
  1276  // promoteExecutables moves transactions that have become processable from the
  1277  // future queue to the set of pending transactions. During this process, all
  1278  // invalidated transactions (low nonce, low balance) are deleted.
  1279  func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Transaction {
  1280  	// Track the promoted transactions to broadcast them at once
  1281  	var promoted []*types.Transaction
  1282  
  1283  	// Iterate over all accounts and promote any executable transactions
  1284  	for _, addr := range accounts {
  1285  		list := pool.queue[addr]
  1286  		if list == nil {
  1287  			continue // Just in case someone calls with a non existing account
  1288  		}
  1289  		// Drop all transactions that are deemed too old (low nonce)
  1290  		forwards := list.Forward(pool.currentState.GetNonce(addr))
  1291  		for _, tx := range forwards {
  1292  			hash := tx.Hash()
  1293  			pool.all.Remove(hash)
  1294  		}
  1295  		log.Trace("Removed old queued transactions", "count", len(forwards))
  1296  		// Drop all transactions that are too costly (low balance or out of gas)
  1297  		drops, _ := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)
  1298  		for _, tx := range drops {
  1299  			hash := tx.Hash()
  1300  			pool.all.Remove(hash)
  1301  		}
  1302  		log.Trace("Removed unpayable queued transactions", "count", len(drops))
  1303  		queuedNofundsMeter.Mark(int64(len(drops)))
  1304  
  1305  		// Gather all executable transactions and promote them
  1306  		readies := list.Ready(pool.pendingNonces.get(addr))
  1307  		for _, tx := range readies {
  1308  			hash := tx.Hash()
  1309  			if pool.promoteTx(addr, hash, tx) {
  1310  				promoted = append(promoted, tx)
  1311  			}
  1312  		}
  1313  		log.Trace("Promoted queued transactions", "count", len(promoted))
  1314  		queuedGauge.Dec(int64(len(readies)))
  1315  
  1316  		// Drop all transactions over the allowed limit
  1317  		var caps types.Transactions
  1318  		if !pool.locals.contains(addr) {
  1319  			caps = list.Cap(int(pool.config.AccountQueue))
  1320  			for _, tx := range caps {
  1321  				hash := tx.Hash()
  1322  				pool.all.Remove(hash)
  1323  				log.Trace("Removed cap-exceeding queued transaction", "hash", hash)
  1324  			}
  1325  			queuedRateLimitMeter.Mark(int64(len(caps)))
  1326  		}
  1327  		// Mark all the items dropped as removed
  1328  		pool.priced.Removed(len(forwards) + len(drops) + len(caps))
  1329  		queuedGauge.Dec(int64(len(forwards) + len(drops) + len(caps)))
  1330  		if pool.locals.contains(addr) {
  1331  			localGauge.Dec(int64(len(forwards) + len(drops) + len(caps)))
  1332  		}
  1333  		// Delete the entire queue entry if it became empty.
  1334  		if list.Empty() {
  1335  			delete(pool.queue, addr)
  1336  			delete(pool.beats, addr)
  1337  		}
  1338  	}
  1339  	return promoted
  1340  }
  1341  
  1342  // truncatePending removes transactions from the pending queue if the pool is above the
  1343  // pending limit. The algorithm tries to reduce transaction counts by an approximately
  1344  // equal number for all for accounts with many pending transactions.
  1345  func (pool *TxPool) truncatePending() {
  1346  	pending := uint64(0)
  1347  	for _, list := range pool.pending {
  1348  		pending += uint64(list.Len())
  1349  	}
  1350  	if pending <= pool.config.GlobalSlots {
  1351  		return
  1352  	}
  1353  
  1354  	pendingBeforeCap := pending
  1355  	// Assemble a spam order to penalize large transactors first
  1356  	spammers := prque.New(nil)
  1357  	for addr, list := range pool.pending {
  1358  		// Only evict transactions from high rollers
  1359  		if !pool.locals.contains(addr) && uint64(list.Len()) > pool.config.AccountSlots {
  1360  			spammers.Push(addr, int64(list.Len()))
  1361  		}
  1362  	}
  1363  	// Gradually drop transactions from offenders
  1364  	offenders := []common.Address{}
  1365  	for pending > pool.config.GlobalSlots && !spammers.Empty() {
  1366  		// Retrieve the next offender if not local address
  1367  		offender, _ := spammers.Pop()
  1368  		offenders = append(offenders, offender.(common.Address))
  1369  
  1370  		// Equalize balances until all the same or below threshold
  1371  		if len(offenders) > 1 {
  1372  			// Calculate the equalization threshold for all current offenders
  1373  			threshold := pool.pending[offender.(common.Address)].Len()
  1374  
  1375  			// Iteratively reduce all offenders until below limit or threshold reached
  1376  			for pending > pool.config.GlobalSlots && pool.pending[offenders[len(offenders)-2]].Len() > threshold {
  1377  				for i := 0; i < len(offenders)-1; i++ {
  1378  					list := pool.pending[offenders[i]]
  1379  
  1380  					caps := list.Cap(list.Len() - 1)
  1381  					for _, tx := range caps {
  1382  						// Drop the transaction from the global pools too
  1383  						hash := tx.Hash()
  1384  						pool.all.Remove(hash)
  1385  
  1386  						// Update the account nonce to the dropped transaction
  1387  						pool.pendingNonces.setIfLower(offenders[i], tx.Nonce())
  1388  						log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
  1389  					}
  1390  					pool.priced.Removed(len(caps))
  1391  					pendingGauge.Dec(int64(len(caps)))
  1392  					if pool.locals.contains(offenders[i]) {
  1393  						localGauge.Dec(int64(len(caps)))
  1394  					}
  1395  					pending--
  1396  				}
  1397  			}
  1398  		}
  1399  	}
  1400  
  1401  	// If still above threshold, reduce to limit or min allowance
  1402  	if pending > pool.config.GlobalSlots && len(offenders) > 0 {
  1403  		for pending > pool.config.GlobalSlots && uint64(pool.pending[offenders[len(offenders)-1]].Len()) > pool.config.AccountSlots {
  1404  			for _, addr := range offenders {
  1405  				list := pool.pending[addr]
  1406  
  1407  				caps := list.Cap(list.Len() - 1)
  1408  				for _, tx := range caps {
  1409  					// Drop the transaction from the global pools too
  1410  					hash := tx.Hash()
  1411  					pool.all.Remove(hash)
  1412  
  1413  					// Update the account nonce to the dropped transaction
  1414  					pool.pendingNonces.setIfLower(addr, tx.Nonce())
  1415  					log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
  1416  				}
  1417  				pool.priced.Removed(len(caps))
  1418  				pendingGauge.Dec(int64(len(caps)))
  1419  				if pool.locals.contains(addr) {
  1420  					localGauge.Dec(int64(len(caps)))
  1421  				}
  1422  				pending--
  1423  			}
  1424  		}
  1425  	}
  1426  	pendingRateLimitMeter.Mark(int64(pendingBeforeCap - pending))
  1427  }
  1428  
  1429  // truncateQueue drops the oldes transactions in the queue if the pool is above the global queue limit.
  1430  func (pool *TxPool) truncateQueue() {
  1431  	queued := uint64(0)
  1432  	for _, list := range pool.queue {
  1433  		queued += uint64(list.Len())
  1434  	}
  1435  	if queued <= pool.config.GlobalQueue {
  1436  		return
  1437  	}
  1438  
  1439  	// Sort all accounts with queued transactions by heartbeat
  1440  	addresses := make(addressesByHeartbeat, 0, len(pool.queue))
  1441  	for addr := range pool.queue {
  1442  		if !pool.locals.contains(addr) { // don't drop locals
  1443  			addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]})
  1444  		}
  1445  	}
  1446  	sort.Sort(addresses)
  1447  
  1448  	// Drop transactions until the total is below the limit or only locals remain
  1449  	for drop := queued - pool.config.GlobalQueue; drop > 0 && len(addresses) > 0; {
  1450  		addr := addresses[len(addresses)-1]
  1451  		list := pool.queue[addr.address]
  1452  
  1453  		addresses = addresses[:len(addresses)-1]
  1454  
  1455  		// Drop all transactions if they are less than the overflow
  1456  		if size := uint64(list.Len()); size <= drop {
  1457  			for _, tx := range list.Flatten() {
  1458  				pool.removeTx(tx.Hash(), true)
  1459  			}
  1460  			drop -= size
  1461  			queuedRateLimitMeter.Mark(int64(size))
  1462  			continue
  1463  		}
  1464  		// Otherwise drop only last few transactions
  1465  		txs := list.Flatten()
  1466  		for i := len(txs) - 1; i >= 0 && drop > 0; i-- {
  1467  			pool.removeTx(txs[i].Hash(), true)
  1468  			drop--
  1469  			queuedRateLimitMeter.Mark(1)
  1470  		}
  1471  	}
  1472  }
  1473  
  1474  // demoteUnexecutables removes invalid and processed transactions from the pools
  1475  // executable/pending queue and any subsequent transactions that become unexecutable
  1476  // are moved back into the future queue.
  1477  //
  1478  // Note: transactions are not marked as removed in the priced list because re-heaping
  1479  // is always explicitly triggered by SetBaseFee and it would be unnecessary and wasteful
  1480  // to trigger a re-heap is this function
  1481  func (pool *TxPool) demoteUnexecutables() {
  1482  	// Iterate over all accounts and demote any non-executable transactions
  1483  	for addr, list := range pool.pending {
  1484  		nonce := pool.currentState.GetNonce(addr)
  1485  
  1486  		// Drop all transactions that are deemed too old (low nonce)
  1487  		olds := list.Forward(nonce)
  1488  		for _, tx := range olds {
  1489  			hash := tx.Hash()
  1490  			pool.all.Remove(hash)
  1491  			log.Trace("Removed old pending transaction", "hash", hash)
  1492  		}
  1493  		// Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later
  1494  		drops, invalids := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)
  1495  		for _, tx := range drops {
  1496  			hash := tx.Hash()
  1497  			log.Trace("Removed unpayable pending transaction", "hash", hash)
  1498  			pool.all.Remove(hash)
  1499  		}
  1500  		pendingNofundsMeter.Mark(int64(len(drops)))
  1501  
  1502  		for _, tx := range invalids {
  1503  			hash := tx.Hash()
  1504  			log.Trace("Demoting pending transaction", "hash", hash)
  1505  
  1506  			// Internal shuffle shouldn't touch the lookup set.
  1507  			pool.enqueueTx(hash, tx, false, false)
  1508  		}
  1509  		pendingGauge.Dec(int64(len(olds) + len(drops) + len(invalids)))
  1510  		if pool.locals.contains(addr) {
  1511  			localGauge.Dec(int64(len(olds) + len(drops) + len(invalids)))
  1512  		}
  1513  		// If there's a gap in front, alert (should never happen) and postpone all transactions
  1514  		if list.Len() > 0 && list.txs.Get(nonce) == nil {
  1515  			gapped := list.Cap(0)
  1516  			for _, tx := range gapped {
  1517  				hash := tx.Hash()
  1518  				log.Error("Demoting invalidated transaction", "hash", hash)
  1519  
  1520  				// Internal shuffle shouldn't touch the lookup set.
  1521  				pool.enqueueTx(hash, tx, false, false)
  1522  			}
  1523  			pendingGauge.Dec(int64(len(gapped)))
  1524  			// This might happen in a reorg, so log it to the metering
  1525  			blockReorgInvalidatedTx.Mark(int64(len(gapped)))
  1526  		}
  1527  		// Delete the entire pending entry if it became empty.
  1528  		if list.Empty() {
  1529  			delete(pool.pending, addr)
  1530  		}
  1531  	}
  1532  }
  1533  
  1534  // addressByHeartbeat is an account address tagged with its last activity timestamp.
  1535  type addressByHeartbeat struct {
  1536  	address   common.Address
  1537  	heartbeat time.Time
  1538  }
  1539  
  1540  type addressesByHeartbeat []addressByHeartbeat
  1541  
  1542  func (a addressesByHeartbeat) Len() int           { return len(a) }
  1543  func (a addressesByHeartbeat) Less(i, j int) bool { return a[i].heartbeat.Before(a[j].heartbeat) }
  1544  func (a addressesByHeartbeat) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
  1545  
  1546  // accountSet is simply a set of addresses to check for existence, and a signer
  1547  // capable of deriving addresses from transactions.
  1548  type accountSet struct {
  1549  	accounts map[common.Address]struct{}
  1550  	signer   types.Signer
  1551  	cache    *[]common.Address
  1552  }
  1553  
  1554  // newAccountSet creates a new address set with an associated signer for sender
  1555  // derivations.
  1556  func newAccountSet(signer types.Signer, addrs ...common.Address) *accountSet {
  1557  	as := &accountSet{
  1558  		accounts: make(map[common.Address]struct{}),
  1559  		signer:   signer,
  1560  	}
  1561  	for _, addr := range addrs {
  1562  		as.add(addr)
  1563  	}
  1564  	return as
  1565  }
  1566  
  1567  // contains checks if a given address is contained within the set.
  1568  func (as *accountSet) contains(addr common.Address) bool {
  1569  	_, exist := as.accounts[addr]
  1570  	return exist
  1571  }
  1572  
  1573  func (as *accountSet) empty() bool {
  1574  	return len(as.accounts) == 0
  1575  }
  1576  
  1577  // containsTx checks if the sender of a given tx is within the set. If the sender
  1578  // cannot be derived, this method returns false.
  1579  func (as *accountSet) containsTx(tx *types.Transaction) bool {
  1580  	if addr, err := types.Sender(as.signer, tx); err == nil {
  1581  		return as.contains(addr)
  1582  	}
  1583  	return false
  1584  }
  1585  
  1586  // add inserts a new address into the set to track.
  1587  func (as *accountSet) add(addr common.Address) {
  1588  	as.accounts[addr] = struct{}{}
  1589  	as.cache = nil
  1590  }
  1591  
  1592  // addTx adds the sender of tx into the set.
  1593  func (as *accountSet) addTx(tx *types.Transaction) {
  1594  	if addr, err := types.Sender(as.signer, tx); err == nil {
  1595  		as.add(addr)
  1596  	}
  1597  }
  1598  
  1599  // flatten returns the list of addresses within this set, also caching it for later
  1600  // reuse. The returned slice should not be changed!
  1601  func (as *accountSet) flatten() []common.Address {
  1602  	if as.cache == nil {
  1603  		accounts := make([]common.Address, 0, len(as.accounts))
  1604  		for account := range as.accounts {
  1605  			accounts = append(accounts, account)
  1606  		}
  1607  		as.cache = &accounts
  1608  	}
  1609  	return *as.cache
  1610  }
  1611  
  1612  // merge adds all addresses from the 'other' set into 'as'.
  1613  func (as *accountSet) merge(other *accountSet) {
  1614  	for addr := range other.accounts {
  1615  		as.accounts[addr] = struct{}{}
  1616  	}
  1617  	as.cache = nil
  1618  }
  1619  
  1620  // txLookup is used internally by TxPool to track transactions while allowing
  1621  // lookup without mutex contention.
  1622  //
  1623  // Note, although this type is properly protected against concurrent access, it
  1624  // is **not** a type that should ever be mutated or even exposed outside of the
  1625  // transaction pool, since its internal state is tightly coupled with the pools
  1626  // internal mechanisms. The sole purpose of the type is to permit out-of-bound
  1627  // peeking into the pool in TxPool.Get without having to acquire the widely scoped
  1628  // TxPool.mu mutex.
  1629  //
  1630  // This lookup set combines the notion of "local transactions", which is useful
  1631  // to build upper-level structure.
  1632  type txLookup struct {
  1633  	slots   int
  1634  	lock    sync.RWMutex
  1635  	locals  map[common.Hash]*types.Transaction
  1636  	remotes map[common.Hash]*types.Transaction
  1637  }
  1638  
  1639  // newTxLookup returns a new txLookup structure.
  1640  func newTxLookup() *txLookup {
  1641  	return &txLookup{
  1642  		locals:  make(map[common.Hash]*types.Transaction),
  1643  		remotes: make(map[common.Hash]*types.Transaction),
  1644  	}
  1645  }
  1646  
  1647  // Range calls f on each key and value present in the map. The callback passed
  1648  // should return the indicator whether the iteration needs to be continued.
  1649  // Callers need to specify which set (or both) to be iterated.
  1650  func (t *txLookup) Range(f func(hash common.Hash, tx *types.Transaction, local bool) bool, local bool, remote bool) {
  1651  	t.lock.RLock()
  1652  	defer t.lock.RUnlock()
  1653  
  1654  	if local {
  1655  		for key, value := range t.locals {
  1656  			if !f(key, value, true) {
  1657  				return
  1658  			}
  1659  		}
  1660  	}
  1661  	if remote {
  1662  		for key, value := range t.remotes {
  1663  			if !f(key, value, false) {
  1664  				return
  1665  			}
  1666  		}
  1667  	}
  1668  }
  1669  
  1670  // Get returns a transaction if it exists in the lookup, or nil if not found.
  1671  func (t *txLookup) Get(hash common.Hash) *types.Transaction {
  1672  	t.lock.RLock()
  1673  	defer t.lock.RUnlock()
  1674  
  1675  	if tx := t.locals[hash]; tx != nil {
  1676  		return tx
  1677  	}
  1678  	return t.remotes[hash]
  1679  }
  1680  
  1681  // GetLocal returns a transaction if it exists in the lookup, or nil if not found.
  1682  func (t *txLookup) GetLocal(hash common.Hash) *types.Transaction {
  1683  	t.lock.RLock()
  1684  	defer t.lock.RUnlock()
  1685  
  1686  	return t.locals[hash]
  1687  }
  1688  
  1689  // GetRemote returns a transaction if it exists in the lookup, or nil if not found.
  1690  func (t *txLookup) GetRemote(hash common.Hash) *types.Transaction {
  1691  	t.lock.RLock()
  1692  	defer t.lock.RUnlock()
  1693  
  1694  	return t.remotes[hash]
  1695  }
  1696  
  1697  // Count returns the current number of transactions in the lookup.
  1698  func (t *txLookup) Count() int {
  1699  	t.lock.RLock()
  1700  	defer t.lock.RUnlock()
  1701  
  1702  	return len(t.locals) + len(t.remotes)
  1703  }
  1704  
  1705  // LocalCount returns the current number of local transactions in the lookup.
  1706  func (t *txLookup) LocalCount() int {
  1707  	t.lock.RLock()
  1708  	defer t.lock.RUnlock()
  1709  
  1710  	return len(t.locals)
  1711  }
  1712  
  1713  // RemoteCount returns the current number of remote transactions in the lookup.
  1714  func (t *txLookup) RemoteCount() int {
  1715  	t.lock.RLock()
  1716  	defer t.lock.RUnlock()
  1717  
  1718  	return len(t.remotes)
  1719  }
  1720  
  1721  // Slots returns the current number of slots used in the lookup.
  1722  func (t *txLookup) Slots() int {
  1723  	t.lock.RLock()
  1724  	defer t.lock.RUnlock()
  1725  
  1726  	return t.slots
  1727  }
  1728  
  1729  // Add adds a transaction to the lookup.
  1730  func (t *txLookup) Add(tx *types.Transaction, local bool) {
  1731  	t.lock.Lock()
  1732  	defer t.lock.Unlock()
  1733  
  1734  	t.slots += numSlots(tx)
  1735  	slotsGauge.Update(int64(t.slots))
  1736  
  1737  	if local {
  1738  		t.locals[tx.Hash()] = tx
  1739  	} else {
  1740  		t.remotes[tx.Hash()] = tx
  1741  	}
  1742  }
  1743  
  1744  // Remove removes a transaction from the lookup.
  1745  func (t *txLookup) Remove(hash common.Hash) {
  1746  	t.lock.Lock()
  1747  	defer t.lock.Unlock()
  1748  
  1749  	tx, ok := t.locals[hash]
  1750  	if !ok {
  1751  		tx, ok = t.remotes[hash]
  1752  	}
  1753  	if !ok {
  1754  		log.Error("No transaction found to be deleted", "hash", hash)
  1755  		return
  1756  	}
  1757  	t.slots -= numSlots(tx)
  1758  	slotsGauge.Update(int64(t.slots))
  1759  
  1760  	delete(t.locals, hash)
  1761  	delete(t.remotes, hash)
  1762  }
  1763  
  1764  // RemoteToLocals migrates the transactions belongs to the given locals to locals
  1765  // set. The assumption is held the locals set is thread-safe to be used.
  1766  func (t *txLookup) RemoteToLocals(locals *accountSet) int {
  1767  	t.lock.Lock()
  1768  	defer t.lock.Unlock()
  1769  
  1770  	var migrated int
  1771  	for hash, tx := range t.remotes {
  1772  		if locals.containsTx(tx) {
  1773  			t.locals[hash] = tx
  1774  			delete(t.remotes, hash)
  1775  			migrated += 1
  1776  		}
  1777  	}
  1778  	return migrated
  1779  }
  1780  
  1781  // RemotesBelowTip finds all remote transactions below the given tip threshold.
  1782  func (t *txLookup) RemotesBelowTip(threshold *big.Int) types.Transactions {
  1783  	found := make(types.Transactions, 0, 128)
  1784  	t.Range(func(hash common.Hash, tx *types.Transaction, local bool) bool {
  1785  		if tx.GasTipCapIntCmp(threshold) < 0 {
  1786  			found = append(found, tx)
  1787  		}
  1788  		return true
  1789  	}, false, true) // Only iterate remotes
  1790  	return found
  1791  }
  1792  
  1793  // numSlots calculates the number of slots needed for a single transaction.
  1794  func numSlots(tx *types.Transaction) int {
  1795  	return int((tx.Size() + txSlotSize - 1) / txSlotSize)
  1796  }