[blocks] return list of block hash filtered by definition hash
This commit is contained in:
parent
ac997f3d9e
commit
4ff2aad94a
@ -47,13 +47,15 @@ class BitcoinRoutes {
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/audit-summary', this.getBlockAuditSummary)
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/tx/:txid/audit', this.$getBlockTxAuditSummary)
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/height', this.getBlockTipHeight)
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/definition/list', this.getBlockDefinitionHashes)
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/definition/current', this.getCurrentBlockDefinitionHash)
|
||||
.post(config.MEMPOOL.API_URL_PREFIX + 'psbt/addparents', this.postPsbtCompletion)
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks-bulk/:from', this.getBlocksByBulk.bind(this))
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks-bulk/:from/:to', this.getBlocksByBulk.bind(this))
|
||||
// Temporarily add txs/package endpoint for all backends until esplora supports it
|
||||
.post(config.MEMPOOL.API_URL_PREFIX + 'txs/package', this.$submitPackage)
|
||||
// Internal routes
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'internal/blocks/definition/list', this.getBlockDefinitionHashes)
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'internal/blocks/definition/current', this.getCurrentBlockDefinitionHash)
|
||||
.get(config.MEMPOOL.API_URL_PREFIX + 'internal/blocks/:definitionHash', this.getBlocksByDefinitionHash)
|
||||
;
|
||||
|
||||
if (config.MEMPOOL.BACKEND !== 'esplora') {
|
||||
@ -660,7 +662,7 @@ class BitcoinRoutes {
|
||||
}
|
||||
}
|
||||
|
||||
private async getBlockDefinitionHashes(req: Request, res: Response) {
|
||||
private async getBlockDefinitionHashes(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const result = await blocks.$getBlockDefinitionHashes();
|
||||
if (!result) {
|
||||
@ -674,7 +676,7 @@ class BitcoinRoutes {
|
||||
}
|
||||
}
|
||||
|
||||
private async getCurrentBlockDefinitionHash(req: Request, res: Response) {
|
||||
private async getCurrentBlockDefinitionHash(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const currentSha = await poolsUpdater.getShaFromDb();
|
||||
if (!currentSha) {
|
||||
@ -688,6 +690,24 @@ class BitcoinRoutes {
|
||||
}
|
||||
}
|
||||
|
||||
private async getBlocksByDefinitionHash(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
if (typeof(req.params.definitionHash) !== 'string') {
|
||||
res.status(400).send('Parameter "hash" must be a valid string');
|
||||
return;
|
||||
}
|
||||
const blocksHash = await blocks.$getBlocksByDefinitionHash(req.params.definitionHash as string);
|
||||
if (!blocksHash) {
|
||||
handleError(req, res, 503, `Service Temporarily Unavailable`);
|
||||
return;
|
||||
}
|
||||
res.setHeader('content-type', 'application/json');
|
||||
res.send(blocksHash);
|
||||
} catch (e) {
|
||||
handleError(req, res, 500, e instanceof Error ? e.message : e);
|
||||
}
|
||||
}
|
||||
|
||||
private getBlockTipHeight(req: Request, res: Response) {
|
||||
try {
|
||||
const result = blocks.getCurrentBlockHeight();
|
||||
|
@ -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 [];
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user