Wrap orphaned blocks updater into try/catch

This commit is contained in:
nymkappa 2023-02-18 14:53:21 +09:00
parent 281899f551
commit e2fe39f241
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04

View File

@ -19,24 +19,28 @@ class ChainTips {
private orphanedBlocks: OrphanedBlock[] = []; private orphanedBlocks: OrphanedBlock[] = [];
public async updateOrphanedBlocks(): Promise<void> { public async updateOrphanedBlocks(): Promise<void> {
this.chainTips = await bitcoinClient.getChainTips(); try {
this.orphanedBlocks = []; this.chainTips = await bitcoinClient.getChainTips();
this.orphanedBlocks = [];
for (const chain of this.chainTips) { for (const chain of this.chainTips) {
if (chain.status === 'valid-fork' || chain.status === 'valid-headers' || chain.status === 'headers-only') { if (chain.status === 'valid-fork' || chain.status === 'valid-headers' || chain.status === 'headers-only') {
let block = await bitcoinClient.getBlock(chain.hash); let block = await bitcoinClient.getBlock(chain.hash);
while (block && block.confirmations === -1) { while (block && block.confirmations === -1) {
this.orphanedBlocks.push({ this.orphanedBlocks.push({
height: block.height, height: block.height,
hash: block.hash, hash: block.hash,
status: chain.status status: chain.status
}); });
block = await bitcoinClient.getBlock(block.previousblockhash); block = await bitcoinClient.getBlock(block.previousblockhash);
}
} }
} }
}
logger.debug(`Updated orphaned blocks cache. Found ${this.orphanedBlocks.length} orphaned blocks`); logger.debug(`Updated orphaned blocks cache. Found ${this.orphanedBlocks.length} orphaned blocks`);
} catch (e) {
logger.err(`Cannot get fetch orphaned blocks. Reason: ${e instanceof Error ? e.message : e}`);
}
} }
public getOrphanedBlocksAtHeight(height: number): OrphanedBlock[] { public getOrphanedBlocksAtHeight(height: number): OrphanedBlock[] {