From d923b2ff524f7efd530cf6be3c186236b0358c89 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Thu, 19 May 2022 12:53:37 -0700 Subject: [PATCH] Add mutex and retry logic to BlockInfoByNumber BlockInfoByNumber was missing the mutex read lock and retry on error handling that all the other functions had. --- packages/arb-util/ethutils/client.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/arb-util/ethutils/client.go b/packages/arb-util/ethutils/client.go index 1177a4152d..f94cec01bf 100644 --- a/packages/arb-util/ethutils/client.go +++ b/packages/arb-util/ethutils/client.go @@ -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) @@ -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 { @@ -133,6 +123,11 @@ 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 { @@ -140,7 +135,10 @@ func (r *RPCEthClient) BlockInfoByNumber(ctx context.Context, number *big.Int) ( } 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 {