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