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);