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

Commit 996bef7

Browse files
committed
FABJ-430 Networkconfig handlers
NetworkConfig does not allow to specify custom PeerOptions PS 02 Pass in the network config to handlers and provide peers org. Change-Id: Ica809450e01276984245525ee41017b90be64d04 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 1865ed8 commit 996bef7

File tree

4 files changed

+232
-22
lines changed

4 files changed

+232
-22
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ also has some descriptions on what they do. Most server timeout request can be
292292
You only ever *join* a peer belonging to _your own organization_ to a channel once at the beginning. You would only *add* peers
293293
from other organizations or peers of your own organization you've already *joined* like when recreating the channel SDK object.
294294

295+
296+
### Transaction sent to orderer results in future with exception *validation code: xxx* Where can I find what that means?
297+
298+
See Fabric protobuf protos/peer/transaction.proto's [TxValidationCode](https://github.com/hyperledger/fabric/blob/dce0e5d8e7bbce5315d0895e5d1460640700285b/protos/peer/transaction.proto#L115-L143)
299+
300+
295301
### java.security.InvalidKeyException: Illegal key size
296302

297303
If you get this error, this means your JDK does not capable of handling unlimited strength crypto algorithms. To fix this issue, You will need to download the JCE libraries for your version of JDK. Please follow the instructions <a href="http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters">here</a> to download and install the JCE for your version of the JDK.

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,46 @@ public static HFClient createNewInstance() {
151151
return new HFClient();
152152
}
153153

154+
/**
155+
* Configures a channel based on information loaded from a Network Config file.
156+
* Note that it is up to the caller to initialize the returned channel.
157+
*
158+
* @param channelName The name of the channel to be configured
159+
* @param networkConfig The network configuration to use to configure the channel
160+
* @param networkConfigAddPeerHandler A handler that will create and add peers to the channel.
161+
* @param networkConfigAddOrdererHandler A handler that will create orderers and add orderers to the channel.
162+
* @return The configured channel, or null if the channel is not defined in the configuration
163+
* @throws InvalidArgumentException
164+
*/
165+
public Channel loadChannelFromConfig(String channelName, NetworkConfig networkConfig,
166+
NetworkConfig.NetworkConfigAddPeerHandler networkConfigAddPeerHandler,
167+
NetworkConfig.NetworkConfigAddOrdererHandler networkConfigAddOrdererHandler) throws InvalidArgumentException, NetworkConfigurationException {
168+
clientCheck();
169+
170+
// Sanity checks
171+
if (channelName == null || channelName.isEmpty()) {
172+
throw new InvalidArgumentException("channelName must be specified");
173+
}
174+
175+
if (networkConfig == null) {
176+
throw new InvalidArgumentException("networkConfig must be specified");
177+
}
178+
179+
if (null == networkConfigAddPeerHandler) {
180+
throw new InvalidArgumentException("networkConfigAddPeerHandler is null.");
181+
}
182+
183+
if (null == networkConfigAddOrdererHandler) {
184+
throw new InvalidArgumentException("networkConfigAddOrdererHandler is null.");
185+
}
186+
187+
if (channels.containsKey(channelName)) {
188+
throw new InvalidArgumentException(format("Channel with name %s already exists", channelName));
189+
}
190+
191+
return networkConfig.loadChannel(this, channelName, networkConfigAddPeerHandler, networkConfigAddOrdererHandler);
192+
}
193+
154194
/**
155195
* Configures a channel based on information loaded from a Network Config file.
156196
* Note that it is up to the caller to initialize the returned channel.

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

Lines changed: 123 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.Collection;
2626
import java.util.Collections;
27-
import java.util.EnumSet;
2827
import java.util.HashMap;
2928
import java.util.HashSet;
3029
import java.util.LinkedList;
@@ -54,6 +53,7 @@
5453
import org.hyperledger.fabric.sdk.Peer.PeerRole;
5554
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
5655
import org.hyperledger.fabric.sdk.exception.NetworkConfigurationException;
56+
import org.hyperledger.fabric.sdk.helper.Utils;
5757
import org.hyperledger.fabric.sdk.identity.X509Enrollment;
5858
import org.hyperledger.fabric.sdk.security.CryptoSuite;
5959
import org.yaml.snakeyaml.Yaml;
@@ -166,8 +166,6 @@ public Properties getOrdererProperties(String name) throws InvalidArgumentExcept
166166

167167
}
168168

169-
170-
171169
/**
172170
* Set a specific peer's properties.
173171
*
@@ -381,6 +379,33 @@ public Collection<OrgInfo> getOrganizationInfos() {
381379
return Collections.unmodifiableCollection(organizations.values());
382380
}
383381

382+
/**
383+
* Find organizations for a peer.
384+
*
385+
* @param peerName name of peer
386+
* @return returns map of orgName to {@link OrgInfo} that the peer belongs to.
387+
* @throws InvalidArgumentException
388+
*/
389+
public Map<String, OrgInfo> getPeerOrgInfos(final String peerName) throws InvalidArgumentException {
390+
if (Utils.isNullOrEmpty(peerName)) {
391+
throw new InvalidArgumentException("peerName can not be null or empty.");
392+
}
393+
394+
if (organizations == null || organizations.isEmpty()) {
395+
return new HashMap<>();
396+
}
397+
398+
Map<String, OrgInfo> ret = new HashMap<>(16);
399+
organizations.forEach((name, orgInfo) -> {
400+
401+
if (orgInfo.getPeerNames().contains(peerName)) {
402+
ret.put(name, orgInfo);
403+
}
404+
});
405+
406+
return ret;
407+
}
408+
384409
/**
385410
* Returns the admin user associated with the client organization
386411
*
@@ -417,6 +442,17 @@ public UserInfo getPeerAdmin(String orgName) throws NetworkConfigurationExceptio
417442
* @return A configured Channel instance
418443
*/
419444
Channel loadChannel(HFClient client, String channelName) throws NetworkConfigurationException {
445+
return loadChannel(client, channelName, networkConfigAddPeerHandlerDefault, networkConfigAddOrdererHandlerDefault);
446+
}
447+
448+
/**
449+
* Returns a channel configured using the details in the Network Configuration file
450+
*
451+
* @param client The associated client
452+
* @param channelName The name of the channel
453+
* @return A configured Channel instance
454+
*/
455+
Channel loadChannel(HFClient client, String channelName, NetworkConfigAddPeerHandler networkConfigAddPeerHandler, NetworkConfigAddOrdererHandler networkConfigAddOrdererHandler) throws NetworkConfigurationException {
420456

421457
if (logger.isTraceEnabled()) {
422458
logger.trace(format("NetworkConfig.loadChannel: %s", channelName));
@@ -435,7 +471,7 @@ Channel loadChannel(HFClient client, String channelName) throws NetworkConfigura
435471
// Note that by rights this should never happen as HFClient.loadChannelFromConfig should have already checked for this!
436472
throw new NetworkConfigurationException(format("Channel %s is already configured in the client!", channelName));
437473
}
438-
channel = reconstructChannel(client, channelName, jsonChannel);
474+
channel = reconstructChannel(client, channelName, jsonChannel, networkConfigAddPeerHandler, networkConfigAddOrdererHandler);
439475
} else {
440476

441477
final Set<String> channelNames = getChannelNames();
@@ -502,10 +538,8 @@ private void createAllPeers() throws NetworkConfigurationException {
502538
throw new NetworkConfigurationException("INTERNAL ERROR: peers has already been initialized!");
503539
}
504540

505-
506541
peers = new HashMap<>();
507542

508-
509543
// peers is a JSON object containing a nested object for each peer
510544
JsonObject jsonPeers = getJsonObject(jsonConfig, "peers");
511545

@@ -584,7 +618,7 @@ private void createAllOrganizations(Map<String, JsonObject> foundCertificateAuth
584618
}
585619

586620
// Reconstructs an existing channel
587-
private Channel reconstructChannel(HFClient client, String channelName, JsonObject jsonChannel) throws NetworkConfigurationException {
621+
private Channel reconstructChannel(HFClient client, String channelName, JsonObject jsonChannel, NetworkConfigAddPeerHandler networkConfigAddPeerHandler, NetworkConfigAddOrdererHandler networkConfigAddOrdererHandler) throws NetworkConfigurationException {
588622

589623
Channel channel = null;
590624

@@ -593,19 +627,26 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
593627

594628
// orderers is an array of orderer name strings
595629
JsonArray ordererNames = getJsonValueAsArray(jsonChannel.get("orderers"));
596-
boolean foundOrderer = false;
597630

598631
//out("Orderer names: " + (ordererNames == null ? "null" : ordererNames.toString()));
599632
if (ordererNames != null) {
600633
for (JsonValue jsonVal : ordererNames) {
601634

602635
String ordererName = getJsonValueAsString(jsonVal);
603-
Orderer orderer = getOrderer(client, ordererName);
604-
if (orderer == null) {
636+
637+
// Orderer orderer = getOrderer(client, ordererName);
638+
Node node = orderers.get(ordererName);
639+
if (null == node) {
605640
throw new NetworkConfigurationException(format("Error constructing channel %s. Orderer %s not defined in configuration", channelName, ordererName));
606641
}
607-
channel.addOrderer(orderer);
608-
foundOrderer = true;
642+
643+
logger.debug(format("Channel %s, adding orderer %s, url: %s", channel.getName(), ordererName, node.url));
644+
Properties nodeProps = node.properties;
645+
if (null != nodeProps) {
646+
nodeProps = (Properties) nodeProps.clone();
647+
}
648+
649+
networkConfigAddOrdererHandler.addOrderer(this, client, channel, ordererName, node.url, nodeProps, node.jsonObject);
609650
}
610651
}
611652

@@ -628,8 +669,8 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
628669
throw new NetworkConfigurationException(format("Error constructing channel %s. Invalid peer entry: %s", channelName, peerName));
629670
}
630671

631-
Peer peer = getPeer(client, peerName);
632-
if (peer == null) {
672+
Node node = peers.get(peerName);
673+
if (node == null) {
633674
throw new NetworkConfigurationException(format("Error constructing channel %s. Peer %s not defined in configuration", channelName, peerName));
634675
}
635676

@@ -640,13 +681,15 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
640681
setPeerRole(channelName, peerOptions, jsonPeer, peerRole);
641682
}
642683

643-
foundPeer = true;
684+
logger.debug(format("Channel %s, adding peer %s, url: %s", channel.getName(), peerName, node.url));
644685

645-
if (peerOptions.peerRoles == null) { // means no roles were found but there is an event hub so define all roles but eventing.
646-
peerOptions.setPeerRoles(EnumSet.of(PeerRole.ENDORSING_PEER, PeerRole.CHAINCODE_QUERY, PeerRole.LEDGER_QUERY));
686+
Properties nodeProps = node.properties;
687+
if (null != nodeProps) {
688+
nodeProps = (Properties) nodeProps.clone();
647689
}
690+
networkConfigAddPeerHandler.addPeer(this, client, channel, peerName, node.url, nodeProps, peerOptions, node.jsonObject);
648691

649-
channel.addPeer(peer, peerOptions);
692+
foundPeer = true;
650693

651694
}
652695

@@ -664,6 +707,62 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
664707
return channel;
665708
}
666709

710+
/**
711+
* Interface defining handler for adding peers.
712+
*/
713+
714+
public interface NetworkConfigAddPeerHandler {
715+
716+
/**
717+
* @param networkConfig The network configuration.
718+
* @param client The client to be used to create the peer.
719+
* @param channel The channel the peer is to be added.
720+
* @param peerName The peer's name.
721+
* @param peerURL The peers's url
722+
* @param peerProperties properties that were found in the networkconfig
723+
* @param peerOptions options when adding peer to the channel.
724+
* @param jsonPeer json peer was created
725+
* @throws NetworkConfigurationException
726+
*/
727+
void addPeer(NetworkConfig networkConfig, HFClient client, Channel channel, String peerName, String peerURL, Properties peerProperties, PeerOptions peerOptions, JsonObject jsonPeer) throws NetworkConfigurationException;
728+
}
729+
730+
private static final NetworkConfigAddPeerHandler networkConfigAddPeerHandlerDefault = (networkConfig, client, channel, peerName, peerURL, peerProperties, peerOptions, jsonPeer) -> {
731+
try {
732+
Peer peer = client.newPeer(peerName, peerURL, peerProperties);
733+
channel.addPeer(peer, peerOptions);
734+
} catch (Exception e) {
735+
throw new NetworkConfigurationException(format("Error on creating channel %s peer %s", channel.getName(), peerName), e);
736+
}
737+
};
738+
739+
/**
740+
* Interface defining handler for adding orderers.
741+
*/
742+
public interface NetworkConfigAddOrdererHandler {
743+
744+
/**
745+
* @param networkConfig The network configuration.
746+
* @param client The client to be used to create the orderer.
747+
* @param channel The channel the orderer is to be added.
748+
* @param ordererName The orderer's name.
749+
* @param ordererURL The orderers's url
750+
* @param ordererProperties properties that were found in the networkconfig
751+
* @param jsonOrderer json orderer was created
752+
* @throws NetworkConfigurationException
753+
*/
754+
void addOrderer(NetworkConfig networkConfig, HFClient client, Channel channel, String ordererName, String ordererURL, Properties ordererProperties, JsonObject jsonOrderer) throws NetworkConfigurationException;
755+
}
756+
757+
private static final NetworkConfigAddOrdererHandler networkConfigAddOrdererHandlerDefault = (networkConfig, client, channel, ordererName, ordererURL, ordererProperties, jsonOrderer) -> {
758+
try {
759+
Orderer orderer = client.newOrderer(ordererName, ordererURL, ordererProperties);
760+
channel.addOrderer(orderer);
761+
} catch (Exception e) {
762+
throw new NetworkConfigurationException(format("Error on creating channel %s orderer %s", channel.getName(), ordererName), e);
763+
}
764+
};
765+
667766
private static void setPeerRole(String channelName, PeerOptions peerOptions, JsonObject jsonPeer, PeerRole role) throws NetworkConfigurationException {
668767
String propName = roleNameRemap(role);
669768
JsonValue val = jsonPeer.get(propName);
@@ -732,7 +831,7 @@ private Node createNode(String nodeName, JsonObject jsonNode, String urlPropName
732831
// Extract the pem details
733832
getTLSCerts(nodeName, jsonNode, props);
734833

735-
return new Node(nodeName, url, props);
834+
return new Node(nodeName, url, props, jsonNode);
736835
}
737836

738837
private void getTLSCerts(String nodeName, JsonObject jsonOrderer, Properties props) {
@@ -1036,12 +1135,14 @@ private class Node {
10361135

10371136
private final String name;
10381137
private final String url;
1138+
public JsonObject jsonObject;
10391139
private Properties properties;
10401140

1041-
Node(String name, String url, Properties properties) {
1141+
private Node(String name, String url, Properties properties, JsonObject jsonObject) {
10421142
this.url = url;
10431143
this.name = name;
10441144
this.properties = properties;
1145+
this.jsonObject = jsonObject;
10451146
}
10461147

10471148
private String getName() {
@@ -1179,11 +1280,11 @@ public String getMspId() {
11791280
}
11801281

11811282
public List<String> getPeerNames() {
1182-
return peerNames;
1283+
return new LinkedList<>(peerNames);
11831284
}
11841285

11851286
public List<CAInfo> getCertificateAuthorities() {
1186-
return certificateAuthorities;
1287+
return new LinkedList<>(certificateAuthorities);
11871288
}
11881289

11891290
/**

0 commit comments

Comments
 (0)