fix: use actual indexed block count for X-total-count header

This commit is contained in:
rodribp 2026-08-11 16:46:24 -06:00
parent abd8defcbb
commit a0e74fcf9e
No known key found for this signature in database
GPG key ID: DBB99FAE51E608F0
2 changed files with 17 additions and 3 deletions

View file

@ -1176,16 +1176,18 @@ class BitcoinRoutes {
const op = (req.params.op) as 'and' | 'or' | 'nor' | undefined; const op = (req.params.op) as 'and' | 'or' | 'nor' | undefined;
const mask = BigInt(req.params.mask ?? 0n); const mask = BigInt(req.params.mask ?? 0n);
const { tip } = await FlagValueRepository.$getTipAndTailIndexedByBucketSize(bucketSize) || { tip: undefined }; const { tip, tail } = await FlagValueRepository.$getTipAndTailIndexedByBucketSize(bucketSize) || { tip: undefined, tail: undefined };
if (!tip) { if (tip === undefined || tail === undefined) {
handleError(req, res, 400, `Failed to get latest indexed flag values for ${interval}`); handleError(req, res, 400, `Failed to get latest indexed flag values for ${interval}`);
return; return;
} }
const totalCount = await FlagValueRepository.$getTotalBlocksIndexedByBucketSize(bucketSize === 1 ? 1008 : bucketSize) ?? tip - tail;
const startHeight = presets[interval].retentionSpan !== -1 ? (tip - presets[interval].retentionSpan) : -1; const startHeight = presets[interval].retentionSpan !== -1 ? (tip - presets[interval].retentionSpan) : -1;
const txsCount = await FlagValueRepository.$queryTxCountBasedOnMask(mask, bucketSize, op, startHeight); const txsCount = await FlagValueRepository.$queryTxCountBasedOnMask(mask, bucketSize, op, startHeight);
res.header('X-total-count', tip.toString()); res.header('X-total-count', totalCount.toString());
res.header('Expires', new Date(Date.now() + 1000 * 3600 * 24 * (presets[interval].bucketSizes[0] / 144)).toUTCString()); res.header('Expires', new Date(Date.now() + 1000 * 3600 * 24 * (presets[interval].bucketSizes[0] / 144)).toUTCString());
res.send(txsCount); res.send(txsCount);
} catch (e: any) { } catch (e: any) {

View file

@ -128,6 +128,18 @@ class FlagValuesRepository {
logger.err(`Cannot delete flag values above ${height}. Reason: ` + (e instanceof Error ? e.message : e)); logger.err(`Cannot delete flag values above ${height}. Reason: ` + (e instanceof Error ? e.message : e));
} }
} }
public async $getTotalBlocksIndexedByBucketSize(bucketSize: number): Promise<number | null> {
try {
const [rows]: any[] = await DB.query(`SELECT (count(distinct start_height) * ?) as total FROM flag_values WHERE bucket_size = ?`, [bucketSize, bucketSize.toString()]);
if (rows !== null && rows.length > 0) {
return rows[0].total;
}
} catch (e) {
logger.err(`Cannot get total blocks indexed in flag_values. Reason: ` + (e instanceof Error ? e.message : e));
}
return null;
}
} }
export default new FlagValuesRepository(); export default new FlagValuesRepository();