fix missing canonical hash on stale block responses

This commit is contained in:
mononaut 2026-06-17 11:59:54 +00:00 committed by rodribp
parent 2db57928c4
commit f654704169
No known key found for this signature in database
GPG key ID: CC349739DA92EE20

View file

@ -575,6 +575,31 @@ class BlocksRepository {
}
}
/**
* Get the canonical block hash at a given height
* @asyncSafe
*/
public async $getCanonicalBlockHashByHeight(height: number): Promise<string | null> {
try {
const [rows]: any[] = await DB.query(`
SELECT hash
FROM blocks
WHERE height = ? AND stale = 0
LIMIT 1`,
[height]
);
if (rows.length <= 0) {
return null;
}
return rows[0].hash;
} catch (e) {
logger.err(`Cannot get canonical block hash at height ${height}. Reason: ` + (e instanceof Error ? e.message : e));
throw e;
}
}
/**
* Get one block by hash
* @asyncSafe
@ -1258,6 +1283,9 @@ class BlocksRepository {
blk.mediantime = dbBlk.mediantime;
blk.indexVersion = dbBlk.index_version;
blk.stale = dbBlk.stale;
if (dbBlk.stale) {
blk.canonical = await this.$getCanonicalBlockHashByHeight(dbBlk.height) || undefined;
}
// BlockExtension
extras.totalFees = dbBlk.totalFees;
extras.medianFee = dbBlk.medianFee;