add cpfp progress marker to avoid reindexing early blocks

This commit is contained in:
Mononaut 2023-01-09 10:25:25 -06:00
parent b50936f001
commit 01c96f80f9
No known key found for this signature in database
GPG key ID: A3F058E41374C04E
2 changed files with 31 additions and 1 deletions

View file

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

View file

@ -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<void> {
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);