From 7a059ba29476a0cad6a4fd704061abfd1e774e4a Mon Sep 17 00:00:00 2001 From: Mononaut Date: Sun, 16 Jul 2023 17:12:26 +0900 Subject: [PATCH] get chain tip direct from Bitcoin Core to avoid race conditions --- backend/src/api/bitcoin/bitcoin-api.ts | 10 ++-------- backend/src/api/blocks.ts | 15 +++++++++------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/backend/src/api/bitcoin/bitcoin-api.ts b/backend/src/api/bitcoin/bitcoin-api.ts index a0cc41770..cbcb2c571 100644 --- a/backend/src/api/bitcoin/bitcoin-api.ts +++ b/backend/src/api/bitcoin/bitcoin-api.ts @@ -65,17 +65,11 @@ class BitcoinApi implements AbstractBitcoinApi { } $getBlockHeightTip(): Promise { - return this.bitcoindClient.getChainTips() - .then((result: IBitcoinApi.ChainTips[]) => { - return result.find(tip => tip.status === 'active')!.height; - }); + return this.bitcoindClient.getBlockCount(); } $getBlockHashTip(): Promise { - return this.bitcoindClient.getChainTips() - .then((result: IBitcoinApi.ChainTips[]) => { - return result.find(tip => tip.status === 'active')!.hash; - }); + return this.bitcoindClient.getBestBlockHash(); } $getTxIdsForBlock(hash: string): Promise { diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 5939421a7..fdf32f438 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -76,11 +76,14 @@ class Blocks { blockHash: string, blockHeight: number, onlyCoinbase: boolean, + txIds: string[] | null = null, quiet: boolean = false, addMempoolData: boolean = false, ): Promise { const transactions: TransactionExtended[] = []; - const txIds: string[] = await bitcoinApi.$getTxIdsForBlock(blockHash); + if (!txIds) { + txIds = await bitcoinApi.$getTxIdsForBlock(blockHash); + } const mempool = memPool.getMempool(); let transactionsFound = 0; @@ -554,7 +557,7 @@ class Blocks { } const blockHash = await bitcoinApi.$getBlockHash(blockHeight); const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(blockHash); - const transactions = await this.$getTransactionsExtended(blockHash, block.height, true, true); + const transactions = await this.$getTransactionsExtended(blockHash, block.height, true, null, true); const blockExtended = await this.$getBlockExtended(block, transactions); newlyIndexed++; @@ -586,7 +589,7 @@ class Blocks { let fastForwarded = false; let handledBlocks = 0; - const blockHeightTip = await bitcoinApi.$getBlockHeightTip(); + const blockHeightTip = await bitcoinCoreApi.$getBlockHeightTip(); this.updateTimerProgress(timer, 'got block height tip'); if (this.blocks.length === 0) { @@ -639,11 +642,11 @@ class Blocks { } this.updateTimerProgress(timer, `getting block data for ${this.currentBlockHeight}`); - const blockHash = await bitcoinApi.$getBlockHash(this.currentBlockHeight); + const blockHash = await bitcoinCoreApi.$getBlockHash(this.currentBlockHeight); const verboseBlock = await bitcoinClient.getBlock(blockHash, 2); const block = BitcoinApi.convertBlock(verboseBlock); - const txIds: string[] = await bitcoinApi.$getTxIdsForBlock(blockHash); - const transactions = await this.$getTransactionsExtended(blockHash, block.height, false, false, true) as MempoolTransactionExtended[]; + const txIds: string[] = verboseBlock.tx.map(tx => tx.txid); + const transactions = await this.$getTransactionsExtended(blockHash, block.height, false, txIds, false, true) as MempoolTransactionExtended[]; if (config.MEMPOOL.BACKEND !== 'esplora') { // fill in missing transaction fee data from verboseBlock for (let i = 0; i < transactions.length; i++) {