Skip to content

Commit

Permalink
FABJ-434 Expose org info for peers
Browse files Browse the repository at this point in the history
Change-Id: Ie346fa5d45089f4d0a86c9daf011bf5baffc5e67
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed May 24, 2019
1 parent a8ef7ce commit 519cc0a
Show file tree
Hide file tree
Showing 10 changed files with 452 additions and 14 deletions.
170 changes: 166 additions & 4 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public class Channel implements Serializable {
private final Collection<Peer> peers = Collections.synchronizedSet(new HashSet<>());
private final Map<Peer, PeerOptions> peerOptionsMap = Collections.synchronizedMap(new HashMap<>());
private transient Map<String, Peer> peerEndpointMap = Collections.synchronizedMap(new HashMap<>());
private Map<String, Collection<Peer>> peerMSPIDMap = new HashMap<>();
private Map<String, Collection<Orderer>> ordererMSPIDMap = new HashMap<>();
private final Map<PeerRole, Set<Peer>> peerRoleSetMap = Collections.synchronizedMap(new HashMap<>());
private transient String chaincodeEventUpgradeListenerHandle;
private transient String transactionListenerProcessorHandle;
Expand Down Expand Up @@ -642,6 +644,7 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
peers.add(peer);
peerOptionsMap.put(peer, peerOptions.clone());
peerEndpointMap.put(peer.getEndpoint(), peer);
addPeerMSPIDMap(peer);

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

Expand All @@ -652,9 +655,7 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
TLSCertificateKeyPair tlsCertificateKeyPair = tlsCertificateBuilder.clientCert();
peer.setTLSCertificateKeyPair(tlsCertificateKeyPair);
}

discoveryEndpoints.add(peer.getEndpoint());

}

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

private void addPeerMSPIDMap(final Peer peer) {
Properties properties = peer.getProperties();

if (null != properties) {
final String mspid = properties.getProperty(Peer.PEER_ORGANIZATION_MSPID_PROPERTY);
if (!isNullOrEmpty(mspid)) {
logger.debug(format("Channel %s mapping peer %s to mspid %s", name, peer, mspid));
synchronized (peerMSPIDMap) {
peerMSPIDMap.computeIfAbsent(mspid, k -> new HashSet<Peer>()).add(peer);
}
}
}
}

private void removePeerMSPIDMap(final Peer peer) {
Properties properties = peer.getProperties();

if (null != properties) {
final String mspid = properties.getProperty(Peer.PEER_ORGANIZATION_MSPID_PROPERTY);
if (!isNullOrEmpty(mspid)) {
logger.debug(format("Channel %s removing mapping peer %s to mspid %s", name, peer, mspid));
synchronized (peerMSPIDMap) {
final Collection<Peer> peers = peerMSPIDMap.get(mspid);
if (peers != null) {
peers.remove(peer);
if (peers.isEmpty()) {
peerMSPIDMap.remove(mspid);
}

}
}
}
}
}

/**
* Get peers that belong to an organization from the organization's MSPID
* These values may not be available till after the channel is initialized.
*
* @param mspid The organizaiions MSPID
* @return A collection of Peers that belong to the organization with that mspid.
* @throws InvalidArgumentException
*/

public Collection<Peer> getPeersForOrganization(String mspid) throws InvalidArgumentException {

if (isNullOrEmpty(mspid)) {
throw new InvalidArgumentException("The mspid parameter may not be null or empty string.");
}
synchronized (peerMSPIDMap) {

final Collection<Peer> peers = peerMSPIDMap.get(mspid);
if (peers == null) {
return Collections.emptySet();
} else {
return new LinkedList<>(peers); // return a copy.
}
}
}

/**
* Collection of strings which are the MSPIDs of all the peer organization added.
* These values may not be available till after the channel is initialized.
*
* @return The collection of mspids
*/

public Collection<String> getPeersOrganizationMSPIDs() {
synchronized (peerMSPIDMap) {
return new LinkedList<>(peerMSPIDMap.keySet());
}
}

/**
* Join the peer to the channel. The peer is added with all roles see {@link PeerOptions}
*
Expand Down Expand Up @@ -808,6 +882,7 @@ public Channel joinPeer(Orderer orderer, Peer peer, PeerOptions peerOptions) thr
} catch (Exception e) {
logger.error(format("%s removing peer %s due to exception %s", toString(), peer, e.getMessage()));
peers.remove(peer);
removePeerMSPIDMap(peer);
logger.error(e);
throw new ProposalException(e.getMessage(), e);
}
Expand Down Expand Up @@ -901,6 +976,7 @@ private void removePeerInternal(Peer peer) {
peers.remove(peer);
peerOptionsMap.remove(peer);
peerEndpointMap.remove(peer.getEndpoint());
removePeerMSPIDMap(peer);

for (Set<Peer> peerRoleSet : peerRoleSetMap.values()) {
peerRoleSet.remove(peer);
Expand Down Expand Up @@ -931,6 +1007,16 @@ public Channel addOrderer(Orderer orderer) throws InvalidArgumentException {
orderer.setChannel(this);
ordererEndpointMap.put(orderer.getEndpoint(), orderer);
orderers.add(orderer);
final Properties properties = orderer.getProperties();
if (properties != null) {
final String mspid = properties.getProperty(Orderer.ORDERER_ORGANIZATION_MSPID_PROPERTY);
if (!isNullOrEmpty(mspid)) {
synchronized (ordererMSPIDMap) {
ordererMSPIDMap.computeIfAbsent(mspid, k -> new HashSet<>()).add(orderer);
}
}
}

return this;
}

Expand All @@ -949,9 +1035,60 @@ public void removeOrderer(Orderer orderer) throws InvalidArgumentException {
ordererEndpointMap.remove(orderer.getEndpoint());
orderers.remove(orderer);
orderer.shutdown(true);
final Properties properties = orderer.getProperties();
if (properties != null) {
final String mspid = properties.getProperty(Orderer.ORDERER_ORGANIZATION_MSPID_PROPERTY);
if (!isNullOrEmpty(mspid)) {
synchronized (ordererMSPIDMap) {
final Collection<Orderer> orderers = ordererMSPIDMap.get(mspid);
orderers.remove(orderer);
if (orderers.isEmpty()) {
ordererMSPIDMap.remove(mspid);
}
}
}
}

}

/**
* Get orderers that belong to an organization from the organization's MSPID
* These values may not be available till after the channel is initialized.
*
* @param mspid The organizaiions MSPID
* @return A collection of Orderers that belong to the organization with that mspid.
* @throws InvalidArgumentException
*/

public Collection<Orderer> getOrderersForOrganization(String mspid) throws InvalidArgumentException {

if (isNullOrEmpty(mspid)) {
throw new InvalidArgumentException("The mspid parameter may not be null or empty string.");
}
synchronized (ordererMSPIDMap) {

final Collection<Orderer> orderers = ordererMSPIDMap.get(mspid);
if (orderers == null) {
return Collections.emptySet();
} else {
return new LinkedList<>(orderers); // return a copy.
}
}
}

/**
* Collection of strings which are the MSPIDs of all the orderer organization added.
* These values may not be available till after the channel is initialized.
*
* @return The collection of mspids
*/

public Collection<String> getOrderersOrganizationMSPIDs() {
synchronized (ordererMSPIDMap) {
return new LinkedList<>(ordererMSPIDMap.keySet());
}
}

public PeerOptions getPeersOptions(Peer peer) {
PeerOptions ret = peerOptionsMap.get(peer);
if (ret != null) {
Expand Down Expand Up @@ -1211,19 +1348,20 @@ public Map<String, Orderer> getEndpointMap() {
});

for (SDEndorser sdEndorser : sdNetwork.getEndorsers()) {
final String sdEndorserMspid = sdEndorser.getMspid();
Peer peer = peerEndpointMap.get(sdEndorser.getEndpoint());
if (null == peer) {
if (shutdown) {
return;
}

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

sdPeerAddition.addPeer(new SDPeerAdditionInfo() {

@Override
public String getMspId() {
return sdEndorser.getMspid();
return sdEndorserMspid;
}

@Override
Expand Down Expand Up @@ -1261,6 +1399,23 @@ public Map<String, Peer> getEndpointMap() {
}

});
} else if (discoveryEndpoints.contains(sdEndorser.getEndpoint())) {

//hackfest here.... if the user didn't supply msspid retro fit for disovery peers
if (peer.getProperties() == null || isNullOrEmpty(peer.getProperties().getProperty(Peer.PEER_ORGANIZATION_MSPID_PROPERTY))) {

synchronized (peerMSPIDMap) {
peerMSPIDMap.computeIfAbsent(sdEndorserMspid, k -> new HashSet<>()).add(peer);
}
Properties properties = peer.getProperties();
if (properties == null) {
properties = new Properties();
}
properties.put(Peer.PEER_ORGANIZATION_MSPID_PROPERTY, sdEndorserMspid);
peer.setProperties(properties);

}

}

}
Expand Down Expand Up @@ -1475,6 +1630,8 @@ public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws In
properties.put("pemBytes", pemBytes);
}

properties.put(Orderer.ORDERER_ORGANIZATION_MSPID_PROPERTY, sdOrdererAdditionInfo.getMspId());

Orderer orderer = sdOrdererAdditionInfo.getClient().newOrderer(endpoint,
protocol + "//" + endpoint,
properties);
Expand Down Expand Up @@ -1516,6 +1673,8 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
properties.put("clientCertFile", clientCertFile);
}

properties.put(Peer.PEER_ORGANIZATION_MSPID_PROPERTY, sdPeerAddition.getMspId());

byte[] clientKeyBytes = (byte[]) findClientProp(config, "clientKeyBytes", mspid, endpoint, null);
String clientKeyFile = (String) findClientProp(config, "clientKeyFile", mspid, endpoint, null);
if (null != clientKeyBytes) {
Expand Down Expand Up @@ -6146,6 +6305,9 @@ public synchronized void shutdown(boolean force) {
}
peers.clear(); // make sure.

peerMSPIDMap.clear();
ordererMSPIDMap.clear();

peerEndpointMap.clear();
ordererEndpointMap.clear();

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/hyperledger/fabric/sdk/HFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ public Channel deSerializeChannel(byte[] channelBytes)
* peerEventRegistrationWaitTime - Time in milliseconds to wait for peer eventing service registration.
* </li>
* <li>
* org.hyperledger.fabric.sdk.peer.organization_mspid {@link Peer#PEER_ORGANIZATION_MSPID_PROPERTY} - Associates peer to an organization by its mspid.
* </li>
* <li>
* grpc.NettyChannelBuilderOption.&lt;methodName&gt; where methodName is any method on
* grpc ManagedChannelBuilder. If more than one argument to the method is needed then the
* parameters need to be supplied in an array of Objects.
Expand Down Expand Up @@ -724,6 +727,9 @@ public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentExc
* hostname verifications during TLS handshake.
* </li>
* <li>
* org.hyperledger.fabric.sdk.orderer.organization_mspid {@link Orderer#ORDERER_ORGANIZATION_MSPID_PROPERTY} - Associates orderer to an organization by its mspid.
* </li>
* <li>
* grpc.NettyChannelBuilderOption.&lt;methodName&gt; where methodName is any method on
* grpc ManagedChannelBuilder. If more than one argument to the method is needed then the
* parameters need to be supplied in an array of Objects.
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/hyperledger/fabric/sdk/NetworkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,19 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg, Map<String, JsonOb
JsonArray jsonPeers = getJsonValueAsArray(jsonOrg.get("peers"));
if (jsonPeers != null) {
for (JsonValue peer : jsonPeers) {
String peerName = getJsonValueAsString(peer);
final String peerName = getJsonValueAsString(peer);
if (peerName != null) {
org.addPeerName(peerName);
final Node node = peers.get(peerName);
if (null != node) {
if (null == node.properties) {
node.properties = new Properties();
}
node.properties.put(Peer.PEER_ORGANIZATION_MSPID_PROPERTY, org.getMspId());

} else {
throw new NetworkConfigurationException(format("Organization %s has peer %s listed not found in any channel peer list.", orgName, peerName));
}
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/org/hyperledger/fabric/sdk/Orderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

import static java.lang.String.format;
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.Utils.isNullOrEmpty;
import static org.hyperledger.fabric.sdk.helper.Utils.parseGrpcUrl;

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

this.name = name;
this.url = url;
this.properties = properties == null ? null : (Properties) properties.clone(); //keep our own copy.
this.properties = properties == null ? new Properties() : (Properties) properties.clone(); //keep our own copy.
logger.trace("Created " + toString());

}
Expand Down Expand Up @@ -253,8 +255,21 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
id = config.getNextID();
}

private transient String toString;

@Override
public String toString() {
return "Orderer{id: " + id + ", channelName: " + channelName + ", name:" + name + ", url: " + url + "}";
String ltoString = toString;
if (ltoString == null) {
String mspid = "";

if (properties != null && !isNullOrEmpty(properties.getProperty(ORDERER_ORGANIZATION_MSPID_PROPERTY))) {
mspid = ", mspid: " + properties.getProperty(ORDERER_ORGANIZATION_MSPID_PROPERTY);
}

ltoString = "Orderer{id: " + id + ", channelName: " + channelName + ", name:" + name + ", url: " + url + mspid + "}";
toString = ltoString;
}
return ltoString;
}
} // end Orderer
Loading

0 comments on commit 519cc0a

Please sign in to comment.