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; diff --git a/backend/src/repositories/DifficultyAdjustmentsRepository.ts b/backend/src/repositories/DifficultyAdjustmentsRepository.ts index 6952b3be9..910c65c10 100644 --- a/backend/src/repositories/DifficultyAdjustmentsRepository.ts +++ b/backend/src/repositories/DifficultyAdjustmentsRepository.ts @@ -46,9 +46,38 @@ class DifficultyAdjustmentsRepository { query += ` GROUP BY UNIX_TIMESTAMP(time) DIV ${86400}`; if (descOrder === true) { - query += ` ORDER BY time DESC`; + query += ` ORDER BY height DESC`; } else { - query += ` ORDER BY time`; + query += ` ORDER BY height`; + } + + try { + const [rows] = await DB.query(query); + return rows as IndexedDifficultyAdjustment[]; + } catch (e) { + logger.err(`Cannot get difficulty adjustments from the database. Reason: ` + (e instanceof Error ? e.message : e)); + throw e; + } + } + + public async $getRawAdjustments(interval: string | null, descOrder: boolean = false): Promise { + interval = Common.getSqlInterval(interval); + + let query = `SELECT + UNIX_TIMESTAMP(time) as time, + height as height, + difficulty as difficulty, + adjustment as adjustment + FROM difficulty_adjustments`; + + if (interval) { + query += ` WHERE time BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`; + } + + if (descOrder === true) { + query += ` ORDER BY height DESC`; + } else { + query += ` ORDER BY height`; } try { diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 57c64ddf5..90a4536c2 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -734,7 +734,7 @@ class Routes { public async $getDifficultyAdjustments(req: Request, res: Response) { try { - const difficulty = await DifficultyAdjustmentsRepository.$getAdjustments(req.params.interval, true); + const difficulty = await DifficultyAdjustmentsRepository.$getRawAdjustments(req.params.interval, true); res.header('Pragma', 'public'); res.header('Cache-control', 'public'); res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString());