Fix version update & error handling in Goggles indexing

This commit is contained in:
Mononaut 2024-01-23 17:18:47 +00:00
parent 58f143f867
commit 8ca2b2b5c0
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E
3 changed files with 57 additions and 50 deletions

View File

@ -606,55 +606,59 @@ class Blocks {
logger.debug(`Classifying blocks and templates from #${currentBlockHeight} to #${minHeight}`, logger.tags.goggles); logger.debug(`Classifying blocks and templates from #${currentBlockHeight} to #${minHeight}`, logger.tags.goggles);
for (let height = currentBlockHeight; height >= 0; height--) { for (let height = currentBlockHeight; height >= 0; height--) {
let txs: TransactionExtended[] | null = null; try {
if (unclassifiedBlocks[height]) { let txs: TransactionExtended[] | null = null;
const blockHash = unclassifiedBlocks[height]; if (unclassifiedBlocks[height]) {
// fetch transactions const blockHash = unclassifiedBlocks[height];
txs = (await bitcoinApi.$getTxsForBlock(blockHash)).map(tx => transactionUtils.extendTransaction(tx)); // fetch transactions
// add CPFP txs = (await bitcoinApi.$getTxsForBlock(blockHash)).map(tx => transactionUtils.extendTransaction(tx));
const cpfpSummary = Common.calculateCpfp(height, txs, true); // add CPFP
// classify const cpfpSummary = Common.calculateCpfp(height, txs, true);
const { transactions: classifiedTxs } = this.summarizeBlockTransactions(blockHash, cpfpSummary.transactions);
BlocksSummariesRepository.$saveTransactions(height, blockHash, classifiedTxs, 1);
}
if (unclassifiedTemplates[height]) {
// classify template
const blockHash = unclassifiedTemplates[height];
const template = await BlocksSummariesRepository.$getTemplate(blockHash);
const alreadyClassified = template?.transactions.reduce((classified, tx) => (classified || tx.flags > 0), false);
let classifiedTemplate = template?.transactions || [];
if (!alreadyClassified) {
const templateTxs: (TransactionExtended | TransactionClassified)[] = [];
const blockTxMap: { [txid: string]: TransactionExtended } = {};
for (const tx of (txs || [])) {
blockTxMap[tx.txid] = tx;
}
for (const templateTx of (template?.transactions || [])) {
let tx: TransactionExtended | null = blockTxMap[templateTx.txid];
if (!tx) {
try {
tx = await transactionUtils.$getTransactionExtended(templateTx.txid, false, true, false);
} catch (e) {
// transaction probably not found
}
}
templateTxs.push(tx || templateTx);
}
const cpfpSummary = Common.calculateCpfp(height, txs?.filter(tx => tx.effectiveFeePerVsize != null) as TransactionExtended[], true);
// classify // classify
const { transactions: classifiedTxs } = this.summarizeBlockTransactions(blockHash, cpfpSummary.transactions); const { transactions: classifiedTxs } = this.summarizeBlockTransactions(blockHash, cpfpSummary.transactions);
const classifiedTxMap: { [txid: string]: TransactionClassified } = {}; await BlocksSummariesRepository.$saveTransactions(height, blockHash, classifiedTxs, 1);
for (const tx of classifiedTxs) {
classifiedTxMap[tx.txid] = tx;
}
classifiedTemplate = classifiedTemplate.map(tx => {
if (classifiedTxMap[tx.txid]) {
tx.flags = classifiedTxMap[tx.txid].flags || 0;
}
return tx;
});
} }
BlocksSummariesRepository.$saveTemplate({ height, template: { id: blockHash, transactions: classifiedTemplate }, version: 1 }); if (unclassifiedTemplates[height]) {
// classify template
const blockHash = unclassifiedTemplates[height];
const template = await BlocksSummariesRepository.$getTemplate(blockHash);
const alreadyClassified = template?.transactions.reduce((classified, tx) => (classified || tx.flags > 0), false);
let classifiedTemplate = template?.transactions || [];
if (!alreadyClassified) {
const templateTxs: (TransactionExtended | TransactionClassified)[] = [];
const blockTxMap: { [txid: string]: TransactionExtended } = {};
for (const tx of (txs || [])) {
blockTxMap[tx.txid] = tx;
}
for (const templateTx of (template?.transactions || [])) {
let tx: TransactionExtended | null = blockTxMap[templateTx.txid];
if (!tx) {
try {
tx = await transactionUtils.$getTransactionExtended(templateTx.txid, false, true, false);
} catch (e) {
// transaction probably not found
}
}
templateTxs.push(tx || templateTx);
}
const cpfpSummary = Common.calculateCpfp(height, txs?.filter(tx => tx.effectiveFeePerVsize != null) as TransactionExtended[], true);
// classify
const { transactions: classifiedTxs } = this.summarizeBlockTransactions(blockHash, cpfpSummary.transactions);
const classifiedTxMap: { [txid: string]: TransactionClassified } = {};
for (const tx of classifiedTxs) {
classifiedTxMap[tx.txid] = tx;
}
classifiedTemplate = classifiedTemplate.map(tx => {
if (classifiedTxMap[tx.txid]) {
tx.flags = classifiedTxMap[tx.txid].flags || 0;
}
return tx;
});
}
await BlocksSummariesRepository.$saveTemplate({ height, template: { id: blockHash, transactions: classifiedTemplate }, version: 1 });
}
} catch (e) {
logger.warn(`Failed to classify template or block summary at ${height}`, logger.tags.goggles);
} }
// timing & logging // timing & logging

View File

@ -281,6 +281,7 @@ export interface BlockExtended extends IEsploraApi.Block {
export interface BlockSummary { export interface BlockSummary {
id: string; id: string;
transactions: TransactionClassified[]; transactions: TransactionClassified[];
version?: number;
} }
export interface AuditSummary extends BlockAudit { export interface AuditSummary extends BlockAudit {

View File

@ -23,8 +23,8 @@ class BlocksSummariesRepository {
await DB.query(` await DB.query(`
INSERT INTO blocks_summaries INSERT INTO blocks_summaries
SET height = ?, transactions = ?, id = ?, version = ? SET height = ?, transactions = ?, id = ?, version = ?
ON DUPLICATE KEY UPDATE transactions = ?`, ON DUPLICATE KEY UPDATE transactions = ?, version = ?`,
[blockHeight, transactionsStr, blockId, version, transactionsStr]); [blockHeight, transactionsStr, blockId, version, transactionsStr, version]);
} catch (e: any) { } catch (e: any) {
logger.debug(`Cannot save block summary transactions for ${blockId}. Reason: ${e instanceof Error ? e.message : e}`); logger.debug(`Cannot save block summary transactions for ${blockId}. Reason: ${e instanceof Error ? e.message : e}`);
throw e; throw e;
@ -39,8 +39,9 @@ class BlocksSummariesRepository {
INSERT INTO blocks_templates (id, template, version) INSERT INTO blocks_templates (id, template, version)
VALUE (?, ?, ?) VALUE (?, ?, ?)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
template = ? template = ?,
`, [blockId, transactions, params.version, transactions]); version = ?
`, [blockId, transactions, params.version, transactions, params.version]);
} catch (e: any) { } catch (e: any) {
if (e.errno === 1062) { // ER_DUP_ENTRY - This scenario is possible upon node backend restart if (e.errno === 1062) { // ER_DUP_ENTRY - This scenario is possible upon node backend restart
logger.debug(`Cannot save block template for ${blockId} because it has already been indexed, ignoring`); logger.debug(`Cannot save block template for ${blockId} because it has already been indexed, ignoring`);
@ -57,6 +58,7 @@ class BlocksSummariesRepository {
return { return {
id: templates[0].id, id: templates[0].id,
transactions: JSON.parse(templates[0].template), transactions: JSON.parse(templates[0].template),
version: templates[0].version,
}; };
} }
} catch (e) { } catch (e) {