From 0d921cf7a63ef775521cff2fd95f2745e8ee632a Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 5 Jan 2023 13:02:53 -0600 Subject: [PATCH 01/10] don't rely on blocks table for cpfp indexing progress --- backend/src/api/blocks.ts | 1 - backend/src/api/database-migration.ts | 7 +++++- backend/src/repositories/BlocksRepository.ts | 24 +++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 8292fe241..1376964eb 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -806,7 +806,6 @@ class Blocks { ancestors[vin.txid] = true; }); } - await blocksRepository.$setCPFPIndexed(hash); } } diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index 6e0e95699..68087fdb5 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -4,7 +4,7 @@ import logger from '../logger'; import { Common } from './common'; class DatabaseMigration { - private static currentVersion = 49; + private static currentVersion = 50; private queryTimeout = 3600_000; private statisticsAddedIndexed = false; private uniqueLogs: string[] = []; @@ -442,6 +442,11 @@ class DatabaseMigration { await this.$executeQuery('TRUNCATE TABLE `blocks_audits`'); await this.updateToSchemaVersion(49); } + + if (databaseSchemaVersion < 50 && isBitcoin === true) { + await this.$executeQuery('ALTER TABLE `blocks` DROP COLUMN `cpfp_indexed`'); + await this.updateToSchemaVersion(50); + } } /** diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index 78a8fcce2..5c52a8ba0 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -8,6 +8,8 @@ import HashratesRepository from './HashratesRepository'; import { escape } from 'mysql2'; import BlocksSummariesRepository from './BlocksSummariesRepository'; import DifficultyAdjustmentsRepository from './DifficultyAdjustmentsRepository'; +import bitcoinClient from '../api/bitcoin/bitcoin-client'; +import config from '../config'; class BlocksRepository { /** @@ -667,16 +669,26 @@ class BlocksRepository { */ public async $getCPFPUnindexedBlocks(): Promise { try { - const [rows]: any = await DB.query(`SELECT height, hash FROM blocks WHERE cpfp_indexed = 0 ORDER BY height DESC`); - return rows; + const blockchainInfo = await bitcoinClient.getBlockchainInfo(); + const currentBlockHeight = blockchainInfo.blocks; + const [lastHeightRows]: any = await DB.query(`SELECT MIN(height) AS minHeight from cpfp_clusters`); + const lastHeight = (lastHeightRows.length && lastHeightRows[0].minHeight != null) ? lastHeightRows[0].minHeight : currentBlockHeight; + + let indexingBlockAmount = Math.min(config.MEMPOOL.INDEXING_BLOCKS_AMOUNT, blockchainInfo.blocks); + if (indexingBlockAmount <= -1) { + indexingBlockAmount = currentBlockHeight + 1; + } + const firstHeight = Math.max(0, currentBlockHeight - indexingBlockAmount + 1); + + if (firstHeight < lastHeight) { + const [rows]: any = await DB.query(`SELECT height, hash FROM blocks WHERE height BETWEEN ? AND ? ORDER BY height DESC`, [firstHeight, lastHeight]); + return rows; + } } catch (e) { logger.err('Cannot fetch CPFP unindexed blocks. Reason: ' + (e instanceof Error ? e.message : e)); throw e; } - } - - public async $setCPFPIndexed(hash: string): Promise { - await DB.query(`UPDATE blocks SET cpfp_indexed = 1 WHERE hash = ?`, [hash]); + return []; } /** From ee95d033ac4386bbfbc99cbca0ad3ff78540a272 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Sat, 7 Jan 2023 15:51:31 -0600 Subject: [PATCH 02/10] remove slow cpfp indexing path --- backend/src/api/blocks.ts | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 1376964eb..29fac7850 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -753,33 +753,14 @@ class Blocks { public async $indexCPFP(hash: string, height: number): Promise { let transactions; - if (Common.blocksSummariesIndexingEnabled()) { - transactions = await this.$getStrippedBlockTransactions(hash); - const rawBlock = await bitcoinApi.$getRawBlock(hash); - const block = Block.fromBuffer(rawBlock); - const txMap = {}; - for (const tx of block.transactions || []) { - txMap[tx.getId()] = tx; - } - for (const tx of transactions) { - // convert from bitcoinjs to esplora vin format - if (txMap[tx.txid]?.ins) { - tx.vin = txMap[tx.txid].ins.map(vin => { - return { - txid: vin.hash.slice().reverse().toString('hex') - }; - }); - } - } - } else { - const block = await bitcoinClient.getBlock(hash, 2); - transactions = block.tx.map(tx => { - tx.vsize = tx.weight / 4; - tx.fee *= 100_000_000; - return tx; - }); - } + const block = await bitcoinClient.getBlock(hash, 2); + transactions = block.tx.map(tx => { + tx.vsize = tx.weight / 4; + tx.fee *= 100_000_000; + return tx; + }); + let cluster: TransactionStripped[] = []; let ancestors: { [txid: string]: boolean } = {}; for (let i = transactions.length - 1; i >= 0; i--) { From 7793eaecbc193567f9622d9b0c93ab193ed4ccd1 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Mon, 9 Jan 2023 08:34:33 -0600 Subject: [PATCH 03/10] fix cpfp indexing rate calculation --- backend/src/api/blocks.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 29fac7850..a782dbacc 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -341,6 +341,7 @@ class Blocks { try { // Get all indexed block hash const unindexedBlocks = await blocksRepository.$getCPFPUnindexedBlocks(); + logger.info(`Indexing cpfp data for ${unindexedBlocks.length} blocks`); if (!unindexedBlocks?.length) { return; @@ -357,7 +358,7 @@ class Blocks { const elapsedSeconds = Math.max(1, new Date().getTime() / 1000 - timer); if (elapsedSeconds > 5) { const runningFor = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt)); - const blockPerSeconds = Math.max(1, countThisRun / elapsedSeconds); + const blockPerSeconds = (countThisRun / elapsedSeconds); const progress = Math.round(count / unindexedBlocks.length * 10000) / 100; logger.debug(`Indexing cpfp clusters for #${block.height} | ~${blockPerSeconds.toFixed(2)} blocks/sec | total: ${count}/${unindexedBlocks.length} (${progress}%) | elapsed: ${runningFor} seconds`); timer = new Date().getTime() / 1000; From b50936f001406c3465b7350a3d54cf26c517eddb Mon Sep 17 00:00:00 2001 From: Mononaut Date: Mon, 9 Jan 2023 10:24:12 -0600 Subject: [PATCH 04/10] compact schemas for cpfp tables --- backend/src/api/blocks.ts | 15 ++-- backend/src/api/database-migration.ts | 21 +++++ backend/src/repositories/BlocksRepository.ts | 2 +- backend/src/repositories/CpfpRepository.ts | 86 +++++++++++++++++-- .../src/repositories/TransactionRepository.ts | 53 +++++++----- 5 files changed, 142 insertions(+), 35 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index a782dbacc..2b1f88518 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -22,12 +22,10 @@ import poolsParser from './pools-parser'; import BlocksSummariesRepository from '../repositories/BlocksSummariesRepository'; import BlocksAuditsRepository from '../repositories/BlocksAuditsRepository'; import cpfpRepository from '../repositories/CpfpRepository'; -import transactionRepository from '../repositories/TransactionRepository'; import mining from './mining/mining'; import DifficultyAdjustmentsRepository from '../repositories/DifficultyAdjustmentsRepository'; import PricesRepository from '../repositories/PricesRepository'; import priceUpdater from '../tasks/price-updater'; -import { Block } from 'bitcoinjs-lib'; class Blocks { private blocks: BlockExtended[] = []; @@ -753,10 +751,8 @@ class Blocks { } public async $indexCPFP(hash: string, height: number): Promise { - let transactions; - const block = await bitcoinClient.getBlock(hash, 2); - transactions = block.tx.map(tx => { + const transactions = block.tx.map(tx => { tx.vsize = tx.weight / 4; tx.fee *= 100_000_000; return tx; @@ -775,9 +771,12 @@ class Blocks { }); const effectiveFeePerVsize = totalFee / totalVSize; if (cluster.length > 1) { - await cpfpRepository.$saveCluster(height, cluster.map(tx => { return { txid: tx.txid, weight: tx.vsize * 4, fee: tx.fee || 0 }; }), effectiveFeePerVsize); - for (const tx of cluster) { - await transactionRepository.$setCluster(tx.txid, cluster[0].txid); + const roundedEffectiveFee = Math.round(effectiveFeePerVsize * 100) / 100; + const equalFee = cluster.reduce((acc, tx) => { + return (acc && Math.round(((tx.fee || 0) / tx.vsize) * 100) / 100 === roundedEffectiveFee); + }, true); + if (!equalFee) { + await cpfpRepository.$saveCluster(cluster[0].txid, height, cluster.map(tx => { return { txid: tx.txid, weight: tx.vsize * 4, fee: tx.fee || 0 }; }), effectiveFeePerVsize); } } cluster = []; diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index 68087fdb5..6d7901ffa 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -445,6 +445,8 @@ class DatabaseMigration { if (databaseSchemaVersion < 50 && isBitcoin === true) { await this.$executeQuery('ALTER TABLE `blocks` DROP COLUMN `cpfp_indexed`'); + await this.$executeQuery(this.getCreateCompactCPFPTableQuery(), await this.$checkIfTableExists('compact_cpfp_clusters')); + await this.$executeQuery(this.getCreateCompactTransactionsTableQuery(), await this.$checkIfTableExists('compact_transactions')); await this.updateToSchemaVersion(50); } } @@ -918,6 +920,25 @@ class DatabaseMigration { ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; } + private getCreateCompactCPFPTableQuery(): string { + return `CREATE TABLE IF NOT EXISTS compact_cpfp_clusters ( + root binary(32) NOT NULL, + height int(10) NOT NULL, + txs BLOB DEFAULT NULL, + fee_rate float unsigned NOT NULL, + PRIMARY KEY (root), + INDEX (height) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; + } + + private getCreateCompactTransactionsTableQuery(): string { + return `CREATE TABLE IF NOT EXISTS compact_transactions ( + txid binary(32) NOT NULL, + cluster binary(32) DEFAULT NULL, + PRIMARY KEY (txid) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; + } + public async $truncateIndexedData(tables: string[]) { const allowedTables = ['blocks', 'hashrates', 'prices']; diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index 5c52a8ba0..6cd5d2785 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -671,7 +671,7 @@ class BlocksRepository { try { const blockchainInfo = await bitcoinClient.getBlockchainInfo(); const currentBlockHeight = blockchainInfo.blocks; - const [lastHeightRows]: any = await DB.query(`SELECT MIN(height) AS minHeight from cpfp_clusters`); + const [lastHeightRows]: any = await DB.query(`SELECT MIN(height) AS minHeight from compact_cpfp_clusters`); const lastHeight = (lastHeightRows.length && lastHeightRows[0].minHeight != null) ? lastHeightRows[0].minHeight : currentBlockHeight; let indexingBlockAmount = Math.min(config.MEMPOOL.INDEXING_BLOCKS_AMOUNT, blockchainInfo.blocks); diff --git a/backend/src/repositories/CpfpRepository.ts b/backend/src/repositories/CpfpRepository.ts index 563e6ede1..a5f17540a 100644 --- a/backend/src/repositories/CpfpRepository.ts +++ b/backend/src/repositories/CpfpRepository.ts @@ -1,34 +1,72 @@ +import cluster, { Cluster } from 'cluster'; +import { RowDataPacket } from 'mysql2'; import DB from '../database'; import logger from '../logger'; import { Ancestor } from '../mempool.interfaces'; +import transactionRepository from '../repositories/TransactionRepository'; class CpfpRepository { - public async $saveCluster(height: number, txs: Ancestor[], effectiveFeePerVsize: number): Promise { + public async $saveCluster(clusterRoot: string, height: number, txs: Ancestor[], effectiveFeePerVsize: number): Promise { + if (!txs[0]) { + return; + } try { - const txsJson = JSON.stringify(txs); + const packedTxs = Buffer.from(this.pack(txs)); await DB.query( ` - INSERT INTO cpfp_clusters(root, height, txs, fee_rate) - VALUE (?, ?, ?, ?) + INSERT INTO compact_cpfp_clusters(root, height, txs, fee_rate) + VALUE (UNHEX(?), ?, ?, ?) ON DUPLICATE KEY UPDATE height = ?, txs = ?, fee_rate = ? `, - [txs[0].txid, height, txsJson, effectiveFeePerVsize, height, txsJson, effectiveFeePerVsize, height] + [clusterRoot, height, packedTxs, effectiveFeePerVsize, height, packedTxs, effectiveFeePerVsize] ); + for (const tx of txs) { + await transactionRepository.$setCluster(tx.txid, clusterRoot); + } } catch (e: any) { logger.err(`Cannot save cpfp cluster into db. Reason: ` + (e instanceof Error ? e.message : e)); throw e; } } + public async $getCluster(clusterRoot: string): Promise { + const [clusterRows]: any = await DB.query( + ` + SELECT * + FROM compact_cpfp_clusters + WHERE root = UNHEX(?) + `, + [clusterRoot] + ); + const cluster = clusterRows[0]; + cluster.txs = this.unpack(cluster.txs); + return cluster; + } + public async $deleteClustersFrom(height: number): Promise { logger.info(`Delete newer cpfp clusters from height ${height} from the database`); try { + const [rows] = await DB.query( + ` + SELECT txs, height, root from compact_cpfp_clusters + WHERE height >= ? + `, + [height] + ) as RowDataPacket[][]; + if (rows?.length) { + for (let clusterToDelete of rows) { + const txs = this.unpack(clusterToDelete.txs); + for (let tx of txs) { + await transactionRepository.$removeTransaction(tx.txid); + } + } + } await DB.query( ` - DELETE from cpfp_clusters + DELETE from compact_cpfp_clusters WHERE height >= ? `, [height] @@ -38,6 +76,42 @@ class CpfpRepository { throw e; } } + + public pack(txs: Ancestor[]): ArrayBuffer { + const buf = new ArrayBuffer(44 * txs.length); + const view = new DataView(buf); + txs.forEach((tx, i) => { + const offset = i * 44; + for (let x = 0; x < 32; x++) { + // store txid in little-endian + view.setUint8(offset + (31 - x), parseInt(tx.txid.slice(x * 2, (x * 2) + 2), 16)); + } + view.setUint32(offset + 32, tx.weight); + view.setBigUint64(offset + 36, BigInt(Math.round(tx.fee))); + }); + return buf; + } + + public unpack(buf: Buffer): Ancestor[] { + if (!buf) { + return []; + } + + const arrayBuffer = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); + const txs: Ancestor[] = []; + const view = new DataView(arrayBuffer); + for (let offset = 0; offset < arrayBuffer.byteLength; offset += 44) { + const txid = Array.from(new Uint8Array(arrayBuffer, offset, 32)).reverse().map(b => b.toString(16).padStart(2, '0')).join(''); + const weight = view.getUint32(offset + 32); + const fee = Number(view.getBigUint64(offset + 36)); + txs.push({ + txid, + weight, + fee + }); + } + return txs; + } } export default new CpfpRepository(); \ No newline at end of file diff --git a/backend/src/repositories/TransactionRepository.ts b/backend/src/repositories/TransactionRepository.ts index 74debb833..2d05f0e14 100644 --- a/backend/src/repositories/TransactionRepository.ts +++ b/backend/src/repositories/TransactionRepository.ts @@ -1,6 +1,7 @@ import DB from '../database'; import logger from '../logger'; import { Ancestor, CpfpInfo } from '../mempool.interfaces'; +import cpfpRepository from './CpfpRepository'; interface CpfpSummary { txid: string; @@ -12,20 +13,20 @@ interface CpfpSummary { } class TransactionRepository { - public async $setCluster(txid: string, cluster: string): Promise { + public async $setCluster(txid: string, clusterRoot: string): Promise { try { await DB.query( ` - INSERT INTO transactions + INSERT INTO compact_transactions ( txid, cluster ) - VALUE (?, ?) + VALUE (UNHEX(?), UNHEX(?)) ON DUPLICATE KEY UPDATE - cluster = ? + cluster = UNHEX(?) ;`, - [txid, cluster, cluster] + [txid, clusterRoot, clusterRoot] ); } catch (e: any) { logger.err(`Cannot save transaction cpfp cluster into db. Reason: ` + (e instanceof Error ? e.message : e)); @@ -35,18 +36,19 @@ class TransactionRepository { public async $getCpfpInfo(txid: string): Promise { try { - let query = ` - SELECT * - FROM transactions - LEFT JOIN cpfp_clusters AS cluster ON cluster.root = transactions.cluster - WHERE transactions.txid = ? - `; - const [rows]: any = await DB.query(query, [txid]); - if (rows.length) { - rows[0].txs = JSON.parse(rows[0].txs) as Ancestor[]; - if (rows[0]?.txs?.length) { - return this.convertCpfp(rows[0]); - } + const [txRows]: any = await DB.query( + ` + SELECT HEX(txid) as id, HEX(cluster) as root + FROM compact_transactions + WHERE txid = UNHEX(?) + `, + [txid] + ); + if (txRows.length && txRows[0].root != null) { + const txid = txRows[0].id.toLowerCase(); + const clusterId = txRows[0].root.toLowerCase(); + const cluster = await cpfpRepository.$getCluster(clusterId); + return this.convertCpfp(txid, cluster); } } catch (e) { logger.err('Cannot get transaction cpfp info from db. Reason: ' + (e instanceof Error ? e.message : e)); @@ -54,12 +56,23 @@ class TransactionRepository { } } - private convertCpfp(cpfp: CpfpSummary): CpfpInfo { + public async $removeTransaction(txid: string): Promise { + await DB.query( + ` + DELETE FROM compact_transactions + WHERE txid = UNHEX(?) + `, + [txid] + ); + } + + private convertCpfp(txid, cluster): CpfpInfo { const descendants: Ancestor[] = []; const ancestors: Ancestor[] = []; let matched = false; - for (const tx of cpfp.txs) { - if (tx.txid === cpfp.txid) { + + for (const tx of cluster.txs) { + if (tx.txid === txid) { matched = true; } else if (!matched) { descendants.push(tx); From 01c96f80f9f99fda558ec9df93ca63f7f88b1c6a Mon Sep 17 00:00:00 2001 From: Mononaut Date: Mon, 9 Jan 2023 10:25:25 -0600 Subject: [PATCH 05/10] add cpfp progress marker to avoid reindexing early blocks --- backend/src/api/blocks.ts | 4 +++- backend/src/repositories/CpfpRepository.ts | 28 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 2b1f88518..c70bb67f7 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -350,7 +350,7 @@ class Blocks { let countThisRun = 0; let timer = new Date().getTime() / 1000; const startedAt = new Date().getTime() / 1000; - + let lastHeight; for (const block of unindexedBlocks) { // Logging const elapsedSeconds = Math.max(1, new Date().getTime() / 1000 - timer); @@ -365,11 +365,13 @@ class Blocks { await this.$indexCPFP(block.hash, block.height); // Calculate and save CPFP data for transactions in this block + lastHeight = block.height; // Logging count++; countThisRun++; } if (count > 0) { + await cpfpRepository.$insertProgressMarker(lastHeight); logger.notice(`CPFP indexing completed: indexed ${count} blocks`); } else { logger.debug(`CPFP indexing completed: indexed ${count} blocks`); diff --git a/backend/src/repositories/CpfpRepository.ts b/backend/src/repositories/CpfpRepository.ts index a5f17540a..6d091437c 100644 --- a/backend/src/repositories/CpfpRepository.ts +++ b/backend/src/repositories/CpfpRepository.ts @@ -77,6 +77,34 @@ class CpfpRepository { } } + // insert a dummy row to mark that we've indexed as far as this block + public async $insertProgressMarker(height: number): Promise { + try { + const [rows]: any = await DB.query( + ` + SELECT root + FROM compact_cpfp_clusters + WHERE height = ? + `, + [height] + ); + if (!rows?.length) { + const rootBuffer = Buffer.alloc(32); + rootBuffer.writeInt32LE(height); + await DB.query( + ` + INSERT INTO compact_cpfp_clusters(root, height, fee_rate) + VALUE (?, ?, ?) + `, + [rootBuffer, height, 0] + ); + } + } catch (e: any) { + logger.err(`Cannot insert cpfp progress marker. Reason: ` + (e instanceof Error ? e.message : e)); + throw e; + } + } + public pack(txs: Ancestor[]): ArrayBuffer { const buf = new ArrayBuffer(44 * txs.length); const view = new DataView(buf); From fcd047f30231ff91eb134cc7188bf0e46fedc4cf Mon Sep 17 00:00:00 2001 From: Mononaut Date: Mon, 9 Jan 2023 10:25:56 -0600 Subject: [PATCH 06/10] remove redundant fields from CPFP interfaces --- .../src/repositories/TransactionRepository.ts | 1 - .../transaction/transaction.component.ts | 27 +++++++------------ .../src/app/interfaces/node-api.interface.ts | 1 - 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/backend/src/repositories/TransactionRepository.ts b/backend/src/repositories/TransactionRepository.ts index 2d05f0e14..fc6af973d 100644 --- a/backend/src/repositories/TransactionRepository.ts +++ b/backend/src/repositories/TransactionRepository.ts @@ -83,7 +83,6 @@ class TransactionRepository { return { descendants, ancestors, - effectiveFeePerVsize: cpfp.fee_rate }; } } diff --git a/frontend/src/app/components/transaction/transaction.component.ts b/frontend/src/app/components/transaction/transaction.component.ts index 575c00637..81ae2f055 100644 --- a/frontend/src/app/components/transaction/transaction.component.ts +++ b/frontend/src/app/components/transaction/transaction.component.ts @@ -131,26 +131,17 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { this.cpfpInfo = null; return; } - if (cpfpInfo.effectiveFeePerVsize) { - this.tx.effectiveFeePerVsize = cpfpInfo.effectiveFeePerVsize; - } else { - const lowerFeeParents = cpfpInfo.ancestors.filter( - (parent) => parent.fee / (parent.weight / 4) < this.tx.feePerVsize - ); - let totalWeight = - this.tx.weight + - lowerFeeParents.reduce((prev, val) => prev + val.weight, 0); - let totalFees = - this.tx.fee + - lowerFeeParents.reduce((prev, val) => prev + val.fee, 0); - if (cpfpInfo?.bestDescendant) { - totalWeight += cpfpInfo?.bestDescendant.weight; - totalFees += cpfpInfo?.bestDescendant.fee; - } + const relatives = [...cpfpInfo.ancestors, ...cpfpInfo.descendants || [cpfpInfo.bestDescendant]]; + let totalWeight = + this.tx.weight + + relatives.reduce((prev, val) => prev + val.weight, 0); + let totalFees = + this.tx.fee + + relatives.reduce((prev, val) => prev + val.fee, 0); + + this.tx.effectiveFeePerVsize = totalFees / (totalWeight / 4); - this.tx.effectiveFeePerVsize = totalFees / (totalWeight / 4); - } if (!this.tx.status.confirmed) { this.stateService.markBlock$.next({ txFeePerVSize: this.tx.effectiveFeePerVsize, diff --git a/frontend/src/app/interfaces/node-api.interface.ts b/frontend/src/app/interfaces/node-api.interface.ts index 2e6b94988..f72886870 100644 --- a/frontend/src/app/interfaces/node-api.interface.ts +++ b/frontend/src/app/interfaces/node-api.interface.ts @@ -24,7 +24,6 @@ export interface CpfpInfo { ancestors: Ancestor[]; descendants?: Ancestor[]; bestDescendant?: BestDescendant | null; - effectiveFeePerVsize?: number; } export interface DifficultyAdjustment { From 8de3fd098879d677c9c337465117fcfa5743b705 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 10 Jan 2023 17:13:11 -0600 Subject: [PATCH 07/10] batch db inserts for cpfp data --- backend/src/api/blocks.ts | 16 ++-- backend/src/repositories/CpfpRepository.ts | 82 ++++++++++++++++++- .../src/repositories/TransactionRepository.ts | 24 ++++++ 3 files changed, 113 insertions(+), 9 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index c70bb67f7..69fa208b8 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -760,6 +760,8 @@ class Blocks { return tx; }); + const clusters: any[] = []; + let cluster: TransactionStripped[] = []; let ancestors: { [txid: string]: boolean } = {}; for (let i = transactions.length - 1; i >= 0; i--) { @@ -773,13 +775,12 @@ class Blocks { }); const effectiveFeePerVsize = totalFee / totalVSize; if (cluster.length > 1) { - const roundedEffectiveFee = Math.round(effectiveFeePerVsize * 100) / 100; - const equalFee = cluster.reduce((acc, tx) => { - return (acc && Math.round(((tx.fee || 0) / tx.vsize) * 100) / 100 === roundedEffectiveFee); - }, true); - if (!equalFee) { - await cpfpRepository.$saveCluster(cluster[0].txid, height, cluster.map(tx => { return { txid: tx.txid, weight: tx.vsize * 4, fee: tx.fee || 0 }; }), effectiveFeePerVsize); - } + clusters.push({ + root: cluster[0].txid, + height, + txs: cluster.map(tx => { return { txid: tx.txid, weight: tx.vsize * 4, fee: tx.fee || 0 }; }), + effectiveFeePerVsize, + }); } cluster = []; ancestors = {}; @@ -789,6 +790,7 @@ class Blocks { ancestors[vin.txid] = true; }); } + await cpfpRepository.$batchSaveClusters(clusters); } } diff --git a/backend/src/repositories/CpfpRepository.ts b/backend/src/repositories/CpfpRepository.ts index 6d091437c..6fe041763 100644 --- a/backend/src/repositories/CpfpRepository.ts +++ b/backend/src/repositories/CpfpRepository.ts @@ -10,6 +10,15 @@ class CpfpRepository { if (!txs[0]) { return; } + // skip clusters of transactions with the same fees + const roundedEffectiveFee = Math.round(effectiveFeePerVsize * 100) / 100; + const equalFee = txs.reduce((acc, tx) => { + return (acc && Math.round(((tx.fee || 0) / (tx.weight / 4)) * 100) / 100 === roundedEffectiveFee); + }, true); + if (equalFee) { + return; + } + try { const packedTxs = Buffer.from(this.pack(txs)); await DB.query( @@ -23,8 +32,14 @@ class CpfpRepository { `, [clusterRoot, height, packedTxs, effectiveFeePerVsize, height, packedTxs, effectiveFeePerVsize] ); - for (const tx of txs) { - await transactionRepository.$setCluster(tx.txid, clusterRoot); + const maxChunk = 10; + let chunkIndex = 0; + while (chunkIndex < txs.length) { + const chunk = txs.slice(chunkIndex, chunkIndex + maxChunk).map(tx => { + return { txid: tx.txid, cluster: clusterRoot }; + }); + await transactionRepository.$batchSetCluster(chunk); + chunkIndex += maxChunk; } } catch (e: any) { logger.err(`Cannot save cpfp cluster into db. Reason: ` + (e instanceof Error ? e.message : e)); @@ -32,6 +47,69 @@ class CpfpRepository { } } + public async $batchSaveClusters(clusters: { root: string, height: number, txs: any, effectiveFeePerVsize: number}[]): Promise { + try { + const clusterValues: any[] = []; + const txs: any[] = []; + + for (const cluster of clusters) { + if (cluster.txs?.length > 1) { + const roundedEffectiveFee = Math.round(cluster.effectiveFeePerVsize * 100) / 100; + const equalFee = cluster.txs.reduce((acc, tx) => { + return (acc && Math.round(((tx.fee || 0) / (tx.weight / 4)) * 100) / 100 === roundedEffectiveFee); + }, true); + if (!equalFee) { + clusterValues.push([ + cluster.root, + cluster.height, + Buffer.from(this.pack(cluster.txs)), + cluster.effectiveFeePerVsize + ]); + for (const tx of cluster.txs) { + txs.push({ txid: tx.txid, cluster: cluster.root }); + } + } + } + } + + if (!clusterValues.length) { + return; + } + + const maxChunk = 100; + let chunkIndex = 0; + // insert transactions in batches of up to 100 rows + while (chunkIndex < txs.length) { + const chunk = txs.slice(chunkIndex, chunkIndex + maxChunk); + await transactionRepository.$batchSetCluster(chunk); + chunkIndex += maxChunk; + } + + chunkIndex = 0; + // insert clusters in batches of up to 100 rows + while (chunkIndex < clusterValues.length) { + const chunk = clusterValues.slice(chunkIndex, chunkIndex + maxChunk); + let query = ` + INSERT IGNORE INTO compact_cpfp_clusters(root, height, txs, fee_rate) + VALUES + `; + query += chunk.map(chunk => { + return (' (UNHEX(?), ?, ?, ?)'); + }) + ';'; + const values = chunk.flat(); + await DB.query( + query, + values + ); + chunkIndex += maxChunk; + } + return; + } catch (e: any) { + logger.err(`Cannot save cpfp clusters into db. Reason: ` + (e instanceof Error ? e.message : e)); + throw e; + } + } + public async $getCluster(clusterRoot: string): Promise { const [clusterRows]: any = await DB.query( ` diff --git a/backend/src/repositories/TransactionRepository.ts b/backend/src/repositories/TransactionRepository.ts index fc6af973d..061617451 100644 --- a/backend/src/repositories/TransactionRepository.ts +++ b/backend/src/repositories/TransactionRepository.ts @@ -34,6 +34,30 @@ class TransactionRepository { } } + public async $batchSetCluster(txs): Promise { + try { + let query = ` + INSERT IGNORE INTO compact_transactions + ( + txid, + cluster + ) + VALUES + `; + query += txs.map(tx => { + return (' (UNHEX(?), UNHEX(?))'); + }) + ';'; + const values = txs.map(tx => [tx.txid, tx.cluster]).flat(); + await DB.query( + query, + values + ); + } catch (e: any) { + logger.err(`Cannot save cpfp transactions into db. Reason: ` + (e instanceof Error ? e.message : e)); + throw e; + } + } + public async $getCpfpInfo(txid: string): Promise { try { const [txRows]: any = await DB.query( From f0d3bb87c60b3281e970852459d80386ae537fb8 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 10 Jan 2023 17:15:55 -0600 Subject: [PATCH 08/10] handle gaps in indexed CPFP data --- backend/src/api/blocks.ts | 29 +++++++++----------- backend/src/repositories/BlocksRepository.ts | 24 ++++++++++------ backend/src/repositories/CpfpRepository.ts | 13 +++++---- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 69fa208b8..9ab572401 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -338,10 +338,10 @@ class Blocks { try { // Get all indexed block hash - const unindexedBlocks = await blocksRepository.$getCPFPUnindexedBlocks(); - logger.info(`Indexing cpfp data for ${unindexedBlocks.length} blocks`); + const unindexedBlockHeights = await blocksRepository.$getCPFPUnindexedBlocks(); + logger.info(`Indexing cpfp data for ${unindexedBlockHeights.length} blocks`); - if (!unindexedBlocks?.length) { + if (!unindexedBlockHeights?.length) { return; } @@ -350,32 +350,26 @@ class Blocks { let countThisRun = 0; let timer = new Date().getTime() / 1000; const startedAt = new Date().getTime() / 1000; - let lastHeight; - for (const block of unindexedBlocks) { + for (const height of unindexedBlockHeights) { // Logging + const hash = await bitcoinApi.$getBlockHash(height); const elapsedSeconds = Math.max(1, new Date().getTime() / 1000 - timer); if (elapsedSeconds > 5) { const runningFor = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt)); const blockPerSeconds = (countThisRun / elapsedSeconds); - const progress = Math.round(count / unindexedBlocks.length * 10000) / 100; - logger.debug(`Indexing cpfp clusters for #${block.height} | ~${blockPerSeconds.toFixed(2)} blocks/sec | total: ${count}/${unindexedBlocks.length} (${progress}%) | elapsed: ${runningFor} seconds`); + const progress = Math.round(count / unindexedBlockHeights.length * 10000) / 100; + logger.debug(`Indexing cpfp clusters for #${height} | ~${blockPerSeconds.toFixed(2)} blocks/sec | total: ${count}/${unindexedBlockHeights.length} (${progress}%) | elapsed: ${runningFor} seconds`); timer = new Date().getTime() / 1000; countThisRun = 0; } - await this.$indexCPFP(block.hash, block.height); // Calculate and save CPFP data for transactions in this block + await this.$indexCPFP(hash, height); // Calculate and save CPFP data for transactions in this block - lastHeight = block.height; // Logging count++; countThisRun++; } - if (count > 0) { - await cpfpRepository.$insertProgressMarker(lastHeight); - logger.notice(`CPFP indexing completed: indexed ${count} blocks`); - } else { - logger.debug(`CPFP indexing completed: indexed ${count} blocks`); - } + logger.notice(`CPFP indexing completed: indexed ${count} blocks`); } catch (e) { logger.err(`CPFP indexing failed. Trying again in 10 seconds. Reason: ${(e instanceof Error ? e.message : e)}`); throw e; @@ -790,7 +784,10 @@ class Blocks { ancestors[vin.txid] = true; }); } - await cpfpRepository.$batchSaveClusters(clusters); + const result = await cpfpRepository.$batchSaveClusters(clusters); + if (!result) { + await cpfpRepository.$insertProgressMarker(height); + } } } diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index 6cd5d2785..df98719b9 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -671,19 +671,25 @@ class BlocksRepository { try { const blockchainInfo = await bitcoinClient.getBlockchainInfo(); const currentBlockHeight = blockchainInfo.blocks; - const [lastHeightRows]: any = await DB.query(`SELECT MIN(height) AS minHeight from compact_cpfp_clusters`); - const lastHeight = (lastHeightRows.length && lastHeightRows[0].minHeight != null) ? lastHeightRows[0].minHeight : currentBlockHeight; - - let indexingBlockAmount = Math.min(config.MEMPOOL.INDEXING_BLOCKS_AMOUNT, blockchainInfo.blocks); + let indexingBlockAmount = Math.min(config.MEMPOOL.INDEXING_BLOCKS_AMOUNT, currentBlockHeight); if (indexingBlockAmount <= -1) { indexingBlockAmount = currentBlockHeight + 1; } - const firstHeight = Math.max(0, currentBlockHeight - indexingBlockAmount + 1); + const minHeight = Math.max(0, currentBlockHeight - indexingBlockAmount + 1); - if (firstHeight < lastHeight) { - const [rows]: any = await DB.query(`SELECT height, hash FROM blocks WHERE height BETWEEN ? AND ? ORDER BY height DESC`, [firstHeight, lastHeight]); - return rows; - } + const [rows]: any[] = await DB.query(` + SELECT height + FROM compact_cpfp_clusters + WHERE height <= ? AND height >= ? + ORDER BY height DESC; + `, [currentBlockHeight, minHeight]); + + const indexedHeights = {}; + rows.forEach((row) => { indexedHeights[row.height] = true; }); + const allHeights: number[] = Array.from(Array(currentBlockHeight - minHeight + 1).keys(), n => n + minHeight).reverse(); + const unindexedHeights = allHeights.filter(x => !indexedHeights[x]); + + return unindexedHeights; } catch (e) { logger.err('Cannot fetch CPFP unindexed blocks. Reason: ' + (e instanceof Error ? e.message : e)); throw e; diff --git a/backend/src/repositories/CpfpRepository.ts b/backend/src/repositories/CpfpRepository.ts index 6fe041763..ce7432d5b 100644 --- a/backend/src/repositories/CpfpRepository.ts +++ b/backend/src/repositories/CpfpRepository.ts @@ -6,9 +6,9 @@ import { Ancestor } from '../mempool.interfaces'; import transactionRepository from '../repositories/TransactionRepository'; class CpfpRepository { - public async $saveCluster(clusterRoot: string, height: number, txs: Ancestor[], effectiveFeePerVsize: number): Promise { + public async $saveCluster(clusterRoot: string, height: number, txs: Ancestor[], effectiveFeePerVsize: number): Promise { if (!txs[0]) { - return; + return false; } // skip clusters of transactions with the same fees const roundedEffectiveFee = Math.round(effectiveFeePerVsize * 100) / 100; @@ -16,7 +16,7 @@ class CpfpRepository { return (acc && Math.round(((tx.fee || 0) / (tx.weight / 4)) * 100) / 100 === roundedEffectiveFee); }, true); if (equalFee) { - return; + return false; } try { @@ -41,13 +41,14 @@ class CpfpRepository { await transactionRepository.$batchSetCluster(chunk); chunkIndex += maxChunk; } + return true; } catch (e: any) { logger.err(`Cannot save cpfp cluster into db. Reason: ` + (e instanceof Error ? e.message : e)); throw e; } } - public async $batchSaveClusters(clusters: { root: string, height: number, txs: any, effectiveFeePerVsize: number}[]): Promise { + public async $batchSaveClusters(clusters: { root: string, height: number, txs: any, effectiveFeePerVsize: number}[]): Promise { try { const clusterValues: any[] = []; const txs: any[] = []; @@ -73,7 +74,7 @@ class CpfpRepository { } if (!clusterValues.length) { - return; + return false; } const maxChunk = 100; @@ -103,7 +104,7 @@ class CpfpRepository { ); chunkIndex += maxChunk; } - return; + return true; } catch (e: any) { logger.err(`Cannot save cpfp clusters into db. Reason: ` + (e instanceof Error ? e.message : e)); throw e; From bd30f2eb12f26ccd5e55d874bb771744e7b29572 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 10 Jan 2023 17:24:18 -0600 Subject: [PATCH 09/10] migrate cpfp data from old to new schemas --- backend/src/api/database-migration.ts | 70 +++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index 6d7901ffa..42f223417 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -2,9 +2,12 @@ import config from '../config'; import DB from '../database'; import logger from '../logger'; import { Common } from './common'; +import blocksRepository from '../repositories/BlocksRepository'; +import cpfpRepository from '../repositories/CpfpRepository'; +import { RowDataPacket } from 'mysql2'; class DatabaseMigration { - private static currentVersion = 50; + private static currentVersion = 52; private queryTimeout = 3600_000; private statisticsAddedIndexed = false; private uniqueLogs: string[] = []; @@ -443,11 +446,27 @@ class DatabaseMigration { await this.updateToSchemaVersion(49); } - if (databaseSchemaVersion < 50 && isBitcoin === true) { + if (databaseSchemaVersion < 50) { await this.$executeQuery('ALTER TABLE `blocks` DROP COLUMN `cpfp_indexed`'); + await this.updateToSchemaVersion(50); + } + + if (databaseSchemaVersion < 51) { + await this.$executeQuery('ALTER TABLE `cpfp_clusters` ADD INDEX `height` (`height`)'); + await this.updateToSchemaVersion(51); + } + + if (databaseSchemaVersion < 52) { await this.$executeQuery(this.getCreateCompactCPFPTableQuery(), await this.$checkIfTableExists('compact_cpfp_clusters')); await this.$executeQuery(this.getCreateCompactTransactionsTableQuery(), await this.$checkIfTableExists('compact_transactions')); - await this.updateToSchemaVersion(50); + try { + await this.$convertCompactCpfpTables(); + await this.$executeQuery('DROP TABLE IF EXISTS `cpfp_clusters`'); + await this.$executeQuery('DROP TABLE IF EXISTS `transactions`'); + await this.updateToSchemaVersion(52); + } catch(e) { + logger.warn('' + (e instanceof Error ? e.message : e)); + } } } @@ -925,7 +944,7 @@ class DatabaseMigration { root binary(32) NOT NULL, height int(10) NOT NULL, txs BLOB DEFAULT NULL, - fee_rate float unsigned NOT NULL, + fee_rate float unsigned, PRIMARY KEY (root), INDEX (height) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; @@ -959,6 +978,49 @@ class DatabaseMigration { logger.warn(`Unable to erase indexed data`); } } + + private async $convertCompactCpfpTables(): Promise { + try { + const batchSize = 250; + const maxHeight = await blocksRepository.$mostRecentBlockHeight() || 0; + const [minHeightRows]: any = await DB.query(`SELECT MIN(height) AS minHeight from cpfp_clusters`); + const minHeight = (minHeightRows.length && minHeightRows[0].minHeight != null) ? minHeightRows[0].minHeight : maxHeight; + let height = maxHeight; + + // Logging + let timer = new Date().getTime() / 1000; + const startedAt = new Date().getTime() / 1000; + + while (height > minHeight) { + const [rows] = await DB.query( + ` + SELECT * from cpfp_clusters + WHERE height <= ? AND height > ? + ORDER BY height + `, + [height, height - batchSize] + ) as RowDataPacket[][]; + if (rows?.length) { + await cpfpRepository.$batchSaveClusters(rows.map(row => { + return { + root: row.root, + height: row.height, + txs: JSON.parse(row.txs), + effectiveFeePerVsize: row.fee_rate, + }; + })); + } + + const elapsed = new Date().getTime() / 1000 - timer; + const runningFor = new Date().getTime() / 1000 - startedAt; + logger.debug(`Migrated cpfp data from block ${height} to ${height - batchSize} in ${elapsed.toFixed(2)} seconds | total elapsed: ${runningFor.toFixed(2)} seconds`); + timer = new Date().getTime() / 1000; + height -= batchSize; + } + } catch (e) { + logger.warn(`Failed to migrate cpfp transaction data`); + } + } } export default new DatabaseMigration(); From 2548d2a5e9add8f72d108c4c125b1a88e051f230 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Wed, 11 Jan 2023 08:41:00 -0600 Subject: [PATCH 10/10] fix frontend js error on unconfirmed non-cpfp transactions --- .../app/components/transaction/transaction.component.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/frontend/src/app/components/transaction/transaction.component.ts b/frontend/src/app/components/transaction/transaction.component.ts index 81ae2f055..e01092189 100644 --- a/frontend/src/app/components/transaction/transaction.component.ts +++ b/frontend/src/app/components/transaction/transaction.component.ts @@ -131,8 +131,11 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { this.cpfpInfo = null; return; } - - const relatives = [...cpfpInfo.ancestors, ...cpfpInfo.descendants || [cpfpInfo.bestDescendant]]; + // merge ancestors/descendants + const relatives = [...(cpfpInfo.ancestors || []), ...(cpfpInfo.descendants || [])]; + if (cpfpInfo.bestDescendant && !cpfpInfo.descendants?.length) { + relatives.push(cpfpInfo.bestDescendant); + } let totalWeight = this.tx.weight + relatives.reduce((prev, val) => prev + val.weight, 0);