Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// select a target peer for the chaincode to see if it's instantiated
// these are the same as the install targets, so if one of the peers has already instantiated the chaincode,
// then the other targets also had done the same
let org = this.networkUtil.getOrganizationOfPeer(targetPeers[0]);
let admin = this.adminProfiles.get(org);
/** @link{ChaincodeQueryResponse} */
let queryResponse;
try {
queryResponse = await admin.getChannel(channel, true).queryInstantiatedChaincodes(targetPeers[0], true);
} catch (err) {
throw new Error(`Couldn't query whether ${chaincodeInfo.id}@${chaincodeInfo.version} is instantiated on ${targetPeers[0]}: ${err.message}`);
}
CaliperUtils.assertDefined(queryResponse);
CaliperUtils.assertProperty(queryResponse, 'queryResponse', 'chaincodes');
if (queryResponse.chaincodes.some(
cc => cc.name === chaincodeInfo.id && cc.version === chaincodeInfo.version)) {
logger.info(`${chaincodeInfo.id}@${chaincodeInfo.version} is already instantiated in ${channel}`);
continue;
}
chaincodeInstantiated = true;
let txId = admin.newTransactionID(true);
/** @link{ChaincodeInstantiateUpgradeRequest} */
let request = {
targets: targetPeers,
chaincodeId: ccObject.id,
chaincodeVersion: ccObject.version,
chaincodeType: ccObject.language,
let ordererRequest = {
txId: txId,
proposalResponses: proposalResponses,
proposal: proposal
};
/** @link{BroadcastResponse} */
let broadcastResponse;
try {
broadcastResponse = await admin.getChannel(channel, true).sendTransaction(ordererRequest);
} catch (err) {
throw new Error(`Orderer error for instantiating ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel}: ${err.message}`);
}
CaliperUtils.assertDefined(broadcastResponse);
CaliperUtils.assertProperty(broadcastResponse, 'broadcastResponse', 'status');
if (broadcastResponse.status !== 'SUCCESS') {
throw new Error(`Orderer error for instantiating ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel}: ${broadcastResponse.status}`);
}
// since every event promise is resolved, this shouldn't throw an error
let eventResults = await Promise.all(eventPromises);
// if we received an error, propagate it
if (eventResults.some(er => er instanceof Error)) {
let errMsg = `The following errors occured while instantiating ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel}:`;
let err; // keep the last error
for (let eventResult of eventResults) {
if (eventResult instanceof Error) {
err = eventResult;
errMsg += `\n\t- ${eventResult.message}`;
proposalResponses.forEach((propResponse, index) => {
if (propResponse instanceof Error) {
let errMsg = `Install proposal error for ${chaincodeInfo.id}@${chaincodeInfo.version} on ${orgPeerTargets[index]}: ${propResponse.message}`;
errors.push(new Error(errMsg));
return;
}
/** @link{ProposalResponse} */
CaliperUtils.assertProperty(propResponse, 'propResponse', 'response');
/** @link{ResponseObject} */
let response = propResponse.response;
CaliperUtils.assertProperty(response, 'response', 'status');
if (response.status !== 200) {
let errMsg = `Unsuccessful install status for ${chaincodeInfo.id}@${chaincodeInfo.version} on ${orgPeerTargets[index]}: ${propResponse.response.message}`;
errors.push(new Error(errMsg));
}
});
} catch (err) {
}
let txId = admin.newTransactionID(true);
let request = {
config: configUpdate,
signatures: signatures,
name: channel,
txId: txId
};
try {
/** @link{BroadcastResponse} */
let broadcastResponse = await admin.createChannel(request);
CaliperUtils.assertDefined(broadcastResponse, `The returned broadcast response for creating Channel '${channel}' is undefined`);
CaliperUtils.assertProperty(broadcastResponse, 'broadcastResponse', 'status');
if (broadcastResponse.status !== 'SUCCESS') {
throw new Error(`Orderer response indicated unsuccessful Channel '${channel}' creation: ${broadcastResponse.status}`);
}
} catch (err) {
throw new Error(`Couldn't create Channel '${channel}': ${err.message}`);
}
logger.info(`Channel '${channel}' successfully created`);
}
return channelCreated;
}
proposalResponses.forEach((propResponse, index) => {
if (propResponse instanceof Error) {
let errMsg = `Install proposal error for ${chaincodeInfo.id}@${chaincodeInfo.version} on ${orgPeerTargets[index]}: ${propResponse.message}`;
errors.push(new Error(errMsg));
return;
}
/** @link{ProposalResponse} */
CaliperUtils.assertProperty(propResponse, 'propResponse', 'response');
/** @link{ResponseObject} */
let response = propResponse.response;
CaliperUtils.assertProperty(response, 'response', 'status');
if (response.status !== 200) {
let errMsg = `Unsuccessful install status for ${chaincodeInfo.id}@${chaincodeInfo.version} on ${orgPeerTargets[index]}: ${propResponse.response.message}`;
errors.push(new Error(errMsg));
}
});
} catch (err) {