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;
}
}

View File

@@ -6,7 +6,7 @@ import logger from '../logger';
const sleep = (ms: number) => new Promise(res => setTimeout(res, ms));
class DatabaseMigration {
private static currentVersion = 3;
private static currentVersion = 4;
private queryTimeout = 120000;
private statisticsAddedIndexed = false;
@@ -85,6 +85,8 @@ class DatabaseMigration {
}
if (databaseSchemaVersion < 3) {
await this.$executeQuery(connection, this.getCreatePoolsTableQuery(), await this.$checkIfTableExists('pools'));
}
if (databaseSchemaVersion < 4) {
await this.$executeQuery(connection, this.getCreateBlocksTableQuery(), await this.$checkIfTableExists('blocks'));
}
connection.release();

View File

@@ -1,33 +1,30 @@
import { PoolInfo, PoolStats } from "../mempool.interfaces";
import BlocksRepository, { EmptyBlocks } from "../repositories/BlocksRepository";
import PoolsRepository from "../repositories/PoolsRepository";
import bitcoinClient from "./bitcoin/bitcoin-client";
import BitcoinApi from "./bitcoin/bitcoin-api";
import { PoolInfo, PoolStats } from '../mempool.interfaces';
import BlocksRepository, { EmptyBlocks } from '../repositories/BlocksRepository';
import PoolsRepository from '../repositories/PoolsRepository';
import bitcoinClient from './bitcoin/bitcoin-client';
import BitcoinApi from './bitcoin/bitcoin-api';
class Mining {
private bitcoinApi: BitcoinApi;
constructor() {
this.bitcoinApi = new BitcoinApi(bitcoinClient);
}
/**
* Generate high level overview of the pool ranks and general stats
*/
public async $getPoolsStats(interval: string = "100 YEAR") : Promise<object> {
let poolsStatistics = {};
public async $getPoolsStats(interval: string = '100 YEAR') : Promise<object> {
const poolsStatistics = {};
const blockHeightTip = await this.bitcoinApi.$getBlockHeightTip();
const lastBlockHashrate = await this.bitcoinApi.$getEstimatedHashrate(blockHeightTip);
const blockHeightTip = await bitcoinClient.getBlockCount();
const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(120, blockHeightTip);
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
const blockCount: number = await BlocksRepository.$blockCount(interval);
const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$countEmptyBlocks(interval);
let poolsStats: PoolStats[] = [];
const poolsStats: PoolStats[] = [];
let rank = 1;
poolsInfo.forEach((poolInfo: PoolInfo) => {
let poolStat: PoolStats = {
const poolStat: PoolStats = {
poolId: poolInfo.poolId, // mysql row id
name: poolInfo.name,
link: poolInfo.link,
@@ -41,11 +38,11 @@ class Mining {
}
}
poolsStats.push(poolStat);
})
});
poolsStatistics["blockCount"] = blockCount;
poolsStatistics["lastEstimatedHashrate"] = lastBlockHashrate;
poolsStatistics["pools"] = poolsStats;
poolsStatistics['blockCount'] = blockCount;
poolsStatistics['lastEstimatedHashrate'] = lastBlockHashrate;
poolsStatistics['pools'] = poolsStats;
return poolsStatistics;
}