Skip to content

Commit

Permalink
Fix edge case where existing range end block causes crashes (#121)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
iuwqyir authored Dec 4, 2024
2 parents 7aeffe8 + da8a524 commit fa85f6b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/orchestrator/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func NewBoundlessPoller(rpc rpc.IRPCClient, storage storage.IStorage) *Poller {
}
}

var ErrNoNewBlocks = fmt.Errorf("no new blocks to poll")

func NewPoller(rpc rpc.IRPCClient, storage storage.IStorage) *Poller {
poller := NewBoundlessPoller(rpc, storage)
untilBlock := big.NewInt(int64(config.Cfg.Poller.UntilBlock))
Expand Down Expand Up @@ -89,7 +91,9 @@ func (p *Poller) Start() {
blockRangeMutex.Unlock()

if err != nil {
log.Error().Err(err).Msg("Error getting block range")
if err != ErrNoNewBlocks {
log.Error().Err(err).Msg("Failed to get block range to poll")
}
continue
}

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

func (p *Poller) reachedPollLimit(blockNumber *big.Int) bool {
return p.pollUntilBlock.Sign() > 0 && blockNumber.Cmp(p.pollUntilBlock) >= 0
return blockNumber == nil || (p.pollUntilBlock.Sign() > 0 && blockNumber.Cmp(p.pollUntilBlock) >= 0)
}

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

0 comments on commit fa85f6b

Please sign in to comment.