Skip to content

Commit

Permalink
FAB-10917 NodeSDK - return channel methods
Browse files Browse the repository at this point in the history
Peer roles are channel based and as such needed to be
set on Peer per channel. A new object has been introduced
to hold the peer and information about the peer, ChannelPeer.
This object will be unique per channel as a Peer may be on many
Channels. User will not get ChannelPeer objects on the
channel.getPeers() call. The ChannelPeer will wrapper the Peer
calls such that a ChannelPeer may be used as a Peer. The ChannelPeer
will also have the isInRole and setRole methods that were incorrectly
placed on the Peer. Having the ChannelPeer will make it easier to get
the ChannelEventHub, just use the getChannelEventHub() method on the
ChannelPeer object.

Change-Id: I7b718530805af99336d98210db99f1c46f2bc4e5
Signed-off-by: Bret Harrison <[email protected]>
  • Loading branch information
harrisob committed Jun 29, 2018
1 parent acf0e04 commit b0b4c49
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
60 changes: 49 additions & 11 deletions fabric-client/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ const Channel = class {

/**
* @typedef {Object} InitializeRequest
* @property {string | Peer} target - Optional. The target peer to be used
* @property {string | Peer | ChannelPeer} target - Optional. The target peer to be used
* to make the initialization requests for configuration information.
* Default is to use the first {@link Peer} assigned to this channel.
* Default is to use the first ChannelPeer assigned to this channel.
* @property {boolean} discover - Optional. Use the discovery service on the
* the target peer to load the configuration and network information.
* Default is true. When set to false the target peer will use
Expand Down Expand Up @@ -503,13 +503,15 @@ const Channel = class {
}

/**
* This method will return a {@link Peer} instance if assigned to this
* channel. Peers that have been created by the {@link Client} {@link newPeer}
* This method will return a {@link ChannelPeer} instance if assigned to this
* channel. Peers that have been created by the {@link Client#newPeer}
* method and then added to this channel may be reference by the url if no
* name was provided in the options during the create.
* A {@link ChannelPeer} provides a reference to peer and channel event hub along
* with how this peer is being used on this channel.
*
* @param {string} name - The name of the peer
* @returns {Peer} The Peer instance.
* @returns {ChannelPeer} The ChannelPeer instance.
*/
getPeer(name) {
const channel_peer = this._channel_peers.get(name);
Expand All @@ -518,7 +520,7 @@ const Channel = class {
throw new Error(util.format(PEER_NOT_ASSIGNED_MSG, name));
}

return channel_peer.getPeer();
return channel_peer;
}

/**
Expand All @@ -540,18 +542,30 @@ const Channel = class {
}

/**
* Returns a list of peers assigned to this channel instance.
* @returns {Peer[]} The peer list on the channel.
* Returns a list of {@link ChannelPeer} assigned to this channel instance.
* A {@link ChannelPeer} provides a reference to peer and channel event hub along
* with how this peer is being used on this channel.
* @returns {ChannelPeer[]} The channel peer list on the channel.
*/
getPeers() {
logger.debug('getPeers - list size: %s.', this._channel_peers.size);
const peers = [];
this._channel_peers.forEach((channel_peer) => {
peers.push(channel_peer.getPeer());
peers.push(channel_peer);
});
return peers;
}

/**
* Returns a list of {@link ChannelPeer} assigned to this channel instance.
* A {@link ChannelPeer} provides a reference to peer and channel event hub along
* with how this peer is being used on this channel.
* @returns {ChannelPeer[]} The channel peer list on the channel.
*/
getChannelPeers() {
return this.getPeers();
}

/**
* Add the orderer object to the channel object, this is a client-side-only operation.
* An application may add more than one orderer object to the channel object, however
Expand Down Expand Up @@ -592,7 +606,7 @@ const Channel = class {

/**
* This method will return a {@link Orderer} instance if assigned to this
* channel. Peers that have been created by the {@link Client} {@link newOrderer}
* channel. Peers that have been created by the {@link Client#newOrderer}
* method and then added to this channel may be reference by the url if no
* name was provided in the options during the create.
*
Expand Down Expand Up @@ -651,7 +665,7 @@ const Channel = class {
* This method will create a new ChannelEventHub if one does not exist.
*
* @param {string} name - The peer name associated with this channel event hub.
* Use the {@link Peer}{@link getName} method to get the name of a
* Use the {@link Peer#getName} method to get the name of a
* peer instance that has been added to this channel.
* @returns {ChannelEventHub} - The ChannelEventHub associated with the peer.
*/
Expand Down Expand Up @@ -2800,6 +2814,8 @@ const Channel = class {
}
} else if(target_peer && target_peer.constructor && target_peer.constructor.name === 'Peer') {
targets.push(target_peer);
} else if(target_peer && target_peer.constructor && target_peer.constructor.name === 'ChannelPeer') {
targets.push(target_peer.getPeer());
} else {
throw new Error('Target peer is not a valid peer object instance');
}
Expand Down Expand Up @@ -3182,6 +3198,8 @@ const ChannelPeer = class {

/**
* Close the associated peer service connections.
* <br>see {@link Peer#close}
* <br>see {@link ChannelEventHub#close}
*/
close() {
this._peer.close();
Expand Down Expand Up @@ -3286,6 +3304,26 @@ const ChannelPeer = class {
getPeer() {
return this._peer;
}

/**
* Wrapper method for the associated peer so this object may be used as a {@link Peer}
* {@link Peer#sendProposal}
*/
sendProposal(proposal, timeout) {
return this._peer.sendProposal(proposal, timeout);
}

/**
* Wrapper method for the associated peer so this object may be used as a {@link Peer}
* {@link Peer#sendDiscovery}
*/
sendDiscovery(request, timeout) {
return this._peer.sendDiscovery(request, timeout);
}

toString() {
return this._peer.toString();
}
}; //endof ChannelPeer

/*
Expand Down
1 change: 0 additions & 1 deletion fabric-client/lib/Orderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ var Orderer = class extends Remote {
},
(error) =>{
logger.error('Orderer %s has an error %s ', self.getUrl(), error.toString());
self.close();
return Promise.reject(error);
});
}
Expand Down
6 changes: 6 additions & 0 deletions test/integration/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ test('\n\n***** D I S C O V E R Y *****\n\n', async function(t) {

await testUtil.queryChannelAsAdmin(t, client, channel, tx_id_string, );

const discovered_peers = channel.getPeers();
const force_target_request = {
target: discovered_peers[0]
};
await testUtil.invokeAsAdmin(t, client, channel, force_target_request);

t.pass('End discovery testing');
t.end();
});
7 changes: 7 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ test('\n\n ** Channel - method tests **\n\n', function (t) {
);
t.equal(_channel.getOrderers()[0].toString(), 'Orderer:{url:grpc://somehost.com:1234}', 'checking channel getOrderers()');
t.equal(_channel.getPeers()[0].toString(), 'Peer:{url:grpc://somehost.com:1234}', 'checking channel getPeers()');
t.equal(_channel.getChannelPeers()[0].toString(), 'Peer:{url:grpc://somehost.com:1234}', 'checking channel getPeers()');
let peer = _channel.getPeer('peer1');
t.ok(peer, 'Check that peer was returned');
t.equal(peer.toString(), 'Peer:{url:grpc://somehost.com:1234}', 'checking channel peer is the same one');
peer = _channel.getChannelPeer('peer1');
t.ok(peer, 'Check that peer was returned');
t.equal(peer.toString(), 'Peer:{url:grpc://somehost.com:1234}', 'checking channel peer is the same one');
t.throws(
function () {
var orderer = new Orderer('grpc://somehost.com:1234');
Expand Down
4 changes: 2 additions & 2 deletions test/unit/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ test('\n\n ** configuration testing **\n\n', function (t) {
t.equals('mychannel',channel.getName(),'Channel should be named');
t.equals(channel.getPeers().length, 4, 'Peers should be four');
const peer = channel.getPeers()[0];
if(peer instanceof Peer) t.pass('Successfully got a peer');
else t.fail('Failed to get a peer');
if(peer && peer.constructor && peer.constructor.name === 'ChannelPeer') t.pass('Successfully got a channel peer');
else t.fail('Failed to get a channel peer');
},
null,
'Should be able to instantiate a new instance of "Channel" with orderer, org and peer defined in the network configuration'
Expand Down

0 comments on commit b0b4c49

Please sign in to comment.