diff --git a/asset-transfer-basic/rest-api-typescript/src/fabric.ts b/asset-transfer-basic/rest-api-typescript/src/fabric.ts index ab099842..d96abe9b 100644 --- a/asset-transfer-basic/rest-api-typescript/src/fabric.ts +++ b/asset-transfer-basic/rest-api-typescript/src/fabric.ts @@ -24,6 +24,7 @@ import { TransactionError, TransactionNotFoundError, } from './errors'; +import fabproto6 from 'fabric-protos'; export const getNetwork = async (gateway: Gateway): Promise => { const network = await gateway.getNetwork(config.channelName); @@ -289,3 +290,19 @@ export const blockEventHandler = (redis: Redis): BlockListener => { return blockListner; }; + +export const getChainInfo = async (qscc: Contract): Promise => { + try { + const data = await qscc.evaluateTransaction( + 'GetChainInfo', + config.channelName + ); + const info = fabproto6.common.BlockchainInfo.decode(data); + const blockHeight = info.height.toString(); + logger.info('Current block height: %s', blockHeight); + return true; + } catch (e) { + logger.error(e, 'Unable to get blockchain info'); + return false; + } +}; diff --git a/asset-transfer-basic/rest-api-typescript/src/server.ts b/asset-transfer-basic/rest-api-typescript/src/server.ts index e2586cba..c0b51a38 100644 --- a/asset-transfer-basic/rest-api-typescript/src/server.ts +++ b/asset-transfer-basic/rest-api-typescript/src/server.ts @@ -10,10 +10,17 @@ import pinoMiddleware from 'pino-http'; import { logger } from './logger'; import { assetsRouter } from './assets.router'; import { transactionsRouter } from './transactions.router'; -import { getContracts, getGateway, getNetwork } from './fabric'; +import { getContracts, getGateway, getNetwork, getChainInfo } from './fabric'; import { redis } from './redis'; +import { Contract } from 'fabric-network'; -const { BAD_REQUEST, INTERNAL_SERVER_ERROR, NOT_FOUND, OK } = StatusCodes; +const { + BAD_REQUEST, + INTERNAL_SERVER_ERROR, + NOT_FOUND, + OK, + SERVICE_UNAVAILABLE, +} = StatusCodes; export const createServer = async (): Promise => { const app = express(); @@ -63,12 +70,20 @@ export const createServer = async (): Promise => { timestamp: new Date().toISOString(), }) ); - app.get('/live', (_req, res) => - res.status(OK).json({ - status: getReasonPhrase(OK), - timestamp: new Date().toISOString(), - }) - ); + app.get('/live', async (_req, res) => { + const qscc: Contract = _req.app.get('contracts').qscc; + if ((await getChainInfo(qscc)) === true) { + res.status(OK).json({ + status: getReasonPhrase(OK), + timestamp: new Date().toISOString(), + }); + } else { + res.status(SERVICE_UNAVAILABLE).json({ + status: getReasonPhrase(SERVICE_UNAVAILABLE), + timestamp: new Date().toISOString(), + }); + } + }); // TODO delete me app.get('/error', (_req, _res) => {