Fix first diff adjustmeent if INDEXING_BLOCK_AMOUNT is not -1

This commit is contained in:
nymkappa 2022-07-09 13:14:09 +02:00 committed by softsimon
parent f4667c0892
commit cdf0fe0335
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
2 changed files with 25 additions and 17 deletions

View File

@ -184,7 +184,7 @@ class Mining {
} }
try { try {
const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlockTimestamp()); const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlock()).timestamp;
const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0)); const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0));
const genesisTimestamp = genesisBlock.time * 1000; const genesisTimestamp = genesisBlock.time * 1000;
@ -285,7 +285,7 @@ class Mining {
return; return;
} }
const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlockTimestamp()); const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlock()).timestamp;
try { try {
const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0)); const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0));
@ -389,31 +389,37 @@ class Mining {
} }
const blocks: any = await BlocksRepository.$getBlocksDifficulty(); const blocks: any = await BlocksRepository.$getBlocksDifficulty();
const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0));
let currentDifficulty = 0; let currentDifficulty = genesisBlock.difficulty;
let totalIndexed = 0; let totalIndexed = 0;
if (indexedHeights[0] !== true) { if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT === -1 && indexedHeights[0] !== true) {
const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0));
await DifficultyAdjustmentsRepository.$saveAdjustments({ await DifficultyAdjustmentsRepository.$saveAdjustments({
time: genesisBlock.time, time: genesisBlock.time,
height: 0, height: 0,
difficulty: genesisBlock.difficulty, difficulty: currentDifficulty,
adjustment: 0.0, adjustment: 0.0,
}); });
} }
const oldestConsecutiveBlock = await BlocksRepository.$getOldestConsecutiveBlock();
if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT !== -1) {
currentDifficulty = oldestConsecutiveBlock.difficulty;
}
let totalBlockChecked = 0; let totalBlockChecked = 0;
let timer = new Date().getTime() / 1000; let timer = new Date().getTime() / 1000;
for (const block of blocks) { for (const block of blocks) {
if (block.difficulty !== currentDifficulty) { if (block.difficulty !== currentDifficulty) {
if (block.height === 0 || indexedHeights[block.height] === true) { // Already indexed if (indexedHeights[block.height] === true) { // Already indexed
currentDifficulty = block.difficulty; if (block.height >= oldestConsecutiveBlock.height) {
currentDifficulty = block.difficulty;
}
continue; continue;
} }
let adjustment = block.difficulty / Math.max(1, currentDifficulty); let adjustment = block.difficulty / currentDifficulty;
adjustment = Math.round(adjustment * 1000000) / 1000000; // Remove float point noise adjustment = Math.round(adjustment * 1000000) / 1000000; // Remove float point noise
await DifficultyAdjustmentsRepository.$saveAdjustments({ await DifficultyAdjustmentsRepository.$saveAdjustments({
@ -424,8 +430,10 @@ class Mining {
}); });
totalIndexed++; totalIndexed++;
currentDifficulty = block.difficulty; if (block.height >= oldestConsecutiveBlock.height) {
} currentDifficulty = block.difficulty;
}
}
totalBlockChecked++; totalBlockChecked++;
const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - timer)); const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - timer));

View File

@ -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<number> { public async $getOldestConsecutiveBlock(): Promise<any> {
try { 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) { for (let i = 0; i < rows.length - 1; ++i) {
if (rows[i].height - rows[i + 1].height > 1) { 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) { } catch (e) {
logger.err('Cannot generate block size and weight history. Reason: ' + (e instanceof Error ? e.message : e)); logger.err('Cannot generate block size and weight history. Reason: ' + (e instanceof Error ? e.message : e));
throw e; throw e;