Skip to content

Commit

Permalink
fix: monitor fails if batch requests are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Nov 23, 2023
1 parent a3adf1f commit ebd2dcd
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitor

import (
"context"
"errors"
"fmt"
"math/big"
"sync"
Expand All @@ -23,6 +24,8 @@ import (
"github.com/rs/zerolog/log"
)

var errBatchRequestsNotSupported = errors.New("batch requests are not supported")

var (
windowSize int
batchSize int
Expand Down Expand Up @@ -88,6 +91,11 @@ func monitor(ctx context.Context) error {
}
ec := ethclient.NewClient(rpc)

// Check if batch requests are supported.
if err := checkBatchRequestsSupport(ctx, ec.Client()); err != nil {

Check failure on line 95 in cmd/monitor/monitor.go

View workflow job for this annotation

GitHub Actions / Lint

declaration of "err" shadows declaration at line 87
return errBatchRequestsNotSupported
}

ms := new(monitorStatus)
ms.BlocksLock.Lock()
ms.BlockCache, _ = lru.New(blockCacheLimit)
Expand Down Expand Up @@ -240,11 +248,9 @@ func (ms *monitorStatus) getBlockRange(ctx context.Context, from, to *big.Int, r
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 3 * time.Minute
retryable := func() error {
err := rpc.BatchCallContext(ctx, blms)
return err
return rpc.BatchCallContext(ctx, blms)
}
err := backoff.Retry(retryable, b)
if err != nil {
if err := backoff.Retry(retryable, b); err != nil {
return err
}

Expand Down Expand Up @@ -703,3 +709,13 @@ func max(nums ...int) int {
}
return m
}

// checkBatchRequestsSupport checks if batch requests are supported by making a sample batch request.
// https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_blocknumber
func checkBatchRequestsSupport(ctx context.Context, ec *ethrpc.Client) error {
batchRequest := []ethrpc.BatchElem{
{Method: "eth_blockNumber"},
{Method: "eth_blockNumber"},
}
return ec.BatchCallContext(ctx, batchRequest)
}

0 comments on commit ebd2dcd

Please sign in to comment.