-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move catchup up to listener level, use RPC client and add retries
- Loading branch information
Showing
5 changed files
with
122 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package relayer | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
MaxBlocksPerRequest = 200 | ||
// Max buffer size for subscription channels | ||
maxClientSubscriptionBuffer = 20000 | ||
// Max number of retries for RPC calls before failing catchup | ||
maxRpcRetries = 5 | ||
) | ||
|
||
func (lstnr *Listener) processFromHeight(height *big.Int, done chan bool) { | ||
defer close(done) | ||
lstnr.logger.Info( | ||
"Processing historical logs", | ||
zap.String("fromBlockHeight", height.String()), | ||
zap.String("blockchainID", lstnr.sourceBlockchain.BlockchainID), | ||
) | ||
if height == nil { | ||
lstnr.logger.Error("Cannot process logs from nil height") | ||
done <- false | ||
return | ||
} | ||
|
||
// Grab the latest block before filtering logs so we don't miss any before updating the db | ||
latestBlockHeight, err := lstnr.ethClient.BlockNumber(context.Background()) | ||
if err != nil { | ||
lstnr.logger.Error( | ||
"Failed to get latest block", | ||
zap.String("blockchainID", lstnr.sourceBlockchain.BlockchainID), | ||
zap.Error(err), | ||
) | ||
done <- false | ||
return | ||
} | ||
|
||
bigLatestBlockHeight := big.NewInt(0).SetUint64(latestBlockHeight) | ||
|
||
//nolint:lll | ||
for fromBlock := big.NewInt(0).Set(height); fromBlock.Cmp(bigLatestBlockHeight) <= 0; fromBlock.Add(fromBlock, big.NewInt(MaxBlocksPerRequest)) { | ||
toBlock := big.NewInt(0).Add(fromBlock, big.NewInt(MaxBlocksPerRequest-1)) | ||
|
||
// clamp to latest known block because we've already subscribed | ||
// to new blocks and we don't want to double-process any blocks | ||
// created after that subscription but before the determination | ||
// of this "latest" | ||
if toBlock.Cmp(bigLatestBlockHeight) > 0 { | ||
toBlock.Set(bigLatestBlockHeight) | ||
} | ||
|
||
for i := 0; i < maxRpcRetries; i++ { | ||
err = lstnr.processBlockRange(fromBlock, toBlock) | ||
if err == nil { | ||
break | ||
} | ||
lstnr.logger.Warn("Failed to process block range", zap.Error(err), zap.Int("try", i)) | ||
if i == maxRpcRetries-1 { | ||
lstnr.logger.Error("Failed to process block range after max retries", zap.Error(err)) | ||
return | ||
} | ||
// Sleep before retrying with increasing backoff | ||
time.Sleep(time.Duration(1+i) * time.Second) | ||
} | ||
} | ||
done <- true | ||
} | ||
|
||
// Process Warp messages from the block range [fromBlock, toBlock], inclusive | ||
func (lstnr *Listener) processBlockRange( | ||
fromBlock, toBlock *big.Int, | ||
) error { | ||
for i := fromBlock.Int64(); i <= toBlock.Int64(); i++ { | ||
header, err := lstnr.ethClient.HeaderByNumber(context.Background(), big.NewInt(i)) | ||
if err != nil { | ||
lstnr.logger.Error( | ||
"Failed to get header by number", | ||
zap.String("blockchainID", lstnr.sourceBlockchain.BlockchainID), | ||
zap.Error(err), | ||
) | ||
return err | ||
} | ||
lstnr.headersChan <- header | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters