Skip to content

Commit c9d5a1f

Browse files
authored
fix new chain not using configured reorg check start block (#173)
### TL;DR Fixed reorg handler logic to properly handle cases where the configured start block is ahead of the latest committed block. ### What changed? - Refactored `getInitialCheckedBlockNumber` function to improve readability by using more descriptive variable names and restructuring the conditional logic - Added a new check in `getReorgCheckRange` to handle the case where the latest checked block is ahead of the latest committed block, preventing unnecessary processing ### How to test? 1. Configure a reorg handler with a start block that is ahead of the latest committed block 2. Verify that the reorg handler logs the message "Committing has not reached the configured reorg check start block" and continues without error 3. Confirm that the reorg handler correctly uses the configured block when appropriate based on the ForceFromBlock setting ### Why make this change? This change prevents the reorg handler from attempting to process blocks that haven't been committed yet, which could lead to errors or unexpected behavior. The refactoring also improves code readability and maintainability by using clearer variable names and more logical flow.
2 parents d532f55 + 9440077 commit c9d5a1f

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

internal/orchestrator/reorg_handler.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ func NewReorgHandler(rpc rpc.IRPCClient, storage storage.IStorage) *ReorgHandler
4949
}
5050

5151
func getInitialCheckedBlockNumber(storage storage.IStorage, chainId *big.Int) *big.Int {
52-
bn := big.NewInt(int64(config.Cfg.ReorgHandler.FromBlock))
53-
if !config.Cfg.ReorgHandler.ForceFromBlock {
54-
storedFromBlock, err := storage.OrchestratorStorage.GetLastReorgCheckedBlockNumber(chainId)
55-
if err != nil {
56-
log.Debug().Err(err).Msgf("Error getting last reorg checked block number, using configured: %s", bn)
57-
return bn
58-
}
59-
if storedFromBlock.Sign() <= 0 {
60-
log.Debug().Msgf("Last reorg checked block number not found, using configured: %s", bn)
61-
return bn
62-
}
63-
log.Debug().Msgf("Last reorg checked block number found, using: %s", storedFromBlock)
64-
return storedFromBlock
52+
configuredBn := big.NewInt(int64(config.Cfg.ReorgHandler.FromBlock))
53+
if config.Cfg.ReorgHandler.ForceFromBlock {
54+
log.Debug().Msgf("Force from block reorg check flag set, using configured: %s", configuredBn)
55+
return configuredBn
56+
}
57+
storedBn, err := storage.OrchestratorStorage.GetLastReorgCheckedBlockNumber(chainId)
58+
if err != nil {
59+
log.Debug().Err(err).Msgf("Error getting last reorg checked block number, using configured: %s", configuredBn)
60+
return configuredBn
61+
}
62+
if storedBn.Sign() <= 0 {
63+
log.Debug().Msgf("Last reorg checked block number not found, using configured: %s", configuredBn)
64+
return configuredBn
6565
}
66-
log.Debug().Msgf("Force from block reorg check flag set, using configured: %s", bn)
67-
return bn
66+
log.Debug().Msgf("Last reorg checked block number found, using: %s", storedBn)
67+
return storedBn
6868
}
6969

7070
func (rh *ReorgHandler) Start(ctx context.Context) {
@@ -148,6 +148,11 @@ func (rh *ReorgHandler) getReorgCheckRange(latestCheckedBlock *big.Int) (*big.In
148148
if err != nil {
149149
return nil, nil, fmt.Errorf("error getting latest committed block: %w", err)
150150
}
151+
if latestCheckedBlock.Cmp(latestCommittedBlock) > 0 {
152+
log.Debug().Msgf("Committing has not reached the configured reorg check start block: %s (reorg start) > %s (last committed)", latestCheckedBlock.String(), latestCommittedBlock.String())
153+
return latestCheckedBlock, latestCheckedBlock, nil
154+
}
155+
151156
if new(big.Int).Sub(latestCommittedBlock, latestCheckedBlock).Cmp(big.NewInt(int64(rh.blocksPerScan))) < 0 {
152157
// diff between latest committed and latest checked is less than blocksPerScan, so we will look back from the latest committed block
153158
fromBlock := new(big.Int).Sub(latestCommittedBlock, big.NewInt(int64(rh.blocksPerScan)))

0 commit comments

Comments
 (0)