Skip to content

Commit

Permalink
Merge "FAB-13372 Fabric-Samples return error msg"
Browse files Browse the repository at this point in the history
  • Loading branch information
mastersingh24 authored and Gerrit Code Review committed Jan 4, 2019
2 parents 461b6ab + e9b9477 commit 39e8ec4
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 139 deletions.
33 changes: 23 additions & 10 deletions balance-transfer/app/create-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,32 @@ var createChannel = async function(channelName, channelConfigPath, username, org
};

// send to orderer
var response = await client.createChannel(request)
logger.debug(' response ::%j', response);
if (response && response.status === 'SUCCESS') {
logger.debug('Successfully created the channel.');
let response = {
success: true,
message: 'Channel \'' + channelName + '\' created Successfully'
};
return response;
const result = await client.createChannel(request)
logger.debug(' result ::%j', result);
if (result) {
if (result.status === 'SUCCESS') {
logger.debug('Successfully created the channel.');
const response = {
success: true,
message: 'Channel \'' + channelName + '\' created Successfully'
};
return response;
} else {
logger.error('Failed to create the channel. status:' + result.status + ' reason:' + result.info);
const response = {
success: false,
message: 'Channel \'' + channelName + '\' failed to create status:' + result.status + ' reason:' + result.info
};
return response;
}
} else {
logger.error('\n!!!!!!!!! Failed to create the channel \'' + channelName +
'\' !!!!!!!!!\n\n');
throw new Error('Failed to create the channel \'' + channelName + '\'');
const response = {
success: false,
message: 'Failed to create the channel \'' + channelName + '\'',
};
return response;
}
} catch (err) {
logger.error('Failed to initialize the channel: ' + err.stack ? err.stack : err);
Expand Down
30 changes: 14 additions & 16 deletions balance-transfer/app/install-chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,17 @@ var installChaincode = async function(peers, chaincodeName, chaincodePath,
// lets have a look at the responses to see if they are
// all good, if good they will also include signatures
// required to be committed
var all_good = true;
for (var i in proposalResponses) {
let one_good = false;
if (proposalResponses && proposalResponses[i].response &&
proposalResponses[i].response.status === 200) {
one_good = true;
for (const i in proposalResponses) {
if (proposalResponses[i] instanceof Error) {
error_message = util.format('install proposal resulted in an error :: %s', proposalResponses[i].toString());
logger.error(error_message);
} else if (proposalResponses[i].response && proposalResponses[i].response.status === 200) {
logger.info('install proposal was good');
} else {
logger.error('install proposal was bad %j',proposalResponses.toJSON());
all_good = false;
error_message = util.format('install proposal was bad for an unknown reason %j', proposalResponses[i]);
logger.error(error_message);
}
all_good = all_good & one_good;
}
if (all_good) {
logger.info('Successfully sent install Proposal and received ProposalResponse');
} else {
error_message = 'Failed to send install Proposal or receive valid response. Response null or status is not 200'
logger.error(error_message);
}
} catch(error) {
logger.error('Failed to install due to error: ' + error.stack ? error.stack : error);
Expand All @@ -74,15 +68,19 @@ var installChaincode = async function(peers, chaincodeName, chaincodePath,
let message = util.format('Successfully installed chaincode');
logger.info(message);
// build a response to send back to the REST caller
let response = {
const response = {
success: true,
message: message
};
return response;
} else {
let message = util.format('Failed to install due to:%s',error_message);
logger.error(message);
throw new Error(message);
const response = {
success: false,
message: message
};
return response;
}
};
exports.installChaincode = installChaincode;
111 changes: 57 additions & 54 deletions balance-transfer/app/instantiate-chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,35 @@
* limitations under the License.
*/
'use strict';
var util = require('util');
var helper = require('./helper.js');
var logger = helper.getLogger('instantiate-chaincode');
const util = require('util');
const helper = require('./helper.js');
const logger = helper.getLogger('instantiate-chaincode');

var instantiateChaincode = async function(peers, channelName, chaincodeName, chaincodeVersion, functionName, chaincodeType, args, username, org_name) {
const instantiateChaincode = async function(peers, channelName, chaincodeName, chaincodeVersion, functionName, chaincodeType, args, username, org_name) {
logger.debug('\n\n============ Instantiate chaincode on channel ' + channelName +
' ============\n');
var error_message = null;

let error_message = null;
let client = null;
let channel = null;
try {
// first setup the client for this org
var client = await helper.getClientForOrg(org_name, username);
client = await helper.getClientForOrg(org_name, username);
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
var channel = client.getChannel(channelName);
channel = client.getChannel(channelName);
if(!channel) {
let message = util.format('Channel %s was not defined in the connection profile', channelName);
logger.error(message);
throw new Error(message);
}
var tx_id = client.newTransactionID(true); // Get an admin based transactionID
const tx_id = client.newTransactionID(true); // Get an admin based transactionID
// An admin based transactionID will
// indicate that admin identity should
// be used to sign the proposal request.
// will need the transaction ID string for the event registration later
var deployId = tx_id.getTransactionID();
const deployId = tx_id.getTransactionID();

// send proposal to endorser
var request = {
const request = {
targets : peers,
chaincodeId: chaincodeName,
chaincodeType: chaincodeType,
Expand Down Expand Up @@ -70,23 +71,24 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
// the returned object has both the endorsement results
// and the actual proposal, the proposal will be needed
// later when we send a transaction to the orderer
var proposalResponses = results[0];
var proposal = results[1];
const proposalResponses = results[0];
const proposal = results[1];

// lets have a look at the responses to see if they are
// all good, if good they will also include signatures
// required to be committed
var all_good = true;
for (var i in proposalResponses) {
let one_good = false;
if (proposalResponses && proposalResponses[i].response &&
proposalResponses[i].response.status === 200) {
one_good = true;
// look at the responses to see if they are all are good
// response will also include signatures required to be committed
let all_good = true;
for (const i in proposalResponses) {
if (proposalResponses[i] instanceof Error) {
all_good = false;
error_message = util.format('instantiate proposal resulted in an error :: %s', proposalResponses[i].toString());
logger.error(error_message);
} else if (proposalResponses[i].response && proposalResponses[i].response.status === 200) {
logger.info('instantiate proposal was good');
} else {
logger.error('instantiate proposal was bad');
all_good = false;
error_message = util.format('instantiate proposal was bad for an unknown reason %j', proposalResponses[i]);
logger.error(error_message);
}
all_good = all_good & one_good;
}

if (all_good) {
Expand All @@ -97,8 +99,8 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha

// wait for the channel-based event hub to tell us that the
// instantiate transaction was committed on the peer
var promises = [];
let event_hubs = channel.getChannelEventHubsForOrg();
const promises = [];
const event_hubs = channel.getChannelEventHubsForOrg();
logger.debug('found %s eventhubs for this organization %s',event_hubs.length, org_name);
event_hubs.forEach((eh) => {
let instantiateEventPromise = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -138,22 +140,22 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
promises.push(instantiateEventPromise);
});

var orderer_request = {
const orderer_request = {
txId: tx_id, // must include the transaction id so that the outbound
// transaction to the orderer will be signed by the admin
// id as was the proposal above, notice that transactionID
// generated above was based on the admin id not the current
// user assigned to the 'client' instance.
// transaction to the orderer will be signed by the admin id
// the same as the proposal above, notice that transactionID
// generated above was based on the admin id not the current
// user assigned to the 'client' instance.
proposalResponses: proposalResponses,
proposal: proposal
};
var sendPromise = channel.sendTransaction(orderer_request);
const sendPromise = channel.sendTransaction(orderer_request);
// put the send to the orderer last so that the events get registered and
// are ready for the orderering and committing
promises.push(sendPromise);
let results = await Promise.all(promises);
const results = await Promise.all(promises);
logger.debug(util.format('------->>> R E S P O N S E : %j', results));
let response = results.pop(); // orderer results are last in the results
const response = results.pop(); // orderer results are last in the results
if (response.status === 'SUCCESS') {
logger.info('Successfully sent transaction to the orderer.');
} else {
Expand All @@ -162,9 +164,9 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
}

// now see what each of the event hubs reported
for(let i in results) {
let event_hub_result = results[i];
let event_hub = event_hubs[i];
for(const i in results) {
const event_hub_result = results[i];
const event_hub = event_hubs[i];
logger.debug('Event results for event hub :%s',event_hub.getPeerAddr());
if(typeof event_hub_result === 'string') {
logger.debug(event_hub_result);
Expand All @@ -173,30 +175,31 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
logger.debug(event_hub_result.toString());
}
}
} else {
error_message = util.format('Failed to send Proposal and receive all good ProposalResponse');
logger.debug(error_message);
}
} catch (error) {
logger.error('Failed to send instantiate due to error: ' + error.stack ? error.stack : error);
error_message = error.toString();
} finally {
if (channel) {
channel.close();
}
}

if (!error_message) {
let message = util.format(
'Successfully instantiate chaincode in organization %s to the channel \'%s\'',
org_name, channelName);
logger.info(message);
// build a response to send back to the REST caller
let response = {
success: true,
message: message
};
return response;
} else {
let message = util.format('Failed to instantiate. cause:%s',error_message);
let success = true;
let message = util.format('Successfully instantiate chaincode in organization %s to the channel \'%s\'', org_name, channelName);
if (error_message) {
message = util.format('Failed to instantiate the chaincode. cause:%s',error_message);
success = false;
logger.error(message);
throw new Error(message);
} else {
logger.info(message);
}

// build a response to send back to the REST caller
const response = {
success: success,
message: message
};
return response;
};
exports.instantiateChaincode = instantiateChaincode;
Loading

0 comments on commit 39e8ec4

Please sign in to comment.