Skip to content

Commit

Permalink
Merge pull request #24 from Akachain/develop
Browse files Browse the repository at this point in the history
Add default endorsement policy requiring all organization endorsement
  • Loading branch information
eledra89 authored Mar 13, 2020
2 parents c3a020d + afe540d commit 1f218fa
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
55 changes: 31 additions & 24 deletions lib/helper/ChaincodeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ const caService = new CAService()
class ChaincodeService {
constructor() {}

async installChaincode(orgName, {chaincodePath, chaincodeId, metadataPath, chaincodeVersion, chaincodeType}) {
async installChaincode(orgName, {
chaincodePath,
chaincodeId,
metadataPath,
chaincodeVersion,
chaincodeType
}) {
let errorMessage = null;
try {
const client = await common.getClientForOrg(orgName, null, true);
Expand Down Expand Up @@ -70,7 +76,13 @@ class ChaincodeService {
}
}

async initChaincode(orgName, channelName, {chaincodeId, chaincodeVersion, chaincodeType, args}) {
async initChaincode(orgName, channelName, {
chaincodeId,
chaincodeVersion,
chaincodeType,
args,
endorsementPolicy
}) {
logger.debug(`\n\n============ Instantiate chaincode on channel ${channelName} ============\n`);
let errorMessage = null;
try {
Expand All @@ -92,7 +104,10 @@ class ChaincodeService {
// be used to sign the proposal request.
// will need the transaction ID string for the event registration later
const deployId = txId.getTransactionID();

if (endorsementPolicy === '' || !endorsementPolicy) {
endorsementPolicy = await common.getDefaultEndorsermentPolicy(channel);
}
logger.debug('Default Endorsement Policy: ', JSON.stringify(endorsementPolicy));

// send proposal to endorser
const request = {
Expand All @@ -102,6 +117,7 @@ class ChaincodeService {
chaincodeType,
args,
txId,
'endorsement-policy': endorsementPolicy,
};

const results = await channel.sendInstantiateProposal(request, 160000);
Expand Down Expand Up @@ -236,7 +252,7 @@ class ChaincodeService {
// build a response to send back to the REST caller
const response = {
success: true,
message
message
};
return response;
}
Expand All @@ -245,7 +261,13 @@ class ChaincodeService {
throw new Error(message);
}

async upgradeChaincode(orgName, channelName, {chaincodeId, chaincodeVersion, chaincodeType, args}) {
async upgradeChaincode(orgName, channelName, {
chaincodeId,
chaincodeVersion,
chaincodeType,
args,
endorsementPolicy
}) {
logger.debug(`\n\n============ Upgrade chaincode on channel ${channelName} ============\n`);
let errorMessage = null;
try {
Expand All @@ -267,25 +289,10 @@ class ChaincodeService {
// be used to sign the proposal request.
// will need the transaction ID string for the event registration later
const deployId = txId.getTransactionID();

let endorsementPolicy;
// if (channelName === 'utopchannel') {
// endorsementPolicy = {
// identities: [
// { role: { name: 'member', mspId: 'akcMSP' }},
// { role: { name: 'member', mspId: 'utopMSP' }},
// { role: { name: 'member', mspId: 'frtMSP' }},
// { role: { name: 'member', mspId: 'fsoftMSP' }}
// ],
// policy: {
// '3-of': [
// { 'signed-by': 0},
// { 'signed-by': 1},
// { '1-of': [{ 'signed-by': 2 }, { 'signed-by': 3 }]}
// ]
// }
// };
// }
if (endorsementPolicy === '' || !endorsementPolicy) {
endorsementPolicy = await common.getDefaultEndorsermentPolicy(channel);
}
logger.debug('Default Endorsement Policy: ', JSON.stringify(endorsementPolicy));

// send proposal to endorser
const request = {
Expand Down
38 changes: 38 additions & 0 deletions lib/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Import lib
const hfc = require('fabric-client');
const promClient = require('prom-client');
const _ = require('lodash');

const logger = require('./logger').getLogger('akc-node-sdk');

Expand All @@ -21,6 +22,42 @@ global.CHANNEL = null;
* FUNCTION SECTION
*/

/**
* Get an instance of client initialized with the network end points
* @param {Client.Channel} channel
*/
const getDefaultEndorsermentPolicy = async (channel) => {
const arrPeers = await channel.getChannelPeers();
const endorsementPolicy = {};
const identities = [];
const policy = [];
const listMSP = [];
if (arrPeers.length > 0) {
_.forEach(arrPeers, (value) => {
listMSP.push(value.getMspid());
});
const uniqMSP = _.uniq(listMSP);
for (let i = 0; i < uniqMSP.length; i += 1) {
const identity = {};
const role = {
name: 'member',
mspId: uniqMSP[i],
};
identity.role = role;
identities.push(identity);
policy.push({
'signed-by': i,
});
}
endorsementPolicy.identities = identities;
const key = `${uniqMSP.length}-of`;
endorsementPolicy.policy = {
[key]: policy,
};
}
return endorsementPolicy;
};

/**
* Get an instance of client initialized with the network end points
* @param {string} orgName
Expand Down Expand Up @@ -115,6 +152,7 @@ const requestCounter = new promClient.Counter({
help: 'Counter of error requests'
});

exports.getDefaultEndorsermentPolicy = getDefaultEndorsermentPolicy;
exports.getClientForOrg = getClientForOrg;
exports.getChannel = getChannel;
exports.requestCounter = requestCounter;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"fabric-client": "~1.4.0",
"js-sha256": "^0.9.0",
"log4js": "^4.4.0",
"lodash": "^4.17.15",
"prom-client": "^11.5.0"
},
"publishConfig": { "registry": "https://npm.pkg.github.com/" },
Expand Down

0 comments on commit 1f218fa

Please sign in to comment.