Remove buggy tx vout value fetching and improve performances

This commit is contained in:
nymkappa 2022-08-02 13:03:32 +02:00
parent 5287490894
commit 7fdf95ad34
No known key found for this signature in database
GPG key ID: E155910B16E8BD04

View file

@ -1,4 +1,5 @@
import { existsSync, promises } from 'fs'; import { existsSync, promises } from 'fs';
import bitcoinApiFactory from '../../../api/bitcoin/bitcoin-api-factory';
import bitcoinClient from '../../../api/bitcoin/bitcoin-client'; import bitcoinClient from '../../../api/bitcoin/bitcoin-client';
import config from '../../../config'; import config from '../../../config';
import DB from '../../../database'; import DB from '../../../database';
@ -79,26 +80,10 @@ class FundingTxFetcher {
const outputIdx = parts[2]; const outputIdx = parts[2];
let block = this.blocksCache[blockHeight]; let block = this.blocksCache[blockHeight];
// Check if we have the block in the `blocks_summaries` table to avoid calling core
if (!block) {
const [rows] = await DB.query(`
SELECT UNIX_TIMESTAMP(blocks.blockTimestamp) AS time, blocks_summaries.transactions AS tx
FROM blocks_summaries
JOIN blocks ON blocks.hash = blocks_summaries.id
WHERE blocks_summaries.height = ${blockHeight}
`);
block = rows[0] ?? null;
if (block) {
block.tx = JSON.parse(block.tx);
if (block.tx.length === 0) {
block = null;
}
}
}
// Fetch it from core // Fetch it from core
if (!block) { if (!block) {
const blockHash = await bitcoinClient.getBlockHash(parseInt(blockHeight, 10)); const blockHash = await bitcoinClient.getBlockHash(parseInt(blockHeight, 10));
block = await bitcoinClient.getBlock(blockHash, 2); block = await bitcoinClient.getBlock(blockHash, 1);
} }
this.blocksCache[block.height] = block; this.blocksCache[block.height] = block;
@ -109,10 +94,14 @@ class FundingTxFetcher {
} }
} }
const txid = block.tx[txIdx];
const rawTx = await bitcoinClient.getRawTransaction(txid);
const tx = await bitcoinClient.decodeRawTransaction(rawTx);
this.fundingTxCache[channelId] = { this.fundingTxCache[channelId] = {
timestamp: block.time, timestamp: block.time,
txid: block.tx[txIdx].txid, txid: txid,
value: block.tx[txIdx].value / 100000000 ?? block.tx[txIdx].vout[outputIdx].value, value: tx.vout[outputIdx].value,
}; };
++this.channelNewlyProcessed; ++this.channelNewlyProcessed;