Skip to content

Commit

Permalink
[FABN-989] Remove redundant packaging code
Browse files Browse the repository at this point in the history
Remove the _getChaincodeDeploymentSpec() function and use
the Package.fromDirectory() function. Also, async/await
Packager.package(), because it looks nicer.

Change-Id: Id7471e9ca23861ebe9002b6cb55cf38b6a2cb101
Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone committed Oct 31, 2018
1 parent d4f847b commit 9baa401
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 170 deletions.
52 changes: 19 additions & 33 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,25 @@ const Client = class extends BaseClient {
throw new Error('Missing peer objects in install chaincode request');
}

const cdsBytes = await _getChaincodeDeploymentSpec(request, this.isDevMode());
let cdsBytes;
if (request.chaincodePackage) {
cdsBytes = request.chaincodePackage;
logger.debug(`installChaincode - using specified chaincode package (${cdsBytes.length} bytes)`);
} else if (this.isDevMode()) {
cdsBytes = null;
logger.debug('installChaincode - in dev mode, refusing to package chaincode');
} else {
const cdsPkg = await Package.fromDirectory({
name: request.chaincodeId,
version: request.chaincodeVersion,
path: request.chaincodePath,
type: request.chaincodeType,
metadataPath: request.metadataPath
});
cdsBytes = await cdsPkg.toBuffer();
logger.debug(`installChaincode - built chaincode package (${cdsBytes.length} bytes)`);
}

// TODO add ESCC/VSCC info here ??????
const lcccSpec = {
type: clientUtils.translateCCType(request.chaincodeType),
Expand Down Expand Up @@ -1743,38 +1761,6 @@ function readFile(path) {
});
}

// internal utility to get the serialized deployment spec for installing chaincode
async function _getChaincodeDeploymentSpec(request, devMode) {
if (request.chaincodePackage && Buffer.isBuffer(request.chaincodePackage)) {
logger.debug('installChaincode - using included package');
return request.chaincodePackage;
} else {
return new Promise((resolve, reject) => {
return Packager.package(request.chaincodePath, request.chaincodeType, devMode, request.metadataPath)
.then((data) => {
const ccSpec = {
type: clientUtils.translateCCType(request.chaincodeType),
chaincode_id: {
name: request.chaincodeId,
path: request.chaincodePath,
version: request.chaincodeVersion
}
};
logger.debug('installChaincode - ccSpec %s ', JSON.stringify(ccSpec));
const chaincodeDeploymentSpec = new _ccProto.ChaincodeDeploymentSpec();
chaincodeDeploymentSpec.setChaincodeSpec(ccSpec);
// DATA may or may not be present depending on devmode settings
if (data) {
chaincodeDeploymentSpec.setCodePackage(data);
logger.debug('installChaincode - created new package');
}
resolve(chaincodeDeploymentSpec.toBuffer());
})
.catch(reject);
});
}
}

// internal utility method to check and convert any strings to protobuf signatures
function _stringToSignature(string_signatures) {
const signatures = [];
Expand Down
55 changes: 27 additions & 28 deletions fabric-client/lib/Packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,38 @@ const logger = utils.getLogger('packager');
* The path to the top-level directory containing metadata descriptors
* @returns {Promise} A promise for the data as a byte array
*/
module.exports.package = function(chaincodePath, chaincodeType, devmode, metadataPath) {
module.exports.package = async function (chaincodePath, chaincodeType, devmode, metadataPath) {
logger.debug('packager: chaincodePath: %s, chaincodeType: %s, devmode: %s, metadataPath: %s',
chaincodePath,chaincodeType,devmode, metadataPath);
return new Promise((resolve, reject) => {
if (devmode) {
logger.debug('packager: Skipping chaincode packaging due to devmode configuration');
return resolve(null);
}

if (!chaincodePath || chaincodePath && chaincodePath.length < 1) {
// Verify that chaincodePath is being passed
return reject(new Error('Missing chaincodePath parameter'));
}
if (devmode) {
logger.debug('packager: Skipping chaincode packaging due to devmode configuration');
return null;
}

const type = chaincodeType ? chaincodeType : 'golang';
logger.debug('packager: type %s ',type);
if (!chaincodePath || chaincodePath && chaincodePath.length < 1) {
// Verify that chaincodePath is being passed
throw new Error('Missing chaincodePath parameter');
}

let handler;
const type = chaincodeType ? chaincodeType : 'golang';
logger.debug('packager: type %s ',type);

switch (type.toLowerCase()) {
case 'car':
handler = new Car();
break;
case 'node':
handler = new Node();
break;
case 'java':
handler = new Java();
break;
default:
handler = new Golang(['.go','.c','.h','.s']);
}
let handler;

return resolve(handler.package(chaincodePath, metadataPath));
});
switch (type.toLowerCase()) {
case 'car':
handler = new Car();
break;
case 'node':
handler = new Node();
break;
case 'java':
handler = new Java();
break;
default:
handler = new Golang(['.go','.c','.h','.s']);
}

return handler.package(chaincodePath, metadataPath);
};
Loading

0 comments on commit 9baa401

Please sign in to comment.