From 1ebfa655ecaa6a3754581e3ba254c4e09a72830b Mon Sep 17 00:00:00 2001 From: iuwqyir Date: Wed, 11 Dec 2024 14:09:12 +0200 Subject: [PATCH] fix reorg handler force from block --- internal/orchestrator/reorg_handler.go | 7 ++++++- internal/storage/clickhouse.go | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) 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)