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
This commit is contained in:
prathamesh0 2021-12-17 10:23:04 +05:30 committed by GitHub
parent 105b26d6a3
commit d087667177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 9 deletions

View File

@ -167,13 +167,5 @@ export class EventWatcher {
async _handlePruningComplete (jobData: any): Promise<void> {
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);
}
}

View File

@ -131,6 +131,12 @@ const prefetchBlocks = async (
}
});
try {
await Promise.all(fetchBlockPromises);
} catch (error: any) {
log(error.message);
log('Exiting gracefully');
process.exit(0);
}
}
};

View File

@ -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);
}
}