Skip to content

Commit

Permalink
fixing retry when not able to fetch a block
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Jan 17, 2024
1 parent 990aaae commit de55d16
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
14 changes: 12 additions & 2 deletions block/fetcher/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,18 @@ func (f *RPCFetcher) Fetch(ctx context.Context, requestedSlot uint64) (out *pbbs
}

resolvedSlot, blockResult, err := f.fetch(ctx, requestedSlot)
if err != nil {
return nil, fmt.Errorf("fetching block %d: %w", requestedSlot, err)
for {
blockResult, err = f.rpcClient.GetBlockWithOpts(ctx, requestedSlot, GetBlockOpts)
if err != nil {
var rpcErr *jsonrpc.RPCError
errors.As(err, &rpcErr)
if rpcErr != nil && rpcErr.Code == -32009 {
requestedSlot += 1
continue
}
return nil, fmt.Errorf("fetching block %d: %w", requestedSlot, err)
}
break
}

block, err := blockFromBlockResult(resolvedSlot, f.latestConfirmedSlot, f.latestFinalizedSlot, blockResult)
Expand Down
20 changes: 17 additions & 3 deletions cmd/firesol/rpc/fetcher.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package rpc

import (
"errors"
"fmt"
"strconv"
"time"

"github.com/gagliardetto/solana-go/rpc"
"github.com/gagliardetto/solana-go/rpc/jsonrpc"
"github.com/spf13/cobra"
"github.com/streamingfast/bstream"
"github.com/streamingfast/cli/sflags"
Expand Down Expand Up @@ -68,9 +70,21 @@ func fetchRunE(logger *zap.Logger, tracer logging.Tracer) firecore.CommandExecut
}

logger.Info("Found latest slot", zap.Uint64("slot_number", latestSlot))
requestedBlock, err := rpcClient.GetBlockWithOpts(ctx, latestSlot, fetcher.GetBlockOpts)
if err != nil {
return fmt.Errorf("getting requested block %d: %w", latestSlot, err)

var requestedBlock *rpc.GetBlockResult
for {
requestedBlock, err = rpcClient.GetBlockWithOpts(ctx, latestSlot, fetcher.GetBlockOpts)
if err != nil {
var rpcErr *jsonrpc.RPCError
if errors.As(err, &rpcErr) {
if rpcErr.Code == -32004 {
time.Sleep(latestBlockRetryInterval)
continue
}
}
return fmt.Errorf("getting requested block %d: %w", latestSlot, err)
}
break
}

err = poller.Run(ctx, startBlock, bstream.NewBlockRef(requestedBlock.Blockhash.String(), latestSlot))
Expand Down

0 comments on commit de55d16

Please sign in to comment.