Compare commits

...

9 Commits

Author SHA1 Message Date
nymkappa
20d0df9d5c
Merge branch 'master' into nymkappa/health-check 2024-10-30 13:24:14 +01:00
nymkappa
cef6127e69
Merge branch 'master' into nymkappa/health-check 2024-09-06 10:10:36 +02:00
nymkappa
6d945890f4
[health api] move into /internal 2024-04-07 20:38:01 +09:00
nymkappa
5922c616df
Merge branch 'master' into nymkappa/health-check 2024-04-07 20:34:47 +09:00
nymkappa
14d8f67878
Merge branch 'master' into nymkappa/health-check 2024-03-25 10:45:28 +09:00
nymkappa
f80c0738b2
[health api] add indexing progress 2023-10-02 11:08:10 +02:00
nymkappa
545b3e7325
[health api] better error handling 2023-10-02 10:51:54 +02:00
nymkappa
3c23e3ff84
[health api] replace space with _ | only add esplora if enabled 2023-09-26 16:44:08 +02:00
nymkappa
8a51f32e63
[nodejs backend] added /api/v1/health 2023-09-26 16:36:29 +02:00
2 changed files with 138 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import websocketHandler from '../websocket-handler';
import mempool from '../mempool';
import feeApi from '../fee-api';
import mempoolBlocks from '../mempool-blocks';
import bitcoinApi from './bitcoin-api-factory';
import bitcoinApi, { bitcoinCoreApi } from './bitcoin-api-factory';
import { Common } from '../common';
import backendInfo from '../backend-info';
import transactionUtils from '../transaction-utils';
@ -21,6 +21,7 @@ import transactionRepository from '../../repositories/TransactionRepository';
import rbfCache from '../rbf-cache';
import { calculateMempoolTxCpfp } from '../cpfp';
import { handleError } from '../../utils/api';
import BlocksRepository from '../../repositories/BlocksRepository';
class BitcoinRoutes {
public initRoutes(app: Application) {
@ -80,8 +81,87 @@ class BitcoinRoutes {
.get(config.MEMPOOL.API_URL_PREFIX + 'address-prefix/:prefix', this.getAddressPrefix)
;
}
app.get('/api/internal/health', this.generateHealthReport);
}
private async generateHealthReport(req: Request, res: Response): Promise<void> {
let response = {
core: {
height: -1
},
mempool: {
height: -1,
indexing: {
enabled: Common.indexingEnabled(),
blocks: {
count: -1,
progress: -1,
withCpfp: {
count: -1,
progress: -1,
},
withCoinStats: {
count: -1,
progress: -1,
}
},
}
},
};
try {
// Bitcoin Core
let bitcoinCoreIndexes: number | string;
try {
bitcoinCoreIndexes = await bitcoinClient.getIndexInfo();
for (const indexName in bitcoinCoreIndexes as any) {
response.core[indexName.replace(/ /g,'_')] = bitcoinCoreIndexes[indexName];
}
} catch (e: any) {
response.core['error'] = e.message;
}
try {
response.core.height = await bitcoinCoreApi.$getBlockHeightTip();
} catch (e: any) {
response.core['error'] = e.message;
}
// Mempool
response.mempool.height = blocks.getCurrentBlockHeight();
if (Common.indexingEnabled()) {
const indexingBlockAmount = (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT === -1 ? response.core.height : config.MEMPOOL.INDEXING_BLOCKS_AMOUNT);
const computeProgress = (count: number): number => Math.min(1.0, Math.round(count / indexingBlockAmount * 100) / 100);
response.mempool.indexing.blocks.count = await BlocksRepository.$getIndexedBlockCount();
response.mempool.indexing.blocks.progress = computeProgress(response.mempool.indexing.blocks.count);
response.mempool.indexing.blocks.withCpfp.count = await BlocksRepository.$getIndexedCpfpBlockCount();
response.mempool.indexing.blocks.withCpfp.progress = computeProgress(response.mempool.indexing.blocks.withCpfp.count);
response.mempool.indexing.blocks.withCoinStats.count = await BlocksRepository.$getIndexedCoinStatsBlockCount();
response.mempool.indexing.blocks.withCoinStats.progress = computeProgress(response.mempool.indexing.blocks.withCoinStats.count);
}
// Esplora
if (config.MEMPOOL.BACKEND === 'esplora') {
try {
response['esplora'] = {
height: await bitcoinApi.$getBlockHeightTip()
};
} catch (e: any) {
response['esplora'] = {
height: -1,
error: e.message
};
}
}
res.json(response);
} catch (e: any) {
logger.err(`Unable to generate health report. Exception: ${JSON.stringify(e)}`);
logger.err(e.stack);
res.status(500).send(e instanceof Error ? e.message : e);
}
}
private getInitData(req: Request, res: Response) {
try {

View File

@ -391,7 +391,7 @@ class BlocksRepository {
/**
* Get blocks count for a period
*/
public async $blockCountBetweenHeight(startHeight: number, endHeight: number): Promise<number> {
public async $blockCountBetweenHeight(startHeight: number, endHeight: number): Promise<number> {
const params: any[] = [];
let query = `SELECT count(height) as blockCount
FROM blocks
@ -729,7 +729,7 @@ class BlocksRepository {
/**
* Get the historical averaged block fee rate percentiles
*/
public async $getHistoricalBlockFeeRates(div: number, interval: string | null): Promise<any> {
public async $getHistoricalBlockFeeRates(div: number, interval: string | null): Promise<any> {
try {
let query = `SELECT
CAST(AVG(height) as INT) as avgHeight,
@ -760,7 +760,7 @@ class BlocksRepository {
/**
* Get the historical averaged block sizes
*/
public async $getHistoricalBlockSizes(div: number, interval: string | null): Promise<any> {
public async $getHistoricalBlockSizes(div: number, interval: string | null): Promise<any> {
try {
let query = `SELECT
CAST(AVG(height) as INT) as avgHeight,
@ -785,7 +785,7 @@ class BlocksRepository {
/**
* Get the historical averaged block weights
*/
public async $getHistoricalBlockWeights(div: number, interval: string | null): Promise<any> {
public async $getHistoricalBlockWeights(div: number, interval: string | null): Promise<any> {
try {
let query = `SELECT
CAST(AVG(height) as INT) as avgHeight,
@ -823,7 +823,7 @@ class BlocksRepository {
/**
* Get a list of blocks that have not had CPFP data indexed
*/
public async $getCPFPUnindexedBlocks(): Promise<number[]> {
public async $getCPFPUnindexedBlocks(): Promise<number[]> {
try {
const blockchainInfo = await bitcoinClient.getBlockchainInfo();
const currentBlockHeight = blockchainInfo.blocks;
@ -893,7 +893,7 @@ class BlocksRepository {
/**
* Save block price by batch
*/
public async $saveBlockPrices(blockPrices: BlockPrice[]): Promise<void> {
public async $saveBlockPrices(blockPrices: BlockPrice[]): Promise<void> {
try {
let query = `INSERT INTO blocks_prices(height, price_id) VALUES`;
for (const price of blockPrices) {
@ -1153,6 +1153,57 @@ class BlocksRepository {
blk.extras = <BlockExtension>extras;
return <BlockExtended>blk;
}
/**
* Count how many blocks are indexed
*/
public async $getIndexedBlockCount(): Promise<number> {
try {
const [res]: any[] = await DB.query(`SELECT COUNT(hash) as count FROM blocks`);
if (!res || !res.length) {
logger.err(`Unable to count indexed blocks in our db`);
return -1;
}
return res[0].count;
} catch (e) {
logger.err(`Unable to count indexed blocks in our db. Exception: ${JSON.stringify(e)}`);
return -1;
}
}
/**
* Count how many blocks are indexed with CPFP data
*/
public async $getIndexedCpfpBlockCount(): Promise<number> {
try {
const [res]: any[] = await DB.query(`SELECT COUNT(DISTINCT height) as count FROM compact_cpfp_clusters`);
if (!res || !res.length) {
logger.err(`Unable to count indexed blocks with CPFP data in our db`);
return -1;
}
return res[0].count;
} catch (e) {
logger.err(`Unable to count indexed blocks with CPFP data in our db. Exception: ${JSON.stringify(e)}`);
return -1;
}
}
/**
* Count how many blocks are indexed with coin stats data
*/
public async $getIndexedCoinStatsBlockCount(): Promise<number> {
try {
const [res]: any[] = await DB.query(`SELECT COUNT(hash) as count FROM blocks WHERE utxoset_size IS NOT NULL && total_input_amt IS NOT NULL`);
if (!res || !res.length) {
logger.err(`Unable to count indexed blocks with coin stats data in our db`);
return -1;
}
return res[0].count;
} catch (e) {
logger.err(`Unable to count indexed blocks with coin stats data in our db. Exception: ${JSON.stringify(e)}`);
return -1;
}
}
}
export default new BlocksRepository();