SHARE
TWEET

Untitled

a guest Mar 24th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copyright 2019 KT All Rights Reserved.
  3.  */
  4.  
  5. const log4js = require('log4js');
  6.  
  7. const logger = log4js.getLogger('Manager');
  8. logger.setLevel('DEBUG');
  9.  
  10. const util = require('util');
  11. const hfc = require('fabric-client');
  12.  
  13. hfc.setLogger(logger);
  14.  
  15. async function sendTransactionProposal(channelMap, chainCodeName, fcn, args) {
  16.   const channel = channelMap.get('channel');
  17.   const txId = channel._clientContext.newTransactionID();
  18.  
  19.   const request = {
  20.     chaincodeId: chainCodeName,
  21.     fcn,
  22.     args,
  23.     chainId: channel._name,
  24.     txId,
  25.   };
  26.   logger.debug(`request:${JSON.stringify(request)}`);
  27.  
  28.   let results = null;
  29.   try {
  30.     results = await channel.sendTransactionProposal(request);
  31.   } catch (e) {
  32.     throw new Error(`error occurred during transaction proposal ${e.toString()}`);
  33.   }
  34.  
  35.   const proposalResult = checkError(results[0]);
  36.  
  37.   if (proposalResult) {
  38.     const responseMap = new Map();
  39.     responseMap.set('txId', txId);
  40.     responseMap.set('proposalResponses', results[0]);
  41.     responseMap.set('proposal', results[1]);
  42.     responseMap.set('channel', channel);
  43.     responseMap.set('eventHubs', channelMap.get('eventHubs'));
  44.     return responseMap;
  45.   }
  46.   return null;
  47. }
  48. exports.sendTransactionProposal = sendTransactionProposal;
  49.  
  50. async function sendTransaction(parameters) {
  51.   const trxRequest = {
  52.     txId: parameters.get('txId'),
  53.     proposalResponses: parameters.get('proposalResponses'),
  54.     proposal: parameters.get('proposal'),
  55.   };
  56.  
  57.   const promises = await buildTransactionPromises(parameters.get('eventHubs'), parameters.get('txId'));
  58.   const sendPromise = parameters.get('channel').sendTransaction(trxRequest);
  59.   promises.push(sendPromise);
  60.  
  61.   const results = await Promise.all(promises);
  62.   const response = results.pop();
  63.   if (response.status === 'SUCCESS') {
  64.     logger.info('Successfully sent transaction to the orderer.');
  65.   } else {
  66.     throw new Error(util.format('Failed to order the transaction. Error code: %s', response.status));
  67.   }
  68. }
  69. exports.sendTransaction = sendTransaction;
  70.  
  71. async function buildTransactionPromises(eventHubs, txId) {
  72.   const promises = [];
  73.   eventHubs.forEach(eh => {
  74.     logger.debug('invokeEventPromise - setting up event');
  75.     const invokeEventPromise = new Promise((resolve, reject) => {
  76.       const ehTimeOut = setTimeout(() => {
  77.         const message = `REQUEST_TIMEOUT:${eh.getPeerAddr()}`;
  78.         logger.error(message);
  79.       }, 3000);
  80.       eh.registerTxEvent(txId.getTransactionID(), (tx, code, blockNum) => {
  81.         logger.info('The chain code invoke chain code transaction has been committed on peer %s', eh.getPeerAddr());
  82.         logger.info('Transaction %s has status of %s in blocl %s', tx, code, blockNum);
  83.         clearTimeout(ehTimeOut);
  84.  
  85.         if (code !== 'VALID') {
  86.           const message = util.format('The invoke chain code transaction was invalid, code:%s', code);
  87.           logger.error(message);
  88.           reject(new Error(message));
  89.         } else {
  90.           const message = 'The invoke chain code transaction was valid.';
  91.           logger.info(message);
  92.           resolve(message);
  93.         }
  94.       }, err => {
  95.         clearTimeout(ehTimeOut);
  96.         logger.error(err);
  97.         reject(err);
  98.       },
  99.       { unregister: true, disconnect: true },
  100.       );
  101.     });
  102.     promises.push(invokeEventPromise);
  103.   });
  104.  
  105.   return promises;
  106. }
  107. exports.buildTransactionPromises = buildTransactionPromises;
  108.  
  109. function checkError(proposalResponses) {
  110.   let allGood = true;
  111.   for (const i in proposalResponses) {
  112.     let oneGood = false;
  113.     if (proposalResponses && proposalResponses[i].response &&
  114.             proposalResponses[i].response.status === 200) {
  115.       oneGood = true;
  116.       logger.info('invoke chaincode proposal was good');
  117.     } else {
  118.       logger.error(proposalResponses[i]);
  119.       logger.error('invoke chaincode proposal was bad');
  120.     }
  121.     allGood &= oneGood;
  122.   }
  123.  
  124.   return allGood;
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top