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

Commit 96f5327

Browse files
andrew-colemanbestbeforetoday
authored andcommitted
FGJ-33 ‘As Localhost’ option for discovery
For use when running fabric network locally in docker. Set the env-var ORG_HYPERLEDGER_FABRIC_SDK_SERVICE_DISCOVERY_AS_LOCALHOST=true Aligns behaviour with fabric-sdk-node Change-Id: I3291c3caca974b0280272ec6230393827831adac Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
1 parent 84681e1 commit 96f5327

File tree

7 files changed

+176
-25
lines changed

7 files changed

+176
-25
lines changed

.classpath

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/classes" path="target/generated-sources/protobuf/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="src" output="target/classes" path="target/generated-sources/protobuf/grpc-java">
16+
<attributes>
17+
<attribute name="optional" value="true"/>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
22+
<attributes>
23+
<attribute name="optional" value="true"/>
24+
<attribute name="maven.pomderived" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
28+
<attributes>
29+
<attribute name="maven.pomderived" value="true"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
33+
<attributes>
34+
<attribute name="maven.pomderived" value="true"/>
35+
</attributes>
36+
</classpathentry>
37+
<classpathentry excluding="**" kind="src" output="target/classes" path="src">
38+
<attributes>
39+
<attribute name="maven.pomderived" value="true"/>
40+
</attributes>
41+
</classpathentry>
42+
<classpathentry kind="output" path="target/classes"/>
43+
</classpath>

.project

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>fabric-sdk-java</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.jdt.core.javanature</nature>
26+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
27+
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
28+
</natures>
29+
</projectDescription>

pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
<tag>fabric-sdk-java-1.0</tag>
2929
</scm>
3030
<properties>
31-
<grpc.version>1.20.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
32-
<protobuf.version>3.7.1</protobuf.version>
33-
<bouncycastle.version>1.61</bouncycastle.version>
34-
<httpclient.version>4.5.8</httpclient.version>
31+
<grpc.version>1.23.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
32+
<protobuf.version>3.10.0</protobuf.version>
33+
<bouncycastle.version>1.63</bouncycastle.version>
34+
<httpclient.version>4.5.10</httpclient.version>
3535
<javadoc.version>3.0.1</javadoc.version>
3636
<skipITs>true</skipITs>
3737
<alpn-boot-version>8.1.7.v20160121</alpn-boot-version>
@@ -94,12 +94,12 @@
9494
<dependency>
9595
<groupId>io.netty</groupId>
9696
<artifactId>netty-tcnative-boringssl-static</artifactId>
97-
<version>2.0.25.Final</version>
97+
<version>2.0.26.Final</version>
9898
</dependency>
9999
<dependency>
100100
<groupId>io.netty</groupId>
101101
<artifactId>netty-codec-http2</artifactId>
102-
<version>4.1.35.Final</version>
102+
<version>4.1.41.Final</version>
103103
</dependency>
104104
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
105105
<dependency>
@@ -192,7 +192,7 @@
192192
<dependency>
193193
<groupId>org.yaml</groupId>
194194
<artifactId>snakeyaml</artifactId>
195-
<version>1.24</version>
195+
<version>1.25</version>
196196
</dependency>
197197

198198
<!-- https://mvnrepository.com/artifact/org.miracl.milagro.amcl/milagro-crypto-java -->
@@ -236,7 +236,7 @@
236236
<plugin>
237237
<groupId>org.apache.maven.plugins</groupId>
238238
<artifactId>maven-surefire-plugin</artifactId>
239-
<version>3.0.0-M3</version>
239+
<version>2.19.1</version>
240240
<configuration>
241241
<argLine>${surefireArgLine}</argLine>
242242
<includes>
@@ -248,7 +248,7 @@
248248
<plugin>
249249
<groupId>org.apache.maven.plugins</groupId>
250250
<artifactId>maven-failsafe-plugin</artifactId>
251-
<version>3.0.0-M3</version>
251+
<version>2.19.1</version>
252252
<configuration>
253253
<argLine>${failsafeArgLine}</argLine>
254254
<includes>

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public class Channel implements Serializable {
204204
private transient ScheduledExecutorService sweeperExecutorService;
205205
private transient String blh = null;
206206
private transient ServiceDiscovery serviceDiscovery;
207+
private static final boolean asLocalhost = config.discoverAsLocalhost();
207208

208209
{
209210
for (Peer.PeerRole peerRole : EnumSet.allOf(PeerRole.class)) {
@@ -1340,6 +1341,11 @@ public byte[][] getTLSIntermediateCerts() {
13401341
public Map<String, Orderer> getEndpointMap() {
13411342
return Collections.unmodifiableMap(Channel.this.ordererEndpointMap);
13421343
}
1344+
1345+
@Override
1346+
public Properties getProperties() {
1347+
return sdOrderer.getProperties();
1348+
}
13431349
});
13441350
}
13451351

@@ -1418,6 +1424,20 @@ public Map<String, Peer> getEndpointMap() {
14181424
return Collections.unmodifiableMap(Channel.this.peerEndpointMap);
14191425
}
14201426

1427+
@Override
1428+
public String getName() {
1429+
return sdEndorser.getName();
1430+
}
1431+
1432+
@Override
1433+
public Properties getProperties() {
1434+
Properties properties = new Properties();
1435+
if (asLocalhost) {
1436+
properties.put("hostnameOverride",
1437+
sdEndorser.getName().substring(0, sdEndorser.getName().lastIndexOf(':')));
1438+
}
1439+
return properties;
1440+
}
14211441
});
14221442
} else if (discoveryEndpoints.contains(sdEndorser.getEndpoint())) {
14231443

@@ -1450,6 +1470,7 @@ public void setServiceDiscoveryProperties(Properties serviceDiscoveryProperties)
14501470
}
14511471

14521472
public interface SDPeerAdditionInfo {
1473+
String getName();
14531474

14541475
String getMspId();
14551476

@@ -1489,6 +1510,7 @@ default byte[] getAllTLSCerts() throws ServiceDiscoveryException {
14891510

14901511
Map<String, Peer> getEndpointMap();
14911512

1513+
Properties getProperties();
14921514
}
14931515

14941516
public interface SDPeerAddition {
@@ -1555,6 +1577,8 @@ public interface SDOrdererAdditionInfo {
15551577

15561578
String getEndpoint();
15571579

1580+
Properties getProperties();
1581+
15581582
String getMspId();
15591583

15601584
Channel getChannel();
@@ -1613,7 +1637,7 @@ public SDOrdererDefaultAddition(Properties config) {
16131637
@Override
16141638
public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws InvalidArgumentException, ServiceDiscoveryException {
16151639

1616-
Properties properties = new Properties();
1640+
Properties properties = sdOrdererAdditionInfo.getProperties();
16171641
final String endpoint = sdOrdererAdditionInfo.getEndpoint();
16181642
final String mspid = sdOrdererAdditionInfo.getMspId();
16191643

@@ -1672,7 +1696,8 @@ public SDOPeerDefaultAddition(Properties config) {
16721696
@Override
16731697
public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentException, ServiceDiscoveryException {
16741698

1675-
Properties properties = new Properties();
1699+
Properties properties = sdPeerAddition.getProperties();
1700+
final String name = sdPeerAddition.getName();
16761701
final String endpoint = sdPeerAddition.getEndpoint();
16771702
final String mspid = sdPeerAddition.getMspId();
16781703

@@ -1713,12 +1738,12 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
17131738
properties.put("pemBytes", pemBytes);
17141739
}
17151740

1716-
peer = sdPeerAddition.getClient().newPeer(endpoint,
1741+
peer = sdPeerAddition.getClient().newPeer(name,
17171742
protocol + "//" + endpoint,
17181743
properties);
17191744

17201745
sdPeerAddition.getChannel().addPeer(peer, createPeerOptions().setPeerRoles(
1721-
EnumSet.of(PeerRole.ENDORSING_PEER, PeerRole.EVENT_SOURCE, PeerRole.LEDGER_QUERY, PeerRole.CHAINCODE_QUERY))); //application can decide on roles.
1746+
EnumSet.of(PeerRole.ENDORSING_PEER, PeerRole.EVENT_SOURCE, PeerRole.LEDGER_QUERY, PeerRole.CHAINCODE_QUERY, PeerRole.SERVICE_DISCOVERY))); //application can decide on roles.
17221747

17231748
return peer;
17241749
}
@@ -4579,6 +4604,21 @@ public byte[][] getTLSIntermediateCerts() {
45794604
public Map<String, Peer> getEndpointMap() {
45804605
return Collections.unmodifiableMap(Channel.this.peerEndpointMap);
45814606
}
4607+
4608+
@Override
4609+
public String getName() {
4610+
return sdEndorser.getName();
4611+
}
4612+
4613+
@Override
4614+
public Properties getProperties() {
4615+
Properties properties = new Properties();
4616+
if (asLocalhost) {
4617+
properties.put("hostnameOverride",
4618+
sdEndorser.getName().substring(0, sdEndorser.getName().lastIndexOf(':')));
4619+
}
4620+
return properties;
4621+
}
45824622
});
45834623
}
45844624
endorsers.put(sdEndorser, epeer);

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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.Objects;
29+
import java.util.Properties;
2930
import java.util.Random;
3031
import java.util.Set;
3132
import java.util.concurrent.Executors;
@@ -66,6 +67,7 @@ public class ServiceDiscovery {
6667
private final TransactionContext transactionContext;
6768
private final String channelName;
6869
private volatile Map<String, SDChaindcode> chaindcodeMap = new HashMap<>();
70+
private static final boolean asLocalhost = config.discoverAsLocalhost();
6971

7072
ServiceDiscovery(Channel channel, Collection<Peer> serviceDiscoveryPeers, TransactionContext transactionContext) {
7173
this.serviceDiscoveryPeers = serviceDiscoveryPeers;
@@ -305,7 +307,8 @@ SDNetwork networkDiscovery(TransactionContext ltransactionContext, boolean force
305307
Protocol.Endpoints value = i.getValue();
306308
for (Protocol.Endpoint l : value.getEndpointList()) {
307309
logger.trace(format("Channel: %s peer: %s discovered orderer MSPID: %s, endpoint: %s:%s", channelName, serviceDiscoveryPeer, mspid, l.getHost(), l.getPort()));
308-
String endpoint = (l.getHost() + ":" + l.getPort()).trim().toLowerCase();
310+
String host = asLocalhost ? "localhost" : l.getHost();
311+
String endpoint = (host + ":" + l.getPort()).trim().toLowerCase();
309312

310313
SDOrderer discoveredAlready = ordererEndpoints.get(endpoint);
311314
if (discoveredAlready != null) {
@@ -317,7 +320,12 @@ SDNetwork networkDiscovery(TransactionContext ltransactionContext, boolean force
317320
continue;
318321
}
319322

320-
final SDOrderer sdOrderer = new SDOrderer(mspid, endpoint, lsdNetwork.getTlsCerts(mspid), lsdNetwork.getTlsIntermediateCerts(mspid));
323+
Properties properties = new Properties();
324+
if (asLocalhost) {
325+
properties.put("hostnameOverride", l.getHost());
326+
}
327+
328+
final SDOrderer sdOrderer = new SDOrderer(mspid, endpoint, lsdNetwork.getTlsCerts(mspid), lsdNetwork.getTlsIntermediateCerts(mspid), properties);
321329

322330
ordererEndpoints.put(sdOrderer.getEndPoint(), sdOrderer);
323331
}
@@ -333,7 +341,7 @@ SDNetwork networkDiscovery(TransactionContext ltransactionContext, boolean force
333341
final Protocol.Peers peer = peers.getValue();
334342

335343
for (Protocol.Peer pp : peer.getPeersList()) {
336-
SDEndorser ppp = new SDEndorser(pp, lsdNetwork.getTlsCerts(mspId), lsdNetwork.getTlsIntermediateCerts(mspId));
344+
SDEndorser ppp = new SDEndorser(pp, lsdNetwork.getTlsCerts(mspId), lsdNetwork.getTlsIntermediateCerts(mspId), asLocalhost);
337345

338346
SDEndorser discoveredAlready = lsdNetwork.endorsers.get(ppp.getEndpoint());
339347
if (null != discoveredAlready) {
@@ -370,12 +378,14 @@ public static class SDOrderer {
370378
private final Collection<byte[]> tlsCerts;
371379
private final Collection<byte[]> tlsIntermediateCerts;
372380
private final String endPoint;
381+
private final Properties properties;
373382

374-
SDOrderer(String mspid, String endPoint, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts) {
383+
SDOrderer(String mspid, String endPoint, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts, Properties properties) {
375384
this.mspid = mspid;
376385
this.endPoint = endPoint;
377386
this.tlsCerts = tlsCerts;
378387
this.tlsIntermediateCerts = tlsIntermediateCerts;
388+
this.properties = properties;
379389
}
380390

381391
public Collection<byte[]> getTlsIntermediateCerts() {
@@ -393,6 +403,10 @@ public String getMspid() {
393403
public Collection<byte[]> getTlsCerts() {
394404
return tlsCerts;
395405
}
406+
407+
public Properties getProperties() {
408+
return properties;
409+
}
396410
}
397411

398412
Map<String, SDChaindcode> discoverEndorserEndpoints(TransactionContext transactionContext, List<List<ServiceDiscoveryChaincodeCalls>> chaincodeNames) throws ServiceDiscoveryException {
@@ -520,7 +534,7 @@ Map<String, SDChaindcode> discoverEndorserEndpoints(TransactionContext transacti
520534
List<SDEndorser> sdEndorsers = new LinkedList<>();
521535

522536
for (Protocol.Peer pp : peers.getPeersList()) {
523-
SDEndorser ppp = new SDEndorser(pp, null, null);
537+
SDEndorser ppp = new SDEndorser(pp, null, null, asLocalhost);
524538
final String endPoint = ppp.getEndpoint();
525539
SDEndorser nppp = sdNetwork.getEndorserByEndpoint(endPoint);
526540
if (null == nppp) {
@@ -1096,19 +1110,23 @@ public static class SDEndorser {
10961110
private List<Message.Chaincode> chaincodesList;
10971111
// private final Protocol.Peer proto;
10981112
private String endPoint = null;
1113+
private String name = null;
10991114
private String mspid;
11001115
private long ledgerHeight = -1L;
11011116
private final Collection<byte[]> tlsCerts;
11021117
private final Collection<byte[]> tlsIntermediateCerts;
1118+
private final boolean asLocalhost;
11031119

11041120
SDEndorser() { // for testing only
11051121
tlsCerts = null;
11061122
tlsIntermediateCerts = null;
1123+
asLocalhost = false;
11071124
}
11081125

1109-
SDEndorser(Protocol.Peer peerRet, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts) {
1126+
SDEndorser(Protocol.Peer peerRet, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts, boolean asLocalhost) {
11101127
this.tlsCerts = tlsCerts;
11111128
this.tlsIntermediateCerts = tlsIntermediateCerts;
1129+
this.asLocalhost = asLocalhost;
11121130

11131131
parseEndpoint(peerRet);
11141132
parseLedgerHeight(peerRet);
@@ -1123,6 +1141,10 @@ Collection<byte[]> getTLSIntermediateCerts() {
11231141
return tlsIntermediateCerts;
11241142
}
11251143

1144+
public String getName() {
1145+
return name;
1146+
}
1147+
11261148
public String getEndpoint() {
11271149
return endPoint;
11281150
}
@@ -1151,9 +1173,13 @@ private String parseEndpoint(Protocol.Peer peerRet) throws InvalidProtocolBuffer
11511173
throw new RuntimeException(format("Error %s", "bad"));
11521174
}
11531175
Message.AliveMessage aliveMsg = gossipMessageMemberInfo.getAliveMsg();
1154-
endPoint = aliveMsg.getMembership().getEndpoint();
1155-
if (endPoint != null) {
1156-
endPoint = endPoint.toLowerCase().trim(); //makes easier on comparing.
1176+
name = aliveMsg.getMembership().getEndpoint();
1177+
if (name != null) {
1178+
if (asLocalhost) {
1179+
endPoint = "localhost" + name.substring(name.lastIndexOf(':'));
1180+
} else {
1181+
endPoint = name.toLowerCase().trim(); //makes easier on comparing.
1182+
}
11571183
}
11581184
} catch (InvalidProtocolBufferException e) {
11591185
throw new InvalidProtocolBufferRuntimeException(e);

0 commit comments

Comments
 (0)