Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethstats: added some error handling in ethstats.go #29834

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ 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
Expand All @@ -640,12 +641,13 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
// 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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping the old check of block == nil seems safer as it will guarantee that we won't crash later when accessing it. I don't particularly mind retrieving and printing teh error, but would keep the check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karalabe Its the same thing, right? If there is an error fetching the block, the the block returned will be nil and we are returning nil. If the error is not nil, that means the we have a block which is not nil.

But I agree with @fjl :

The PR doesn't meaningfully improve the logic

I can change the log level to DEBUG, but upto you guys to decide. Thanks.

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