Skip to content

Commit

Permalink
Add retries
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanTinianov committed Oct 2, 2024
1 parent 4d72403 commit df8a356
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
6 changes: 5 additions & 1 deletion pkg/solana/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ func TestSolanaChain_MultiNode_GetClient(t *testing.T) {

ch := solcfg.Chain{}
ch.SetDefaults()
mn := solcfg.MultiNodeConfig{}
mn := solcfg.MultiNodeConfig{
MultiNode: solcfg.MultiNode{
Enabled: ptr(true),
},
}
mn.SetDefaults()

cfg := &solcfg.TOMLConfig{
Expand Down
41 changes: 35 additions & 6 deletions pkg/solana/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,48 @@ func (c *Client) SubscribeToFinalizedHeads(ctx context.Context) (<-chan *Head, m
}

func (c *Client) LatestBlock(ctx context.Context) (*Head, error) {
// capture chStopInFlight to ensure we are not updating chainInfo with observations related to previous life cycle
//ctx, cancel, chStopInFlight, _, _ := c.acquireQueryCtx(ctx, c.rpcTimeout)

latestBlockHeight, err := c.rpc.GetBlockHeight(ctx, rpc.CommitmentConfirmed)
if err != nil {
return nil, err
}

block, err := c.rpc.GetBlock(ctx, latestBlockHeight)
if err != nil {
return nil, err
// TODO: Trying to see if retries will fix testing issue
retries := 5 // Number of retries
for i := 0; i < retries; i++ {
block, err := c.rpc.GetBlock(ctx, latestBlockHeight)
if err == nil {
head := &Head{GetBlockResult: *block}
c.onNewHead(ctx, c.chStopInFlight, head)
return head, nil
}

// Log the error or handle as needed
fmt.Printf("Error fetching block: %v\n", err)

// Retry after a short delay
time.Sleep(2 * time.Second)
}

head := &Head{GetBlockResult: *block}
c.onNewHead(ctx, c.chStopInFlight, head)
return head, nil
return nil, fmt.Errorf("failed to fetch block after %d retries", retries)

/*
latestBlockHeight, err := c.rpc.GetBlockHeight(ctx, rpc.CommitmentConfirmed)
if err != nil {
return nil, err
}
block, err := c.rpc.GetBlock(ctx, latestBlockHeight)
if err != nil {
return nil, err
}
head := &Head{GetBlockResult: *block}
c.onNewHead(ctx, c.chStopInFlight, head)
return head, nil
*/
}

func (c *Client) LatestFinalizedBlock(ctx context.Context) (*Head, error) {
Expand Down

0 comments on commit df8a356

Please sign in to comment.