Skip to content

Commit

Permalink
fix: handle exceptions in nextEvents (#281)
Browse files Browse the repository at this point in the history
Previously if we encountered exception there it could result in entire app crashing.
  • Loading branch information
Sekhmet authored Mar 15, 2024
1 parent aaac87f commit 3a97296
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,30 @@ export default class Checkpoint {
return;
}

const toBlock = Math.min(blockNum + BLOCK_PRELOAD, this.prefetchEndBlock);
const checkpoints = await this.networkProvider.getCheckpointsRange(blockNum, toBlock);
let nextBlock = blockNum;
try {
const toBlock = Math.min(blockNum + BLOCK_PRELOAD, this.prefetchEndBlock);
const checkpoints = await this.networkProvider.getCheckpointsRange(blockNum, toBlock);

await this.store.insertCheckpoints(checkpoints);
await this.store.setMetadata(MetadataId.LastPrefetchedBlock, toBlock);
await this.store.insertCheckpoints(checkpoints);
await this.store.setMetadata(MetadataId.LastPrefetchedBlock, toBlock);

this.log.info(
{ blockNumber: blockNum, checkpointsLength: checkpoints.length },
'checkpoints inserted'
);
this.log.info(
{ blockNumber: blockNum, checkpointsLength: checkpoints.length },
'checkpoints inserted'
);

nextBlock = blockNum + BLOCK_PRELOAD + 1;
} catch (e) {
this.log.error(
{ blockNumber: blockNum, err: e },
'error occurred during checkpoint fetching'
);

await Promise.delay(10000);
}

this.nextEvents(blockNum + BLOCK_PRELOAD + 1);
this.nextEvents(nextBlock);
}

private async next(blockNum: number) {
Expand Down

0 comments on commit 3a97296

Please sign in to comment.