Skip to content

Commit 449bfe2

Browse files
rjl493456442gzliudan
authored andcommitted
ethstats: prevent panic if head block is not available ethereum#29020
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.
1 parent 58184f0 commit 449bfe2

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

ethstats/ethstats.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ func (s *Service) reportBlock(conn *connWrapper, block *types.Block) error {
633633
// Gather the block details from the header or block chain
634634
details := s.assembleBlockStats(block)
635635

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

@@ -686,9 +690,15 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
686690
// check if backend is a full node
687691
fullBackend, ok := s.backend.(fullNodeBackend)
688692
if ok {
693+
// Retrieve current chain head if no block is given.
689694
if block == nil {
690695
block = fullBackend.CurrentBlock()
691696
}
697+
// Short circuit if no block is available. It might happen when
698+
// the blockchain is reorging.
699+
if block == nil {
700+
return nil
701+
}
692702
header = block.Header()
693703
td = fullBackend.GetTd(context.Background(), header.Hash())
694704

0 commit comments

Comments
 (0)