Skip to content

Commit 996d70b

Browse files
authored
Fix force range for reorg checks (#126)
### TL;DR Added block range validation and optimized block header queries to prevent unnecessary reorg checks. ### What changed? - Added validation to skip reorg checks when the most recent and last checked block numbers are equal - Modified the `LookbackBlockHeaders` query to include a lower bound on block numbers, creating a specific range for querying - Enhanced logging to better indicate when reorg checks are skipped ### How to test? 1. Run the system with chain monitoring enabled 2. Verify logs show skipped reorg checks when blocks are equal 3. Confirm block header queries return results only within the specified range 4. Validate that reorg detection still functions correctly for actual reorgs ### Why make this change? The previous implementation would perform unnecessary reorg checks when block numbers were identical and could potentially query more blocks than needed. These changes improve efficiency by avoiding redundant operations and limiting the query scope to only relevant block ranges.
2 parents 6b6d94f + 1ebfa65 commit 996d70b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

internal/orchestrator/reorg_handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ func (rh *ReorgHandler) RunFromBlock(lookbackFrom *big.Int) (lastCheckedBlock *b
102102
return nil, nil
103103
}
104104
mostRecentBlockHeader := blockHeaders[0]
105-
log.Debug().Msgf("Checking for reorgs from block %s to %s", mostRecentBlockHeader.Number.String(), blockHeaders[len(blockHeaders)-1].Number.String())
105+
lastBlockHeader := blockHeaders[len(blockHeaders)-1]
106+
if mostRecentBlockHeader.Number.Cmp(lastBlockHeader.Number) == 0 {
107+
log.Debug().Msgf("Most recent (%s) and last checked (%s) block numbers are equal, skipping reorg check", mostRecentBlockHeader.Number.String(), lastBlockHeader.Number.String())
108+
return nil, nil
109+
}
110+
log.Debug().Msgf("Checking for reorgs from block %s to %s", mostRecentBlockHeader.Number.String(), lastBlockHeader.Number.String())
106111
reorgEndIndex := findReorgEndIndex(blockHeaders)
107112
if reorgEndIndex == -1 {
108113
return mostRecentBlockHeader.Number, nil

internal/storage/clickhouse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ func (c *ClickHouseConnector) SetLastReorgCheckedBlockNumber(chainId *big.Int, b
932932
}
933933

934934
func (c *ClickHouseConnector) LookbackBlockHeaders(chainId *big.Int, limit int, lookbackStart *big.Int) (blockHeaders []common.BlockHeader, err error) {
935-
query := fmt.Sprintf("SELECT number, hash, parent_hash FROM %s.blocks WHERE chain_id = %s AND number <= %s AND is_deleted = 0 ORDER BY number DESC", c.cfg.Database, chainId.String(), lookbackStart.String())
935+
lookbackEnd := new(big.Int).Sub(lookbackStart, big.NewInt(int64(limit)))
936+
query := fmt.Sprintf("SELECT number, hash, parent_hash FROM %s.blocks WHERE chain_id = %s AND number <= %s AND number > %s AND is_deleted = 0 ORDER BY number DESC", c.cfg.Database, chainId.String(), lookbackStart.String(), lookbackEnd.String())
936937
query += getLimitClause(limit)
937938

938939
rows, err := c.conn.Query(context.Background(), query)

0 commit comments

Comments
 (0)