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