Skip to content

Commit

Permalink
FAB-10856 NodeSDK getByOrg not working
Browse files Browse the repository at this point in the history
With the addition of discovery, the get-by-orgs methods are no
longer working. The connection profile uses an org name and has
a mspid, where fabric discover just has a mspid. The  Client, Channel,
and Peers objects will now all work with the mspid as the organizational
reference.

Change-Id: I099b3c335b0d04cae3a75e10bf9b4b449005870d
Signed-off-by: Bret Harrison <[email protected]>
  • Loading branch information
harrisob committed Jun 27, 2018
1 parent 3948c22 commit 4293680
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 92 deletions.
14 changes: 8 additions & 6 deletions docs/tutorials/network-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ peer0.org2.example.com:
```
Notice that we have left off the targets parameter of the request object. This will have the fabric client do a lookup of peers on this channel in the connection profile configuration. The fabric client will be looking for peers defined in the role of `endorsingPeer`. The fabric client will then send the proposal to the located peers and return all the endorsements in the `results` object.

There may be a need to have only the peers in a specific organization.
There may be a need to have only the peers in a specific organization. Use the mspid of the organization.
```
var peers = getPeersForOrg('Org1');
var peers = getPeersForOrg('Org1MSP');
```
Or maybe for the organization that is defined in the client section of the connection profile.
```
Expand All @@ -293,13 +293,13 @@ When there is a connection profile configuration loaded and the query call is no
* These are fabric client based queries and require the user have an admin role or indicate that the admin identity should be used. These queries do not use the connection profile config lookup to find a peer to use and must be passed the target peer.
- queryChannels
- queryInstalledChaincodes
* These queries are channel based queries that require a peer with the ledgerQuery role.
* These queries are channel based queries that require a peer with the `ledgerQuery` role.
- queryInstantiatedChaincodes (user must be an admin or indicate that the assigned admin identity should be used)
- queryInfo
- queryBlockByHash
- queryBlock
- queryTransaction
* this is a channel based query and requires a peer with the chaincodeQuery role.
* this is a channel based query and requires a peer with the `chaincodeQuery` role.
- queryByChaincode


Expand All @@ -318,9 +318,11 @@ peer0.org1.example.com:
tlsCACerts:
path: test/fixtures/channel/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tlscacerts/org1.example.com-cert.pem
```
The following will be a list of event hubs that are within the 'Org1' organization. All peers referenced by an organization that the 'eventSource' set to true.
The following will be a list of event hubs that are within the 'Org1' organization.
All peers referenced by an organization that the 'eventSource' set to true.
Use the mspid of the organization.
```
var channel_event_hubs = channel.getChannelEventHubsForOrg('Org1');
var channel_event_hubs = channel.getChannelEventHubsForOrg('Org1MSP');
```

The following will be a list of channel-based event hubs that are within the organization defined in the client section of the connection profile.
Expand Down
89 changes: 52 additions & 37 deletions fabric-client/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ const Channel = class {
return self._discover(discover_request).then((discover_results) => {
// chaincode names are in each peer
if(discover_results && discover_results.peers_by_org) {
for(let org_name in discover_results.peers_by_org) {
const org = discover_results.peers_by_org[org_name];
for(let mspid in discover_results.peers_by_org) {
const org = discover_results.peers_by_org[mspid];
for(let peer_index in org.peers) {
const peer = org.peers[peer_index];
for(let chaincode_index in peer.chaincodes) {
Expand Down Expand Up @@ -462,13 +462,14 @@ const Channel = class {
*
* @param {Peer} peer - An instance of the Peer class that has been initialized with URL
* and other gRPC options such as TLS credentials and request timeout.
* @param {string} org_name - The organization this peer belongs.
* @param {string} mspid - The mpsid of the organization this peer belongs.
* @param {string} mspid - The MSP this peer is using.
* @param {ChannelPeerRoles} roles - Optional. The roles this peer will perform
* on this channel. A role that is not defined will default to true
* @param {boolean} replace - If an orderer exist with the same name, replace
* with this one.
*/
addPeer(peer, org_name, roles, replace) {
addPeer(peer, mspid, roles, replace) {
const name = peer.getName();
const check = this._channel_peers.get(name);
if(check) {
Expand All @@ -486,7 +487,7 @@ const Channel = class {
}
logger.debug('/n adding a new peer --name: %s --URL: %s',peer.getName(),peer.getUrl());

const channel_peer = new ChannelPeer(org_name, this, peer, roles);
const channel_peer = new ChannelPeer(mspid, this, peer, roles);
this._channel_peers.set(name, channel_peer);
}

Expand Down Expand Up @@ -668,18 +669,25 @@ const Channel = class {

/**
* Returns a list of {@link ChannelEventHub} based on the peers that are
* defined in this channel that are in the named organization.
* defined in this channel that are in the organization.
*
* @param {string} org_name - Optional - The name of an organization
* @param {string} mspid - Optional - The mspid of an organization
* @returns {ChannelEventHub[]} An array of ChannelEventHub instances
*/
getChannelEventHubsForOrg(org_name) {
getChannelEventHubsForOrg(mspid) {
const method = 'getChannelEventHubsForOrg';
logger.debug('%s - starting', method);
let _mspid = null;
if(!mspid) {
_mspid = this._clientContext.getMspid();
logger.debug('%s - starting - using client mspid: %s', method, _mspid);
} else {
_mspid = mspid;
logger.debug('%s - starting - mspid: %s', method, _mspid);
}

const channel_event_hubs = [];
this._channel_peers.forEach((channel_peer) => {
if (channel_peer.isInOrg(org_name)) {
if (channel_peer.isInOrg(_mspid)) {
if (channel_peer.isInRole(Constants.NetworkConfig.EVENT_SOURCE_ROLE)) {
channel_event_hubs.push(channel_peer.getChannelEventHub());
} else {
Expand All @@ -695,16 +703,23 @@ const Channel = class {
* Returns a list of {@link Peer} that are
* defined in this channel that are in the named organization.
*
* @param {string} org_name - Optional - The name of an organization
* @param {string} mspid - Optional - The name of an organization
* @returns {Peer[]} An array of Peer instances
*/
getPeersForOrg(org_name) {
getPeersForOrg(mspid) {
const method = 'getPeersForOrg';
logger.debug('%s - starting', method);
let _mspid = null;
if(!mspid) {
_mspid = this._clientContext._mspid;
logger.debug('%s - starting - using client mspid: %s', method, _mspid);
} else {
_mspid = mspid;
logger.debug('%s - starting - mspid: %s', method, _mspid);
}

const peers = [];
this._channel_peers.forEach((channel_peer) => {
if (channel_peer.isInOrg(org_name)) {
if (channel_peer.isInOrg(_mspid)) {
peers.push(channel_peer.getPeer());
}
});
Expand Down Expand Up @@ -1025,12 +1040,12 @@ const Channel = class {
*/
if (q_config.orderers) {
config.orderers = {};
for (let org_name in q_config.orderers) {
logger.debug('%s - found orderer org: ', method, org_name);
config.orderers[org_name] = {};
config.orderers[org_name].endpoints = [];
for (let index in q_config.orderers[org_name].endpoint) {
config.orderers[org_name].endpoints.push(q_config.orderers[org_name].endpoint[index]);
for (let mspid in q_config.orderers) {
logger.debug('%s - found orderer org: ', method, mspid);
config.orderers[mspid] = {};
config.orderers[mspid].endpoints = [];
for (let index in q_config.orderers[mspid].endpoint) {
config.orderers[mspid].endpoints.push(q_config.orderers[mspid].endpoint[index]);
}
}
}
Expand All @@ -1046,10 +1061,10 @@ const Channel = class {
logger.debug('%s - start', method);
const peers_by_org = {};
if (q_members && q_members.peers_by_org) {
for (let org_name in q_members.peers_by_org) {
logger.debug('%s - found org:%s', method, org_name);
peers_by_org[org_name] = {};
peers_by_org[org_name].peers = this._processPeers(q_members.peers_by_org[org_name].peers);
for (let mspid in q_members.peers_by_org) {
logger.debug('%s - found org:%s', method, mspid);
peers_by_org[mspid] = {};
peers_by_org[mspid].peers = this._processPeers(q_members.peers_by_org[mspid].peers);
}
}
return peers_by_org;
Expand Down Expand Up @@ -3131,21 +3146,21 @@ const ChannelPeer = class {
/**
* Construct a ChannelPeer object with the given Peer and opts.
* A channel peer object holds channel based references:
* Organization name this peer belongs.
* MSP ID of the Organization this peer belongs.
* {@link Channel} object used to know the channel this peer is interacting.
* {@link Peer} object used for interacting with the Hyperledger fabric network.
* {@link ChannelEventHub} object used for listening to block changes on the channel.
* List of {@link ChannelPeerRoles} to indicate the roles this peer performs on the channel.
*
* The roles this Peer performs on this channel are indicated with is object.
*
* @param {string} org_name - The organization name this peer belongs.
* @param {string} mspid - The mspid of the organization this peer belongs.
* @param {Channel} channel - The Channel instance.
* @param {Peer} peer - The Peer instance.
* @param {ChannelPeerRoles} roles - The roles for this peer.
*/
constructor(org_name, channel, peer, roles) {
this._org_name = org_name; // if null, then peer belongs to all organizations
constructor(mspid, channel, peer, roles) {
this._mspid = mspid;
if(channel && channel.constructor && channel.constructor.name === 'Channel') {
if(peer && peer.constructor && peer.constructor.name === 'Peer') {
this._channel = channel;
Expand Down Expand Up @@ -3175,13 +3190,14 @@ const ChannelPeer = class {
}
}


/**
* Get the organization name.
* Get the MSP ID.
*
* @returns {string} The organization name.
* @returns {string} The mspId.
*/
getOrganizationName() {
return this._org_name;
getMspid() {
return this._mspid;
}

/**
Expand Down Expand Up @@ -3235,15 +3251,14 @@ const ChannelPeer = class {
* The default is true when the incoming organization name is not defined.
* The default will be true when this peer does not have the organization name defined.
*
* @param {string} mspid - The mspid of the organnization
* @returns {boolean} If this peer belongs to the organization.
*/
isInOrg(org_name) {
if(!org_name) {
return true;
} else if(typeof this._org_name === 'undefined' || this._org_name == null) {
isInOrg(mspid) {
if(!mspid || !this._mspid) {
return true;
} else {
return org_name === this._org_name;
return mspid === this._mspid;
}
}

Expand Down
61 changes: 29 additions & 32 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const Client = class extends BaseClient {

constructor() {
super();
this._mspid = null; // The organization name
this._mspid = null; // The mspid id and the organization id

this._stateStore = null;
this._userContext = null;
Expand Down Expand Up @@ -311,25 +311,23 @@ const Client = class extends BaseClient {
}

/**
* Returns a list of {@link Peer} for the named organization as defined
* in the currently loaded network configuration. If no organization is
* Returns a list of {@link Peer} for the mspid of an organization as defined
* in the currently loaded network configuration. If no id is
* provided then the organization named in the currently active network
* configuration's client section will be used.
*
* @param {string} org_name - Optional - The name of an organization
* @param {string} mspid - Optional - The mspid of an organization
* @returns {Peer[]} An array of Peer instances that are defined for this organization
*/
getPeersForOrg(org_name) {
if (this._network_config) {
if (!org_name && this._network_config.hasClient()) {
const client = this._network_config.getClientConfig();
org_name = client.organization;
}
if (org_name) {
const organization = this._network_config.getOrganization(org_name);
if (organization) {
return organization.getPeers();
}
getPeersForOrg(mspid) {
let _mspid = mspid;
if (!mspid ) {
_mspid = this._mspid;
}
if (_mspid && this._network_config) {
const organization = this._network_config.getOrganizationByMspId(_mspid);
if (organization) {
return organization.getPeers();
}
}

Expand Down Expand Up @@ -405,27 +403,25 @@ const Client = class extends BaseClient {
}

/**
* Returns a list of {@link EventHub} for the named organization as defined
* in the currently loaded network configuration. If no organization is
* provided then the organization named in the currently active network
* Returns a list of {@link EventHub} for an organization as defined
* in the currently loaded network configuration. If no organization mspid is
* provided then the organization referenced in the currently active network
* configuration's client section will be used. The list will be based on
* the peers in the organization that have the "eventUrl" setting.
*
* @param {string} org_name - Optional - The name of an organization
* @param {string} mspid - Optional - The mspid of an organization
* @returns {EventHub[]} An array of EventHub instances that are defined for this organization
*/
getEventHubsForOrg(org_name) {
getEventHubsForOrg(mspid) {
let event_hubs = [];
if (this._network_config) {
if (!org_name && this._network_config.hasClient()) {
let client = this._network_config.getClientConfig();
org_name = client.organization;
}
if (org_name) {
let organization = this._network_config.getOrganization(org_name);
if (organization) {
event_hubs = organization.getEventHubs();
}
let _mspid = mspid;
if (!mspid ) {
_mspid = this._mspid;
}
if (_mspid && this._network_config) {
const organization = this._network_config.getOrganizationByMspId(_mspid);
if (organization) {
event_hubs = organization.getEventHubs();
}
}

Expand Down Expand Up @@ -525,8 +521,8 @@ const Client = class extends BaseClient {
}

/**
* Returns the mspid of the currently loaded client's organization
* as defined in the network configuration.
* Returns the mspid of the client. The mspid is also used as the
* reference to the organization.
*
* @returns {string} the mspid of the organization defined in the client
* section of the loaded network configuration
Expand All @@ -535,6 +531,7 @@ const Client = class extends BaseClient {
return this._mspid;
}


/**
* Returns a new {@link TransactionID} object. Fabric transaction ids are constructed
* as a hash of a nonce concatenated with the signing identity's serialized bytes. The
Expand Down
Loading

0 comments on commit 4293680

Please sign in to comment.