From d08766717719caad3bdeb8cb0d233816ac085d80 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Fri, 17 Dec 2021 10:23:04 +0530 Subject: [PATCH] Update sync status after pruning before processing next block (#318) * Update sync status after pruning before processing next block * Exit gracefully on error while prefetching blocks --- packages/util/src/events.ts | 8 -------- packages/util/src/fill.ts | 8 +++++++- packages/util/src/job-runner.ts | 7 +++++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/util/src/events.ts b/packages/util/src/events.ts index e4ff1a8e..56a1f7b4 100644 --- a/packages/util/src/events.ts +++ b/packages/util/src/events.ts @@ -167,13 +167,5 @@ export class EventWatcher { async _handlePruningComplete (jobData: any): Promise { const { pruneBlockHeight } = jobData; log(`Job onComplete pruning at height ${pruneBlockHeight}`); - - const blocks = await this._indexer.getBlocksAtHeight(pruneBlockHeight, false); - - // Only one canonical (not pruned) block should exist at the pruned height. - assert(blocks.length === 1); - const [block] = blocks; - - await this._indexer.updateSyncStatusCanonicalBlock(block.blockHash, block.blockNumber); } } diff --git a/packages/util/src/fill.ts b/packages/util/src/fill.ts index 04ba5023..ca89cbe9 100644 --- a/packages/util/src/fill.ts +++ b/packages/util/src/fill.ts @@ -131,6 +131,12 @@ const prefetchBlocks = async ( } }); - await Promise.all(fetchBlockPromises); + try { + await Promise.all(fetchBlockPromises); + } catch (error: any) { + log(error.message); + log('Exiting gracefully'); + process.exit(0); + } } }; diff --git a/packages/util/src/job-runner.ts b/packages/util/src/job-runner.ts index 42117079..2664fe64 100644 --- a/packages/util/src/job-runner.ts +++ b/packages/util/src/job-runner.ts @@ -90,6 +90,7 @@ export class JobRunner { // Should be at least 1. assert(blocksAtHeight.length); + let newCanonicalBlockHash; // We have more than one node at this height, so prune all nodes not reachable from indexed block at max reorg depth from prune height. // This will lead to orphaned nodes, which will get pruned at the next height. if (blocksAtHeight.length > 1) { @@ -97,6 +98,7 @@ export class JobRunner { // Get ancestor blockHash from indexed block at prune height. const ancestorBlockHash = await this._indexer.getAncestorAtDepth(indexedBlock.blockHash, MAX_REORG_DEPTH); + newCanonicalBlockHash = ancestorBlockHash; const blocksToBePruned = blocksAtHeight.filter(block => ancestorBlockHash !== block.blockHash); @@ -104,7 +106,12 @@ export class JobRunner { // Mark blocks pruned which are not the ancestor block. await this._indexer.markBlocksAsPruned(blocksToBePruned); } + } else { + newCanonicalBlockHash = blocksAtHeight[0].blockHash; } + + // Update the canonical block in the SyncStatus. + await this._indexer.updateSyncStatusCanonicalBlock(newCanonicalBlockHash, pruneBlockHeight); } }