Merge pull request #2051 from mempool/nymkappa/bugfix/diff-adj-table-raw-db-data

Fix diff adj table using raw db value
This commit is contained in:
wiz 2022-07-10 13:14:56 +02:00 committed by GitHub
commit 59d10fd3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 20 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;

View File

@ -46,9 +46,38 @@ class DifficultyAdjustmentsRepository {
query += ` GROUP BY UNIX_TIMESTAMP(time) DIV ${86400}`; query += ` GROUP BY UNIX_TIMESTAMP(time) DIV ${86400}`;
if (descOrder === true) { if (descOrder === true) {
query += ` ORDER BY time DESC`; query += ` ORDER BY height DESC`;
} else { } 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<IndexedDifficultyAdjustment[]> {
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 { try {

View File

@ -734,7 +734,7 @@ class Routes {
public async $getDifficultyAdjustments(req: Request, res: Response) { public async $getDifficultyAdjustments(req: Request, res: Response) {
try { 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('Pragma', 'public');
res.header('Cache-control', 'public'); res.header('Cache-control', 'public');
res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString()); res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString());