Wrap block indexing into a try/catch since we don't use async when calling that function
This commit is contained in:
parent
f8f9108ae1
commit
a271c39ba8
@ -150,41 +150,45 @@ class Blocks {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let currentBlockHeight = await bitcoinClient.getBlockCount();
|
try {
|
||||||
const indexedBlockCount = await blocksRepository.$blockCount();
|
let currentBlockHeight = await bitcoinClient.getBlockCount();
|
||||||
|
const indexedBlockCount = await blocksRepository.$blockCount();
|
||||||
|
|
||||||
logger.info(`Starting block indexing. Current tip at block #${currentBlockHeight}`);
|
logger.info(`Starting block indexing. Current tip at block #${currentBlockHeight}`);
|
||||||
logger.info(`Need to index ${currentBlockHeight - indexedBlockCount} blocks. Working on it!`);
|
logger.info(`Need to index ${currentBlockHeight - indexedBlockCount} blocks. Working on it!`);
|
||||||
|
|
||||||
const chunkSize = 10000;
|
const chunkSize = 10000;
|
||||||
while (currentBlockHeight >= 0) {
|
while (currentBlockHeight >= 0) {
|
||||||
const endBlock = Math.max(0, currentBlockHeight - chunkSize + 1);
|
const endBlock = Math.max(0, currentBlockHeight - chunkSize + 1);
|
||||||
const missingBlockHeights: number[] = await blocksRepository.$getMissingBlocksBetweenHeights(
|
const missingBlockHeights: number[] = await blocksRepository.$getMissingBlocksBetweenHeights(
|
||||||
currentBlockHeight, endBlock);
|
currentBlockHeight, endBlock);
|
||||||
if (missingBlockHeights.length <= 0) {
|
if (missingBlockHeights.length <= 0) {
|
||||||
logger.debug(`No missing blocks between #${currentBlockHeight} to #${endBlock}, moving on`);
|
logger.debug(`No missing blocks between #${currentBlockHeight} to #${endBlock}, moving on`);
|
||||||
currentBlockHeight -= chunkSize;
|
currentBlockHeight -= chunkSize;
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(`Indexing ${chunkSize} blocks from #${currentBlockHeight} to #${endBlock}`);
|
|
||||||
|
|
||||||
for (const blockHeight of missingBlockHeights) {
|
|
||||||
try {
|
|
||||||
logger.debug(`Indexing block #${blockHeight}`);
|
|
||||||
const blockHash = await bitcoinApi.$getBlockHash(blockHeight);
|
|
||||||
const block = await bitcoinApi.$getBlock(blockHash);
|
|
||||||
const transactions = await this.$getTransactionsExtended(blockHash, block.height, true);
|
|
||||||
const blockExtended = this.getBlockExtended(block, transactions);
|
|
||||||
const miner = await this.$findBlockMiner(blockExtended.coinbaseTx);
|
|
||||||
const coinbase: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(transactions[0].txid, true);
|
|
||||||
await blocksRepository.$saveBlockInDatabase(blockExtended, blockHash, coinbase.hex, miner);
|
|
||||||
} catch (e) {
|
|
||||||
logger.err(`Something went wrong while indexing blocks.` + e);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
currentBlockHeight -= chunkSize;
|
logger.info(`Indexing ${chunkSize} blocks from #${currentBlockHeight} to #${endBlock}`);
|
||||||
|
|
||||||
|
for (const blockHeight of missingBlockHeights) {
|
||||||
|
try {
|
||||||
|
logger.debug(`Indexing block #${blockHeight}`);
|
||||||
|
const blockHash = await bitcoinApi.$getBlockHash(blockHeight);
|
||||||
|
const block = await bitcoinApi.$getBlock(blockHash);
|
||||||
|
const transactions = await this.$getTransactionsExtended(blockHash, block.height, true);
|
||||||
|
const blockExtended = this.getBlockExtended(block, transactions);
|
||||||
|
const miner = await this.$findBlockMiner(blockExtended.coinbaseTx);
|
||||||
|
const coinbase: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(transactions[0].txid, true);
|
||||||
|
await blocksRepository.$saveBlockInDatabase(blockExtended, blockHash, coinbase.hex, miner);
|
||||||
|
} catch (e) {
|
||||||
|
logger.err(`Something went wrong while indexing blocks.` + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
currentBlockHeight -= chunkSize;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logger.err('An error occured in $generateBlockDatabase(). Skipping block indexing. ' + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user