ethclient: fix panic when requesting missing blocks #26817 (#1375)

This commit is contained in:
Daniel Liu 2025-08-22 16:46:55 +08:00 committed by GitHub
parent 9acddd1792
commit 0e6d2f4b94
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -125,15 +125,19 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface
err := ec.c.CallContext(ctx, &raw, method, args...)
if err != nil {
return nil, err
} else if len(raw) == 0 {
return nil, ethereum.ErrNotFound
}
// Decode header and transactions.
var head *types.Header
var body rpcBlock
if err := json.Unmarshal(raw, &head); err != nil {
return nil, err
}
// When the block is not found, the API returns JSON null.
if head == nil {
return nil, ethereum.ErrNotFound
}
var body rpcBlock
if err := json.Unmarshal(raw, &body); err != nil {
return nil, err
}