loop is the transaction pool's main event loop, waiting for and reacting to outside blockchain events as well as for various reporting and transaction eviction events.
()
| 347 | // outside blockchain events as well as for various reporting and transaction |
| 348 | // eviction events. |
| 349 | func (pool *TxPool) loop() { |
| 350 | defer pool.wg.Done() |
| 351 | |
| 352 | // Start the stats reporting and transaction eviction tickers |
| 353 | var prevPending, prevQueued, prevStales int |
| 354 | |
| 355 | report := time.NewTicker(statsReportInterval) |
| 356 | defer report.Stop() |
| 357 | |
| 358 | evict := time.NewTicker(evictionInterval) |
| 359 | defer evict.Stop() |
| 360 | |
| 361 | journal := time.NewTicker(pool.config.Rejournal) |
| 362 | defer journal.Stop() |
| 363 | |
| 364 | rebroadcast := time.NewTicker(rebroadcastTriggerTime) |
| 365 | defer rebroadcast.Stop() |
| 366 | |
| 367 | // Track the previous head headers for transaction reorgs |
| 368 | head := pool.chain.CurrentBlock() |
| 369 | |
| 370 | // Keep waiting for and reacting to the various events |
| 371 | for { |
| 372 | select { |
| 373 | // Handle ChainHeadEvent |
| 374 | case ev := <-pool.chainHeadCh: |
| 375 | if ev.Block != nil { |
| 376 | pool.mu.Lock() |
| 377 | pool.reset(head.Header(), ev.Block.Header()) |
| 378 | head = ev.Block |
| 379 | |
| 380 | pool.mu.Unlock() |
| 381 | } |
| 382 | // Be unsubscribed due to system stopped |
| 383 | case <-pool.chainHeadSub.Err(): |
| 384 | return |
| 385 | |
| 386 | // Handle stats reporting ticks |
| 387 | case <-report.C: |
| 388 | pool.mu.RLock() |
| 389 | pending, queued := pool.stats() |
| 390 | stales := pool.priced.stales |
| 391 | pool.mu.RUnlock() |
| 392 | |
| 393 | if pending != prevPending || queued != prevQueued || stales != prevStales { |
| 394 | log.Debug("Transaction pool status report", "executable", pending, "queued", queued, "stales", stales) |
| 395 | prevPending, prevQueued, prevStales = pending, queued, stales |
| 396 | } |
| 397 | |
| 398 | // Handle inactive account transaction eviction |
| 399 | case <-evict.C: |
| 400 | pool.mu.Lock() |
| 401 | for addr := range pool.queue { |
| 402 | // Skip local transactions from the eviction mechanism |
| 403 | if pool.locals.contains(addr) { |
| 404 | continue |
| 405 | } |
| 406 | // Any non-locals old enough should be removed |
no test coverage detected