Generate mining basic pool ranking (sorted by block found) for a specified timeframe

This commit is contained in:
nymkappa
2022-01-06 19:59:33 +09:00
parent 4646cc6df3
commit 5ca98af7d1
16 changed files with 366 additions and 48 deletions

View File

@@ -115,6 +115,11 @@ class BitcoinApi implements AbstractBitcoinApi {
return outSpends;
}
$getEstimatedHashrate(blockHeight: number): Promise<number> {
// 120 is the default block span in Core
return this.bitcoindClient.getNetworkHashPs(120, blockHeight);
}
protected async $convertTransaction(transaction: IBitcoinApi.Transaction, addPrevout: boolean): Promise<IEsploraApi.Transaction> {
let esploraTransaction: IEsploraApi.Transaction = {
txid: transaction.txid,

View File

@@ -115,7 +115,7 @@ class Blocks {
*/
private async $findBlockMiner(txMinerInfo: TransactionMinerInfo | undefined) : Promise<PoolTag> {
if (txMinerInfo === undefined) {
return poolsRepository.getUnknownPool();
return await poolsRepository.$getUnknownPool();
}
const asciiScriptSig = transactionUtils.hex2ascii(txMinerInfo.vin[0].scriptsig);
@@ -139,7 +139,7 @@ class Blocks {
}
}
return poolsRepository.getUnknownPool();
return await poolsRepository.$getUnknownPool();
}
/**
@@ -147,7 +147,7 @@ class Blocks {
*/
public async $generateBlockDatabase() {
let currentBlockHeight = await bitcoinApi.$getBlockHeightTip();
let maxBlocks = 100; // tmp
let maxBlocks = 1008*2; // tmp
while (currentBlockHeight-- > 0 && maxBlocks-- > 0) {
if (await blocksRepository.$isBlockAlreadyIndexed(currentBlockHeight)) {

53
backend/src/api/mining.ts Normal file
View File

@@ -0,0 +1,53 @@
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 = {};
const lastBlockHashrate = await this.bitcoinApi.$getEstimatedHashrate(717960);
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
const blockCount: number = await BlocksRepository.$blockCount(interval);
const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$countEmptyBlocks(interval);
let poolsStats: PoolStats[] = [];
let rank = 1;
poolsInfo.forEach((poolInfo: PoolInfo) => {
let poolStat: PoolStats = {
poolId: poolInfo.poolId, // mysql row id
name: poolInfo.name,
link: poolInfo.link,
blockCount: poolInfo.blockCount,
rank: rank++,
emptyBlocks: 0,
}
for (let i = 0; i < emptyBlocks.length; ++i) {
if (emptyBlocks[i].poolId === poolInfo.poolId) {
poolStat.emptyBlocks++;
}
}
poolsStats.push(poolStat);
})
poolsStatistics["blockCount"] = blockCount;
poolsStatistics["poolsStats"] = poolsStats;
poolsStatistics["lastEstimatedHashrate"] = lastBlockHashrate;
return poolsStatistics;
}
}
export default new Mining();