Skip to content

Commit fa85f6b

Browse files
authored
Fix edge case where existing range end block causes crashes (#121)
### TL;DR Improved error handling and logging for blockchain polling operations ### What changed? - Added a new `ErrNoNewBlocks` error type to explicitly handle cases when no new blocks are available - Updated error logging to suppress errors when no new blocks are found - Enhanced the `reachedPollLimit` function to handle nil block numbers - Modified `getNextBlockRange` to return `ErrNoNewBlocks` instead of nil when no blocks are available ### How to test? 1. Run the poller with a blockchain that has no new blocks 2. Verify that no error messages appear in the logs 3. Test with nil block numbers to ensure proper handling 4. Verify that polling continues normally when new blocks become available ### Why make this change? The previous implementation would log errors for expected scenarios (like no new blocks being available), creating unnecessary noise in the logs. This change provides clearer error handling and better distinguishes between actual errors and expected states, making the system easier to monitor and debug.
2 parents 7aeffe8 + da8a524 commit fa85f6b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

internal/orchestrator/poller.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func NewBoundlessPoller(rpc rpc.IRPCClient, storage storage.IStorage) *Poller {
5252
}
5353
}
5454

55+
var ErrNoNewBlocks = fmt.Errorf("no new blocks to poll")
56+
5557
func NewPoller(rpc rpc.IRPCClient, storage storage.IStorage) *Poller {
5658
poller := NewBoundlessPoller(rpc, storage)
5759
untilBlock := big.NewInt(int64(config.Cfg.Poller.UntilBlock))
@@ -89,7 +91,9 @@ func (p *Poller) Start() {
8991
blockRangeMutex.Unlock()
9092

9193
if err != nil {
92-
log.Error().Err(err).Msg("Error getting block range")
94+
if err != ErrNoNewBlocks {
95+
log.Error().Err(err).Msg("Failed to get block range to poll")
96+
}
9397
continue
9498
}
9599

@@ -132,7 +136,7 @@ func (p *Poller) Poll(blockNumbers []*big.Int) (lastPolledBlock *big.Int) {
132136
}
133137

134138
func (p *Poller) reachedPollLimit(blockNumber *big.Int) bool {
135-
return p.pollUntilBlock.Sign() > 0 && blockNumber.Cmp(p.pollUntilBlock) >= 0
139+
return blockNumber == nil || (p.pollUntilBlock.Sign() > 0 && blockNumber.Cmp(p.pollUntilBlock) >= 0)
136140
}
137141

138142
func (p *Poller) getNextBlockRange() ([]*big.Int, error) {
@@ -145,7 +149,7 @@ func (p *Poller) getNextBlockRange() ([]*big.Int, error) {
145149
startBlock := new(big.Int).Add(p.lastPolledBlock, big.NewInt(1))
146150
if startBlock.Cmp(latestBlock) > 0 {
147151
log.Debug().Msgf("Start block %s is greater than latest block %s, skipping", startBlock, latestBlock)
148-
return nil, nil
152+
return nil, ErrNoNewBlocks
149153
}
150154
endBlock := p.getEndBlockForRange(startBlock, latestBlock)
151155
if startBlock.Cmp(endBlock) > 0 {

0 commit comments

Comments
 (0)