Merge pull request #4068 from mempool/mononaut/electrs-blocks

Get blocks from electrs again
This commit is contained in:
wiz 2023-08-01 15:33:13 +09:00 committed by GitHub
commit 97877053bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -423,6 +423,7 @@ class Mining {
const blocks: any = await BlocksRepository.$getBlocksDifficulty(); const blocks: any = await BlocksRepository.$getBlocksDifficulty();
const genesisBlock: IEsploraApi.Block = await bitcoinApi.$getBlock(await bitcoinApi.$getBlockHash(0)); const genesisBlock: IEsploraApi.Block = await bitcoinApi.$getBlock(await bitcoinApi.$getBlockHash(0));
let currentDifficulty = genesisBlock.difficulty; let currentDifficulty = genesisBlock.difficulty;
let currentBits = genesisBlock.bits;
let totalIndexed = 0; let totalIndexed = 0;
if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT === -1 && indexedHeights[0] !== true) { if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT === -1 && indexedHeights[0] !== true) {
@ -436,17 +437,18 @@ class Mining {
const oldestConsecutiveBlock = await BlocksRepository.$getOldestConsecutiveBlock(); const oldestConsecutiveBlock = await BlocksRepository.$getOldestConsecutiveBlock();
if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT !== -1) { if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT !== -1) {
currentDifficulty = oldestConsecutiveBlock.difficulty; currentBits = oldestConsecutiveBlock.bits;
} }
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.bits !== currentBits) {
if (indexedHeights[block.height] === true) { // Already indexed if (indexedHeights[block.height] === true) { // Already indexed
if (block.height >= oldestConsecutiveBlock.height) { if (block.height >= oldestConsecutiveBlock.height) {
currentDifficulty = block.difficulty; currentDifficulty = block.difficulty;
currentBits = block.bits;
} }
continue; continue;
} }
@ -464,6 +466,7 @@ class Mining {
totalIndexed++; totalIndexed++;
if (block.height >= oldestConsecutiveBlock.height) { if (block.height >= oldestConsecutiveBlock.height) {
currentDifficulty = block.difficulty; currentDifficulty = block.difficulty;
currentBits = block.bits;
} }
} }

View File

@ -541,7 +541,7 @@ class BlocksRepository {
*/ */
public async $getBlocksDifficulty(): Promise<object[]> { public async $getBlocksDifficulty(): Promise<object[]> {
try { try {
const [rows]: any[] = await DB.query(`SELECT UNIX_TIMESTAMP(blockTimestamp) as time, height, difficulty FROM blocks`); const [rows]: any[] = await DB.query(`SELECT UNIX_TIMESTAMP(blockTimestamp) as time, height, difficulty, bits FROM blocks`);
return rows; return rows;
} catch (e) { } catch (e) {
logger.err('Cannot get blocks difficulty list from the db. Reason: ' + (e instanceof Error ? e.message : e)); logger.err('Cannot get blocks difficulty list from the db. Reason: ' + (e instanceof Error ? e.message : e));
@ -850,7 +850,7 @@ class BlocksRepository {
*/ */
public async $getOldestConsecutiveBlock(): Promise<any> { public async $getOldestConsecutiveBlock(): Promise<any> {
try { try {
const [rows]: any = await DB.query(`SELECT height, UNIX_TIMESTAMP(blockTimestamp) as timestamp, difficulty FROM blocks ORDER BY height DESC`); const [rows]: any = await DB.query(`SELECT height, UNIX_TIMESTAMP(blockTimestamp) as timestamp, difficulty, bits 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]; return rows[i];