ethstats: report processing time

This commit is contained in:
MariusVanDerWijden 2025-11-18 19:37:10 +01:00
parent 5e6f7374de
commit eac2b2e5b4
3 changed files with 37 additions and 34 deletions

View file

@ -1778,7 +1778,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
// Fire a single chain head event if we've progressed the chain // Fire a single chain head event if we've progressed the chain
defer func() { defer func() {
if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() { if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {
bc.chainHeadFeed.Send(ChainHeadEvent{Header: lastCanon.Header()}) bc.chainHeadFeed.Send(ChainHeadEvent{Header: lastCanon.Header(), Time: uint64(mclock.Now().Sub(stats.startTime))})
} }
}() }()
// Start the parallel header verifier // Start the parallel header verifier

View file

@ -34,4 +34,5 @@ type ChainEvent struct {
type ChainHeadEvent struct { type ChainHeadEvent struct {
Header *types.Header Header *types.Header
Time uint64
} }

View file

@ -218,7 +218,7 @@ func (s *Service) loop(chainHeadCh chan core.ChainHeadEvent, txEventCh chan core
// Start a goroutine that exhausts the subscriptions to avoid events piling up // Start a goroutine that exhausts the subscriptions to avoid events piling up
var ( var (
quitCh = make(chan struct{}) quitCh = make(chan struct{})
headCh = make(chan *types.Header, 1) headCh = make(chan core.ChainHeadEvent, 1)
txCh = make(chan struct{}, 1) txCh = make(chan struct{}, 1)
) )
go func() { go func() {
@ -230,7 +230,7 @@ func (s *Service) loop(chainHeadCh chan core.ChainHeadEvent, txEventCh chan core
// Notify of chain head events, but drop if too frequent // Notify of chain head events, but drop if too frequent
case head := <-chainHeadCh: case head := <-chainHeadCh:
select { select {
case headCh <- head.Header: case headCh <- head:
default: default:
} }
@ -330,7 +330,7 @@ func (s *Service) loop(chainHeadCh chan core.ChainHeadEvent, txEventCh chan core
log.Warn("Requested history report failed", "err", err) log.Warn("Requested history report failed", "err", err)
} }
case head := <-headCh: case head := <-headCh:
if err = s.reportBlock(conn, head); err != nil { if err = s.reportBlock(conn, &head); err != nil {
log.Warn("Block stats report failed", "err", err) log.Warn("Block stats report failed", "err", err)
} }
if err = s.reportPending(conn); err != nil { if err = s.reportPending(conn); err != nil {
@ -582,6 +582,7 @@ type blockStats struct {
TxHash common.Hash `json:"transactionsRoot"` TxHash common.Hash `json:"transactionsRoot"`
Root common.Hash `json:"stateRoot"` Root common.Hash `json:"stateRoot"`
Uncles uncleStats `json:"uncles"` Uncles uncleStats `json:"uncles"`
ProcessingTime uint64 `json:"processingTime"`
} }
// txStats is the information to report about individual transactions. // txStats is the information to report about individual transactions.
@ -601,9 +602,9 @@ func (s uncleStats) MarshalJSON() ([]byte, error) {
} }
// reportBlock retrieves the current chain head and reports it to the stats server. // reportBlock retrieves the current chain head and reports it to the stats server.
func (s *Service) reportBlock(conn *connWrapper, header *types.Header) error { func (s *Service) reportBlock(conn *connWrapper, header *core.ChainHeadEvent) error {
// Gather the block details from the header or block chain // Gather the block details from the header or block chain
details := s.assembleBlockStats(header) details := s.assembleBlockStats(header.Header, header.Time)
// Short circuit if the block detail is not available. // Short circuit if the block detail is not available.
if details == nil { if details == nil {
@ -624,7 +625,7 @@ func (s *Service) reportBlock(conn *connWrapper, header *types.Header) error {
// assembleBlockStats retrieves any required metadata to report a single block // assembleBlockStats retrieves any required metadata to report a single block
// and assembles the block stats. If block is nil, the current head is processed. // and assembles the block stats. If block is nil, the current head is processed.
func (s *Service) assembleBlockStats(header *types.Header) *blockStats { func (s *Service) assembleBlockStats(header *types.Header, time uint64) *blockStats {
// Gather the block infos from the local blockchain // Gather the block infos from the local blockchain
var ( var (
txs []txStats txs []txStats
@ -671,6 +672,7 @@ func (s *Service) assembleBlockStats(header *types.Header) *blockStats {
TxHash: header.TxHash, TxHash: header.TxHash,
Root: header.Root, Root: header.Root,
Uncles: uncles, Uncles: uncles,
ProcessingTime: time,
} }
} }
@ -699,7 +701,7 @@ func (s *Service) reportHistory(conn *connWrapper, list []uint64) error {
// Retrieve the next block if it's known to us // Retrieve the next block if it's known to us
header, _ := s.backend.HeaderByNumber(context.Background(), rpc.BlockNumber(number)) header, _ := s.backend.HeaderByNumber(context.Background(), rpc.BlockNumber(number))
if header != nil { if header != nil {
history[len(history)-1-i] = s.assembleBlockStats(header) history[len(history)-1-i] = s.assembleBlockStats(header, 0)
continue continue
} }
// Ran out of blocks, cut the report short and send // Ran out of blocks, cut the report short and send