From 97ff1e37aaa58d350007e5ddfc925b436b614cdc Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 8 Jul 2022 16:34:00 +0200 Subject: [PATCH] Use bitcoin core instead of esplore for fetch blocks on bitcoin networks --- backend/src/api/blocks.ts | 80 +++++++++++++++---------------- backend/src/utils/blocks-utils.ts | 8 ++-- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 631940dd2..b259f701d 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -168,7 +168,7 @@ class Blocks { blockExtended.extras.avgFeeRate = stats.avgfeerate; } - if (['mainnet', 'testnet', 'signet', 'regtest'].includes(config.MEMPOOL.NETWORK)) { + if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK)) { let pool: PoolTag; if (blockExtended.extras?.coinbaseTx !== undefined) { pool = await this.$findBlockMiner(blockExtended.extras?.coinbaseTx); @@ -405,7 +405,7 @@ class Blocks { if (blockHeightTip >= 2016) { const previousPeriodBlockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff - 2016); - const previousPeriodBlock = await bitcoinApi.$getBlock(previousPeriodBlockHash); + const previousPeriodBlock = await bitcoinClient.getBlock(previousPeriodBlockHash) this.previousDifficultyRetarget = (block.difficulty - previousPeriodBlock.difficulty) / previousPeriodBlock.difficulty * 100; logger.debug(`Initial difficulty adjustment data set.`); } @@ -527,13 +527,15 @@ class Blocks { } } - const block = await bitcoinApi.$getBlock(hash); + let block = await bitcoinClient.getBlock(hash); // Not Bitcoin network, return the block as it if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false) { return block; } + block = prepareBlock(block); + // Bitcoin network, add our custom data on top const transactions = await this.$getTransactionsExtended(hash, block.height, true); const blockExtended = await this.$getBlockExtended(block, transactions); @@ -577,47 +579,43 @@ class Blocks { } public async $getBlocks(fromHeight?: number, limit: number = 15): Promise { - try { - let currentHeight = fromHeight !== undefined ? fromHeight : this.getCurrentBlockHeight(); - const returnBlocks: BlockExtended[] = []; - - if (currentHeight < 0) { - return returnBlocks; - } - - if (currentHeight === 0 && Common.indexingEnabled()) { - currentHeight = await blocksRepository.$mostRecentBlockHeight(); - } - - // Check if block height exist in local cache to skip the hash lookup - const blockByHeight = this.getBlocks().find((b) => b.height === currentHeight); - let startFromHash: string | null = null; - if (blockByHeight) { - startFromHash = blockByHeight.id; - } else if (!Common.indexingEnabled()) { - startFromHash = await bitcoinApi.$getBlockHash(currentHeight); - } - - let nextHash = startFromHash; - for (let i = 0; i < limit && currentHeight >= 0; i++) { - let block = this.getBlocks().find((b) => b.height === currentHeight); - if (block) { - returnBlocks.push(block); - } else if (Common.indexingEnabled()) { - block = await this.$indexBlock(currentHeight); - returnBlocks.push(block); - } else if (nextHash != null) { - block = prepareBlock(await bitcoinApi.$getBlock(nextHash)); - nextHash = block.previousblockhash; - returnBlocks.push(block); - } - currentHeight--; - } + let currentHeight = fromHeight !== undefined ? fromHeight : this.getCurrentBlockHeight(); + const returnBlocks: BlockExtended[] = []; + if (currentHeight < 0) { return returnBlocks; - } catch (e) { - throw e; } + + if (currentHeight === 0 && Common.indexingEnabled()) { + currentHeight = await blocksRepository.$mostRecentBlockHeight(); + } + + // Check if block height exist in local cache to skip the hash lookup + const blockByHeight = this.getBlocks().find((b) => b.height === currentHeight); + let startFromHash: string | null = null; + if (blockByHeight) { + startFromHash = blockByHeight.id; + } else if (!Common.indexingEnabled()) { + startFromHash = await bitcoinApi.$getBlockHash(currentHeight); + } + + let nextHash = startFromHash; + for (let i = 0; i < limit && currentHeight >= 0; i++) { + let block = this.getBlocks().find((b) => b.height === currentHeight); + if (block) { + returnBlocks.push(block); + } else if (Common.indexingEnabled()) { + block = await this.$indexBlock(currentHeight); + returnBlocks.push(block); + } else if (nextHash != null) { + block = prepareBlock(await bitcoinClient.getBlock(nextHash)); + nextHash = block.previousblockhash; + returnBlocks.push(block); + } + currentHeight--; + } + + return returnBlocks; } public getLastDifficultyAdjustmentTime(): number { diff --git a/backend/src/utils/blocks-utils.ts b/backend/src/utils/blocks-utils.ts index 937a37448..0f282bdeb 100644 --- a/backend/src/utils/blocks-utils.ts +++ b/backend/src/utils/blocks-utils.ts @@ -3,14 +3,14 @@ import { BlockExtended } from '../mempool.interfaces'; export function prepareBlock(block: any): BlockExtended { return { id: block.id ?? block.hash, // hash for indexed block - timestamp: block.timestamp ?? block.blockTimestamp, // blockTimestamp for indexed block + timestamp: block.timestamp ?? block.time ?? block.blockTimestamp, // blockTimestamp for indexed block height: block.height, version: block.version, - bits: block.bits, + bits: (typeof block.bits === 'string' ? parseInt(block.bits, 16): block.bits), nonce: block.nonce, difficulty: block.difficulty, - merkle_root: block.merkle_root, - tx_count: block.tx_count, + merkle_root: block.merkle_root ?? block.merkleroot, + tx_count: block.tx_count ?? block.nTx, size: block.size, weight: block.weight, previousblockhash: block.previousblockhash,