diff --git a/fabric-client/lib/Client.js b/fabric-client/lib/Client.js index a20e073ce7..6649a29e48 100644 --- a/fabric-client/lib/Client.js +++ b/fabric-client/lib/Client.js @@ -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. @@ -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); diff --git a/test/unit/client.js b/test/unit/client.js index 63e07b2957..e117497ef6 100644 --- a/test/unit/client.js +++ b/test/unit/client.js @@ -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(); });