tx_pool.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. package core
  17. import (
  18. "errors"
  19. "fmt"
  20. "math/big"
  21. "sort"
  22. "sync"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/state"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/event"
  28. "github.com/ethereum/go-ethereum/logger"
  29. "github.com/ethereum/go-ethereum/logger/glog"
  30. "github.com/ethereum/go-ethereum/metrics"
  31. "github.com/ethereum/go-ethereum/params"
  32. "gopkg.in/karalabe/cookiejar.v2/collections/prque"
  33. )
  34. var (
  35. // Transaction Pool Errors
  36. ErrInvalidSender = errors.New("Invalid sender")
  37. ErrNonce = errors.New("Nonce too low")
  38. ErrCheap = errors.New("Gas price too low for acceptance")
  39. ErrBalance = errors.New("Insufficient balance")
  40. ErrInsufficientFunds = errors.New("Insufficient funds for gas * price + value")
  41. ErrIntrinsicGas = errors.New("Intrinsic gas too low")
  42. ErrGasLimit = errors.New("Exceeds block gas limit")
  43. ErrNegativeValue = errors.New("Negative value")
  44. )
  45. var (
  46. minPendingPerAccount = uint64(16) // Min number of guaranteed transaction slots per address
  47. maxPendingTotal = uint64(4096) // Max limit of pending transactions from all accounts (soft)
  48. maxQueuedPerAccount = uint64(64) // Max limit of queued transactions per address
  49. maxQueuedInTotal = uint64(1024) // Max limit of queued transactions from all accounts
  50. maxQueuedLifetime = 3 * time.Hour // Max amount of time transactions from idle accounts are queued
  51. evictionInterval = time.Minute // Time interval to check for evictable transactions
  52. )
  53. var (
  54. // Metrics for the pending pool
  55. pendingDiscardCounter = metrics.NewCounter("txpool/pending/discard")
  56. pendingReplaceCounter = metrics.NewCounter("txpool/pending/replace")
  57. pendingRLCounter = metrics.NewCounter("txpool/pending/ratelimit") // Dropped due to rate limiting
  58. pendingNofundsCounter = metrics.NewCounter("txpool/pending/nofunds") // Dropped due to out-of-funds
  59. // Metrics for the queued pool
  60. queuedDiscardCounter = metrics.NewCounter("txpool/queued/discard")
  61. queuedReplaceCounter = metrics.NewCounter("txpool/queued/replace")
  62. queuedRLCounter = metrics.NewCounter("txpool/queued/ratelimit") // Dropped due to rate limiting
  63. queuedNofundsCounter = metrics.NewCounter("txpool/queued/nofunds") // Dropped due to out-of-funds
  64. // General tx metrics
  65. invalidTxCounter = metrics.NewCounter("txpool/invalid")
  66. )
  67. type stateFn func() (*state.StateDB, error)
  68. // TxPool contains all currently known transactions. Transactions
  69. // enter the pool when they are received from the network or submitted
  70. // locally. They exit the pool when they are included in the blockchain.
  71. //
  72. // The pool separates processable transactions (which can be applied to the
  73. // current state) and future transactions. Transactions move between those
  74. // two states over time as they are received and processed.
  75. type TxPool struct {
  76. config *params.ChainConfig
  77. currentState stateFn // The state function which will allow us to do some pre checks
  78. pendingState *state.ManagedState
  79. gasLimit func() *big.Int // The current gas limit function callback
  80. minGasPrice *big.Int
  81. eventMux *event.TypeMux
  82. events *event.TypeMuxSubscription
  83. localTx *txSet
  84. signer types.Signer
  85. mu sync.RWMutex
  86. pending map[common.Address]*txList // All currently processable transactions
  87. queue map[common.Address]*txList // Queued but non-processable transactions
  88. all map[common.Hash]*types.Transaction // All transactions to allow lookups
  89. beats map[common.Address]time.Time // Last heartbeat from each known account
  90. wg sync.WaitGroup // for shutdown sync
  91. quit chan struct{}
  92. homestead bool
  93. }
  94. func NewTxPool(config *params.ChainConfig, eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func() *big.Int) *TxPool {
  95. pool := &TxPool{
  96. config: config,
  97. signer: types.NewEIP155Signer(config.ChainId),
  98. pending: make(map[common.Address]*txList),
  99. queue: make(map[common.Address]*txList),
  100. all: make(map[common.Hash]*types.Transaction),
  101. beats: make(map[common.Address]time.Time),
  102. eventMux: eventMux,
  103. currentState: currentStateFn,
  104. gasLimit: gasLimitFn,
  105. minGasPrice: new(big.Int),
  106. pendingState: nil,
  107. localTx: newTxSet(),
  108. events: eventMux.Subscribe(ChainHeadEvent{}, GasPriceChanged{}, RemovedTransactionEvent{}),
  109. quit: make(chan struct{}),
  110. }
  111. pool.resetState()
  112. pool.wg.Add(2)
  113. go pool.eventLoop()
  114. go pool.expirationLoop()
  115. return pool
  116. }
  117. func (pool *TxPool) eventLoop() {
  118. defer pool.wg.Done()
  119. // Track chain events. When a chain events occurs (new chain canon block)
  120. // we need to know the new state. The new state will help us determine
  121. // the nonces in the managed state
  122. for ev := range pool.events.Chan() {
  123. switch ev := ev.Data.(type) {
  124. case ChainHeadEvent:
  125. pool.mu.Lock()
  126. if ev.Block != nil {
  127. if pool.config.IsHomestead(ev.Block.Number()) {
  128. pool.homestead = true
  129. }
  130. }
  131. pool.resetState()
  132. pool.mu.Unlock()
  133. case GasPriceChanged:
  134. pool.mu.Lock()
  135. pool.minGasPrice = ev.Price
  136. pool.mu.Unlock()
  137. case RemovedTransactionEvent:
  138. pool.AddBatch(ev.Txs)
  139. }
  140. }
  141. }
  142. func (pool *TxPool) resetState() {
  143. currentState, err := pool.currentState()
  144. if err != nil {
  145. glog.V(logger.Error).Infof("Failed to get current state: %v", err)
  146. return
  147. }
  148. managedState := state.ManageState(currentState)
  149. if err != nil {
  150. glog.V(logger.Error).Infof("Failed to get managed state: %v", err)
  151. return
  152. }
  153. pool.pendingState = managedState
  154. // validate the pool of pending transactions, this will remove
  155. // any transactions that have been included in the block or
  156. // have been invalidated because of another transaction (e.g.
  157. // higher gas price)
  158. pool.demoteUnexecutables(currentState)
  159. // Update all accounts to the latest known pending nonce
  160. for addr, list := range pool.pending {
  161. txs := list.Flatten() // Heavy but will be cached and is needed by the miner anyway
  162. pool.pendingState.SetNonce(addr, txs[len(txs)-1].Nonce()+1)
  163. }
  164. // Check the queue and move transactions over to the pending if possible
  165. // or remove those that have become invalid
  166. pool.promoteExecutables(currentState)
  167. }
  168. func (pool *TxPool) Stop() {
  169. pool.events.Unsubscribe()
  170. close(pool.quit)
  171. pool.wg.Wait()
  172. glog.V(logger.Info).Infoln("Transaction pool stopped")
  173. }
  174. func (pool *TxPool) State() *state.ManagedState {
  175. pool.mu.RLock()
  176. defer pool.mu.RUnlock()
  177. return pool.pendingState
  178. }
  179. // Stats retrieves the current pool stats, namely the number of pending and the
  180. // number of queued (non-executable) transactions.
  181. func (pool *TxPool) Stats() (pending int, queued int) {
  182. pool.mu.RLock()
  183. defer pool.mu.RUnlock()
  184. for _, list := range pool.pending {
  185. pending += list.Len()
  186. }
  187. for _, list := range pool.queue {
  188. queued += list.Len()
  189. }
  190. return
  191. }
  192. // Content retrieves the data content of the transaction pool, returning all the
  193. // pending as well as queued transactions, grouped by account and sorted by nonce.
  194. func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  195. pool.mu.RLock()
  196. defer pool.mu.RUnlock()
  197. pending := make(map[common.Address]types.Transactions)
  198. for addr, list := range pool.pending {
  199. pending[addr] = list.Flatten()
  200. }
  201. queued := make(map[common.Address]types.Transactions)
  202. for addr, list := range pool.queue {
  203. queued[addr] = list.Flatten()
  204. }
  205. return pending, queued
  206. }
  207. // Pending retrieves all currently processable transactions, groupped by origin
  208. // account and sorted by nonce. The returned transaction set is a copy and can be
  209. // freely modified by calling code.
  210. func (pool *TxPool) Pending() (map[common.Address]types.Transactions, error) {
  211. pool.mu.Lock()
  212. defer pool.mu.Unlock()
  213. state, err := pool.currentState()
  214. if err != nil {
  215. return nil, err
  216. }
  217. // check queue first
  218. pool.promoteExecutables(state)
  219. // invalidate any txs
  220. pool.demoteUnexecutables(state)
  221. pending := make(map[common.Address]types.Transactions)
  222. for addr, list := range pool.pending {
  223. pending[addr] = list.Flatten()
  224. }
  225. return pending, nil
  226. }
  227. // SetLocal marks a transaction as local, skipping gas price
  228. // check against local miner minimum in the future
  229. func (pool *TxPool) SetLocal(tx *types.Transaction) {
  230. pool.mu.Lock()
  231. defer pool.mu.Unlock()
  232. pool.localTx.add(tx.Hash())
  233. }
  234. // validateTx checks whether a transaction is valid according
  235. // to the consensus rules.
  236. func (pool *TxPool) validateTx(tx *types.Transaction) error {
  237. local := pool.localTx.contains(tx.Hash())
  238. // Drop transactions under our own minimal accepted gas price
  239. if !local && pool.minGasPrice.Cmp(tx.GasPrice()) > 0 {
  240. return ErrCheap
  241. }
  242. currentState, err := pool.currentState()
  243. if err != nil {
  244. return err
  245. }
  246. from, err := types.Sender(pool.signer, tx)
  247. if err != nil {
  248. return ErrInvalidSender
  249. }
  250. // Last but not least check for nonce errors
  251. if currentState.GetNonce(from) > tx.Nonce() {
  252. return ErrNonce
  253. }
  254. // Check the transaction doesn't exceed the current
  255. // block limit gas.
  256. if pool.gasLimit().Cmp(tx.Gas()) < 0 {
  257. return ErrGasLimit
  258. }
  259. // Transactions can't be negative. This may never happen
  260. // using RLP decoded transactions but may occur if you create
  261. // a transaction using the RPC for example.
  262. if tx.Value().Cmp(common.Big0) < 0 {
  263. return ErrNegativeValue
  264. }
  265. // Transactor should have enough funds to cover the costs
  266. // cost == V + GP * GL
  267. if currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
  268. return ErrInsufficientFunds
  269. }
  270. intrGas := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
  271. if tx.Gas().Cmp(intrGas) < 0 {
  272. return ErrIntrinsicGas
  273. }
  274. return nil
  275. }
  276. // add validates a transaction and inserts it into the non-executable queue for
  277. // later pending promotion and execution.
  278. func (pool *TxPool) add(tx *types.Transaction) error {
  279. // If the transaction is already known, discard it
  280. hash := tx.Hash()
  281. if pool.all[hash] != nil {
  282. return fmt.Errorf("Known transaction: %x", hash[:4])
  283. }
  284. // Otherwise ensure basic validation passes and queue it up
  285. if err := pool.validateTx(tx); err != nil {
  286. invalidTxCounter.Inc(1)
  287. return err
  288. }
  289. pool.enqueueTx(hash, tx)
  290. // Print a log message if low enough level is set
  291. if glog.V(logger.Debug) {
  292. rcpt := "[NEW_CONTRACT]"
  293. if to := tx.To(); to != nil {
  294. rcpt = common.Bytes2Hex(to[:4])
  295. }
  296. from, _ := types.Sender(pool.signer, tx) // from already verified during tx validation
  297. glog.Infof("(t) 0x%x => %s (%v) %x\n", from[:4], rcpt, tx.Value, hash)
  298. }
  299. return nil
  300. }
  301. // enqueueTx inserts a new transaction into the non-executable transaction queue.
  302. //
  303. // Note, this method assumes the pool lock is held!
  304. func (pool *TxPool) enqueueTx(hash common.Hash, tx *types.Transaction) {
  305. // Try to insert the transaction into the future queue
  306. from, _ := types.Sender(pool.signer, tx) // already validated
  307. if pool.queue[from] == nil {
  308. pool.queue[from] = newTxList(false)
  309. }
  310. inserted, old := pool.queue[from].Add(tx)
  311. if !inserted {
  312. queuedDiscardCounter.Inc(1)
  313. return // An older transaction was better, discard this
  314. }
  315. // Discard any previous transaction and mark this
  316. if old != nil {
  317. delete(pool.all, old.Hash())
  318. queuedReplaceCounter.Inc(1)
  319. }
  320. pool.all[hash] = tx
  321. }
  322. // promoteTx adds a transaction to the pending (processable) list of transactions.
  323. //
  324. // Note, this method assumes the pool lock is held!
  325. func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) {
  326. // Try to insert the transaction into the pending queue
  327. if pool.pending[addr] == nil {
  328. pool.pending[addr] = newTxList(true)
  329. }
  330. list := pool.pending[addr]
  331. inserted, old := list.Add(tx)
  332. if !inserted {
  333. // An older transaction was better, discard this
  334. delete(pool.all, hash)
  335. pendingDiscardCounter.Inc(1)
  336. return
  337. }
  338. // Otherwise discard any previous transaction and mark this
  339. if old != nil {
  340. delete(pool.all, old.Hash())
  341. pendingReplaceCounter.Inc(1)
  342. }
  343. pool.all[hash] = tx // Failsafe to work around direct pending inserts (tests)
  344. // Set the potentially new pending nonce and notify any subsystems of the new tx
  345. pool.beats[addr] = time.Now()
  346. pool.pendingState.SetNonce(addr, tx.Nonce()+1)
  347. go pool.eventMux.Post(TxPreEvent{tx})
  348. }
  349. // Add queues a single transaction in the pool if it is valid.
  350. func (pool *TxPool) Add(tx *types.Transaction) error {
  351. pool.mu.Lock()
  352. defer pool.mu.Unlock()
  353. if err := pool.add(tx); err != nil {
  354. return err
  355. }
  356. state, err := pool.currentState()
  357. if err != nil {
  358. return err
  359. }
  360. pool.promoteExecutables(state)
  361. return nil
  362. }
  363. // AddBatch attempts to queue a batch of transactions.
  364. func (pool *TxPool) AddBatch(txs []*types.Transaction) error {
  365. pool.mu.Lock()
  366. defer pool.mu.Unlock()
  367. for _, tx := range txs {
  368. if err := pool.add(tx); err != nil {
  369. glog.V(logger.Debug).Infoln("tx error:", err)
  370. }
  371. }
  372. state, err := pool.currentState()
  373. if err != nil {
  374. return err
  375. }
  376. pool.promoteExecutables(state)
  377. return nil
  378. }
  379. // Get returns a transaction if it is contained in the pool
  380. // and nil otherwise.
  381. func (pool *TxPool) Get(hash common.Hash) *types.Transaction {
  382. pool.mu.RLock()
  383. defer pool.mu.RUnlock()
  384. return pool.all[hash]
  385. }
  386. // Remove removes the transaction with the given hash from the pool.
  387. func (pool *TxPool) Remove(hash common.Hash) {
  388. pool.mu.Lock()
  389. defer pool.mu.Unlock()
  390. pool.removeTx(hash)
  391. }
  392. // RemoveBatch removes all given transactions from the pool.
  393. func (pool *TxPool) RemoveBatch(txs types.Transactions) {
  394. pool.mu.Lock()
  395. defer pool.mu.Unlock()
  396. for _, tx := range txs {
  397. pool.removeTx(tx.Hash())
  398. }
  399. }
  400. // removeTx removes a single transaction from the queue, moving all subsequent
  401. // transactions back to the future queue.
  402. func (pool *TxPool) removeTx(hash common.Hash) {
  403. // Fetch the transaction we wish to delete
  404. tx, ok := pool.all[hash]
  405. if !ok {
  406. return
  407. }
  408. addr, _ := types.Sender(pool.signer, tx) // already validated during insertion
  409. // Remove it from the list of known transactions
  410. delete(pool.all, hash)
  411. // Remove the transaction from the pending lists and reset the account nonce
  412. if pending := pool.pending[addr]; pending != nil {
  413. if removed, invalids := pending.Remove(tx); removed {
  414. // If no more transactions are left, remove the list
  415. if pending.Empty() {
  416. delete(pool.pending, addr)
  417. delete(pool.beats, addr)
  418. } else {
  419. // Otherwise postpone any invalidated transactions
  420. for _, tx := range invalids {
  421. pool.enqueueTx(tx.Hash(), tx)
  422. }
  423. }
  424. // Update the account nonce if needed
  425. if nonce := tx.Nonce(); pool.pendingState.GetNonce(addr) > nonce {
  426. pool.pendingState.SetNonce(addr, tx.Nonce())
  427. }
  428. }
  429. }
  430. // Transaction is in the future queue
  431. if future := pool.queue[addr]; future != nil {
  432. future.Remove(tx)
  433. if future.Empty() {
  434. delete(pool.queue, addr)
  435. }
  436. }
  437. }
  438. // promoteExecutables moves transactions that have become processable from the
  439. // future queue to the set of pending transactions. During this process, all
  440. // invalidated transactions (low nonce, low balance) are deleted.
  441. func (pool *TxPool) promoteExecutables(state *state.StateDB) {
  442. // Iterate over all accounts and promote any executable transactions
  443. queued := uint64(0)
  444. for addr, list := range pool.queue {
  445. // Drop all transactions that are deemed too old (low nonce)
  446. for _, tx := range list.Forward(state.GetNonce(addr)) {
  447. if glog.V(logger.Debug) {
  448. glog.Infof("Removed old queued transaction: %v", tx)
  449. }
  450. delete(pool.all, tx.Hash())
  451. }
  452. // Drop all transactions that are too costly (low balance)
  453. drops, _ := list.Filter(state.GetBalance(addr))
  454. for _, tx := range drops {
  455. if glog.V(logger.Debug) {
  456. glog.Infof("Removed unpayable queued transaction: %v", tx)
  457. }
  458. delete(pool.all, tx.Hash())
  459. queuedNofundsCounter.Inc(1)
  460. }
  461. // Gather all executable transactions and promote them
  462. for _, tx := range list.Ready(pool.pendingState.GetNonce(addr)) {
  463. if glog.V(logger.Debug) {
  464. glog.Infof("Promoting queued transaction: %v", tx)
  465. }
  466. pool.promoteTx(addr, tx.Hash(), tx)
  467. }
  468. // Drop all transactions over the allowed limit
  469. for _, tx := range list.Cap(int(maxQueuedPerAccount)) {
  470. if glog.V(logger.Debug) {
  471. glog.Infof("Removed cap-exceeding queued transaction: %v", tx)
  472. }
  473. delete(pool.all, tx.Hash())
  474. queuedRLCounter.Inc(1)
  475. }
  476. queued += uint64(list.Len())
  477. // Delete the entire queue entry if it became empty.
  478. if list.Empty() {
  479. delete(pool.queue, addr)
  480. }
  481. }
  482. // If the pending limit is overflown, start equalizing allowances
  483. pending := uint64(0)
  484. for _, list := range pool.pending {
  485. pending += uint64(list.Len())
  486. }
  487. if pending > maxPendingTotal {
  488. pendingBeforeCap := pending
  489. // Assemble a spam order to penalize large transactors first
  490. spammers := prque.New()
  491. for addr, list := range pool.pending {
  492. // Only evict transactions from high rollers
  493. if uint64(list.Len()) > minPendingPerAccount {
  494. // Skip local accounts as pools should maintain backlogs for themselves
  495. for _, tx := range list.txs.items {
  496. if !pool.localTx.contains(tx.Hash()) {
  497. spammers.Push(addr, float32(list.Len()))
  498. }
  499. break // Checking on transaction for locality is enough
  500. }
  501. }
  502. }
  503. // Gradually drop transactions from offenders
  504. offenders := []common.Address{}
  505. for pending > maxPendingTotal && !spammers.Empty() {
  506. // Retrieve the next offender if not local address
  507. offender, _ := spammers.Pop()
  508. offenders = append(offenders, offender.(common.Address))
  509. // Equalize balances until all the same or below threshold
  510. if len(offenders) > 1 {
  511. // Calculate the equalization threshold for all current offenders
  512. threshold := pool.pending[offender.(common.Address)].Len()
  513. // Iteratively reduce all offenders until below limit or threshold reached
  514. for pending > maxPendingTotal && pool.pending[offenders[len(offenders)-2]].Len() > threshold {
  515. for i := 0; i < len(offenders)-1; i++ {
  516. list := pool.pending[offenders[i]]
  517. list.Cap(list.Len() - 1)
  518. pending--
  519. }
  520. }
  521. }
  522. }
  523. // If still above threshold, reduce to limit or min allowance
  524. if pending > maxPendingTotal && len(offenders) > 0 {
  525. for pending > maxPendingTotal && uint64(pool.pending[offenders[len(offenders)-1]].Len()) > minPendingPerAccount {
  526. for _, addr := range offenders {
  527. list := pool.pending[addr]
  528. list.Cap(list.Len() - 1)
  529. pending--
  530. }
  531. }
  532. }
  533. pendingRLCounter.Inc(int64(pendingBeforeCap - pending))
  534. }
  535. // If we've queued more transactions than the hard limit, drop oldest ones
  536. if queued > maxQueuedInTotal {
  537. // Sort all accounts with queued transactions by heartbeat
  538. addresses := make(addresssByHeartbeat, 0, len(pool.queue))
  539. for addr := range pool.queue {
  540. addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]})
  541. }
  542. sort.Sort(addresses)
  543. // Drop transactions until the total is below the limit
  544. for drop := queued - maxQueuedInTotal; drop > 0; {
  545. addr := addresses[len(addresses)-1]
  546. list := pool.queue[addr.address]
  547. addresses = addresses[:len(addresses)-1]
  548. // Drop all transactions if they are less than the overflow
  549. if size := uint64(list.Len()); size <= drop {
  550. for _, tx := range list.Flatten() {
  551. pool.removeTx(tx.Hash())
  552. }
  553. drop -= size
  554. queuedRLCounter.Inc(int64(size))
  555. continue
  556. }
  557. // Otherwise drop only last few transactions
  558. txs := list.Flatten()
  559. for i := len(txs) - 1; i >= 0 && drop > 0; i-- {
  560. pool.removeTx(txs[i].Hash())
  561. drop--
  562. queuedRLCounter.Inc(1)
  563. }
  564. }
  565. }
  566. }
  567. // demoteUnexecutables removes invalid and processed transactions from the pools
  568. // executable/pending queue and any subsequent transactions that become unexecutable
  569. // are moved back into the future queue.
  570. func (pool *TxPool) demoteUnexecutables(state *state.StateDB) {
  571. // Iterate over all accounts and demote any non-executable transactions
  572. for addr, list := range pool.pending {
  573. nonce := state.GetNonce(addr)
  574. // Drop all transactions that are deemed too old (low nonce)
  575. for _, tx := range list.Forward(nonce) {
  576. if glog.V(logger.Debug) {
  577. glog.Infof("Removed old pending transaction: %v", tx)
  578. }
  579. delete(pool.all, tx.Hash())
  580. }
  581. // Drop all transactions that are too costly (low balance), and queue any invalids back for later
  582. drops, invalids := list.Filter(state.GetBalance(addr))
  583. for _, tx := range drops {
  584. if glog.V(logger.Debug) {
  585. glog.Infof("Removed unpayable pending transaction: %v", tx)
  586. }
  587. delete(pool.all, tx.Hash())
  588. pendingNofundsCounter.Inc(1)
  589. }
  590. for _, tx := range invalids {
  591. if glog.V(logger.Debug) {
  592. glog.Infof("Demoting pending transaction: %v", tx)
  593. }
  594. pool.enqueueTx(tx.Hash(), tx)
  595. }
  596. // Delete the entire queue entry if it became empty.
  597. if list.Empty() {
  598. delete(pool.pending, addr)
  599. delete(pool.beats, addr)
  600. }
  601. }
  602. }
  603. // expirationLoop is a loop that periodically iterates over all accounts with
  604. // queued transactions and drop all that have been inactive for a prolonged amount
  605. // of time.
  606. func (pool *TxPool) expirationLoop() {
  607. defer pool.wg.Done()
  608. evict := time.NewTicker(evictionInterval)
  609. defer evict.Stop()
  610. for {
  611. select {
  612. case <-evict.C:
  613. pool.mu.Lock()
  614. for addr := range pool.queue {
  615. if time.Since(pool.beats[addr]) > maxQueuedLifetime {
  616. for _, tx := range pool.queue[addr].Flatten() {
  617. pool.removeTx(tx.Hash())
  618. }
  619. }
  620. }
  621. pool.mu.Unlock()
  622. case <-pool.quit:
  623. return
  624. }
  625. }
  626. }
  627. // addressByHeartbeat is an account address tagged with its last activity timestamp.
  628. type addressByHeartbeat struct {
  629. address common.Address
  630. heartbeat time.Time
  631. }
  632. type addresssByHeartbeat []addressByHeartbeat
  633. func (a addresssByHeartbeat) Len() int { return len(a) }
  634. func (a addresssByHeartbeat) Less(i, j int) bool { return a[i].heartbeat.Before(a[j].heartbeat) }
  635. func (a addresssByHeartbeat) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  636. // txSet represents a set of transaction hashes in which entries
  637. // are automatically dropped after txSetDuration time
  638. type txSet struct {
  639. txMap map[common.Hash]struct{}
  640. txOrd map[uint64]txOrdType
  641. addPtr, delPtr uint64
  642. }
  643. const txSetDuration = time.Hour * 2
  644. // txOrdType represents an entry in the time-ordered list of transaction hashes
  645. type txOrdType struct {
  646. hash common.Hash
  647. time time.Time
  648. }
  649. // newTxSet creates a new transaction set
  650. func newTxSet() *txSet {
  651. return &txSet{
  652. txMap: make(map[common.Hash]struct{}),
  653. txOrd: make(map[uint64]txOrdType),
  654. }
  655. }
  656. // contains returns true if the set contains the given transaction hash
  657. // (not thread safe, should be called from a locked environment)
  658. func (self *txSet) contains(hash common.Hash) bool {
  659. _, ok := self.txMap[hash]
  660. return ok
  661. }
  662. // add adds a transaction hash to the set, then removes entries older than txSetDuration
  663. // (not thread safe, should be called from a locked environment)
  664. func (self *txSet) add(hash common.Hash) {
  665. self.txMap[hash] = struct{}{}
  666. now := time.Now()
  667. self.txOrd[self.addPtr] = txOrdType{hash: hash, time: now}
  668. self.addPtr++
  669. delBefore := now.Add(-txSetDuration)
  670. for self.delPtr < self.addPtr && self.txOrd[self.delPtr].time.Before(delBefore) {
  671. delete(self.txMap, self.txOrd[self.delPtr].hash)
  672. delete(self.txOrd, self.delPtr)
  673. self.delPtr++
  674. }
  675. }