Skip to content

Commit

Permalink
FAB-10940 installChaincode doesn’t restrict to org
Browse files Browse the repository at this point in the history
The installChaincode method on the client doesn’t restrict installation
of chaincode to the client organisation. This addresses that and returns
a utility method that was exposed in 1.1

Change-Id: Ida8531e907c68a1f4aaa7494b6fc1274aacb1495
Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey committed Jun 29, 2018
1 parent 88eb186 commit acf0e04
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 23 deletions.
54 changes: 32 additions & 22 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,37 @@ const Client = class extends BaseClient {
return event_hubs;
}

/*
* Private utility method to get target peers. The peers will be in the organization of this client,
* (meaning the peer has the same mspid). If this client is not assigned a mspid, then all
* peers will be returned defined on the channel. The returned list may be empty if no channels
* are provide, a channel is not found on the list or channel does not have any peers defined.
*
* @param {string||string[]} channel_names channel name or array of channel names
* @returns {ChannelPeer[]} array of channel peers
* @private
*/
getPeersForOrgOnChannel(channel_names) {
if (!Array.isArray(channel_names)) {
channel_names = [channel_names];
}
let peers = [];
const temp_peers = {};
for (let i in channel_names) {
const channel = this.getChannel(channel_names[i]);
const channel_peers = channel.getPeersForOrg(this._mspid);
for (let j in channel_peers) {
const peer = channel_peers[j];
temp_peers[peer.getName()] = peer; // will remove duplicates
}
}
for (let name in temp_peers) {
//TODO: Need to check the roles but cannot do so at present awaiting fix
peers.push(temp_peers[name]);
}
return peers;
}

/**
* Returns a CertificateAuthority implementation as defined by the settings
* in the currently loaded network configuration and the client configuration.
Expand Down Expand Up @@ -1004,28 +1035,7 @@ const Client = class extends BaseClient {
try {
peers = this.getTargetPeers(request.targets);
if (!peers && request.channelNames) {
let channel_names = request.channelNames;
if (!Array.isArray(channel_names)) {
channel_names = [request.channelNames];
}
peers = [];
const temp_peers = {};
for (let i in channel_names) {
const channel = this.getChannel(channel_names[i]);
let org_name = null;
if (this._network_config.hasClient()) {
const client = this._network_config.getClientConfig();
org_name = client.organization;
}
const channel_peers = channel.getPeersForOrg();
for (let j in channel_peers) {
const peer = channel_peers[j];
temp_peers[peer.getName()] = peer; // will remove duplicates
}
}
for (let name in temp_peers) {
peers.push(temp_peers[name]);
}
peers = this.getPeersForOrgOnChannel(request.channelNames);
}
} catch (err) {
return Promise.reject(err);
Expand Down
89 changes: 88 additions & 1 deletion test/unit/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,94 @@ test('\n\n*** Test channel selection if no channel name provided ***\n', (t) =>
t.equals(channel, 'SomeChannelObject', 'correct channel is returned from channel map');
});

t.end();
});

test('\n\n*** Test Client.getPeersForOrgOnChannel ***\n', (t) => {
let config = {
'name': 'test',
'version': '1.0.0',
'client': {
'organization': 'Org1'
},
'channels': {
'testchannel': {
'orderers': [
'orderer.example.com'
],
'peers': {
'peer0.org1.example.com': {}
}
},
'anotherchannel': {
'orderers': [
'orderer.example.com'
],
'peers': {
'peer0.org1.example.com': {},
'peer0.org2.example.com': {}
}
}
},
'organizations': {
'Org1': {
'mspid': 'Org1MSP',
'peers': [
'peer0.org1.example.com'
]
},
'Org2': {
'mspid': 'Org2MSP',
'peers': [
'peer0.org2.example.com'
]
}
},
'orderers': {
'orderer.example.com': {
'url': 'grpc://localhost:7050'
}
},
'peers': {
'peer0.org1.example.com': {
'url': 'grpc://localhost:7051',
'eventUrl': 'grpc://localhost:7053'
},
'peer0.org2.example.com': {
'url': 'grpc://localhost:8051',
'eventUrl': 'grpc://localhost:8053'
}
}
};

let client = Client.loadFromConfig(config);
client.setTlsClientCertAndKey(aPem, aPem);
client._mspid = 'Org1MSP';
let peer_results = client.getPeersForOrgOnChannel();
t.equals(peer_results.length, 1, 'correct number of peers returned when no channel specified');
t.equals(peer_results[0].getName(), 'peer0.org1.example.com', 'correct peer for Org1 returned');

peer_results = client.getPeersForOrgOnChannel('testchannel');
t.equals(peer_results.length, 1, 'correct number of peers returned when testchannel specified');
t.equals(peer_results[0].getName(), 'peer0.org1.example.com', 'correct peer for Org1 returned');

peer_results = client.getPeersForOrgOnChannel('anotherchannel');
t.equals(peer_results.length, 1, 'correct number of peers returned when anotherchannel specified');
t.equals(peer_results[0].getName(), 'peer0.org1.example.com', 'correct peer for Org1 returned');

config.client.organization = 'Org2';
client = Client.loadFromConfig(config);
client._mspid = 'Org2MSP';
client.setTlsClientCertAndKey(aPem, aPem);
peer_results = client.getPeersForOrgOnChannel();
t.equals(peer_results.length, 0, 'correct number of peers returned when no channel specified');

peer_results = client.getPeersForOrgOnChannel('testchannel');
t.equals(peer_results.length, 0, 'correct number of peers returned when testchannel specified');

peer_results = client.getPeersForOrgOnChannel('anotherchannel');
t.equals(peer_results.length, 1, 'correct number of peers returned when anotherchannel specified');
t.equals(peer_results[0].getName(), 'peer0.org2.example.com', 'correct peer for Org2 returned');

t.pass('Should get default channel if no channel name provided defined');
t.end();
});

0 comments on commit acf0e04

Please sign in to comment.