Added blockEvent handler

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
sapthasurendran 2021-07-26 12:38:52 +05:30 committed by James Taylor
parent 804f4a6468
commit 550e95f091
3 changed files with 28 additions and 6 deletions

View file

@ -12,7 +12,10 @@ import {
TxEventHandler,
TxEventHandlerFactory,
Wallets,
Network
Network,
BlockListener,
BlockEvent,
TransactionEvent,
} from 'fabric-network';
import { Redis } from 'ioredis';
import * as config from './config';
@ -25,7 +28,6 @@ import {
TransactionNotFoundError,
} from './errors';
export const getNetwork = async (gateway: Gateway): Promise<Network> => {
const network = await gateway.getNetwork(config.channelName);
return network;
@ -354,3 +356,21 @@ const isDuplicateTransaction = (error: {
return false;
};
export const blockEventHandler = (redis: Redis): BlockListener => {
const blockListner = async (event: BlockEvent) => {
logger.debug('Block event received ');
const transEvents: Array<TransactionEvent> = event.getTransactionEvents();
for (const transEvent of transEvents) {
if (transEvent && transEvent.isValid) {
logger.debug(
'Remove transation with txnId %s',
transEvent.transactionId
);
await clearTransactionDetails(redis, transEvent.transactionId);
}
}
};
return blockListner;
};

View file

@ -2,10 +2,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { Contract } from 'fabric-network';
import { Contract, Network } from 'fabric-network';
import { Redis } from 'ioredis';
import * as config from './config';
import { startRetryLoop } from './fabric';
import { startRetryLoop, blockEventHandler } from './fabric';
import { logger } from './logger';
import { createServer } from './server';
@ -14,6 +14,8 @@ async function main() {
const contract: Contract = app.get('contracts').contract;
const redis: Redis = app.get('redis');
const network: Network = app.get('network');
await network.addBlockListener(blockEventHandler(redis));
startRetryLoop(contract, redis);
app.listen(config.port, () => {

View file

@ -51,10 +51,10 @@ export const createServer = async (): Promise<Application> => {
const gateway = await getGateway();
const contracts = await getContracts(gateway);
const network = await getNetwork(gateway)
const network = await getNetwork(gateway);
app.set('contracts', contracts);
app.set('redis', redis);
app.set('network',network)
app.set('network', network);
// Health routes
app.get('/ready', (_req, res) =>