Added MEMPOOL.INDEXING_BLOCKS_AMOUNT in the config (default 432 blocks)
This commit is contained in:
@@ -146,31 +146,35 @@ class Blocks {
|
||||
* Index all blocks metadata for the mining dashboard
|
||||
*/
|
||||
public async $generateBlockDatabase() {
|
||||
if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false) {
|
||||
if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false ||
|
||||
config.MEMPOOL.INDEXING_BLOCKS_AMOUNT <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let currentBlockHeight = await bitcoinClient.getBlockCount();
|
||||
const indexedBlockCount = await blocksRepository.$blockCount();
|
||||
const lastBlockToIndex = currentBlockHeight - config.MEMPOOL.INDEXING_BLOCKS_AMOUNT + 1;
|
||||
|
||||
logger.info(`Starting block indexing. Current tip at block #${currentBlockHeight}`);
|
||||
logger.info(`Need to index ${currentBlockHeight - indexedBlockCount} blocks. Working on it!`);
|
||||
logger.info(`Indexing blocks from #${currentBlockHeight} to #${lastBlockToIndex}`);
|
||||
|
||||
const chunkSize = 10000;
|
||||
while (currentBlockHeight >= 0) {
|
||||
const endBlock = Math.max(0, currentBlockHeight - chunkSize + 1);
|
||||
while (currentBlockHeight >= lastBlockToIndex) {
|
||||
const endBlock = Math.max(0, lastBlockToIndex, currentBlockHeight - chunkSize + 1);
|
||||
|
||||
const missingBlockHeights: number[] = await blocksRepository.$getMissingBlocksBetweenHeights(
|
||||
currentBlockHeight, endBlock);
|
||||
if (missingBlockHeights.length <= 0) {
|
||||
logger.debug(`No missing blocks between #${currentBlockHeight} to #${endBlock}, moving on`);
|
||||
logger.debug(`No missing blocks between #${currentBlockHeight} to #${endBlock}`);
|
||||
currentBlockHeight -= chunkSize;
|
||||
continue;
|
||||
}
|
||||
|
||||
logger.info(`Indexing ${chunkSize} blocks from #${currentBlockHeight} to #${endBlock}`);
|
||||
logger.debug(`Indexing ${missingBlockHeights.length} blocks from #${currentBlockHeight} to #${endBlock}`);
|
||||
|
||||
for (const blockHeight of missingBlockHeights) {
|
||||
if (blockHeight < lastBlockToIndex) {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
logger.debug(`Indexing block #${blockHeight}`);
|
||||
const blockHash = await bitcoinApi.$getBlockHash(blockHeight);
|
||||
@@ -187,8 +191,10 @@ class Blocks {
|
||||
|
||||
currentBlockHeight -= chunkSize;
|
||||
}
|
||||
logger.info('Block indexing completed');
|
||||
} catch (e) {
|
||||
logger.err('An error occured in $generateBlockDatabase(). Skipping block indexing. ' + e);
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ interface IConfig {
|
||||
BLOCK_WEIGHT_UNITS: number;
|
||||
INITIAL_BLOCKS_AMOUNT: number;
|
||||
MEMPOOL_BLOCKS_AMOUNT: number;
|
||||
INDEXING_BLOCKS_AMOUNT: number;
|
||||
PRICE_FEED_UPDATE_INTERVAL: number;
|
||||
USE_SECOND_NODE_FOR_MINFEE: boolean;
|
||||
EXTERNAL_ASSETS: string[];
|
||||
@@ -77,6 +78,7 @@ const defaults: IConfig = {
|
||||
'BLOCK_WEIGHT_UNITS': 4000000,
|
||||
'INITIAL_BLOCKS_AMOUNT': 8,
|
||||
'MEMPOOL_BLOCKS_AMOUNT': 8,
|
||||
'INDEXING_BLOCKS_AMOUNT': 432, // ~3 days at 10 minutes / block. Set to 0 to disable indexing
|
||||
'PRICE_FEED_UPDATE_INTERVAL': 3600,
|
||||
'USE_SECOND_NODE_FOR_MINFEE': false,
|
||||
'EXTERNAL_ASSETS': [
|
||||
|
||||
@@ -48,6 +48,10 @@ class BlocksRepository {
|
||||
* Get all block height that have not been indexed between [startHeight, endHeight]
|
||||
*/
|
||||
public async $getMissingBlocksBetweenHeights(startHeight: number, endHeight: number): Promise<number[]> {
|
||||
if (startHeight < endHeight) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const connection = await DB.pool.getConnection();
|
||||
const [rows] : any[] = await connection.query(`
|
||||
SELECT height
|
||||
|
||||
Reference in New Issue
Block a user