Use bitcoin core instead of esplore for fetch blocks on bitcoin networks

This commit is contained in:
nymkappa 2022-07-08 16:34:00 +02:00
parent c9c5e8008c
commit 97ff1e37aa
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04
2 changed files with 43 additions and 45 deletions

View File

@ -168,7 +168,7 @@ class Blocks {
blockExtended.extras.avgFeeRate = stats.avgfeerate; 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; let pool: PoolTag;
if (blockExtended.extras?.coinbaseTx !== undefined) { if (blockExtended.extras?.coinbaseTx !== undefined) {
pool = await this.$findBlockMiner(blockExtended.extras?.coinbaseTx); pool = await this.$findBlockMiner(blockExtended.extras?.coinbaseTx);
@ -405,7 +405,7 @@ class Blocks {
if (blockHeightTip >= 2016) { if (blockHeightTip >= 2016) {
const previousPeriodBlockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff - 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; this.previousDifficultyRetarget = (block.difficulty - previousPeriodBlock.difficulty) / previousPeriodBlock.difficulty * 100;
logger.debug(`Initial difficulty adjustment data set.`); 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 // Not Bitcoin network, return the block as it
if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false) { if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false) {
return block; return block;
} }
block = prepareBlock(block);
// Bitcoin network, add our custom data on top // Bitcoin network, add our custom data on top
const transactions = await this.$getTransactionsExtended(hash, block.height, true); const transactions = await this.$getTransactionsExtended(hash, block.height, true);
const blockExtended = await this.$getBlockExtended(block, transactions); const blockExtended = await this.$getBlockExtended(block, transactions);
@ -577,7 +579,6 @@ class Blocks {
} }
public async $getBlocks(fromHeight?: number, limit: number = 15): Promise<BlockExtended[]> { public async $getBlocks(fromHeight?: number, limit: number = 15): Promise<BlockExtended[]> {
try {
let currentHeight = fromHeight !== undefined ? fromHeight : this.getCurrentBlockHeight(); let currentHeight = fromHeight !== undefined ? fromHeight : this.getCurrentBlockHeight();
const returnBlocks: BlockExtended[] = []; const returnBlocks: BlockExtended[] = [];
@ -607,7 +608,7 @@ class Blocks {
block = await this.$indexBlock(currentHeight); block = await this.$indexBlock(currentHeight);
returnBlocks.push(block); returnBlocks.push(block);
} else if (nextHash != null) { } else if (nextHash != null) {
block = prepareBlock(await bitcoinApi.$getBlock(nextHash)); block = prepareBlock(await bitcoinClient.getBlock(nextHash));
nextHash = block.previousblockhash; nextHash = block.previousblockhash;
returnBlocks.push(block); returnBlocks.push(block);
} }
@ -615,9 +616,6 @@ class Blocks {
} }
return returnBlocks; return returnBlocks;
} catch (e) {
throw e;
}
} }
public getLastDifficultyAdjustmentTime(): number { public getLastDifficultyAdjustmentTime(): number {

View File

@ -3,14 +3,14 @@ import { BlockExtended } from '../mempool.interfaces';
export function prepareBlock(block: any): BlockExtended { export function prepareBlock(block: any): BlockExtended {
return <BlockExtended>{ return <BlockExtended>{
id: block.id ?? block.hash, // hash for indexed block 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, height: block.height,
version: block.version, version: block.version,
bits: block.bits, bits: (typeof block.bits === 'string' ? parseInt(block.bits, 16): block.bits),
nonce: block.nonce, nonce: block.nonce,
difficulty: block.difficulty, difficulty: block.difficulty,
merkle_root: block.merkle_root, merkle_root: block.merkle_root ?? block.merkleroot,
tx_count: block.tx_count, tx_count: block.tx_count ?? block.nTx,
size: block.size, size: block.size,
weight: block.weight, weight: block.weight,
previousblockhash: block.previousblockhash, previousblockhash: block.previousblockhash,