Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 519cc0a

Browse files
committed
FABJ-434 Expose org info for peers
Change-Id: Ie346fa5d45089f4d0a86c9daf011bf5baffc5e67 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent a8ef7ce commit 519cc0a

File tree

10 files changed

+452
-14
lines changed

10 files changed

+452
-14
lines changed

src/main/java/org/hyperledger/fabric/sdk/Channel.java

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public class Channel implements Serializable {
180180
private final Collection<Peer> peers = Collections.synchronizedSet(new HashSet<>());
181181
private final Map<Peer, PeerOptions> peerOptionsMap = Collections.synchronizedMap(new HashMap<>());
182182
private transient Map<String, Peer> peerEndpointMap = Collections.synchronizedMap(new HashMap<>());
183+
private Map<String, Collection<Peer>> peerMSPIDMap = new HashMap<>();
184+
private Map<String, Collection<Orderer>> ordererMSPIDMap = new HashMap<>();
183185
private final Map<PeerRole, Set<Peer>> peerRoleSetMap = Collections.synchronizedMap(new HashMap<>());
184186
private transient String chaincodeEventUpgradeListenerHandle;
185187
private transient String transactionListenerProcessorHandle;
@@ -642,6 +644,7 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
642644
peers.add(peer);
643645
peerOptionsMap.put(peer, peerOptions.clone());
644646
peerEndpointMap.put(peer.getEndpoint(), peer);
647+
addPeerMSPIDMap(peer);
645648

646649
if (peerOptions.getPeerRoles().contains(PeerRole.SERVICE_DISCOVERY)) {
647650

@@ -652,9 +655,7 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
652655
TLSCertificateKeyPair tlsCertificateKeyPair = tlsCertificateBuilder.clientCert();
653656
peer.setTLSCertificateKeyPair(tlsCertificateKeyPair);
654657
}
655-
656658
discoveryEndpoints.add(peer.getEndpoint());
657-
658659
}
659660

660661
for (Map.Entry<PeerRole, Set<Peer>> peerRole : peerRoleSetMap.entrySet()) {
@@ -675,6 +676,79 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
675676
return this;
676677
}
677678

679+
private void addPeerMSPIDMap(final Peer peer) {
680+
Properties properties = peer.getProperties();
681+
682+
if (null != properties) {
683+
final String mspid = properties.getProperty(Peer.PEER_ORGANIZATION_MSPID_PROPERTY);
684+
if (!isNullOrEmpty(mspid)) {
685+
logger.debug(format("Channel %s mapping peer %s to mspid %s", name, peer, mspid));
686+
synchronized (peerMSPIDMap) {
687+
peerMSPIDMap.computeIfAbsent(mspid, k -> new HashSet<Peer>()).add(peer);
688+
}
689+
}
690+
}
691+
}
692+
693+
private void removePeerMSPIDMap(final Peer peer) {
694+
Properties properties = peer.getProperties();
695+
696+
if (null != properties) {
697+
final String mspid = properties.getProperty(Peer.PEER_ORGANIZATION_MSPID_PROPERTY);
698+
if (!isNullOrEmpty(mspid)) {
699+
logger.debug(format("Channel %s removing mapping peer %s to mspid %s", name, peer, mspid));
700+
synchronized (peerMSPIDMap) {
701+
final Collection<Peer> peers = peerMSPIDMap.get(mspid);
702+
if (peers != null) {
703+
peers.remove(peer);
704+
if (peers.isEmpty()) {
705+
peerMSPIDMap.remove(mspid);
706+
}
707+
708+
}
709+
}
710+
}
711+
}
712+
}
713+
714+
/**
715+
* Get peers that belong to an organization from the organization's MSPID
716+
* These values may not be available till after the channel is initialized.
717+
*
718+
* @param mspid The organizaiions MSPID
719+
* @return A collection of Peers that belong to the organization with that mspid.
720+
* @throws InvalidArgumentException
721+
*/
722+
723+
public Collection<Peer> getPeersForOrganization(String mspid) throws InvalidArgumentException {
724+
725+
if (isNullOrEmpty(mspid)) {
726+
throw new InvalidArgumentException("The mspid parameter may not be null or empty string.");
727+
}
728+
synchronized (peerMSPIDMap) {
729+
730+
final Collection<Peer> peers = peerMSPIDMap.get(mspid);
731+
if (peers == null) {
732+
return Collections.emptySet();
733+
} else {
734+
return new LinkedList<>(peers); // return a copy.
735+
}
736+
}
737+
}
738+
739+
/**
740+
* Collection of strings which are the MSPIDs of all the peer organization added.
741+
* These values may not be available till after the channel is initialized.
742+
*
743+
* @return The collection of mspids
744+
*/
745+
746+
public Collection<String> getPeersOrganizationMSPIDs() {
747+
synchronized (peerMSPIDMap) {
748+
return new LinkedList<>(peerMSPIDMap.keySet());
749+
}
750+
}
751+
678752
/**
679753
* Join the peer to the channel. The peer is added with all roles see {@link PeerOptions}
680754
*
@@ -808,6 +882,7 @@ public Channel joinPeer(Orderer orderer, Peer peer, PeerOptions peerOptions) thr
808882
} catch (Exception e) {
809883
logger.error(format("%s removing peer %s due to exception %s", toString(), peer, e.getMessage()));
810884
peers.remove(peer);
885+
removePeerMSPIDMap(peer);
811886
logger.error(e);
812887
throw new ProposalException(e.getMessage(), e);
813888
}
@@ -901,6 +976,7 @@ private void removePeerInternal(Peer peer) {
901976
peers.remove(peer);
902977
peerOptionsMap.remove(peer);
903978
peerEndpointMap.remove(peer.getEndpoint());
979+
removePeerMSPIDMap(peer);
904980

905981
for (Set<Peer> peerRoleSet : peerRoleSetMap.values()) {
906982
peerRoleSet.remove(peer);
@@ -931,6 +1007,16 @@ public Channel addOrderer(Orderer orderer) throws InvalidArgumentException {
9311007
orderer.setChannel(this);
9321008
ordererEndpointMap.put(orderer.getEndpoint(), orderer);
9331009
orderers.add(orderer);
1010+
final Properties properties = orderer.getProperties();
1011+
if (properties != null) {
1012+
final String mspid = properties.getProperty(Orderer.ORDERER_ORGANIZATION_MSPID_PROPERTY);
1013+
if (!isNullOrEmpty(mspid)) {
1014+
synchronized (ordererMSPIDMap) {
1015+
ordererMSPIDMap.computeIfAbsent(mspid, k -> new HashSet<>()).add(orderer);
1016+
}
1017+
}
1018+
}
1019+
9341020
return this;
9351021
}
9361022

@@ -949,9 +1035,60 @@ public void removeOrderer(Orderer orderer) throws InvalidArgumentException {
9491035
ordererEndpointMap.remove(orderer.getEndpoint());
9501036
orderers.remove(orderer);
9511037
orderer.shutdown(true);
1038+
final Properties properties = orderer.getProperties();
1039+
if (properties != null) {
1040+
final String mspid = properties.getProperty(Orderer.ORDERER_ORGANIZATION_MSPID_PROPERTY);
1041+
if (!isNullOrEmpty(mspid)) {
1042+
synchronized (ordererMSPIDMap) {
1043+
final Collection<Orderer> orderers = ordererMSPIDMap.get(mspid);
1044+
orderers.remove(orderer);
1045+
if (orderers.isEmpty()) {
1046+
ordererMSPIDMap.remove(mspid);
1047+
}
1048+
}
1049+
}
1050+
}
9521051

9531052
}
9541053

1054+
/**
1055+
* Get orderers that belong to an organization from the organization's MSPID
1056+
* These values may not be available till after the channel is initialized.
1057+
*
1058+
* @param mspid The organizaiions MSPID
1059+
* @return A collection of Orderers that belong to the organization with that mspid.
1060+
* @throws InvalidArgumentException
1061+
*/
1062+
1063+
public Collection<Orderer> getOrderersForOrganization(String mspid) throws InvalidArgumentException {
1064+
1065+
if (isNullOrEmpty(mspid)) {
1066+
throw new InvalidArgumentException("The mspid parameter may not be null or empty string.");
1067+
}
1068+
synchronized (ordererMSPIDMap) {
1069+
1070+
final Collection<Orderer> orderers = ordererMSPIDMap.get(mspid);
1071+
if (orderers == null) {
1072+
return Collections.emptySet();
1073+
} else {
1074+
return new LinkedList<>(orderers); // return a copy.
1075+
}
1076+
}
1077+
}
1078+
1079+
/**
1080+
* Collection of strings which are the MSPIDs of all the orderer organization added.
1081+
* These values may not be available till after the channel is initialized.
1082+
*
1083+
* @return The collection of mspids
1084+
*/
1085+
1086+
public Collection<String> getOrderersOrganizationMSPIDs() {
1087+
synchronized (ordererMSPIDMap) {
1088+
return new LinkedList<>(ordererMSPIDMap.keySet());
1089+
}
1090+
}
1091+
9551092
public PeerOptions getPeersOptions(Peer peer) {
9561093
PeerOptions ret = peerOptionsMap.get(peer);
9571094
if (ret != null) {
@@ -1211,19 +1348,20 @@ public Map<String, Orderer> getEndpointMap() {
12111348
});
12121349

12131350
for (SDEndorser sdEndorser : sdNetwork.getEndorsers()) {
1351+
final String sdEndorserMspid = sdEndorser.getMspid();
12141352
Peer peer = peerEndpointMap.get(sdEndorser.getEndpoint());
12151353
if (null == peer) {
12161354
if (shutdown) {
12171355
return;
12181356
}
12191357

1220-
logger.debug(format("Channel %s doing channel update found new peer mspid: %s, endpoint: %s", name, sdEndorser.getMspid(), sdEndorser.getEndpoint()));
1358+
logger.debug(format("Channel %s doing channel update found new peer mspid: %s, endpoint: %s", name, sdEndorserMspid, sdEndorser.getEndpoint()));
12211359

12221360
sdPeerAddition.addPeer(new SDPeerAdditionInfo() {
12231361

12241362
@Override
12251363
public String getMspId() {
1226-
return sdEndorser.getMspid();
1364+
return sdEndorserMspid;
12271365
}
12281366

12291367
@Override
@@ -1261,6 +1399,23 @@ public Map<String, Peer> getEndpointMap() {
12611399
}
12621400

12631401
});
1402+
} else if (discoveryEndpoints.contains(sdEndorser.getEndpoint())) {
1403+
1404+
//hackfest here.... if the user didn't supply msspid retro fit for disovery peers
1405+
if (peer.getProperties() == null || isNullOrEmpty(peer.getProperties().getProperty(Peer.PEER_ORGANIZATION_MSPID_PROPERTY))) {
1406+
1407+
synchronized (peerMSPIDMap) {
1408+
peerMSPIDMap.computeIfAbsent(sdEndorserMspid, k -> new HashSet<>()).add(peer);
1409+
}
1410+
Properties properties = peer.getProperties();
1411+
if (properties == null) {
1412+
properties = new Properties();
1413+
}
1414+
properties.put(Peer.PEER_ORGANIZATION_MSPID_PROPERTY, sdEndorserMspid);
1415+
peer.setProperties(properties);
1416+
1417+
}
1418+
12641419
}
12651420

12661421
}
@@ -1475,6 +1630,8 @@ public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws In
14751630
properties.put("pemBytes", pemBytes);
14761631
}
14771632

1633+
properties.put(Orderer.ORDERER_ORGANIZATION_MSPID_PROPERTY, sdOrdererAdditionInfo.getMspId());
1634+
14781635
Orderer orderer = sdOrdererAdditionInfo.getClient().newOrderer(endpoint,
14791636
protocol + "//" + endpoint,
14801637
properties);
@@ -1516,6 +1673,8 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
15161673
properties.put("clientCertFile", clientCertFile);
15171674
}
15181675

1676+
properties.put(Peer.PEER_ORGANIZATION_MSPID_PROPERTY, sdPeerAddition.getMspId());
1677+
15191678
byte[] clientKeyBytes = (byte[]) findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
15201679
String clientKeyFile = (String) findClientProp(config, "clientKeyFile", mspid, endpoint, null);
15211680
if (null != clientKeyBytes) {
@@ -6146,6 +6305,9 @@ public synchronized void shutdown(boolean force) {
61466305
}
61476306
peers.clear(); // make sure.
61486307

6308+
peerMSPIDMap.clear();
6309+
ordererMSPIDMap.clear();
6310+
61496311
peerEndpointMap.clear();
61506312
ordererEndpointMap.clear();
61516313

src/main/java/org/hyperledger/fabric/sdk/HFClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ public Channel deSerializeChannel(byte[] channelBytes)
380380
* peerEventRegistrationWaitTime - Time in milliseconds to wait for peer eventing service registration.
381381
* </li>
382382
* <li>
383+
* org.hyperledger.fabric.sdk.peer.organization_mspid {@link Peer#PEER_ORGANIZATION_MSPID_PROPERTY} - Associates peer to an organization by its mspid.
384+
* </li>
385+
* <li>
383386
* grpc.NettyChannelBuilderOption.&lt;methodName&gt; where methodName is any method on
384387
* grpc ManagedChannelBuilder. If more than one argument to the method is needed then the
385388
* parameters need to be supplied in an array of Objects.
@@ -724,6 +727,9 @@ public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentExc
724727
* hostname verifications during TLS handshake.
725728
* </li>
726729
* <li>
730+
* org.hyperledger.fabric.sdk.orderer.organization_mspid {@link Orderer#ORDERER_ORGANIZATION_MSPID_PROPERTY} - Associates orderer to an organization by its mspid.
731+
* </li>
732+
* <li>
727733
* grpc.NettyChannelBuilderOption.&lt;methodName&gt; where methodName is any method on
728734
* grpc ManagedChannelBuilder. If more than one argument to the method is needed then the
729735
* parameters need to be supplied in an array of Objects.

src/main/java/org/hyperledger/fabric/sdk/NetworkConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,9 +864,19 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg, Map<String, JsonOb
864864
JsonArray jsonPeers = getJsonValueAsArray(jsonOrg.get("peers"));
865865
if (jsonPeers != null) {
866866
for (JsonValue peer : jsonPeers) {
867-
String peerName = getJsonValueAsString(peer);
867+
final String peerName = getJsonValueAsString(peer);
868868
if (peerName != null) {
869869
org.addPeerName(peerName);
870+
final Node node = peers.get(peerName);
871+
if (null != node) {
872+
if (null == node.properties) {
873+
node.properties = new Properties();
874+
}
875+
node.properties.put(Peer.PEER_ORGANIZATION_MSPID_PROPERTY, org.getMspId());
876+
877+
} else {
878+
throw new NetworkConfigurationException(format("Organization %s has peer %s listed not found in any channel peer list.", orgName, peerName));
879+
}
870880
}
871881
}
872882
}

src/main/java/org/hyperledger/fabric/sdk/Orderer.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131

3232
import static java.lang.String.format;
3333
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
34+
import static org.hyperledger.fabric.sdk.helper.Utils.isNullOrEmpty;
3435
import static org.hyperledger.fabric.sdk.helper.Utils.parseGrpcUrl;
3536

3637
/**
3738
* The Orderer class represents a orderer to which SDK sends deploy, invoke, or query requests.
3839
*/
3940
public class Orderer implements Serializable {
41+
public static final String ORDERER_ORGANIZATION_MSPID_PROPERTY = "org.hyperledger.fabric.sdk.orderer.organization_mspid";
4042
private static final Config config = Config.getConfig();
4143
private static final Log logger = LogFactory.getLog(Orderer.class);
4244
private static final long serialVersionUID = 4281642068914263247L;
@@ -62,7 +64,7 @@ public class Orderer implements Serializable {
6264

6365
this.name = name;
6466
this.url = url;
65-
this.properties = properties == null ? null : (Properties) properties.clone(); //keep our own copy.
67+
this.properties = properties == null ? new Properties() : (Properties) properties.clone(); //keep our own copy.
6668
logger.trace("Created " + toString());
6769

6870
}
@@ -253,8 +255,21 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
253255
id = config.getNextID();
254256
}
255257

258+
private transient String toString;
259+
256260
@Override
257261
public String toString() {
258-
return "Orderer{id: " + id + ", channelName: " + channelName + ", name:" + name + ", url: " + url + "}";
262+
String ltoString = toString;
263+
if (ltoString == null) {
264+
String mspid = "";
265+
266+
if (properties != null && !isNullOrEmpty(properties.getProperty(ORDERER_ORGANIZATION_MSPID_PROPERTY))) {
267+
mspid = ", mspid: " + properties.getProperty(ORDERER_ORGANIZATION_MSPID_PROPERTY);
268+
}
269+
270+
ltoString = "Orderer{id: " + id + ", channelName: " + channelName + ", name:" + name + ", url: " + url + mspid + "}";
271+
toString = ltoString;
272+
}
273+
return ltoString;
259274
}
260275
} // end Orderer

0 commit comments

Comments
 (0)