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