get chain tip direct from Bitcoin Core to avoid race conditions

This commit is contained in:
Mononaut 2023-07-16 17:12:26 +09:00
parent 8af64900d9
commit 7a059ba294
No known key found for this signature in database
GPG key ID: A3F058E41374C04E
2 changed files with 11 additions and 14 deletions

View file

@ -65,17 +65,11 @@ class BitcoinApi implements AbstractBitcoinApi {
} }
$getBlockHeightTip(): Promise<number> { $getBlockHeightTip(): Promise<number> {
return this.bitcoindClient.getChainTips() return this.bitcoindClient.getBlockCount();
.then((result: IBitcoinApi.ChainTips[]) => {
return result.find(tip => tip.status === 'active')!.height;
});
} }
$getBlockHashTip(): Promise<string> { $getBlockHashTip(): Promise<string> {
return this.bitcoindClient.getChainTips() return this.bitcoindClient.getBestBlockHash();
.then((result: IBitcoinApi.ChainTips[]) => {
return result.find(tip => tip.status === 'active')!.hash;
});
} }
$getTxIdsForBlock(hash: string): Promise<string[]> { $getTxIdsForBlock(hash: string): Promise<string[]> {

View file

@ -76,11 +76,14 @@ class Blocks {
blockHash: string, blockHash: string,
blockHeight: number, blockHeight: number,
onlyCoinbase: boolean, onlyCoinbase: boolean,
txIds: string[] | null = null,
quiet: boolean = false, quiet: boolean = false,
addMempoolData: boolean = false, addMempoolData: boolean = false,
): Promise<TransactionExtended[]> { ): Promise<TransactionExtended[]> {
const transactions: TransactionExtended[] = []; const transactions: TransactionExtended[] = [];
const txIds: string[] = await bitcoinApi.$getTxIdsForBlock(blockHash); if (!txIds) {
txIds = await bitcoinApi.$getTxIdsForBlock(blockHash);
}
const mempool = memPool.getMempool(); const mempool = memPool.getMempool();
let transactionsFound = 0; let transactionsFound = 0;
@ -554,7 +557,7 @@ class Blocks {
} }
const blockHash = await bitcoinApi.$getBlockHash(blockHeight); const blockHash = await bitcoinApi.$getBlockHash(blockHeight);
const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(blockHash); 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); const blockExtended = await this.$getBlockExtended(block, transactions);
newlyIndexed++; newlyIndexed++;
@ -586,7 +589,7 @@ class Blocks {
let fastForwarded = false; let fastForwarded = false;
let handledBlocks = 0; let handledBlocks = 0;
const blockHeightTip = await bitcoinApi.$getBlockHeightTip(); const blockHeightTip = await bitcoinCoreApi.$getBlockHeightTip();
this.updateTimerProgress(timer, 'got block height tip'); this.updateTimerProgress(timer, 'got block height tip');
if (this.blocks.length === 0) { if (this.blocks.length === 0) {
@ -639,11 +642,11 @@ class Blocks {
} }
this.updateTimerProgress(timer, `getting block data for ${this.currentBlockHeight}`); 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 verboseBlock = await bitcoinClient.getBlock(blockHash, 2);
const block = BitcoinApi.convertBlock(verboseBlock); const block = BitcoinApi.convertBlock(verboseBlock);
const txIds: string[] = await bitcoinApi.$getTxIdsForBlock(blockHash); const txIds: string[] = verboseBlock.tx.map(tx => tx.txid);
const transactions = await this.$getTransactionsExtended(blockHash, block.height, false, false, true) as MempoolTransactionExtended[]; const transactions = await this.$getTransactionsExtended(blockHash, block.height, false, txIds, false, true) as MempoolTransactionExtended[];
if (config.MEMPOOL.BACKEND !== 'esplora') { if (config.MEMPOOL.BACKEND !== 'esplora') {
// fill in missing transaction fee data from verboseBlock // fill in missing transaction fee data from verboseBlock
for (let i = 0; i < transactions.length; i++) { for (let i = 0; i < transactions.length; i++) {