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