mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-02-02 08:12:49 +00:00
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:
parent
105b26d6a3
commit
d087667177
@ -167,13 +167,5 @@ export class EventWatcher {
|
|||||||
async _handlePruningComplete (jobData: any): Promise<void> {
|
async _handlePruningComplete (jobData: any): Promise<void> {
|
||||||
const { pruneBlockHeight } = jobData;
|
const { pruneBlockHeight } = jobData;
|
||||||
log(`Job onComplete pruning at height ${pruneBlockHeight}`);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,6 +131,12 @@ const prefetchBlocks = async (
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
await Promise.all(fetchBlockPromises);
|
await Promise.all(fetchBlockPromises);
|
||||||
|
} catch (error: any) {
|
||||||
|
log(error.message);
|
||||||
|
log('Exiting gracefully');
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -90,6 +90,7 @@ export class JobRunner {
|
|||||||
// Should be at least 1.
|
// Should be at least 1.
|
||||||
assert(blocksAtHeight.length);
|
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.
|
// 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.
|
// This will lead to orphaned nodes, which will get pruned at the next height.
|
||||||
if (blocksAtHeight.length > 1) {
|
if (blocksAtHeight.length > 1) {
|
||||||
@ -97,6 +98,7 @@ export class JobRunner {
|
|||||||
|
|
||||||
// Get ancestor blockHash from indexed block at prune height.
|
// Get ancestor blockHash from indexed block at prune height.
|
||||||
const ancestorBlockHash = await this._indexer.getAncestorAtDepth(indexedBlock.blockHash, MAX_REORG_DEPTH);
|
const ancestorBlockHash = await this._indexer.getAncestorAtDepth(indexedBlock.blockHash, MAX_REORG_DEPTH);
|
||||||
|
newCanonicalBlockHash = ancestorBlockHash;
|
||||||
|
|
||||||
const blocksToBePruned = blocksAtHeight.filter(block => ancestorBlockHash !== block.blockHash);
|
const blocksToBePruned = blocksAtHeight.filter(block => ancestorBlockHash !== block.blockHash);
|
||||||
|
|
||||||
@ -104,7 +106,12 @@ export class JobRunner {
|
|||||||
// Mark blocks pruned which are not the ancestor block.
|
// Mark blocks pruned which are not the ancestor block.
|
||||||
await this._indexer.markBlocksAsPruned(blocksToBePruned);
|
await this._indexer.markBlocksAsPruned(blocksToBePruned);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
newCanonicalBlockHash = blocksAtHeight[0].blockHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the canonical block in the SyncStatus.
|
||||||
|
await this._indexer.updateSyncStatusCanonicalBlock(newCanonicalBlockHash, pruneBlockHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user