[blocks] return list of block hash filtered by definition hash

This commit is contained in:
nymkappa
2024-11-29 17:55:06 +01:00
parent ac997f3d9e
commit 4ff2aad94a
2 changed files with 46 additions and 9 deletions

View File

@@ -1463,17 +1463,34 @@ class Blocks {
}
}
public async $getBlockDefinitionHashes(): Promise<string[]> {
public async $getBlockDefinitionHashes(): Promise<string[] | null> {
try {
const [rows]: any = await database.query(`SELECT DISTINCT(definition_hash) FROM blocks`);
if (rows && rows.length) {
if (rows && Array.isArray(rows)) {
return rows.map(r => r.definition_hash);
} else {
logger.debug(`Unable to retreive list of blocks.definition_hash from db (no result)`);
return null;
}
} catch (e) {
// we just return an empty array
logger.debug(`Unable to retreive list of blocks.definition_hash from db (exception: ${e})`);
return null;
}
}
public async $getBlocksByDefinitionHash(definitionHash: string): Promise<string[] | null> {
try {
const [rows]: any = await database.query(`SELECT hash FROM blocks WHERE definition_hash = ?`, [definitionHash]);
if (rows && Array.isArray(rows)) {
return rows.map(r => r.hash);
} else {
logger.debug(`Unable to retreive list of blocks for definition hash ${definitionHash} from db (no result)`);
return null;
}
} catch (e) {
logger.debug(`Unable to retreive list of blocks for definition hash ${definitionHash} from db (exception: ${e})`);
return null;
}
logger.debug(`Unable to retreive list of blocks.definition_hash from db`);
return [];
}
}