ethstats: did some error handling in ethstats.go

This commit is contained in:
Pratik Patil 2024-05-24 15:59:31 +05:30
parent b6474e9f90
commit 69b496bb5f
No known key found for this signature in database
GPG key ID: AFDCA496554874B3

View file

@ -632,6 +632,7 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
td *big.Int td *big.Int
txs []txStats txs []txStats
uncles []*types.Header uncles []*types.Header
err error
) )
// check if backend is a full node // check if backend is a full node
@ -640,13 +641,14 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
// Retrieve current chain head if no block is given. // Retrieve current chain head if no block is given.
if block == nil { if block == nil {
head := fullBackend.CurrentBlock() 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 // Short circuit if no block is available. It might happen when
// the blockchain is reorging. // the blockchain is reorging.
if block == nil { if err != nil {
log.Error("Failed to retrieve block by number", "err", err)
return nil return nil
} }
}
header = block.Header() header = block.Header()
td = fullBackend.GetTd(context.Background(), header.Hash()) td = fullBackend.GetTd(context.Background(), header.Hash())
@ -712,7 +714,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
var block *types.Block var block *types.Block
if ok { if ok {
block, _ = fullBackend.BlockByNumber(context.Background(), rpc.BlockNumber(number)) // TODO ignore error here ? block, _ = fullBackend.BlockByNumber(context.Background(), rpc.BlockNumber(number))
} else { } else {
if header, _ := s.backend.HeaderByNumber(context.Background(), rpc.BlockNumber(number)); header != nil { if header, _ := s.backend.HeaderByNumber(context.Background(), rpc.BlockNumber(number)); header != nil {
block = types.NewBlockWithHeader(header) block = types.NewBlockWithHeader(header)