Skip to content

Commit

Permalink
added error handeling in ethstats.go which prevents node to panic (#1249
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pratikspatil024 authored May 27, 2024
1 parent d95c05b commit 5f6a76b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,11 @@ 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 All @@ -712,14 +717,21 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
td *big.Int
txs []txStats
uncles []*types.Header
err error
)

// check if backend is a full node
fullBackend, ok := s.backend.(fullNodeBackend)
if ok {
if block == nil {
head := fullBackend.CurrentBlock()
block, _ = fullBackend.BlockByNumber(context.Background(), rpc.BlockNumber(head.Number.Uint64()))
block, err = 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 err != nil {
log.Error("Failed to retrieve block by number", "err", err)
return nil
}
}

header = block.Header()
Expand Down Expand Up @@ -791,8 +803,12 @@ func (s *Service) reportHistory(conn *connWrapper, list []uint64) error {
fullBackend, ok := s.backend.(fullNodeBackend)
// Retrieve the next block if it's known to us
var block *types.Block
var err error
if ok {
block, _ = fullBackend.BlockByNumber(context.Background(), rpc.BlockNumber(number)) // TODO ignore error here ?
block, err = fullBackend.BlockByNumber(context.Background(), rpc.BlockNumber(number))
if err != nil {
log.Error("Failed to retrieve block by number", "err", err)
}
} else {
if header, _ := s.backend.HeaderByNumber(context.Background(), rpc.BlockNumber(number)); header != nil {
block = types.NewBlockWithHeader(header)
Expand Down

0 comments on commit 5f6a76b

Please sign in to comment.