used fabric protos insted of blockdecoder

changed import statement

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>

revert back eslintrc

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>

function name change

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>

format changes

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
sapthasurendran 2021-07-23 16:15:27 +05:30 committed by James Taylor
parent fc769cfcb0
commit d4318c381a
2 changed files with 40 additions and 8 deletions

View file

@ -24,6 +24,7 @@ import {
TransactionError,
TransactionNotFoundError,
} from './errors';
import fabproto6 from 'fabric-protos';
export const getNetwork = async (gateway: Gateway): Promise<Network> => {
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<boolean> => {
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;
}
};

View file

@ -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<Application> => {
const app = express();
@ -63,12 +70,20 @@ export const createServer = async (): Promise<Application> => {
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) => {