Create difficulty chart component

This commit is contained in:
nymkappa
2022-02-16 21:20:28 +09:00
parent e2e3546934
commit 7270b1ccac
11 changed files with 192 additions and 16 deletions

View File

@@ -199,6 +199,7 @@ class Blocks {
const chunkSize = 10000;
let totaIndexed = 0;
let indexedThisRun = 0;
while (currentBlockHeight >= lastBlockToIndex) {
const endBlock = Math.max(0, lastBlockToIndex, currentBlockHeight - chunkSize + 1);
@@ -207,9 +208,11 @@ class Blocks {
if (missingBlockHeights.length <= 0) {
logger.debug(`No missing blocks between #${currentBlockHeight} to #${endBlock}`);
currentBlockHeight -= chunkSize;
totaIndexed += chunkSize;
continue;
}
totaIndexed += chunkSize - missingBlockHeights.length;
logger.debug(`Indexing ${missingBlockHeights.length} blocks from #${currentBlockHeight} to #${endBlock}`);
for (const blockHeight of missingBlockHeights) {
@@ -219,8 +222,10 @@ class Blocks {
try {
if (totaIndexed % 100 === 0 || blockHeight === lastBlockToIndex) {
const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt));
const blockPerSeconds = Math.round(totaIndexed / elapsedSeconds);
logger.debug(`Indexing block #${blockHeight} | ~${blockPerSeconds} blocks/sec | total: ${totaIndexed} | elapsed: ${elapsedSeconds} seconds`);
const blockPerSeconds = Math.max(1, Math.round(indexedThisRun / elapsedSeconds));
const progress = Math.round(totaIndexed / indexingBlockAmount * 100);
const timeLeft = Math.round((indexingBlockAmount - totaIndexed) / blockPerSeconds);
logger.debug(`Indexing block #${blockHeight} | ~${blockPerSeconds} blocks/sec | total: ${totaIndexed}/${indexingBlockAmount} (${progress}%) | elapsed: ${elapsedSeconds} seconds | left: ~${timeLeft} seconds`);
}
const blockHash = await bitcoinApi.$getBlockHash(blockHeight);
const block = await bitcoinApi.$getBlock(blockHash);
@@ -228,6 +233,7 @@ class Blocks {
const blockExtended = await this.$getBlockExtended(block, transactions);
await blocksRepository.$saveBlockInDatabase(blockExtended);
++totaIndexed;
++indexedThisRun;
} catch (e) {
logger.err(`Something went wrong while indexing blocks.` + e);
}