Skip to content

Commit 23884ad

Browse files
authored
track latest block immediately on start (#216)
### TL;DR Refactored the ChainTracker to track the latest block number immediately on start and extract the tracking logic into a separate method. ### What changed? - Extracted the block number tracking logic into a new `trackLatestBlockNumber` method - Added a call to `trackLatestBlockNumber` immediately after the ChainTracker starts, before entering the main loop - Updated the ticker handler to call the new method instead of containing the logic directly ### How to test? 1. Start the application and verify that the chain tracker begins tracking the latest block number immediately 2. Check logs to confirm the tracker continues to update at regular intervals 3. Verify that metrics for ChainHead are being set correctly ### Why make this change? This change improves the ChainTracker by: 1. Tracking the latest block number immediately on startup instead of waiting for the first ticker event 2. Improving code organization by extracting the tracking logic into a reusable method 3. Reducing code duplication between the initial tracking and the periodic updates <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Improved internal logic for tracking and updating the latest block number, resulting in more modular and maintainable code. No changes to user-facing functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents ee2637c + ab704ec commit 23884ad

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

internal/orchestrator/chain_tracker.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@ func (ct *ChainTracker) Start(ctx context.Context) {
2929
defer ticker.Stop()
3030

3131
log.Debug().Msgf("Chain tracker running")
32+
ct.trackLatestBlockNumber(ctx)
33+
3234
for {
3335
select {
3436
case <-ctx.Done():
3537
log.Info().Msg("Chain tracker shutting down")
3638
return
3739
case <-ticker.C:
38-
latestBlockNumber, err := ct.rpc.GetLatestBlockNumber(ctx)
39-
if err != nil {
40-
log.Error().Err(err).Msg("Error getting latest block number")
41-
continue
42-
}
43-
latestBlockNumberFloat, _ := latestBlockNumber.Float64()
44-
metrics.ChainHead.Set(latestBlockNumberFloat)
40+
ct.trackLatestBlockNumber(ctx)
4541
}
4642
}
4743
}
44+
45+
func (ct *ChainTracker) trackLatestBlockNumber(ctx context.Context) {
46+
latestBlockNumber, err := ct.rpc.GetLatestBlockNumber(ctx)
47+
if err != nil {
48+
log.Error().Err(err).Msg("Error getting latest block number")
49+
return
50+
}
51+
latestBlockNumberFloat, _ := latestBlockNumber.Float64()
52+
metrics.ChainHead.Set(latestBlockNumberFloat)
53+
}

0 commit comments

Comments
 (0)