Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/** @link{TransactionRequest} */
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;
// 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,
getTargetPeersOfChaincodeOfChannel(chaincodeInfo, channel) {
let cc = this.network.channels[channel].chaincodes.find(
cc => cc.id === chaincodeInfo.id && cc.version === chaincodeInfo.version);
CaliperUtils.assertDefined(cc, `Could not find the following chaincode in the configuration: ${chaincodeInfo.id}@${chaincodeInfo.version}`);
// targets are explicitly defined
if (CaliperUtils.checkProperty(cc, 'targetPeers')) {
return new Set(cc.targetPeers);
}
// we need to gather the target peers from the channel's peer section
// based on their provided functionality (endorsing and cc query)
let results = new Set();
let peers = this.network.channels[channel].peers;
for (let key in peers) {
if (!peers.hasOwnProperty(key)) {
continue;
}
let peer = peers[key];
// if only the peer name is present in the config, then it is a target based on the default values
};
// metadata (like CouchDB indices) are only supported since Fabric v1.1
if (CaliperUtils.checkProperty(ccObject, 'metadataPath')) {
if (!this.networkUtil.isInCompatibilityMode()) {
request.metadataPath = CaliperUtils.resolvePath(ccObject.metadataPath, this.workspaceRoot);
} else {
throw new Error(`Installing ${chaincodeInfo.id}@${chaincodeInfo.version} with metadata is not supported in Fabric v1.0`);
}
}
// install to necessary peers of org and process the results
try {
/** @link{ProposalResponseObject} */
let propRespObject = await admin.installChaincode(request);
CaliperUtils.assertDefined(propRespObject);
/** Array of @link{ProposalResponse} objects */
let proposalResponses = propRespObject[0];
CaliperUtils.assertDefined(proposalResponses);
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} */
if (this.version.lessThan('1.2.0')) {
throw new Error(`${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel}: private collections are supported from Fabric v1.2`);
}
request['collections-config'] = ccObject['collections-config'];
}
/** @link{ProposalResponseObject} */
let response;
try {
response = await admin.getChannel(channel, true).sendInstantiateProposal(request, this.configChaincodeInstantiateTimeout);
} catch (err) {
throw new Error(`Couldn't endorse ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel} on peers [${targetPeers.toString()}]: ${err.message}`);
}
CaliperUtils.assertDefined(response);
/** @link{Array} */
let proposalResponses = response[0];
/** @link{Proposal} */
let proposal = response[1];
CaliperUtils.assertDefined(proposalResponses);
CaliperUtils.assertDefined(proposal);
// check each response
proposalResponses.forEach((propResp, index) => {
CaliperUtils.assertDefined(propResp);
// an Error is returned for a rejected proposal
if (propResp instanceof Error) {
throw new Error(`Invalid endorsement for ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel} from ${targetPeers[index]}: ${propResp.message}`);
} else if (propResp.response.status !== 200) {
throw new Error(`Invalid endorsement for ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel} from ${targetPeers[index]}: status code ${propResp.response.status}`);
/** @link{ProposalResponseObject} */
let response;
try {
response = await admin.getChannel(channel, true).sendInstantiateProposal(request, this.configChaincodeInstantiateTimeout);
} catch (err) {
throw new Error(`Couldn't endorse ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel} on peers [${targetPeers.toString()}]: ${err.message}`);
}
CaliperUtils.assertDefined(response);
/** @link{Array} */
let proposalResponses = response[0];
/** @link{Proposal} */
let proposal = response[1];
CaliperUtils.assertDefined(proposalResponses);
CaliperUtils.assertDefined(proposal);
// check each response
proposalResponses.forEach((propResp, index) => {
CaliperUtils.assertDefined(propResp);
// an Error is returned for a rejected proposal
if (propResp instanceof Error) {
throw new Error(`Invalid endorsement for ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel} from ${targetPeers[index]}: ${propResp.message}`);
} else if (propResp.response.status !== 200) {
throw new Error(`Invalid endorsement for ${chaincodeInfo.id}@${chaincodeInfo.version} in ${channel} from ${targetPeers[index]}: status code ${propResp.response.status}`);
}
});
// connect to every event source of every org in the channel
let eventSources = this._assembleTargetEventSources(channel, targetPeers);
let eventPromises = [];
if (!this.networkUtil.isInCompatibilityMode()) {
request.metadataPath = CaliperUtils.resolvePath(ccObject.metadataPath, this.workspaceRoot);
} else {
throw new Error(`Installing ${chaincodeInfo.id}@${chaincodeInfo.version} with metadata is not supported in Fabric v1.0`);
}
}
// install to necessary peers of org and process the results
try {
/** @link{ProposalResponseObject} */
let propRespObject = await admin.installChaincode(request);
CaliperUtils.assertDefined(propRespObject);
/** Array of @link{ProposalResponse} objects */
let proposalResponses = propRespObject[0];
CaliperUtils.assertDefined(proposalResponses);
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 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;
}