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