feat(metrics): calculate the real pending tx (#983)

* calculate the real pending tx

* update

* move realPendingTx to miner

* update

* calculate the real pending tx by statsWithMinBaseFee

* update

* fix lint

* address comments

* add metrics to StatsWithMinBaseFee

* change read_lock to write_lock
This commit is contained in:
georgehao 2024-08-20 15:39:28 +08:00 committed by GitHub
parent 4b85bbcbd8
commit 2ccacff1bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -124,13 +124,16 @@ var (
dropBetweenReorgHistogram = metrics.NewRegisteredHistogram("txpool/dropbetweenreorg", nil, metrics.NewExpDecaySample(1028, 0.015)) dropBetweenReorgHistogram = metrics.NewRegisteredHistogram("txpool/dropbetweenreorg", nil, metrics.NewExpDecaySample(1028, 0.015))
pendingGauge = metrics.NewRegisteredGauge("txpool/pending", nil) pendingGauge = metrics.NewRegisteredGauge("txpool/pending", nil)
realPendingGauge = metrics.NewRegisteredGauge("txpool/real_pending", nil)
queuedGauge = metrics.NewRegisteredGauge("txpool/queued", nil) queuedGauge = metrics.NewRegisteredGauge("txpool/queued", nil)
realQueuedGauge = metrics.NewRegisteredGauge("txpool/real_queued", nil)
localGauge = metrics.NewRegisteredGauge("txpool/local", nil) localGauge = metrics.NewRegisteredGauge("txpool/local", nil)
slotsGauge = metrics.NewRegisteredGauge("txpool/slots", nil) slotsGauge = metrics.NewRegisteredGauge("txpool/slots", nil)
reheapTimer = metrics.NewRegisteredTimer("txpool/reheap", nil) reheapTimer = metrics.NewRegisteredTimer("txpool/reheap", nil)
txLifecycleTimer = metrics.NewRegisteredTimer("txpool/txfifecycle", nil) txLifecycleTimer = metrics.NewRegisteredTimer("txpool/txfifecycle", nil)
statsWithMinBaseFeeTimer = metrics.NewRegisteredTimer("txpool/stats_min_base_fee", nil)
) )
// TxStatus is the current status of a transaction as seen by the pool. // TxStatus is the current status of a transaction as seen by the pool.
@ -271,6 +274,7 @@ type TxPool struct {
reorgDoneCh chan chan struct{} reorgDoneCh chan chan struct{}
reorgShutdownCh chan struct{} // requests shutdown of scheduleReorgLoop reorgShutdownCh chan struct{} // requests shutdown of scheduleReorgLoop
reorgPauseCh chan bool // requests to pause scheduleReorgLoop reorgPauseCh chan bool // requests to pause scheduleReorgLoop
realTxActivityShutdownCh chan struct{}
wg sync.WaitGroup // tracks loop, scheduleReorgLoop wg sync.WaitGroup // tracks loop, scheduleReorgLoop
initDoneCh chan struct{} // is closed once the pool is initialized (for tests) initDoneCh chan struct{} // is closed once the pool is initialized (for tests)
@ -303,6 +307,7 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
queueTxEventCh: make(chan *types.Transaction), queueTxEventCh: make(chan *types.Transaction),
reorgDoneCh: make(chan chan struct{}), reorgDoneCh: make(chan chan struct{}),
reorgShutdownCh: make(chan struct{}), reorgShutdownCh: make(chan struct{}),
realTxActivityShutdownCh: make(chan struct{}),
reorgPauseCh: make(chan bool), reorgPauseCh: make(chan bool),
initDoneCh: make(chan struct{}), initDoneCh: make(chan struct{}),
gasPrice: new(big.Int).SetUint64(config.PriceLimit), gasPrice: new(big.Int).SetUint64(config.PriceLimit),
@ -336,9 +341,27 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
pool.wg.Add(1) pool.wg.Add(1)
go pool.loop() go pool.loop()
pool.wg.Add(1)
go pool.periodicallyCalculateRealTxActivity()
return pool return pool
} }
func (pool *TxPool) periodicallyCalculateRealTxActivity() {
defer pool.wg.Done()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
pool.StatsWithMinBaseFee(pool.chain.CurrentBlock().BaseFee())
case <-pool.realTxActivityShutdownCh:
log.Info("Real tx activity calculation stopped")
return
}
}
}
// loop is the transaction pool's main event loop, waiting for and reacting to // 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 // outside blockchain events as well as for various reporting and transaction
// eviction events. // eviction events.
@ -372,6 +395,7 @@ func (pool *TxPool) loop() {
// System shutdown. // System shutdown.
case <-pool.chainHeadSub.Err(): case <-pool.chainHeadSub.Err():
close(pool.reorgShutdownCh) close(pool.reorgShutdownCh)
close(pool.realTxActivityShutdownCh)
return return
// Handle stats reporting ticks // Handle stats reporting ticks
@ -503,10 +527,12 @@ func (pool *TxPool) stats() (int, int) {
// StatsWithMinBaseFee retrieves the current pool stats, namely the number of pending and the // StatsWithMinBaseFee retrieves the current pool stats, namely the number of pending and the
// number of queued (non-executable) transactions greater equal minBaseFee. // number of queued (non-executable) transactions greater equal minBaseFee.
func (pool *TxPool) StatsWithMinBaseFee(minBaseFee *big.Int) (int, int) { func (pool *TxPool) StatsWithMinBaseFee(minBaseFee *big.Int) (int, int) {
pool.mu.RLock() statsStart := time.Now()
defer pool.mu.RUnlock() pool.mu.Lock()
pendingTxs, queuedTxs := pool.statsWithMinBaseFee(minBaseFee)
return pool.statsWithMinBaseFee(minBaseFee) pool.mu.Unlock()
statsWithMinBaseFeeTimer.UpdateSince(statsStart)
return pendingTxs, queuedTxs
} }
// statsWithMinBaseFee retrieves the current pool stats, namely the number of pending and the // statsWithMinBaseFee retrieves the current pool stats, namely the number of pending and the
@ -521,6 +547,7 @@ func (pool *TxPool) statsWithMinBaseFee(minBaseFee *big.Int) (int, int) {
pending++ pending++
} }
} }
realPendingGauge.Update(int64(pending))
queued := 0 queued := 0
for _, list := range pool.queue { for _, list := range pool.queue {
@ -531,6 +558,8 @@ func (pool *TxPool) statsWithMinBaseFee(minBaseFee *big.Int) (int, int) {
queued++ queued++
} }
} }
realQueuedGauge.Update(int64(queued))
return pending, queued return pending, queued
} }