From cdf0fe033576d3880a4d34525688cc1206a9cdab Mon Sep 17 00:00:00 2001 From: nymkappa Date: Sat, 9 Jul 2022 13:14:09 +0200 Subject: [PATCH] Fix first diff adjustmeent if INDEXING_BLOCK_AMOUNT is not -1 --- backend/src/api/mining.ts | 32 ++++++++++++-------- backend/src/repositories/BlocksRepository.ts | 10 +++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index 207625be0..50a86e73e 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -184,7 +184,7 @@ class Mining { } try { - const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlockTimestamp()); + const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlock()).timestamp; const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0)); const genesisTimestamp = genesisBlock.time * 1000; @@ -285,7 +285,7 @@ class Mining { return; } - const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlockTimestamp()); + const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlock()).timestamp; try { const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0)); @@ -389,31 +389,37 @@ class Mining { } const blocks: any = await BlocksRepository.$getBlocksDifficulty(); - - let currentDifficulty = 0; + const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0)); + let currentDifficulty = genesisBlock.difficulty; let totalIndexed = 0; - if (indexedHeights[0] !== true) { - const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0)); + if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT === -1 && indexedHeights[0] !== true) { await DifficultyAdjustmentsRepository.$saveAdjustments({ time: genesisBlock.time, height: 0, - difficulty: genesisBlock.difficulty, + difficulty: currentDifficulty, adjustment: 0.0, }); } + const oldestConsecutiveBlock = await BlocksRepository.$getOldestConsecutiveBlock(); + if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT !== -1) { + currentDifficulty = oldestConsecutiveBlock.difficulty; + } + let totalBlockChecked = 0; let timer = new Date().getTime() / 1000; for (const block of blocks) { if (block.difficulty !== currentDifficulty) { - if (block.height === 0 || indexedHeights[block.height] === true) { // Already indexed - currentDifficulty = block.difficulty; + if (indexedHeights[block.height] === true) { // Already indexed + if (block.height >= oldestConsecutiveBlock.height) { + currentDifficulty = block.difficulty; + } continue; } - let adjustment = block.difficulty / Math.max(1, currentDifficulty); + let adjustment = block.difficulty / currentDifficulty; adjustment = Math.round(adjustment * 1000000) / 1000000; // Remove float point noise await DifficultyAdjustmentsRepository.$saveAdjustments({ @@ -424,8 +430,10 @@ class Mining { }); totalIndexed++; - currentDifficulty = block.difficulty; - } + if (block.height >= oldestConsecutiveBlock.height) { + currentDifficulty = block.difficulty; + } + } totalBlockChecked++; const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - timer)); diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index c3f824d61..4388c46f6 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -612,17 +612,17 @@ class BlocksRepository { } /** - * Return the oldest block timestamp from a consecutive chain of block from the most recent one + * Return the oldest block from a consecutive chain of block from the most recent one */ - public async $getOldestConsecutiveBlockTimestamp(): Promise { + public async $getOldestConsecutiveBlock(): Promise { try { - const [rows]: any = await DB.query(`SELECT height, UNIX_TIMESTAMP(blockTimestamp) as timestamp FROM blocks ORDER BY height DESC`); + const [rows]: any = await DB.query(`SELECT height, UNIX_TIMESTAMP(blockTimestamp) as timestamp, difficulty FROM blocks ORDER BY height DESC`); for (let i = 0; i < rows.length - 1; ++i) { if (rows[i].height - rows[i + 1].height > 1) { - return rows[i].timestamp; + return rows[i]; } } - return rows[rows.length - 1].timestamp; + return rows[rows.length - 1]; } catch (e) { logger.err('Cannot generate block size and weight history. Reason: ' + (e instanceof Error ? e.message : e)); throw e;