Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch RPC if block number is less than last recorded block number #1264

Draft
wants to merge 1 commit into
base: releases/v2.1.0
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ func (bm *BlockMonitor) updateLatestBlock() {
bm.mu.Lock()
defer bm.mu.Unlock()

// Check if the fetched block number is less than the last recorded block number.
if bm.latestBlock != nil && header.Number.Uint64() < bm.latestBlock.Number.Uint64() {
logrus.Warnf("Fetched block number %d is less than the last recorded block number %d. Switching to the next best RPC endpoint.",
header.Number.Uint64(), bm.latestBlock.Number.Uint64())

// Attempt to switch to the next best RPC endpoint.
if bm.rpcManager != nil {
switched, err := bm.rpcManager.SwitchToNextBestRPCClient()
if err != nil {
logrus.Errorf("Failed to switch RPC endpoint: %v", err)
} else if switched {
logrus.Info("Switched to the next best RPC endpoint.")
bm.updateClient()
} else {
logrus.Warn("Retaining the current best RPC endpoint as no valid alternate was found.")
}
}
return
}

// Update the latest block only if it changes.
if bm.latestBlock == nil || header.Number.Uint64() != bm.latestBlock.Number.Uint64() {
bm.latestBlock = header
Expand Down