Skip to content

Commit

Permalink
ethstats: prevent panic if head block is not available (#29020)
Browse files Browse the repository at this point in the history
This pull request fixes a flaw in ethstats which can lead to node crash

A panic could happens when the local blockchain is reorging which causes the original head block not to be  reachable (since number->hash canonical mapping is deleted). In order to prevent the panic, the block nilness is now checked in ethstats.
  • Loading branch information
rjl493456442 authored Feb 19, 2024
1 parent 593e303 commit 034bc46
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ func (s *Service) reportBlock(conn *connWrapper, block *types.Block) error {
// Gather the block details from the header or block chain
details := s.assembleBlockStats(block)

// Short circuit if the block detail is not available.
if details == nil {
return nil
}
// Assemble the block report and send it to the server
log.Trace("Sending new block to ethstats", "number", details.Number, "hash", details.Hash)

Expand Down Expand Up @@ -638,10 +642,16 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
// check if backend is a full node
fullBackend, ok := s.backend.(fullNodeBackend)
if ok {
// Retrieve current chain head if no block is given.
if block == nil {
head := fullBackend.CurrentBlock()
block, _ = fullBackend.BlockByNumber(context.Background(), rpc.BlockNumber(head.Number.Uint64()))
}
// Short circuit if no block is available. It might happen when
// the blockchain is reorging.
if block == nil {
return nil
}
header = block.Header()
td = fullBackend.GetTd(context.Background(), header.Hash())

Expand Down

0 comments on commit 034bc46

Please sign in to comment.