Merge pull request #2039 from mempool/nymkappa/bugfix/use-core-for-block-list
Use bitcoin core to fetch blocks
This commit is contained in:
		
						commit
						69623d71b2
					
				| @ -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,47 +579,43 @@ 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[] = []; |  | ||||||
| 
 |  | ||||||
|       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--; |  | ||||||
|       } |  | ||||||
| 
 | 
 | ||||||
|  |     if (currentHeight < 0) { | ||||||
|       return returnBlocks; |       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 { |   public getLastDifficultyAdjustmentTime(): number { | ||||||
|  | |||||||
| @ -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, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user