From 8d65f91ccc48a3607f13701e30688cb3e41bd4d9 Mon Sep 17 00:00:00 2001 From: kenshin-samourai Date: Tue, 17 Sep 2019 15:36:40 +0200 Subject: [PATCH] add new method BlockchainProcessor.rescanBlocks() allowing to rescan a range of blocks --- tracker/blockchain-processor.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tracker/blockchain-processor.js b/tracker/blockchain-processor.js index a26a0cd..27caccd 100644 --- a/tracker/blockchain-processor.js +++ b/tracker/blockchain-processor.js @@ -320,6 +320,39 @@ class BlockchainProcessor extends AbstractProcessor { await db.deleteBlocksAfterHeight(height) } + /** + * Rescan a range of blocks + * @param {integer} fromHeight - height of first block + * @param {integer} toHeight - height of last block + * @returns {Promise} + */ + async rescanBlocks(fromHeight, toHeight) { + // Get highest block processed by the tracker + const highest = await db.getHighestBlock() + const dbMaxHeight = highest.blockHeight + + if (toHeight == null) + toHeight = fromHeight + + toHeight = Math.min(toHeight, dbMaxHeight) + const blockRange = _.range(fromHeight, toHeight + 1) + + Logger.info(`Blocks Rescan : starting a rescan for ${blockRange.length} blocks`) + + // Process the blocks + return util.seriesCall(blockRange, async height => { + try { + Logger.info(`Rescanning block ${height}`) + const hash = await this.client.getblockhash(height) + const header = await this.client.getblockheader(hash) + return this.processBlock(header) + } catch(e) { + Logger.error(e, 'BlockchainProcessor.rescan()') + throw e + } + }, 'Tracker rescan', true) + } + /** * Process a block * @param {object} header - block header