Skip to content

Commit

Permalink
Add mutex and retry logic to BlockInfoByNumber
Browse files Browse the repository at this point in the history
BlockInfoByNumber was missing the mutex read lock and retry on error handling that all the other functions had.
  • Loading branch information
joshuacolvin0 authored and hkalodner committed May 23, 2022
1 parent 1c32e2e commit d923b2f
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions packages/arb-util/ethutils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/rs/zerolog"

"github.com/offchainlabs/arbitrum/packages/arb-util/arblog"
)

var logger = arblog.Logger.With().Str("component", "ethutils").Logger()

const maxErrCount = 5
const errorCountError = maxErrCount * 10

type ReceiptFetcher interface {
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
Expand Down Expand Up @@ -113,16 +110,9 @@ func (r *RPCEthClient) handleCallErr(err error) error {

// If we've had above a threshold number of errors, reinitialize the connection
if totalErrCount >= maxErrCount {
var partialLog *zerolog.Event
if totalErrCount < errorCountError {
partialLog = logger.Warn()
} else {
partialLog = logger.Error()
}
partialLog.
logger.Warn().
Err(err).
Str("url", r.url).
Uint64("count", totalErrCount).
Msg("Reconnecting to client endpoint after repeated errors")

if err := r.reconnect(); err != nil {
Expand All @@ -133,14 +123,22 @@ func (r *RPCEthClient) handleCallErr(err error) error {
}

func (r *RPCEthClient) BlockInfoByNumber(ctx context.Context, number *big.Int) (*BlockInfo, error) {
info, err := r.blockInfoByNumberImpl(ctx, number)
return info, r.handleCallErr(err)
}

func (r *RPCEthClient) blockInfoByNumberImpl(ctx context.Context, number *big.Int) (*BlockInfo, error) {
var raw json.RawMessage
var numParam string
if number != nil {
numParam = hexutil.EncodeBig(number)
} else {
numParam = "latest"
}
if err := r.rpc.CallContext(ctx, &raw, "eth_getBlockByNumber", numParam, false); err != nil {
r.RLock()
err := r.rpc.CallContext(ctx, &raw, "eth_getBlockByNumber", numParam, false)
r.RUnlock()
if err != nil {
return nil, err
}
if len(raw) == 0 {
Expand Down

0 comments on commit d923b2f

Please sign in to comment.