Skip to content

Commit

Permalink
[FAB-6709] NodeSDK get default channel
Browse files Browse the repository at this point in the history
When using a connection profile it would be
nice to not have to know the name of the single
channel that may be in use. The getChannel() method
will be enhanced to return the first channel on the
list.

Change-Id: Iaac0e6d2dd9d4d2235c26b3b62781cbc19eae1c0
Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob committed Nov 3, 2017
1 parent 4f666a4 commit 99129bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 11 additions & 3 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ var Client = class extends BaseClient {
* will be created and populated with {@link Orderer} objects and {@link Peer} objects
* as defined in the network configuration.
*
* @param {string} name - The name of the channel.
* @param {boolean} throwError - Indicates if this method will throw an error if the channel
* is not found. Default is true.
* @param {string} name - Optional. The name of the channel. When omitted the
* first channel defined in the loaded network configuration will be
* returned
* @param {boolean} throwError - Indicates if this method will throw an error
* if the channel is not found. Default is true.
* @returns {Channel} The channel instance
*/
getChannel(name, throwError) {
Expand All @@ -194,6 +196,12 @@ var Client = class extends BaseClient {
else {
// maybe it is defined in the network config
if(this._network_config) {
if(!name) {
let channel_names = Object.keys(this._network_config._network_config.channels);
if(channel_names) {
name = channel_names[0];
}
}
channel = this._network_config.getChannel(name);
this._channels[name] = channel;
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ test('\n\n ** configuration testing **\n\n', function (t) {
t.equals(orderer._options['request-timeout'],30000, ' check that we get this orderer timeout set');
let eventHub = client._network_config.getEventHub('peer0.org1.example.com');
t.equals(eventHub._ep._options['request-timeout'],3000, ' check that we get this eventHub timeout set');
let first_channel = client.getChannel();
if(first_channel && first_channel.getName() === 'mychannel2') {
t.pass('Successfully got the first channel without specifying the name');
} else {
t.fail('Failed to get the first channel');
}

delete client._network_config._network_config.certificateAuthorities['ca-org1'].tlsCACerts;
delete client._network_config._network_config.certificateAuthorities['ca-org1'].httpOptions;
Expand All @@ -179,6 +185,15 @@ test('\n\n ** configuration testing **\n\n', function (t) {
'2 Should be able to run a number of test without error'
);

t.throws(
() => {
var client = new Client();
client.getChannel();
},
/Channel not found for name undefined./,
'Check for Channel not found for name undefined.'
);

t.doesNotThrow(
() => {
var config_loc = path.resolve('test/fixtures/network.yaml');
Expand Down

0 comments on commit 99129bc

Please sign in to comment.