Basic block indexing WIP - Default mining pool icon - Only show mining hashrate on 1d scale

This commit is contained in:
nymkappa
2022-01-18 17:37:04 +09:00
parent b567c0ad43
commit 16ab9b38a0
12 changed files with 230 additions and 72 deletions

View File

@@ -7,10 +7,10 @@ import { Common } from './common';
import diskCache from './disk-cache';
import transactionUtils from './transaction-utils';
import bitcoinClient from './bitcoin/bitcoin-client';
import { DB } from '../database';
import { IEsploraApi } from './bitcoin/esplora-api.interface';
import poolsRepository from '../repositories/PoolsRepository';
import blocksRepository from '../repositories/BlocksRepository';
import BitcoinApi from './bitcoin/bitcoin-api';
class Blocks {
private blocks: BlockExtended[] = [];
@@ -146,22 +146,41 @@ class Blocks {
* Index all blocks metadata for the mining dashboard
*/
public async $generateBlockDatabase() {
let currentBlockHeight = await bitcoinApi.$getBlockHeightTip();
let maxBlocks = 1008*2; // tmp
let currentBlockHeight = await bitcoinClient.getBlockCount();
const indexedBlockCount = await blocksRepository.$blockCount();
while (currentBlockHeight-- > 0 && maxBlocks-- > 0) {
if (await blocksRepository.$isBlockAlreadyIndexed(currentBlockHeight)) {
// logger.debug(`Block #${currentBlockHeight} already indexed, skipping`);
logger.info(`Starting block indexing. Current tip at block #${currentBlockHeight}`);
logger.info(`Need to index ${currentBlockHeight - indexedBlockCount} blocks. Working on it!`);
const chunkSize = 10000;
while (currentBlockHeight >= 0) {
const endBlock = Math.max(0, 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`);
currentBlockHeight -= chunkSize;
continue;
}
logger.debug(`Indexing block #${currentBlockHeight}`);
const blockHash = await bitcoinApi.$getBlockHash(currentBlockHeight);
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);
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;
}
}