github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/tx_pool.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "errors" 21 "fmt" 22 "math" 23 "math/big" 24 "sort" 25 "sync" 26 "time" 27 28 "github.com/vntchain/go-vnt/common" 29 "github.com/vntchain/go-vnt/core/state" 30 "github.com/vntchain/go-vnt/core/types" 31 "github.com/vntchain/go-vnt/event" 32 "github.com/vntchain/go-vnt/log" 33 "github.com/vntchain/go-vnt/metrics" 34 "github.com/vntchain/go-vnt/params" 35 "gopkg.in/karalabe/cookiejar.v2/collections/prque" 36 ) 37 38 const ( 39 // chainHeadChanSize is the size of channel listening to ChainHeadEvent. 40 chainHeadChanSize = 10 41 ) 42 43 var ( 44 // ErrInvalidSender is returned if the transaction contains an invalid signature. 45 ErrInvalidSender = errors.New("invalid sender") 46 47 // ErrNonceTooLow is returned if the nonce of a transaction is lower than the 48 // one present in the local chain. 49 ErrNonceTooLow = errors.New("nonce too low") 50 51 // ErrUnderpriced is returned if a transaction's gas price is below the minimum 52 // configured for the transaction pool. 53 ErrUnderpriced = errors.New("transaction underpriced") 54 55 // ErrReplaceUnderpriced is returned if a transaction is attempted to be replaced 56 // with a different one without the required price bump. 57 ErrReplaceUnderpriced = errors.New("replacement transaction underpriced") 58 59 // ErrInsufficientFunds is returned if the total cost of executing a transaction 60 // is higher than the balance of the user's account. 61 ErrInsufficientFunds = errors.New("insufficient funds for gas * price + value") 62 63 // ErrIntrinsicGas is returned if the transaction is specified to use less gas 64 // than required to start the invocation. 65 ErrIntrinsicGas = errors.New("intrinsic gas too low") 66 67 // ErrGasLimit is returned if a transaction's requested gas limit exceeds the 68 // maximum allowance of the current block. 69 ErrGasLimit = errors.New("exceeds block gas limit") 70 71 // ErrNegativeValue is a sanity error to ensure noone is able to specify a 72 // transaction with a negative value. 73 ErrNegativeValue = errors.New("negative value") 74 75 // ErrOversizedData is returned if the input data of a transaction is greater 76 // than some meaningful limit a user might use. This is not a consensus error 77 // making the transaction invalid, rather a DOS protection. 78 ErrOversizedData = errors.New("oversized data") 79 ) 80 81 var ( 82 evictionInterval = time.Minute // Time interval to check for evictable transactions 83 statsReportInterval = 8 * time.Second // Time interval to report transaction pool stats 84 ) 85 86 var ( 87 // Metrics for the pending pool 88 pendingDiscardCounter = metrics.NewRegisteredCounter("txpool/pending/discard", nil) 89 pendingReplaceCounter = metrics.NewRegisteredCounter("txpool/pending/replace", nil) 90 pendingRateLimitCounter = metrics.NewRegisteredCounter("txpool/pending/ratelimit", nil) // Dropped due to rate limiting 91 pendingNofundsCounter = metrics.NewRegisteredCounter("txpool/pending/nofunds", nil) // Dropped due to out-of-funds 92 93 // Metrics for the queued pool 94 queuedDiscardCounter = metrics.NewRegisteredCounter("txpool/queued/discard", nil) 95 queuedReplaceCounter = metrics.NewRegisteredCounter("txpool/queued/replace", nil) 96 queuedRateLimitCounter = metrics.NewRegisteredCounter("txpool/queued/ratelimit", nil) // Dropped due to rate limiting 97 queuedNofundsCounter = metrics.NewRegisteredCounter("txpool/queued/nofunds", nil) // Dropped due to out-of-funds 98 99 // General tx metrics 100 invalidTxCounter = metrics.NewRegisteredCounter("txpool/invalid", nil) 101 underpricedTxCounter = metrics.NewRegisteredCounter("txpool/underpriced", nil) 102 ) 103 104 // TxStatus is the current status of a transaction as seen by the pool. 105 type TxStatus uint 106 107 const ( 108 TxStatusUnknown TxStatus = iota 109 TxStatusQueued 110 TxStatusPending 111 TxStatusIncluded 112 ) 113 114 // blockChain provides the state of blockchain and current gas limit to do 115 // some pre checks in tx pool and event subscribers. 116 type blockChain interface { 117 CurrentBlock() *types.Block 118 GetBlock(hash common.Hash, number uint64) *types.Block 119 StateAt(root common.Hash) (*state.StateDB, error) 120 121 SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription 122 } 123 124 // TxPoolConfig are the configuration parameters of the transaction pool. 125 type TxPoolConfig struct { 126 NoLocals bool // Whether local transaction handling should be disabled 127 Journal string // Journal of local transactions to survive node restarts 128 Rejournal time.Duration // Time interval to regenerate the local transaction journal 129 130 PriceLimit uint64 // Minimum gas price to enforce for acceptance into the pool 131 PriceBump uint64 // Minimum price bump percentage to replace an already existing transaction (nonce) 132 133 AccountSlots uint64 // Minimum number of executable transaction slots guaranteed per account 134 GlobalSlots uint64 // Maximum number of executable transaction slots for all accounts 135 AccountQueue uint64 // Maximum number of non-executable transaction slots permitted per account 136 GlobalQueue uint64 // Maximum number of non-executable transaction slots for all accounts 137 138 Lifetime time.Duration // Maximum amount of time non-executable transaction are queued 139 } 140 141 // DefaultTxPoolConfig contains the default configurations for the transaction 142 // pool. 143 var DefaultTxPoolConfig = TxPoolConfig{ 144 Journal: "transactions.rlp", 145 Rejournal: time.Hour, 146 147 PriceLimit: 1, 148 PriceBump: 10, 149 150 AccountSlots: 16, 151 GlobalSlots: 4096, 152 AccountQueue: 64, 153 GlobalQueue: 1024, 154 155 Lifetime: 3 * time.Hour, 156 } 157 158 // sanitize checks the provided user configurations and changes anything that's 159 // unreasonable or unworkable. 160 func (config *TxPoolConfig) sanitize() TxPoolConfig { 161 conf := *config 162 if conf.Rejournal < time.Second { 163 log.Warn("Sanitizing invalid txpool journal time", "provided", conf.Rejournal, "updated", time.Second) 164 conf.Rejournal = time.Second 165 } 166 if conf.PriceLimit < 1 { 167 log.Warn("Sanitizing invalid txpool price limit", "provided", conf.PriceLimit, "updated", DefaultTxPoolConfig.PriceLimit) 168 conf.PriceLimit = DefaultTxPoolConfig.PriceLimit 169 } 170 if conf.PriceBump < 1 { 171 log.Warn("Sanitizing invalid txpool price bump", "provided", conf.PriceBump, "updated", DefaultTxPoolConfig.PriceBump) 172 conf.PriceBump = DefaultTxPoolConfig.PriceBump 173 } 174 return conf 175 } 176 177 // TxPool contains all currently known transactions. Transactions 178 // enter the pool when they are received from the network or submitted 179 // locally. They exit the pool when they are included in the blockchain. 180 // 181 // The pool separates processable transactions (which can be applied to the 182 // current state) and future transactions. Transactions move between those 183 // two states over time as they are received and processed. 184 type TxPool struct { 185 config TxPoolConfig 186 chainconfig *params.ChainConfig 187 chain blockChain 188 gasPrice *big.Int 189 txFeed event.Feed 190 scope event.SubscriptionScope 191 chainHeadCh chan ChainHeadEvent 192 chainHeadSub event.Subscription 193 signer types.Signer 194 mu sync.RWMutex 195 196 currentState *state.StateDB // Current state in the blockchain head 197 pendingState *state.ManagedState // Pending state tracking virtual nonces 198 currentMaxGas uint64 // Current gas limit for transaction caps 199 200 locals *accountSet // Set of local transaction to exempt from eviction rules 201 journal *txJournal // Journal of local transaction to back up to disk 202 203 pending map[common.Address]*txList // All currently processable transactions 204 queue map[common.Address]*txList // Queued but non-processable transactions 205 beats map[common.Address]time.Time // Last heartbeat from each known account 206 all *txLookup // All transactions to allow lookups 207 priced *txPricedList // All transactions sorted by price 208 209 wg sync.WaitGroup // for shutdown sync 210 211 } 212 213 // NewTxPool creates a new transaction pool to gather, sort and filter inbound 214 // transactions from the network. 215 func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain blockChain) *TxPool { 216 // Sanitize the input to ensure no vulnerable gas prices are set 217 config = (&config).sanitize() 218 219 // Create the transaction pool with its initial settings 220 pool := &TxPool{ 221 config: config, 222 chainconfig: chainconfig, 223 chain: chain, 224 signer: types.NewHubbleSigner(chainconfig.ChainID), 225 pending: make(map[common.Address]*txList), 226 queue: make(map[common.Address]*txList), 227 beats: make(map[common.Address]time.Time), 228 all: newTxLookup(), 229 chainHeadCh: make(chan ChainHeadEvent, chainHeadChanSize), 230 gasPrice: new(big.Int).SetUint64(config.PriceLimit), 231 } 232 pool.locals = newAccountSet(pool.signer) 233 pool.priced = newTxPricedList(pool.all) 234 pool.reset(nil, chain.CurrentBlock().Header()) 235 236 // If local transactions and journaling is enabled, load from disk 237 if !config.NoLocals && config.Journal != "" { 238 pool.journal = newTxJournal(config.Journal) 239 240 if err := pool.journal.load(pool.AddLocals); err != nil { 241 log.Warn("Failed to load transaction journal", "err", err) 242 } 243 if err := pool.journal.rotate(pool.local()); err != nil { 244 log.Warn("Failed to rotate transaction journal", "err", err) 245 } 246 } 247 // Subscribe events from blockchain 248 pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh) 249 250 // Start the event loop and return 251 pool.wg.Add(1) 252 go pool.loop() 253 254 return pool 255 } 256 257 // loop is the transaction pool's main event loop, waiting for and reacting to 258 // outside blockchain events as well as for various reporting and transaction 259 // eviction events. 260 func (pool *TxPool) loop() { 261 defer pool.wg.Done() 262 263 // Start the stats reporting and transaction eviction tickers 264 var prevPending, prevQueued, prevStales int 265 266 report := time.NewTicker(statsReportInterval) 267 defer report.Stop() 268 269 evict := time.NewTicker(evictionInterval) 270 defer evict.Stop() 271 272 journal := time.NewTicker(pool.config.Rejournal) 273 defer journal.Stop() 274 275 // Track the previous head headers for transaction reorgs 276 head := pool.chain.CurrentBlock() 277 278 // Keep waiting for and reacting to the various events 279 for { 280 select { 281 // Handle ChainHeadEvent 282 case ev := <-pool.chainHeadCh: 283 if ev.Block != nil { 284 pool.mu.Lock() 285 pool.reset(head.Header(), ev.Block.Header()) 286 head = ev.Block 287 288 pool.mu.Unlock() 289 } 290 // Be unsubscribed due to system stopped 291 case <-pool.chainHeadSub.Err(): 292 return 293 294 // Handle stats reporting ticks 295 case <-report.C: 296 pool.mu.RLock() 297 pending, queued := pool.stats() 298 stales := pool.priced.stales 299 pool.mu.RUnlock() 300 301 if pending != prevPending || queued != prevQueued || stales != prevStales { 302 log.Debug("Transaction pool status report", "executable", pending, "queued", queued, "stales", stales) 303 prevPending, prevQueued, prevStales = pending, queued, stales 304 } 305 306 // Handle inactive account transaction eviction 307 case <-evict.C: 308 pool.mu.Lock() 309 for addr := range pool.queue { 310 // Skip local transactions from the eviction mechanism 311 if pool.locals.contains(addr) { 312 continue 313 } 314 // Any non-locals old enough should be removed 315 if time.Since(pool.beats[addr]) > pool.config.Lifetime { 316 for _, tx := range pool.queue[addr].Flatten() { 317 pool.removeTx(tx.Hash(), true) 318 } 319 } 320 } 321 pool.mu.Unlock() 322 323 // Handle local transaction journal rotation 324 case <-journal.C: 325 if pool.journal != nil { 326 pool.mu.Lock() 327 if err := pool.journal.rotate(pool.local()); err != nil { 328 log.Warn("Failed to rotate local tx journal", "err", err) 329 } 330 pool.mu.Unlock() 331 } 332 } 333 } 334 } 335 336 // lockedReset is a wrapper around reset to allow calling it in a thread safe 337 // manner. This method is only ever used in the tester! 338 func (pool *TxPool) lockedReset(oldHead, newHead *types.Header) { 339 pool.mu.Lock() 340 defer pool.mu.Unlock() 341 342 pool.reset(oldHead, newHead) 343 } 344 345 // reset retrieves the current state of the blockchain and ensures the content 346 // of the transaction pool is valid with regard to the chain state. 347 func (pool *TxPool) reset(oldHead, newHead *types.Header) { 348 // If we're reorging an old state, reinject all dropped transactions 349 var reinject types.Transactions 350 351 if oldHead != nil && oldHead.Hash() != newHead.ParentHash { 352 // If the reorg is too deep, avoid doing it (will happen during fast sync) 353 oldNum := oldHead.Number.Uint64() 354 newNum := newHead.Number.Uint64() 355 356 if depth := uint64(math.Abs(float64(oldNum) - float64(newNum))); depth > 64 { 357 log.Debug("Skipping deep transaction reorg", "depth", depth) 358 } else { 359 // Reorg seems shallow enough to pull in all transactions into memory 360 var discarded, included types.Transactions 361 362 var ( 363 rem = pool.chain.GetBlock(oldHead.Hash(), oldHead.Number.Uint64()) 364 add = pool.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64()) 365 ) 366 for rem.NumberU64() > add.NumberU64() { 367 discarded = append(discarded, rem.Transactions()...) 368 if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil { 369 log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash()) 370 return 371 } 372 } 373 for add.NumberU64() > rem.NumberU64() { 374 included = append(included, add.Transactions()...) 375 if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil { 376 log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash()) 377 return 378 } 379 } 380 for rem.Hash() != add.Hash() { 381 discarded = append(discarded, rem.Transactions()...) 382 if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil { 383 log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash()) 384 return 385 } 386 included = append(included, add.Transactions()...) 387 if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil { 388 log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash()) 389 return 390 } 391 } 392 reinject = types.TxDifference(discarded, included) 393 } 394 } 395 // Initialize the internal state to the current head 396 if newHead == nil { 397 newHead = pool.chain.CurrentBlock().Header() // Special case during testing 398 } 399 statedb, err := pool.chain.StateAt(newHead.Root) 400 if err != nil { 401 log.Error("Failed to reset txpool state", "err", err) 402 return 403 } 404 pool.currentState = statedb 405 pool.pendingState = state.ManageState(statedb) 406 pool.currentMaxGas = newHead.GasLimit 407 408 // Inject any transactions discarded due to reorgs 409 log.Debug("Reinjecting stale transactions", "count", len(reinject)) 410 senderCacher.recover(pool.signer, reinject) 411 pool.addTxsLocked(reinject, false) 412 413 // validate the pool of pending transactions, this will remove 414 // any transactions that have been included in the block or 415 // have been invalidated because of another transaction (e.g. 416 // higher gas price) 417 pool.demoteUnexecutables() 418 419 // Update all accounts to the latest known pending nonce 420 for addr, list := range pool.pending { 421 txs := list.Flatten() // Heavy but will be cached and is needed by the producer anyway 422 pool.pendingState.SetNonce(addr, txs[len(txs)-1].Nonce()+1) 423 } 424 // Check the queue and move transactions over to the pending if possible 425 // or remove those that have become invalid 426 pool.promoteExecutables(nil) 427 } 428 429 // Stop terminates the transaction pool. 430 func (pool *TxPool) Stop() { 431 // Unsubscribe all subscriptions registered from txpool 432 pool.scope.Close() 433 434 // Unsubscribe subscriptions registered from blockchain 435 pool.chainHeadSub.Unsubscribe() 436 pool.wg.Wait() 437 438 if pool.journal != nil { 439 pool.journal.close() 440 } 441 log.Info("Transaction pool stopped") 442 } 443 444 // SubscribeNewTxsEvent registers a subscription of NewTxsEvent and 445 // starts sending event to the given channel. 446 func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- NewTxsEvent) event.Subscription { 447 return pool.scope.Track(pool.txFeed.Subscribe(ch)) 448 } 449 450 // GasPrice returns the current gas price enforced by the transaction pool. 451 func (pool *TxPool) GasPrice() *big.Int { 452 pool.mu.RLock() 453 defer pool.mu.RUnlock() 454 455 return new(big.Int).Set(pool.gasPrice) 456 } 457 458 // SetGasPrice updates the minimum price required by the transaction pool for a 459 // new transaction, and drops all transactions below this threshold. 460 func (pool *TxPool) SetGasPrice(price *big.Int) { 461 pool.mu.Lock() 462 defer pool.mu.Unlock() 463 464 pool.gasPrice = price 465 for _, tx := range pool.priced.Cap(price, pool.locals) { 466 pool.removeTx(tx.Hash(), false) 467 } 468 log.Info("Transaction pool price threshold updated", "price", price) 469 } 470 471 // State returns the virtual managed state of the transaction pool. 472 func (pool *TxPool) State() *state.ManagedState { 473 pool.mu.RLock() 474 defer pool.mu.RUnlock() 475 476 return pool.pendingState 477 } 478 479 // Stats retrieves the current pool stats, namely the number of pending and the 480 // number of queued (non-executable) transactions. 481 func (pool *TxPool) Stats() (int, int) { 482 pool.mu.RLock() 483 defer pool.mu.RUnlock() 484 485 return pool.stats() 486 } 487 488 // stats retrieves the current pool stats, namely the number of pending and the 489 // number of queued (non-executable) transactions. 490 func (pool *TxPool) stats() (int, int) { 491 pending := 0 492 for _, list := range pool.pending { 493 pending += list.Len() 494 } 495 queued := 0 496 for _, list := range pool.queue { 497 queued += list.Len() 498 } 499 return pending, queued 500 } 501 502 // Content retrieves the data content of the transaction pool, returning all the 503 // pending as well as queued transactions, grouped by account and sorted by nonce. 504 func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 505 pool.mu.Lock() 506 defer pool.mu.Unlock() 507 508 pending := make(map[common.Address]types.Transactions) 509 for addr, list := range pool.pending { 510 pending[addr] = list.Flatten() 511 } 512 queued := make(map[common.Address]types.Transactions) 513 for addr, list := range pool.queue { 514 queued[addr] = list.Flatten() 515 } 516 return pending, queued 517 } 518 519 // Pending retrieves all currently processable transactions, groupped by origin 520 // account and sorted by nonce. The returned transaction set is a copy and can be 521 // freely modified by calling code. 522 func (pool *TxPool) Pending() (map[common.Address]types.Transactions, error) { 523 pool.mu.Lock() 524 defer pool.mu.Unlock() 525 526 pending := make(map[common.Address]types.Transactions) 527 for addr, list := range pool.pending { 528 pending[addr] = list.Flatten() 529 } 530 return pending, nil 531 } 532 533 // local retrieves all currently known local transactions, groupped by origin 534 // account and sorted by nonce. The returned transaction set is a copy and can be 535 // freely modified by calling code. 536 func (pool *TxPool) local() map[common.Address]types.Transactions { 537 txs := make(map[common.Address]types.Transactions) 538 for addr := range pool.locals.accounts { 539 if pending := pool.pending[addr]; pending != nil { 540 txs[addr] = append(txs[addr], pending.Flatten()...) 541 } 542 if queued := pool.queue[addr]; queued != nil { 543 txs[addr] = append(txs[addr], queued.Flatten()...) 544 } 545 } 546 return txs 547 } 548 549 // validateTx checks whether a transaction is valid according to the consensus 550 // rules and adheres to some heuristic limits of the local node (price and size). 551 func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error { 552 // Heuristic limit, reject transactions over 32KB to prevent DOS attacks 553 if tx.Size() > 32*1024 { 554 return ErrOversizedData 555 } 556 // Transactions can't be negative. This may never happen using RLP decoded 557 // transactions but may occur if you create a transaction using the RPC. 558 if tx.Value().Sign() < 0 { 559 return ErrNegativeValue 560 } 561 // Ensure the transaction doesn't exceed the current block limit gas. 562 if pool.currentMaxGas < tx.Gas() { 563 return ErrGasLimit 564 } 565 // Make sure the transaction is signed properly 566 from, err := types.Sender(pool.signer, tx) 567 if err != nil { 568 return ErrInvalidSender 569 } 570 // Drop non-local transactions under our own minimal accepted gas price 571 local = local || pool.locals.contains(from) // account may be local even if the transaction arrived from the network 572 if !local && pool.gasPrice.Cmp(tx.GasPrice()) > 0 { 573 return ErrUnderpriced 574 } 575 // Ensure the transaction adheres to nonce ordering 576 if pool.currentState.GetNonce(from) > tx.Nonce() { 577 return ErrNonceTooLow 578 } 579 // Transactor should have enough funds to cover the costs 580 // cost == V + GP * GL 581 if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 { 582 return ErrInsufficientFunds 583 } 584 intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil) 585 if err != nil { 586 return err 587 } 588 if tx.Gas() < intrGas { 589 return ErrIntrinsicGas 590 } 591 return nil 592 } 593 594 // add validates a transaction and inserts it into the non-executable queue for 595 // later pending promotion and execution. If the transaction is a replacement for 596 // an already pending or queued one, it overwrites the previous and returns this 597 // so outer code doesn't uselessly call promote. 598 // 599 // If a newly added transaction is marked as local, its sending account will be 600 // whitelisted, preventing any associated transaction from being dropped out of 601 // the pool due to pricing constraints. 602 func (pool *TxPool) add(tx *types.Transaction, local bool) (bool, error) { 603 // If the transaction is already known, discard it 604 hash := tx.Hash() 605 if pool.all.Get(hash) != nil { 606 log.Trace("Discarding already known transaction", "hash", hash) 607 return false, fmt.Errorf("known transaction: %x", hash) 608 } 609 // If the transaction fails basic validation, discard it 610 if err := pool.validateTx(tx, local); err != nil { 611 log.Trace("Discarding invalid transaction", "hash", hash, "err", err) 612 invalidTxCounter.Inc(1) 613 return false, err 614 } 615 // If the transaction pool is full, discard underpriced transactions 616 if uint64(pool.all.Count()) >= pool.config.GlobalSlots+pool.config.GlobalQueue { 617 // If the new transaction is underpriced, don't accept it 618 if !local && pool.priced.Underpriced(tx, pool.locals) { 619 log.Trace("Discarding underpriced transaction", "hash", hash, "price", tx.GasPrice()) 620 underpricedTxCounter.Inc(1) 621 return false, ErrUnderpriced 622 } 623 // New transaction is better than our worse ones, make room for it 624 drop := pool.priced.Discard(pool.all.Count()-int(pool.config.GlobalSlots+pool.config.GlobalQueue-1), pool.locals) 625 for _, tx := range drop { 626 log.Trace("Discarding freshly underpriced transaction", "hash", tx.Hash(), "price", tx.GasPrice()) 627 underpricedTxCounter.Inc(1) 628 pool.removeTx(tx.Hash(), false) 629 } 630 } 631 // If the transaction is replacing an already pending one, do directly 632 from, _ := types.Sender(pool.signer, tx) // already validated 633 if list := pool.pending[from]; list != nil && list.Overlaps(tx) { 634 // Nonce already pending, check if required price bump is met 635 inserted, old := list.Add(tx, pool.config.PriceBump) 636 if !inserted { 637 pendingDiscardCounter.Inc(1) 638 return false, ErrReplaceUnderpriced 639 } 640 // New transaction is better, replace old one 641 if old != nil { 642 pool.all.Remove(old.Hash()) 643 pool.priced.Removed() 644 pendingReplaceCounter.Inc(1) 645 } 646 pool.all.Add(tx) 647 pool.priced.Put(tx) 648 pool.journalTx(from, tx) 649 650 log.Trace("Pooled new executable transaction", "hash", hash, "from", from, "to", tx.To()) 651 652 // We've directly injected a replacement transaction, notify subsystems 653 go pool.txFeed.Send(NewTxsEvent{types.Transactions{tx}}) 654 655 return old != nil, nil 656 } 657 // New transaction isn't replacing a pending one, push into queue 658 replace, err := pool.enqueueTx(hash, tx) 659 if err != nil { 660 return false, err 661 } 662 // Mark local addresses and journal local transactions 663 if local { 664 pool.locals.add(from) 665 } 666 pool.journalTx(from, tx) 667 668 log.Trace("Pooled new future transaction", "hash", hash, "from", from, "to", tx.To()) 669 return replace, nil 670 } 671 672 // enqueueTx inserts a new transaction into the non-executable transaction queue. 673 // 674 // Note, this method assumes the pool lock is held! 675 func (pool *TxPool) enqueueTx(hash common.Hash, tx *types.Transaction) (bool, error) { 676 // Try to insert the transaction into the future queue 677 from, _ := types.Sender(pool.signer, tx) // already validated 678 if pool.queue[from] == nil { 679 pool.queue[from] = newTxList(false) 680 } 681 inserted, old := pool.queue[from].Add(tx, pool.config.PriceBump) 682 if !inserted { 683 // An older transaction was better, discard this 684 queuedDiscardCounter.Inc(1) 685 return false, ErrReplaceUnderpriced 686 } 687 // Discard any previous transaction and mark this 688 if old != nil { 689 pool.all.Remove(old.Hash()) 690 pool.priced.Removed() 691 queuedReplaceCounter.Inc(1) 692 } 693 if pool.all.Get(hash) == nil { 694 pool.all.Add(tx) 695 pool.priced.Put(tx) 696 } 697 return old != nil, nil 698 } 699 700 // journalTx adds the specified transaction to the local disk journal if it is 701 // deemed to have been sent from a local account. 702 func (pool *TxPool) journalTx(from common.Address, tx *types.Transaction) { 703 // Only journal if it's enabled and the transaction is local 704 if pool.journal == nil || !pool.locals.contains(from) { 705 return 706 } 707 if err := pool.journal.insert(tx); err != nil { 708 log.Warn("Failed to journal local transaction", "err", err) 709 } 710 } 711 712 // promoteTx adds a transaction to the pending (processable) list of transactions 713 // and returns whether it was inserted or an older was better. 714 // 715 // Note, this method assumes the pool lock is held! 716 func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) bool { 717 // Try to insert the transaction into the pending queue 718 if pool.pending[addr] == nil { 719 pool.pending[addr] = newTxList(true) 720 } 721 list := pool.pending[addr] 722 723 inserted, old := list.Add(tx, pool.config.PriceBump) 724 if !inserted { 725 // An older transaction was better, discard this 726 pool.all.Remove(hash) 727 pool.priced.Removed() 728 729 pendingDiscardCounter.Inc(1) 730 return false 731 } 732 // Otherwise discard any previous transaction and mark this 733 if old != nil { 734 pool.all.Remove(old.Hash()) 735 pool.priced.Removed() 736 737 pendingReplaceCounter.Inc(1) 738 } 739 // Failsafe to work around direct pending inserts (tests) 740 if pool.all.Get(hash) == nil { 741 pool.all.Add(tx) 742 pool.priced.Put(tx) 743 } 744 // Set the potentially new pending nonce and notify any subsystems of the new tx 745 pool.beats[addr] = time.Now() 746 pool.pendingState.SetNonce(addr, tx.Nonce()+1) 747 748 return true 749 } 750 751 // AddLocal enqueues a single transaction into the pool if it is valid, marking 752 // the sender as a local one in the mean time, ensuring it goes around the local 753 // pricing constraints. 754 func (pool *TxPool) AddLocal(tx *types.Transaction) error { 755 return pool.addTx(tx, !pool.config.NoLocals) 756 } 757 758 // AddRemote enqueues a single transaction into the pool if it is valid. If the 759 // sender is not among the locally tracked ones, full pricing constraints will 760 // apply. 761 func (pool *TxPool) AddRemote(tx *types.Transaction) error { 762 return pool.addTx(tx, false) 763 } 764 765 // AddLocals enqueues a batch of transactions into the pool if they are valid, 766 // marking the senders as a local ones in the mean time, ensuring they go around 767 // the local pricing constraints. 768 func (pool *TxPool) AddLocals(txs []*types.Transaction) []error { 769 return pool.addTxs(txs, !pool.config.NoLocals) 770 } 771 772 // AddRemotes enqueues a batch of transactions into the pool if they are valid. 773 // If the senders are not among the locally tracked ones, full pricing constraints 774 // will apply. 775 func (pool *TxPool) AddRemotes(txs []*types.Transaction) []error { 776 return pool.addTxs(txs, false) 777 } 778 779 // addTx enqueues a single transaction into the pool if it is valid. 780 func (pool *TxPool) addTx(tx *types.Transaction, local bool) error { 781 pool.mu.Lock() 782 defer pool.mu.Unlock() 783 784 // Try to inject the transaction and update any state 785 replace, err := pool.add(tx, local) 786 if err != nil { 787 return err 788 } 789 // If we added a new transaction, run promotion checks and return 790 if !replace { 791 from, _ := types.Sender(pool.signer, tx) // already validated 792 pool.promoteExecutables([]common.Address{from}) 793 } 794 return nil 795 } 796 797 // addTxs attempts to queue a batch of transactions if they are valid. 798 func (pool *TxPool) addTxs(txs []*types.Transaction, local bool) []error { 799 pool.mu.Lock() 800 defer pool.mu.Unlock() 801 802 return pool.addTxsLocked(txs, local) 803 } 804 805 // addTxsLocked attempts to queue a batch of transactions if they are valid, 806 // whilst assuming the transaction pool lock is already held. 807 func (pool *TxPool) addTxsLocked(txs []*types.Transaction, local bool) []error { 808 // Add the batch of transaction, tracking the accepted ones 809 dirty := make(map[common.Address]struct{}) 810 errs := make([]error, len(txs)) 811 812 for i, tx := range txs { 813 var replace bool 814 if replace, errs[i] = pool.add(tx, local); errs[i] == nil { 815 if !replace { 816 from, _ := types.Sender(pool.signer, tx) // already validated 817 dirty[from] = struct{}{} 818 } 819 } 820 } 821 // Only reprocess the internal state if something was actually added 822 if len(dirty) > 0 { 823 addrs := make([]common.Address, 0, len(dirty)) 824 for addr := range dirty { 825 addrs = append(addrs, addr) 826 } 827 pool.promoteExecutables(addrs) 828 } 829 return errs 830 } 831 832 // Status returns the status (unknown/pending/queued) of a batch of transactions 833 // identified by their hashes. 834 func (pool *TxPool) Status(hashes []common.Hash) []TxStatus { 835 pool.mu.RLock() 836 defer pool.mu.RUnlock() 837 838 status := make([]TxStatus, len(hashes)) 839 for i, hash := range hashes { 840 if tx := pool.all.Get(hash); tx != nil { 841 from, _ := types.Sender(pool.signer, tx) // already validated 842 if pool.pending[from] != nil && pool.pending[from].txs.items[tx.Nonce()] != nil { 843 status[i] = TxStatusPending 844 } else { 845 status[i] = TxStatusQueued 846 } 847 } 848 } 849 return status 850 } 851 852 // Get returns a transaction if it is contained in the pool 853 // and nil otherwise. 854 func (pool *TxPool) Get(hash common.Hash) *types.Transaction { 855 return pool.all.Get(hash) 856 } 857 858 // removeTx removes a single transaction from the queue, moving all subsequent 859 // transactions back to the future queue. 860 func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) { 861 // Fetch the transaction we wish to delete 862 tx := pool.all.Get(hash) 863 if tx == nil { 864 return 865 } 866 addr, _ := types.Sender(pool.signer, tx) // already validated during insertion 867 868 // Remove it from the list of known transactions 869 pool.all.Remove(hash) 870 if outofbound { 871 pool.priced.Removed() 872 } 873 // Remove the transaction from the pending lists and reset the account nonce 874 if pending := pool.pending[addr]; pending != nil { 875 if removed, invalids := pending.Remove(tx); removed { 876 // If no more pending transactions are left, remove the list 877 if pending.Empty() { 878 delete(pool.pending, addr) 879 delete(pool.beats, addr) 880 } 881 // Postpone any invalidated transactions 882 for _, tx := range invalids { 883 pool.enqueueTx(tx.Hash(), tx) 884 } 885 // Update the account nonce if needed 886 if nonce := tx.Nonce(); pool.pendingState.GetNonce(addr) > nonce { 887 pool.pendingState.SetNonce(addr, nonce) 888 } 889 return 890 } 891 } 892 // Transaction is in the future queue 893 if future := pool.queue[addr]; future != nil { 894 future.Remove(tx) 895 if future.Empty() { 896 delete(pool.queue, addr) 897 } 898 } 899 } 900 901 // promoteExecutables moves transactions that have become processable from the 902 // future queue to the set of pending transactions. During this process, all 903 // invalidated transactions (low nonce, low balance) are deleted. 904 func (pool *TxPool) promoteExecutables(accounts []common.Address) { 905 // Track the promoted transactions to broadcast them at once 906 var promoted []*types.Transaction 907 908 // Gather all the accounts potentially needing updates 909 if accounts == nil { 910 accounts = make([]common.Address, 0, len(pool.queue)) 911 for addr := range pool.queue { 912 accounts = append(accounts, addr) 913 } 914 } 915 // Iterate over all accounts and promote any executable transactions 916 for _, addr := range accounts { 917 list := pool.queue[addr] 918 if list == nil { 919 continue // Just in case someone calls with a non existing account 920 } 921 // Drop all transactions that are deemed too old (low nonce) 922 for _, tx := range list.Forward(pool.currentState.GetNonce(addr)) { 923 hash := tx.Hash() 924 log.Trace("Removed old queued transaction", "hash", hash) 925 pool.all.Remove(hash) 926 pool.priced.Removed() 927 } 928 // Drop all transactions that are too costly (low balance or out of gas) 929 drops, _ := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas) 930 for _, tx := range drops { 931 hash := tx.Hash() 932 log.Trace("Removed unpayable queued transaction", "hash", hash) 933 pool.all.Remove(hash) 934 pool.priced.Removed() 935 queuedNofundsCounter.Inc(1) 936 } 937 // Gather all executable transactions and promote them 938 for _, tx := range list.Ready(pool.pendingState.GetNonce(addr)) { 939 hash := tx.Hash() 940 if pool.promoteTx(addr, hash, tx) { 941 log.Trace("Promoting queued transaction", "hash", hash) 942 promoted = append(promoted, tx) 943 } 944 } 945 // Drop all transactions over the allowed limit 946 if !pool.locals.contains(addr) { 947 for _, tx := range list.Cap(int(pool.config.AccountQueue)) { 948 hash := tx.Hash() 949 pool.all.Remove(hash) 950 pool.priced.Removed() 951 queuedRateLimitCounter.Inc(1) 952 log.Trace("Removed cap-exceeding queued transaction", "hash", hash) 953 } 954 } 955 // Delete the entire queue entry if it became empty. 956 if list.Empty() { 957 delete(pool.queue, addr) 958 } 959 } 960 // Notify subsystem for new promoted transactions. 961 if len(promoted) > 0 { 962 go pool.txFeed.Send(NewTxsEvent{promoted}) 963 } 964 // If the pending limit is overflown, start equalizing allowances 965 pending := uint64(0) 966 for _, list := range pool.pending { 967 pending += uint64(list.Len()) 968 } 969 if pending > pool.config.GlobalSlots { 970 pendingBeforeCap := pending 971 // Assemble a spam order to penalize large transactors first 972 spammers := prque.New() 973 for addr, list := range pool.pending { 974 // Only evict transactions from high rollers 975 if !pool.locals.contains(addr) && uint64(list.Len()) > pool.config.AccountSlots { 976 spammers.Push(addr, float32(list.Len())) 977 } 978 } 979 // Gradually drop transactions from offenders 980 offenders := []common.Address{} 981 for pending > pool.config.GlobalSlots && !spammers.Empty() { 982 // Retrieve the next offender if not local address 983 offender, _ := spammers.Pop() 984 offenders = append(offenders, offender.(common.Address)) 985 986 // Equalize balances until all the same or below threshold 987 if len(offenders) > 1 { 988 // Calculate the equalization threshold for all current offenders 989 threshold := pool.pending[offender.(common.Address)].Len() 990 991 // Iteratively reduce all offenders until below limit or threshold reached 992 for pending > pool.config.GlobalSlots && pool.pending[offenders[len(offenders)-2]].Len() > threshold { 993 for i := 0; i < len(offenders)-1; i++ { 994 list := pool.pending[offenders[i]] 995 for _, tx := range list.Cap(list.Len() - 1) { 996 // Drop the transaction from the global pools too 997 hash := tx.Hash() 998 pool.all.Remove(hash) 999 pool.priced.Removed() 1000 1001 // Update the account nonce to the dropped transaction 1002 if nonce := tx.Nonce(); pool.pendingState.GetNonce(offenders[i]) > nonce { 1003 pool.pendingState.SetNonce(offenders[i], nonce) 1004 } 1005 log.Trace("Removed fairness-exceeding pending transaction", "hash", hash) 1006 } 1007 pending-- 1008 } 1009 } 1010 } 1011 } 1012 // If still above threshold, reduce to limit or min allowance 1013 if pending > pool.config.GlobalSlots && len(offenders) > 0 { 1014 for pending > pool.config.GlobalSlots && uint64(pool.pending[offenders[len(offenders)-1]].Len()) > pool.config.AccountSlots { 1015 for _, addr := range offenders { 1016 list := pool.pending[addr] 1017 for _, tx := range list.Cap(list.Len() - 1) { 1018 // Drop the transaction from the global pools too 1019 hash := tx.Hash() 1020 pool.all.Remove(hash) 1021 pool.priced.Removed() 1022 1023 // Update the account nonce to the dropped transaction 1024 if nonce := tx.Nonce(); pool.pendingState.GetNonce(addr) > nonce { 1025 pool.pendingState.SetNonce(addr, nonce) 1026 } 1027 log.Trace("Removed fairness-exceeding pending transaction", "hash", hash) 1028 } 1029 pending-- 1030 } 1031 } 1032 } 1033 pendingRateLimitCounter.Inc(int64(pendingBeforeCap - pending)) 1034 } 1035 // If we've queued more transactions than the hard limit, drop oldest ones 1036 queued := uint64(0) 1037 for _, list := range pool.queue { 1038 queued += uint64(list.Len()) 1039 } 1040 if queued > pool.config.GlobalQueue { 1041 // Sort all accounts with queued transactions by heartbeat 1042 addresses := make(addresssByHeartbeat, 0, len(pool.queue)) 1043 for addr := range pool.queue { 1044 if !pool.locals.contains(addr) { // don't drop locals 1045 addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]}) 1046 } 1047 } 1048 sort.Sort(addresses) 1049 1050 // Drop transactions until the total is below the limit or only locals remain 1051 for drop := queued - pool.config.GlobalQueue; drop > 0 && len(addresses) > 0; { 1052 addr := addresses[len(addresses)-1] 1053 list := pool.queue[addr.address] 1054 1055 addresses = addresses[:len(addresses)-1] 1056 1057 // Drop all transactions if they are less than the overflow 1058 if size := uint64(list.Len()); size <= drop { 1059 for _, tx := range list.Flatten() { 1060 pool.removeTx(tx.Hash(), true) 1061 } 1062 drop -= size 1063 queuedRateLimitCounter.Inc(int64(size)) 1064 continue 1065 } 1066 // Otherwise drop only last few transactions 1067 txs := list.Flatten() 1068 for i := len(txs) - 1; i >= 0 && drop > 0; i-- { 1069 pool.removeTx(txs[i].Hash(), true) 1070 drop-- 1071 queuedRateLimitCounter.Inc(1) 1072 } 1073 } 1074 } 1075 } 1076 1077 // demoteUnexecutables removes invalid and processed transactions from the pools 1078 // executable/pending queue and any subsequent transactions that become unexecutable 1079 // are moved back into the future queue. 1080 func (pool *TxPool) demoteUnexecutables() { 1081 // Iterate over all accounts and demote any non-executable transactions 1082 for addr, list := range pool.pending { 1083 nonce := pool.currentState.GetNonce(addr) 1084 1085 // Drop all transactions that are deemed too old (low nonce) 1086 for _, tx := range list.Forward(nonce) { 1087 hash := tx.Hash() 1088 log.Trace("Removed old pending transaction", "hash", hash) 1089 pool.all.Remove(hash) 1090 pool.priced.Removed() 1091 } 1092 // Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later 1093 drops, invalids := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas) 1094 for _, tx := range drops { 1095 hash := tx.Hash() 1096 log.Trace("Removed unpayable pending transaction", "hash", hash) 1097 pool.all.Remove(hash) 1098 pool.priced.Removed() 1099 pendingNofundsCounter.Inc(1) 1100 } 1101 for _, tx := range invalids { 1102 hash := tx.Hash() 1103 log.Trace("Demoting pending transaction", "hash", hash) 1104 pool.enqueueTx(hash, tx) 1105 } 1106 // If there's a gap in front, warn (should never happen) and postpone all transactions 1107 if list.Len() > 0 && list.txs.Get(nonce) == nil { 1108 for _, tx := range list.Cap(0) { 1109 hash := tx.Hash() 1110 log.Error("Demoting invalidated transaction", "hash", hash) 1111 pool.enqueueTx(hash, tx) 1112 } 1113 } 1114 // Delete the entire queue entry if it became empty. 1115 if list.Empty() { 1116 delete(pool.pending, addr) 1117 delete(pool.beats, addr) 1118 } 1119 } 1120 } 1121 1122 // addressByHeartbeat is an account address tagged with its last activity timestamp. 1123 type addressByHeartbeat struct { 1124 address common.Address 1125 heartbeat time.Time 1126 } 1127 1128 type addresssByHeartbeat []addressByHeartbeat 1129 1130 func (a addresssByHeartbeat) Len() int { return len(a) } 1131 func (a addresssByHeartbeat) Less(i, j int) bool { return a[i].heartbeat.Before(a[j].heartbeat) } 1132 func (a addresssByHeartbeat) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 1133 1134 // accountSet is simply a set of addresses to check for existence, and a signer 1135 // capable of deriving addresses from transactions. 1136 type accountSet struct { 1137 accounts map[common.Address]struct{} 1138 signer types.Signer 1139 } 1140 1141 // newAccountSet creates a new address set with an associated signer for sender 1142 // derivations. 1143 func newAccountSet(signer types.Signer) *accountSet { 1144 return &accountSet{ 1145 accounts: make(map[common.Address]struct{}), 1146 signer: signer, 1147 } 1148 } 1149 1150 // contains checks if a given address is contained within the set. 1151 func (as *accountSet) contains(addr common.Address) bool { 1152 _, exist := as.accounts[addr] 1153 return exist 1154 } 1155 1156 // containsTx checks if the sender of a given tx is within the set. If the sender 1157 // cannot be derived, this method returns false. 1158 func (as *accountSet) containsTx(tx *types.Transaction) bool { 1159 if addr, err := types.Sender(as.signer, tx); err == nil { 1160 return as.contains(addr) 1161 } 1162 return false 1163 } 1164 1165 // add inserts a new address into the set to track. 1166 func (as *accountSet) add(addr common.Address) { 1167 as.accounts[addr] = struct{}{} 1168 } 1169 1170 // txLookup is used internally by TxPool to track transactions while allowing lookup without 1171 // mutex contention. 1172 // 1173 // Note, although this type is properly protected against concurrent access, it 1174 // is **not** a type that should ever be mutated or even exposed outside of the 1175 // transaction pool, since its internal state is tightly coupled with the pools 1176 // internal mechanisms. The sole purpose of the type is to permit out-of-bound 1177 // peeking into the pool in TxPool.Get without having to acquire the widely scoped 1178 // TxPool.mu mutex. 1179 type txLookup struct { 1180 all map[common.Hash]*types.Transaction 1181 lock sync.RWMutex 1182 } 1183 1184 // newTxLookup returns a new txLookup structure. 1185 func newTxLookup() *txLookup { 1186 return &txLookup{ 1187 all: make(map[common.Hash]*types.Transaction), 1188 } 1189 } 1190 1191 // Range calls f on each key and value present in the map. 1192 func (t *txLookup) Range(f func(hash common.Hash, tx *types.Transaction) bool) { 1193 t.lock.RLock() 1194 defer t.lock.RUnlock() 1195 1196 for key, value := range t.all { 1197 if !f(key, value) { 1198 break 1199 } 1200 } 1201 } 1202 1203 // Get returns a transaction if it exists in the lookup, or nil if not found. 1204 func (t *txLookup) Get(hash common.Hash) *types.Transaction { 1205 t.lock.RLock() 1206 defer t.lock.RUnlock() 1207 1208 return t.all[hash] 1209 } 1210 1211 // Count returns the current number of items in the lookup. 1212 func (t *txLookup) Count() int { 1213 t.lock.RLock() 1214 defer t.lock.RUnlock() 1215 1216 return len(t.all) 1217 } 1218 1219 // Add adds a transaction to the lookup. 1220 func (t *txLookup) Add(tx *types.Transaction) { 1221 t.lock.Lock() 1222 defer t.lock.Unlock() 1223 1224 t.all[tx.Hash()] = tx 1225 } 1226 1227 // Remove removes a transaction from the lookup. 1228 func (t *txLookup) Remove(hash common.Hash) { 1229 t.lock.Lock() 1230 defer t.lock.Unlock() 1231 1232 delete(t.all, hash) 1233 }