github.com/ylsGit/go-ethereum@v1.6.5/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/big"
    23  	"sort"
    24  	"sync"
    25  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/core/state"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/event"
    31  	"github.com/ethereum/go-ethereum/log"
    32  	"github.com/ethereum/go-ethereum/metrics"
    33  	"github.com/ethereum/go-ethereum/params"
    34  	"gopkg.in/karalabe/cookiejar.v2/collections/prque"
    35  )
    36  
    37  var (
    38  	// Transaction Pool Errors
    39  	ErrInvalidSender      = errors.New("invalid sender")
    40  	ErrNonce              = errors.New("nonce too low")
    41  	ErrUnderpriced        = errors.New("transaction underpriced")
    42  	ErrReplaceUnderpriced = errors.New("replacement transaction underpriced")
    43  	ErrBalance            = errors.New("insufficient balance")
    44  	ErrInsufficientFunds  = errors.New("insufficient funds for gas * price + value")
    45  	ErrIntrinsicGas       = errors.New("intrinsic gas too low")
    46  	ErrGasLimit           = errors.New("exceeds block gas limit")
    47  	ErrNegativeValue      = errors.New("negative value")
    48  )
    49  
    50  var (
    51  	evictionInterval    = time.Minute     // Time interval to check for evictable transactions
    52  	statsReportInterval = 8 * time.Second // Time interval to report transaction pool stats
    53  )
    54  
    55  var (
    56  	// Metrics for the pending pool
    57  	pendingDiscardCounter = metrics.NewCounter("txpool/pending/discard")
    58  	pendingReplaceCounter = metrics.NewCounter("txpool/pending/replace")
    59  	pendingRLCounter      = metrics.NewCounter("txpool/pending/ratelimit") // Dropped due to rate limiting
    60  	pendingNofundsCounter = metrics.NewCounter("txpool/pending/nofunds")   // Dropped due to out-of-funds
    61  
    62  	// Metrics for the queued pool
    63  	queuedDiscardCounter = metrics.NewCounter("txpool/queued/discard")
    64  	queuedReplaceCounter = metrics.NewCounter("txpool/queued/replace")
    65  	queuedRLCounter      = metrics.NewCounter("txpool/queued/ratelimit") // Dropped due to rate limiting
    66  	queuedNofundsCounter = metrics.NewCounter("txpool/queued/nofunds")   // Dropped due to out-of-funds
    67  
    68  	// General tx metrics
    69  	invalidTxCounter     = metrics.NewCounter("txpool/invalid")
    70  	underpricedTxCounter = metrics.NewCounter("txpool/underpriced")
    71  )
    72  
    73  type stateFn func() (*state.StateDB, error)
    74  
    75  // TxPoolConfig are the configuration parameters of the transaction pool.
    76  type TxPoolConfig struct {
    77  	PriceLimit uint64 // Minimum gas price to enforce for acceptance into the pool
    78  	PriceBump  uint64 // Minimum price bump percentage to replace an already existing transaction (nonce)
    79  
    80  	AccountSlots uint64 // Minimum number of executable transaction slots guaranteed per account
    81  	GlobalSlots  uint64 // Maximum number of executable transaction slots for all accounts
    82  	AccountQueue uint64 // Maximum number of non-executable transaction slots permitted per account
    83  	GlobalQueue  uint64 // Maximum number of non-executable transaction slots for all accounts
    84  
    85  	Lifetime time.Duration // Maximum amount of time non-executable transaction are queued
    86  }
    87  
    88  // DefaultTxPoolConfig contains the default configurations for the transaction
    89  // pool.
    90  var DefaultTxPoolConfig = TxPoolConfig{
    91  	PriceLimit: 1,
    92  	PriceBump:  10,
    93  
    94  	AccountSlots: 16,
    95  	GlobalSlots:  4096,
    96  	AccountQueue: 64,
    97  	GlobalQueue:  1024,
    98  
    99  	Lifetime: 3 * time.Hour,
   100  }
   101  
   102  // sanitize checks the provided user configurations and changes anything that's
   103  // unreasonable or unworkable.
   104  func (config *TxPoolConfig) sanitize() TxPoolConfig {
   105  	conf := *config
   106  	if conf.PriceLimit < 1 {
   107  		log.Warn("Sanitizing invalid txpool price limit", "provided", conf.PriceLimit, "updated", DefaultTxPoolConfig.PriceLimit)
   108  		conf.PriceLimit = DefaultTxPoolConfig.PriceLimit
   109  	}
   110  	if conf.PriceBump < 1 {
   111  		log.Warn("Sanitizing invalid txpool price bump", "provided", conf.PriceBump, "updated", DefaultTxPoolConfig.PriceBump)
   112  		conf.PriceBump = DefaultTxPoolConfig.PriceBump
   113  	}
   114  	return conf
   115  }
   116  
   117  // TxPool contains all currently known transactions. Transactions
   118  // enter the pool when they are received from the network or submitted
   119  // locally. They exit the pool when they are included in the blockchain.
   120  //
   121  // The pool separates processable transactions (which can be applied to the
   122  // current state) and future transactions. Transactions move between those
   123  // two states over time as they are received and processed.
   124  type TxPool struct {
   125  	config       TxPoolConfig
   126  	chainconfig  *params.ChainConfig
   127  	currentState stateFn // The state function which will allow us to do some pre checks
   128  	pendingState *state.ManagedState
   129  	gasLimit     func() *big.Int // The current gas limit function callback
   130  	gasPrice     *big.Int
   131  	eventMux     *event.TypeMux
   132  	events       *event.TypeMuxSubscription
   133  	locals       *txSet
   134  	signer       types.Signer
   135  	mu           sync.RWMutex
   136  
   137  	pending map[common.Address]*txList         // All currently processable transactions
   138  	queue   map[common.Address]*txList         // Queued but non-processable transactions
   139  	beats   map[common.Address]time.Time       // Last heartbeat from each known account
   140  	all     map[common.Hash]*types.Transaction // All transactions to allow lookups
   141  	priced  *txPricedList                      // All transactions sorted by price
   142  
   143  	wg   sync.WaitGroup // for shutdown sync
   144  	quit chan struct{}
   145  
   146  	homestead bool
   147  }
   148  
   149  // NewTxPool creates a new transaction pool to gather, sort and filter inbound
   150  // trnsactions from the network.
   151  func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func() *big.Int) *TxPool {
   152  	// Sanitize the input to ensure no vulnerable gas prices are set
   153  	config = (&config).sanitize()
   154  
   155  	// Create the transaction pool with its initial settings
   156  	pool := &TxPool{
   157  		config:       config,
   158  		chainconfig:  chainconfig,
   159  		signer:       types.NewEIP155Signer(chainconfig.ChainId),
   160  		pending:      make(map[common.Address]*txList),
   161  		queue:        make(map[common.Address]*txList),
   162  		beats:        make(map[common.Address]time.Time),
   163  		all:          make(map[common.Hash]*types.Transaction),
   164  		eventMux:     eventMux,
   165  		currentState: currentStateFn,
   166  		gasLimit:     gasLimitFn,
   167  		gasPrice:     new(big.Int).SetUint64(config.PriceLimit),
   168  		pendingState: nil,
   169  		locals:       newTxSet(),
   170  		events:       eventMux.Subscribe(ChainHeadEvent{}, RemovedTransactionEvent{}),
   171  		quit:         make(chan struct{}),
   172  	}
   173  	pool.priced = newTxPricedList(&pool.all)
   174  	pool.resetState()
   175  
   176  	// Start the various events loops and return
   177  	pool.wg.Add(2)
   178  	go pool.eventLoop()
   179  	go pool.expirationLoop()
   180  
   181  	return pool
   182  }
   183  
   184  func (pool *TxPool) eventLoop() {
   185  	defer pool.wg.Done()
   186  
   187  	// Start a ticker and keep track of interesting pool stats to report
   188  	var prevPending, prevQueued, prevStales int
   189  
   190  	report := time.NewTicker(statsReportInterval)
   191  	defer report.Stop()
   192  
   193  	// Track chain events. When a chain events occurs (new chain canon block)
   194  	// we need to know the new state. The new state will help us determine
   195  	// the nonces in the managed state
   196  	for {
   197  		select {
   198  		// Handle any events fired by the system
   199  		case ev, ok := <-pool.events.Chan():
   200  			if !ok {
   201  				return
   202  			}
   203  			switch ev := ev.Data.(type) {
   204  			case ChainHeadEvent:
   205  				pool.mu.Lock()
   206  				if ev.Block != nil {
   207  					if pool.chainconfig.IsHomestead(ev.Block.Number()) {
   208  						pool.homestead = true
   209  					}
   210  				}
   211  				pool.resetState()
   212  				pool.mu.Unlock()
   213  
   214  			case RemovedTransactionEvent:
   215  				pool.AddBatch(ev.Txs)
   216  			}
   217  
   218  		// Handle stats reporting ticks
   219  		case <-report.C:
   220  			pool.mu.RLock()
   221  			pending, queued := pool.stats()
   222  			stales := pool.priced.stales
   223  			pool.mu.RUnlock()
   224  
   225  			if pending != prevPending || queued != prevQueued || stales != prevStales {
   226  				log.Debug("Transaction pool status report", "executable", pending, "queued", queued, "stales", stales)
   227  				prevPending, prevQueued, prevStales = pending, queued, stales
   228  			}
   229  		}
   230  	}
   231  }
   232  
   233  func (pool *TxPool) resetState() {
   234  	currentState, err := pool.currentState()
   235  	if err != nil {
   236  		log.Error("Failed reset txpool state", "err", err)
   237  		return
   238  	}
   239  	pool.pendingState = state.ManageState(currentState)
   240  
   241  	// validate the pool of pending transactions, this will remove
   242  	// any transactions that have been included in the block or
   243  	// have been invalidated because of another transaction (e.g.
   244  	// higher gas price)
   245  	pool.demoteUnexecutables(currentState)
   246  
   247  	// Update all accounts to the latest known pending nonce
   248  	for addr, list := range pool.pending {
   249  		txs := list.Flatten() // Heavy but will be cached and is needed by the miner anyway
   250  		pool.pendingState.SetNonce(addr, txs[len(txs)-1].Nonce()+1)
   251  	}
   252  	// Check the queue and move transactions over to the pending if possible
   253  	// or remove those that have become invalid
   254  	pool.promoteExecutables(currentState, nil)
   255  }
   256  
   257  // Stop terminates the transaction pool.
   258  func (pool *TxPool) Stop() {
   259  	pool.events.Unsubscribe()
   260  	close(pool.quit)
   261  	pool.wg.Wait()
   262  
   263  	log.Info("Transaction pool stopped")
   264  }
   265  
   266  // GasPrice returns the current gas price enforced by the transaction pool.
   267  func (pool *TxPool) GasPrice() *big.Int {
   268  	pool.mu.RLock()
   269  	defer pool.mu.RUnlock()
   270  
   271  	return new(big.Int).Set(pool.gasPrice)
   272  }
   273  
   274  // SetGasPrice updates the minimum price required by the transaction pool for a
   275  // new transaction, and drops all transactions below this threshold.
   276  func (pool *TxPool) SetGasPrice(price *big.Int) {
   277  	pool.mu.Lock()
   278  	defer pool.mu.Unlock()
   279  
   280  	pool.gasPrice = price
   281  	for _, tx := range pool.priced.Cap(price, pool.locals) {
   282  		pool.removeTx(tx.Hash())
   283  	}
   284  	log.Info("Transaction pool price threshold updated", "price", price)
   285  }
   286  
   287  // State returns the virtual managed state of the transaction pool.
   288  func (pool *TxPool) State() *state.ManagedState {
   289  	pool.mu.RLock()
   290  	defer pool.mu.RUnlock()
   291  
   292  	return pool.pendingState
   293  }
   294  
   295  // Stats retrieves the current pool stats, namely the number of pending and the
   296  // number of queued (non-executable) transactions.
   297  func (pool *TxPool) Stats() (int, int) {
   298  	pool.mu.RLock()
   299  	defer pool.mu.RUnlock()
   300  
   301  	return pool.stats()
   302  }
   303  
   304  // stats retrieves the current pool stats, namely the number of pending and the
   305  // number of queued (non-executable) transactions.
   306  func (pool *TxPool) stats() (int, int) {
   307  	pending := 0
   308  	for _, list := range pool.pending {
   309  		pending += list.Len()
   310  	}
   311  	queued := 0
   312  	for _, list := range pool.queue {
   313  		queued += list.Len()
   314  	}
   315  	return pending, queued
   316  }
   317  
   318  // Content retrieves the data content of the transaction pool, returning all the
   319  // pending as well as queued transactions, grouped by account and sorted by nonce.
   320  func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   321  	pool.mu.RLock()
   322  	defer pool.mu.RUnlock()
   323  
   324  	pending := make(map[common.Address]types.Transactions)
   325  	for addr, list := range pool.pending {
   326  		pending[addr] = list.Flatten()
   327  	}
   328  	queued := make(map[common.Address]types.Transactions)
   329  	for addr, list := range pool.queue {
   330  		queued[addr] = list.Flatten()
   331  	}
   332  	return pending, queued
   333  }
   334  
   335  // Pending retrieves all currently processable transactions, groupped by origin
   336  // account and sorted by nonce. The returned transaction set is a copy and can be
   337  // freely modified by calling code.
   338  func (pool *TxPool) Pending() (map[common.Address]types.Transactions, error) {
   339  	pool.mu.Lock()
   340  	defer pool.mu.Unlock()
   341  
   342  	pending := make(map[common.Address]types.Transactions)
   343  	for addr, list := range pool.pending {
   344  		pending[addr] = list.Flatten()
   345  	}
   346  	return pending, nil
   347  }
   348  
   349  // SetLocal marks a transaction as local, skipping gas price
   350  //  check against local miner minimum in the future
   351  func (pool *TxPool) SetLocal(tx *types.Transaction) {
   352  	pool.mu.Lock()
   353  	defer pool.mu.Unlock()
   354  	pool.locals.add(tx.Hash())
   355  }
   356  
   357  // validateTx checks whether a transaction is valid according
   358  // to the consensus rules.
   359  func (pool *TxPool) validateTx(tx *types.Transaction) error {
   360  	local := pool.locals.contains(tx.Hash())
   361  	// Drop transactions under our own minimal accepted gas price
   362  	if !local && pool.gasPrice.Cmp(tx.GasPrice()) > 0 {
   363  		return ErrUnderpriced
   364  	}
   365  
   366  	currentState, err := pool.currentState()
   367  	if err != nil {
   368  		return err
   369  	}
   370  
   371  	from, err := types.Sender(pool.signer, tx)
   372  	if err != nil {
   373  		return ErrInvalidSender
   374  	}
   375  	// Last but not least check for nonce errors
   376  	if currentState.GetNonce(from) > tx.Nonce() {
   377  		return ErrNonce
   378  	}
   379  
   380  	// Check the transaction doesn't exceed the current
   381  	// block limit gas.
   382  	if pool.gasLimit().Cmp(tx.Gas()) < 0 {
   383  		return ErrGasLimit
   384  	}
   385  
   386  	// Transactions can't be negative. This may never happen
   387  	// using RLP decoded transactions but may occur if you create
   388  	// a transaction using the RPC for example.
   389  	if tx.Value().Sign() < 0 {
   390  		return ErrNegativeValue
   391  	}
   392  
   393  	// Transactor should have enough funds to cover the costs
   394  	// cost == V + GP * GL
   395  	if currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
   396  		return ErrInsufficientFunds
   397  	}
   398  
   399  	intrGas := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
   400  	if tx.Gas().Cmp(intrGas) < 0 {
   401  		return ErrIntrinsicGas
   402  	}
   403  
   404  	return nil
   405  }
   406  
   407  // add validates a transaction and inserts it into the non-executable queue for
   408  // later pending promotion and execution. If the transaction is a replacement for
   409  // an already pending or queued one, it overwrites the previous and returns this
   410  // so outer code doesn't uselessly call promote.
   411  func (pool *TxPool) add(tx *types.Transaction) (bool, error) {
   412  	// If the transaction is already known, discard it
   413  	hash := tx.Hash()
   414  	if pool.all[hash] != nil {
   415  		log.Trace("Discarding already known transaction", "hash", hash)
   416  		return false, fmt.Errorf("known transaction: %x", hash)
   417  	}
   418  	// If the transaction fails basic validation, discard it
   419  	if err := pool.validateTx(tx); err != nil {
   420  		log.Trace("Discarding invalid transaction", "hash", hash, "err", err)
   421  		invalidTxCounter.Inc(1)
   422  		return false, err
   423  	}
   424  	// If the transaction pool is full, discard underpriced transactions
   425  	if uint64(len(pool.all)) >= pool.config.GlobalSlots+pool.config.GlobalQueue {
   426  		// If the new transaction is underpriced, don't accept it
   427  		if pool.priced.Underpriced(tx, pool.locals) {
   428  			log.Trace("Discarding underpriced transaction", "hash", hash, "price", tx.GasPrice())
   429  			underpricedTxCounter.Inc(1)
   430  			return false, ErrUnderpriced
   431  		}
   432  		// New transaction is better than our worse ones, make room for it
   433  		drop := pool.priced.Discard(len(pool.all)-int(pool.config.GlobalSlots+pool.config.GlobalQueue-1), pool.locals)
   434  		for _, tx := range drop {
   435  			log.Trace("Discarding freshly underpriced transaction", "hash", tx.Hash(), "price", tx.GasPrice())
   436  			underpricedTxCounter.Inc(1)
   437  			pool.removeTx(tx.Hash())
   438  		}
   439  	}
   440  	// If the transaction is replacing an already pending one, do directly
   441  	from, _ := types.Sender(pool.signer, tx) // already validated
   442  	if list := pool.pending[from]; list != nil && list.Overlaps(tx) {
   443  		// Nonce already pending, check if required price bump is met
   444  		inserted, old := list.Add(tx, pool.config.PriceBump)
   445  		if !inserted {
   446  			pendingDiscardCounter.Inc(1)
   447  			return false, ErrReplaceUnderpriced
   448  		}
   449  		// New transaction is better, replace old one
   450  		if old != nil {
   451  			delete(pool.all, old.Hash())
   452  			pool.priced.Removed()
   453  			pendingReplaceCounter.Inc(1)
   454  		}
   455  		pool.all[tx.Hash()] = tx
   456  		pool.priced.Put(tx)
   457  
   458  		log.Trace("Pooled new executable transaction", "hash", hash, "from", from, "to", tx.To())
   459  		return old != nil, nil
   460  	}
   461  	// New transaction isn't replacing a pending one, push into queue
   462  	replace, err := pool.enqueueTx(hash, tx)
   463  	if err != nil {
   464  		return false, err
   465  	}
   466  	log.Trace("Pooled new future transaction", "hash", hash, "from", from, "to", tx.To())
   467  	return replace, nil
   468  }
   469  
   470  // enqueueTx inserts a new transaction into the non-executable transaction queue.
   471  //
   472  // Note, this method assumes the pool lock is held!
   473  func (pool *TxPool) enqueueTx(hash common.Hash, tx *types.Transaction) (bool, error) {
   474  	// Try to insert the transaction into the future queue
   475  	from, _ := types.Sender(pool.signer, tx) // already validated
   476  	if pool.queue[from] == nil {
   477  		pool.queue[from] = newTxList(false)
   478  	}
   479  	inserted, old := pool.queue[from].Add(tx, pool.config.PriceBump)
   480  	if !inserted {
   481  		// An older transaction was better, discard this
   482  		queuedDiscardCounter.Inc(1)
   483  		return false, ErrReplaceUnderpriced
   484  	}
   485  	// Discard any previous transaction and mark this
   486  	if old != nil {
   487  		delete(pool.all, old.Hash())
   488  		pool.priced.Removed()
   489  		queuedReplaceCounter.Inc(1)
   490  	}
   491  	pool.all[hash] = tx
   492  	pool.priced.Put(tx)
   493  	return old != nil, nil
   494  }
   495  
   496  // promoteTx adds a transaction to the pending (processable) list of transactions.
   497  //
   498  // Note, this method assumes the pool lock is held!
   499  func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) {
   500  	// Try to insert the transaction into the pending queue
   501  	if pool.pending[addr] == nil {
   502  		pool.pending[addr] = newTxList(true)
   503  	}
   504  	list := pool.pending[addr]
   505  
   506  	inserted, old := list.Add(tx, pool.config.PriceBump)
   507  	if !inserted {
   508  		// An older transaction was better, discard this
   509  		delete(pool.all, hash)
   510  		pool.priced.Removed()
   511  
   512  		pendingDiscardCounter.Inc(1)
   513  		return
   514  	}
   515  	// Otherwise discard any previous transaction and mark this
   516  	if old != nil {
   517  		delete(pool.all, old.Hash())
   518  		pool.priced.Removed()
   519  
   520  		pendingReplaceCounter.Inc(1)
   521  	}
   522  	// Failsafe to work around direct pending inserts (tests)
   523  	if pool.all[hash] == nil {
   524  		pool.all[hash] = tx
   525  		pool.priced.Put(tx)
   526  	}
   527  	// Set the potentially new pending nonce and notify any subsystems of the new tx
   528  	pool.beats[addr] = time.Now()
   529  	pool.pendingState.SetNonce(addr, tx.Nonce()+1)
   530  	go pool.eventMux.Post(TxPreEvent{tx})
   531  }
   532  
   533  // Add queues a single transaction in the pool if it is valid.
   534  func (pool *TxPool) Add(tx *types.Transaction) error {
   535  	pool.mu.Lock()
   536  	defer pool.mu.Unlock()
   537  
   538  	// Try to inject the transaction and update any state
   539  	replace, err := pool.add(tx)
   540  	if err != nil {
   541  		return err
   542  	}
   543  	// If we added a new transaction, run promotion checks and return
   544  	if !replace {
   545  		state, err := pool.currentState()
   546  		if err != nil {
   547  			return err
   548  		}
   549  		from, _ := types.Sender(pool.signer, tx) // already validated
   550  		pool.promoteExecutables(state, []common.Address{from})
   551  	}
   552  	return nil
   553  }
   554  
   555  // AddBatch attempts to queue a batch of transactions.
   556  func (pool *TxPool) AddBatch(txs []*types.Transaction) error {
   557  	pool.mu.Lock()
   558  	defer pool.mu.Unlock()
   559  
   560  	// Add the batch of transaction, tracking the accepted ones
   561  	dirty := make(map[common.Address]struct{})
   562  	for _, tx := range txs {
   563  		if replace, err := pool.add(tx); err == nil {
   564  			if !replace {
   565  				from, _ := types.Sender(pool.signer, tx) // already validated
   566  				dirty[from] = struct{}{}
   567  			}
   568  		}
   569  	}
   570  	// Only reprocess the internal state if something was actually added
   571  	if len(dirty) > 0 {
   572  		state, err := pool.currentState()
   573  		if err != nil {
   574  			return err
   575  		}
   576  		addrs := make([]common.Address, 0, len(dirty))
   577  		for addr, _ := range dirty {
   578  			addrs = append(addrs, addr)
   579  		}
   580  		pool.promoteExecutables(state, addrs)
   581  	}
   582  	return nil
   583  }
   584  
   585  // Get returns a transaction if it is contained in the pool
   586  // and nil otherwise.
   587  func (pool *TxPool) Get(hash common.Hash) *types.Transaction {
   588  	pool.mu.RLock()
   589  	defer pool.mu.RUnlock()
   590  
   591  	return pool.all[hash]
   592  }
   593  
   594  // Remove removes the transaction with the given hash from the pool.
   595  func (pool *TxPool) Remove(hash common.Hash) {
   596  	pool.mu.Lock()
   597  	defer pool.mu.Unlock()
   598  
   599  	pool.removeTx(hash)
   600  }
   601  
   602  // RemoveBatch removes all given transactions from the pool.
   603  func (pool *TxPool) RemoveBatch(txs types.Transactions) {
   604  	pool.mu.Lock()
   605  	defer pool.mu.Unlock()
   606  
   607  	for _, tx := range txs {
   608  		pool.removeTx(tx.Hash())
   609  	}
   610  }
   611  
   612  // removeTx removes a single transaction from the queue, moving all subsequent
   613  // transactions back to the future queue.
   614  func (pool *TxPool) removeTx(hash common.Hash) {
   615  	// Fetch the transaction we wish to delete
   616  	tx, ok := pool.all[hash]
   617  	if !ok {
   618  		return
   619  	}
   620  	addr, _ := types.Sender(pool.signer, tx) // already validated during insertion
   621  
   622  	// Remove it from the list of known transactions
   623  	delete(pool.all, hash)
   624  	pool.priced.Removed()
   625  
   626  	// Remove the transaction from the pending lists and reset the account nonce
   627  	if pending := pool.pending[addr]; pending != nil {
   628  		if removed, invalids := pending.Remove(tx); removed {
   629  			// If no more transactions are left, remove the list
   630  			if pending.Empty() {
   631  				delete(pool.pending, addr)
   632  				delete(pool.beats, addr)
   633  			} else {
   634  				// Otherwise postpone any invalidated transactions
   635  				for _, tx := range invalids {
   636  					pool.enqueueTx(tx.Hash(), tx)
   637  				}
   638  			}
   639  			// Update the account nonce if needed
   640  			if nonce := tx.Nonce(); pool.pendingState.GetNonce(addr) > nonce {
   641  				pool.pendingState.SetNonce(addr, tx.Nonce())
   642  			}
   643  		}
   644  	}
   645  	// Transaction is in the future queue
   646  	if future := pool.queue[addr]; future != nil {
   647  		future.Remove(tx)
   648  		if future.Empty() {
   649  			delete(pool.queue, addr)
   650  		}
   651  	}
   652  }
   653  
   654  // promoteExecutables moves transactions that have become processable from the
   655  // future queue to the set of pending transactions. During this process, all
   656  // invalidated transactions (low nonce, low balance) are deleted.
   657  func (pool *TxPool) promoteExecutables(state *state.StateDB, accounts []common.Address) {
   658  	gaslimit := pool.gasLimit()
   659  
   660  	// Gather all the accounts potentially needing updates
   661  	if accounts == nil {
   662  		accounts = make([]common.Address, 0, len(pool.queue))
   663  		for addr, _ := range pool.queue {
   664  			accounts = append(accounts, addr)
   665  		}
   666  	}
   667  	// Iterate over all accounts and promote any executable transactions
   668  	queued := uint64(0)
   669  	for _, addr := range accounts {
   670  		list := pool.queue[addr]
   671  		if list == nil {
   672  			continue // Just in case someone calls with a non existing account
   673  		}
   674  		// Drop all transactions that are deemed too old (low nonce)
   675  		for _, tx := range list.Forward(state.GetNonce(addr)) {
   676  			hash := tx.Hash()
   677  			log.Trace("Removed old queued transaction", "hash", hash)
   678  			delete(pool.all, hash)
   679  			pool.priced.Removed()
   680  		}
   681  		// Drop all transactions that are too costly (low balance or out of gas)
   682  		drops, _ := list.Filter(state.GetBalance(addr), gaslimit)
   683  		for _, tx := range drops {
   684  			hash := tx.Hash()
   685  			log.Trace("Removed unpayable queued transaction", "hash", hash)
   686  			delete(pool.all, hash)
   687  			pool.priced.Removed()
   688  			queuedNofundsCounter.Inc(1)
   689  		}
   690  		// Gather all executable transactions and promote them
   691  		for _, tx := range list.Ready(pool.pendingState.GetNonce(addr)) {
   692  			hash := tx.Hash()
   693  			log.Trace("Promoting queued transaction", "hash", hash)
   694  			pool.promoteTx(addr, hash, tx)
   695  		}
   696  		// Drop all transactions over the allowed limit
   697  		for _, tx := range list.Cap(int(pool.config.AccountQueue)) {
   698  			hash := tx.Hash()
   699  			log.Trace("Removed cap-exceeding queued transaction", "hash", hash)
   700  			delete(pool.all, hash)
   701  			pool.priced.Removed()
   702  			queuedRLCounter.Inc(1)
   703  		}
   704  		queued += uint64(list.Len())
   705  
   706  		// Delete the entire queue entry if it became empty.
   707  		if list.Empty() {
   708  			delete(pool.queue, addr)
   709  		}
   710  	}
   711  	// If the pending limit is overflown, start equalizing allowances
   712  	pending := uint64(0)
   713  	for _, list := range pool.pending {
   714  		pending += uint64(list.Len())
   715  	}
   716  	if pending > pool.config.GlobalSlots {
   717  		pendingBeforeCap := pending
   718  		// Assemble a spam order to penalize large transactors first
   719  		spammers := prque.New()
   720  		for addr, list := range pool.pending {
   721  			// Only evict transactions from high rollers
   722  			if uint64(list.Len()) > pool.config.AccountSlots {
   723  				// Skip local accounts as pools should maintain backlogs for themselves
   724  				for _, tx := range list.txs.items {
   725  					if !pool.locals.contains(tx.Hash()) {
   726  						spammers.Push(addr, float32(list.Len()))
   727  					}
   728  					break // Checking on transaction for locality is enough
   729  				}
   730  			}
   731  		}
   732  		// Gradually drop transactions from offenders
   733  		offenders := []common.Address{}
   734  		for pending > pool.config.GlobalSlots && !spammers.Empty() {
   735  			// Retrieve the next offender if not local address
   736  			offender, _ := spammers.Pop()
   737  			offenders = append(offenders, offender.(common.Address))
   738  
   739  			// Equalize balances until all the same or below threshold
   740  			if len(offenders) > 1 {
   741  				// Calculate the equalization threshold for all current offenders
   742  				threshold := pool.pending[offender.(common.Address)].Len()
   743  
   744  				// Iteratively reduce all offenders until below limit or threshold reached
   745  				for pending > pool.config.GlobalSlots && pool.pending[offenders[len(offenders)-2]].Len() > threshold {
   746  					for i := 0; i < len(offenders)-1; i++ {
   747  						list := pool.pending[offenders[i]]
   748  						list.Cap(list.Len() - 1)
   749  						pending--
   750  					}
   751  				}
   752  			}
   753  		}
   754  		// If still above threshold, reduce to limit or min allowance
   755  		if pending > pool.config.GlobalSlots && len(offenders) > 0 {
   756  			for pending > pool.config.GlobalSlots && uint64(pool.pending[offenders[len(offenders)-1]].Len()) > pool.config.AccountSlots {
   757  				for _, addr := range offenders {
   758  					list := pool.pending[addr]
   759  					list.Cap(list.Len() - 1)
   760  					pending--
   761  				}
   762  			}
   763  		}
   764  		pendingRLCounter.Inc(int64(pendingBeforeCap - pending))
   765  	}
   766  	// If we've queued more transactions than the hard limit, drop oldest ones
   767  	if queued > pool.config.GlobalQueue {
   768  		// Sort all accounts with queued transactions by heartbeat
   769  		addresses := make(addresssByHeartbeat, 0, len(pool.queue))
   770  		for addr := range pool.queue {
   771  			addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]})
   772  		}
   773  		sort.Sort(addresses)
   774  
   775  		// Drop transactions until the total is below the limit
   776  		for drop := queued - pool.config.GlobalQueue; drop > 0; {
   777  			addr := addresses[len(addresses)-1]
   778  			list := pool.queue[addr.address]
   779  
   780  			addresses = addresses[:len(addresses)-1]
   781  
   782  			// Drop all transactions if they are less than the overflow
   783  			if size := uint64(list.Len()); size <= drop {
   784  				for _, tx := range list.Flatten() {
   785  					pool.removeTx(tx.Hash())
   786  				}
   787  				drop -= size
   788  				queuedRLCounter.Inc(int64(size))
   789  				continue
   790  			}
   791  			// Otherwise drop only last few transactions
   792  			txs := list.Flatten()
   793  			for i := len(txs) - 1; i >= 0 && drop > 0; i-- {
   794  				pool.removeTx(txs[i].Hash())
   795  				drop--
   796  				queuedRLCounter.Inc(1)
   797  			}
   798  		}
   799  	}
   800  }
   801  
   802  // demoteUnexecutables removes invalid and processed transactions from the pools
   803  // executable/pending queue and any subsequent transactions that become unexecutable
   804  // are moved back into the future queue.
   805  func (pool *TxPool) demoteUnexecutables(state *state.StateDB) {
   806  	gaslimit := pool.gasLimit()
   807  
   808  	// Iterate over all accounts and demote any non-executable transactions
   809  	for addr, list := range pool.pending {
   810  		nonce := state.GetNonce(addr)
   811  
   812  		// Drop all transactions that are deemed too old (low nonce)
   813  		for _, tx := range list.Forward(nonce) {
   814  			hash := tx.Hash()
   815  			log.Trace("Removed old pending transaction", "hash", hash)
   816  			delete(pool.all, hash)
   817  			pool.priced.Removed()
   818  		}
   819  		// Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later
   820  		drops, invalids := list.Filter(state.GetBalance(addr), gaslimit)
   821  		for _, tx := range drops {
   822  			hash := tx.Hash()
   823  			log.Trace("Removed unpayable pending transaction", "hash", hash)
   824  			delete(pool.all, hash)
   825  			pool.priced.Removed()
   826  			pendingNofundsCounter.Inc(1)
   827  		}
   828  		for _, tx := range invalids {
   829  			hash := tx.Hash()
   830  			log.Trace("Demoting pending transaction", "hash", hash)
   831  			pool.enqueueTx(hash, tx)
   832  		}
   833  		// Delete the entire queue entry if it became empty.
   834  		if list.Empty() {
   835  			delete(pool.pending, addr)
   836  			delete(pool.beats, addr)
   837  		}
   838  	}
   839  }
   840  
   841  // expirationLoop is a loop that periodically iterates over all accounts with
   842  // queued transactions and drop all that have been inactive for a prolonged amount
   843  // of time.
   844  func (pool *TxPool) expirationLoop() {
   845  	defer pool.wg.Done()
   846  
   847  	evict := time.NewTicker(evictionInterval)
   848  	defer evict.Stop()
   849  
   850  	for {
   851  		select {
   852  		case <-evict.C:
   853  			pool.mu.Lock()
   854  			for addr := range pool.queue {
   855  				if time.Since(pool.beats[addr]) > pool.config.Lifetime {
   856  					for _, tx := range pool.queue[addr].Flatten() {
   857  						pool.removeTx(tx.Hash())
   858  					}
   859  				}
   860  			}
   861  			pool.mu.Unlock()
   862  
   863  		case <-pool.quit:
   864  			return
   865  		}
   866  	}
   867  }
   868  
   869  // addressByHeartbeat is an account address tagged with its last activity timestamp.
   870  type addressByHeartbeat struct {
   871  	address   common.Address
   872  	heartbeat time.Time
   873  }
   874  
   875  type addresssByHeartbeat []addressByHeartbeat
   876  
   877  func (a addresssByHeartbeat) Len() int           { return len(a) }
   878  func (a addresssByHeartbeat) Less(i, j int) bool { return a[i].heartbeat.Before(a[j].heartbeat) }
   879  func (a addresssByHeartbeat) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   880  
   881  // txSet represents a set of transaction hashes in which entries
   882  //  are automatically dropped after txSetDuration time
   883  type txSet struct {
   884  	txMap          map[common.Hash]struct{}
   885  	txOrd          map[uint64]txOrdType
   886  	addPtr, delPtr uint64
   887  }
   888  
   889  const txSetDuration = time.Hour * 2
   890  
   891  // txOrdType represents an entry in the time-ordered list of transaction hashes
   892  type txOrdType struct {
   893  	hash common.Hash
   894  	time time.Time
   895  }
   896  
   897  // newTxSet creates a new transaction set
   898  func newTxSet() *txSet {
   899  	return &txSet{
   900  		txMap: make(map[common.Hash]struct{}),
   901  		txOrd: make(map[uint64]txOrdType),
   902  	}
   903  }
   904  
   905  // contains returns true if the set contains the given transaction hash
   906  // (not thread safe, should be called from a locked environment)
   907  func (ts *txSet) contains(hash common.Hash) bool {
   908  	_, ok := ts.txMap[hash]
   909  	return ok
   910  }
   911  
   912  // add adds a transaction hash to the set, then removes entries older than txSetDuration
   913  // (not thread safe, should be called from a locked environment)
   914  func (ts *txSet) add(hash common.Hash) {
   915  	ts.txMap[hash] = struct{}{}
   916  	now := time.Now()
   917  	ts.txOrd[ts.addPtr] = txOrdType{hash: hash, time: now}
   918  	ts.addPtr++
   919  	delBefore := now.Add(-txSetDuration)
   920  	for ts.delPtr < ts.addPtr && ts.txOrd[ts.delPtr].time.Before(delBefore) {
   921  		delete(ts.txMap, ts.txOrd[ts.delPtr].hash)
   922  		delete(ts.txOrd, ts.delPtr)
   923  		ts.delPtr++
   924  	}
   925  }