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