diff --git a/internal/orchestrator/reorg_handler.go b/internal/orchestrator/reorg_handler.go index 4cae050..12657c2 100644 --- a/internal/orchestrator/reorg_handler.go +++ b/internal/orchestrator/reorg_handler.go @@ -102,7 +102,12 @@ func (rh *ReorgHandler) RunFromBlock(lookbackFrom *big.Int) (lastCheckedBlock *b return nil, nil } mostRecentBlockHeader := blockHeaders[0] - log.Debug().Msgf("Checking for reorgs from block %s to %s", mostRecentBlockHeader.Number.String(), blockHeaders[len(blockHeaders)-1].Number.String()) + lastBlockHeader := blockHeaders[len(blockHeaders)-1] + if mostRecentBlockHeader.Number.Cmp(lastBlockHeader.Number) == 0 { + log.Debug().Msgf("Most recent (%s) and last checked (%s) block numbers are equal, skipping reorg check", mostRecentBlockHeader.Number.String(), lastBlockHeader.Number.String()) + return nil, nil + } + log.Debug().Msgf("Checking for reorgs from block %s to %s", mostRecentBlockHeader.Number.String(), lastBlockHeader.Number.String()) reorgEndIndex := findReorgEndIndex(blockHeaders) if reorgEndIndex == -1 { return mostRecentBlockHeader.Number, nil diff --git a/internal/storage/clickhouse.go b/internal/storage/clickhouse.go index 6357974..7248007 100644 --- a/internal/storage/clickhouse.go +++ b/internal/storage/clickhouse.go @@ -932,7 +932,8 @@ func (c *ClickHouseConnector) SetLastReorgCheckedBlockNumber(chainId *big.Int, b } func (c *ClickHouseConnector) LookbackBlockHeaders(chainId *big.Int, limit int, lookbackStart *big.Int) (blockHeaders []common.BlockHeader, err error) { - 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()) + lookbackEnd := new(big.Int).Sub(lookbackStart, big.NewInt(int64(limit))) + 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()) query += getLimitClause(limit) rows, err := c.conn.Query(context.Background(), query)