Adding getBlocks support.

This commit is contained in:
softsimon 2020-12-29 20:41:16 +07:00
parent ae87694bc3
commit 62c78f5b08
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
2 changed files with 35 additions and 1 deletions

View File

@ -214,6 +214,7 @@ class Server {
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId', routes.getTransaction)
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/outspends', routes.getTransactionOutspends)
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash', routes.getBlock)
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/:height', routes.getBlocks)
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks', routes.getBlocks)
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txs/:index', routes.getBlockTransactions)
.get(config.MEMPOOL.API_URL_PREFIX + 'block-height/:height', routes.getBlockHeight)

View File

@ -10,10 +10,12 @@ import websocketHandler from './api/websocket-handler';
import bisqMarket from './api/bisq/markets-api';
import { OptimizedStatistic, RequiredSpec, TransactionExtended } from './mempool.interfaces';
import { MarketsApiError } from './api/bisq/interfaces';
import { IEsploraApi } from './api/bitcoin/esplora-api.interface';
import donations from './api/donations';
import logger from './logger';
import bitcoinApi from './api/bitcoin/bitcoin-api-factory';
import transactionUtils from './api/transaction-utils';
import blocks from './api/blocks';
class Routes {
private cache: { [date: string]: OptimizedStatistic[] } = {
@ -550,7 +552,38 @@ class Routes {
}
public async getBlocks(req: Request, res: Response) {
res.status(404).send('Not implemented');
try {
const returnBlocks: IEsploraApi.Block[] = [];
const latestBlockHeight = blocks.getCurrentBlockHeight();
const fromHeight = parseInt(req.params.height, 10) || latestBlockHeight;
const localBlocks = blocks.getBlocks();
// See if block hight exist in local cache to skip the hash lookup
const blockByHeight = localBlocks.find((b) => b.height === fromHeight);
let startFromHash: string | null = null;
if (blockByHeight) {
startFromHash = blockByHeight.id;
} else {
startFromHash = await bitcoinApi.$getBlockHash(fromHeight);
}
let nextHash = startFromHash;
for (let i = 0; i < 10; i++) {
const localBlock = localBlocks.find((b) => b.id === nextHash);
if (localBlock) {
returnBlocks.push(localBlock);
nextHash = localBlock.previousblockhash;
} else {
const block = await bitcoinApi.$getBlock(nextHash);
returnBlocks.push(block);
nextHash = block.previousblockhash;
}
}
res.json(returnBlocks);
} catch (e) {
res.status(500).send(e.message);
}
}
public async getBlockTransactions(req: Request, res: Response) {