Skip to content

Commit

Permalink
fix(lib): endpoint HeaderByNumber might return nil (#1573)
Browse files Browse the repository at this point in the history
Backports a fix to protect our v0.2 releases against crashes. See #1568
for more.

issue: none
  • Loading branch information
arajasek authored Jul 24, 2024
1 parent 794c873 commit fc9a39b
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ethclient

import (
"context"
"math/big"

"github.com/omni-network/omni/lib/errors"

Expand Down Expand Up @@ -96,20 +97,18 @@ func (w Wrapper) HeaderByType(ctx context.Context, typ HeadType) (*types.Header,
const endpoint = "header_by_type"
defer latency(w.chain, endpoint)()

var header *types.Header
err := w.cl.Client().CallContext(
ctx,
&header,
"eth_getBlockByNumber",
typ.String(),
false,
)
var bn rpc.BlockNumber
if err := bn.UnmarshalJSON([]byte(typ.String())); err != nil {
return nil, errors.Wrap(err, "unmarshal head type")
}

header, err := w.cl.HeaderByNumber(ctx, big.NewInt(int64(bn)))
if err != nil {
incError(w.chain, endpoint)
return nil, errors.Wrap(err, "get block")
err = errors.Wrap(err, "json-rpc", "endpoint", endpoint)
}

return header, nil
return header, err
}

// SetHead sets the current head of the local chain by block number.
Expand Down

0 comments on commit fc9a39b

Please sign in to comment.