Skip to content

Commit 980f3e3

Browse files
rjl493456442jorgemmsilva
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 cddc8c6 commit 980f3e3

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
@@ -611,6 +611,10 @@ func (s *Service) reportBlock(conn *connWrapper, block *types.Block) error {
611611
// Gather the block details from the header or block chain
612612
details := s.assembleBlockStats(block)
613613

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

@@ -638,10 +642,16 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
638642
// check if backend is a full node
639643
fullBackend, ok := s.backend.(fullNodeBackend)
640644
if ok {
645+
// Retrieve current chain head if no block is given.
641646
if block == nil {
642647
head := fullBackend.CurrentBlock()
643648
block, _ = fullBackend.BlockByNumber(context.Background(), rpc.BlockNumber(head.Number.Uint64()))
644649
}
650+
// Short circuit if no block is available. It might happen when
651+
// the blockchain is reorging.
652+
if block == nil {
653+
return nil
654+
}
645655
header = block.Header()
646656
td = fullBackend.GetTd(context.Background(), header.Hash())
647657

0 commit comments

Comments
 (0)