Skip to content

Commit

Permalink
FGJ-33 ‘As Localhost’ option for discovery
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
andrew-coleman authored and bestbeforetoday committed Oct 17, 2019
1 parent 84681e1 commit 96f5327
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 25 deletions.
43 changes: 43 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/protobuf/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/protobuf/grpc-java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
29 changes: 29 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>fabric-sdk-java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>
18 changes: 9 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<tag>fabric-sdk-java-1.0</tag>
</scm>
<properties>
<grpc.version>1.20.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
<protobuf.version>3.7.1</protobuf.version>
<bouncycastle.version>1.61</bouncycastle.version>
<httpclient.version>4.5.8</httpclient.version>
<grpc.version>1.23.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
<protobuf.version>3.10.0</protobuf.version>
<bouncycastle.version>1.63</bouncycastle.version>
<httpclient.version>4.5.10</httpclient.version>
<javadoc.version>3.0.1</javadoc.version>
<skipITs>true</skipITs>
<alpn-boot-version>8.1.7.v20160121</alpn-boot-version>
Expand Down Expand Up @@ -94,12 +94,12 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>2.0.25.Final</version>
<version>2.0.26.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http2</artifactId>
<version>4.1.35.Final</version>
<version>4.1.41.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
Expand Down Expand Up @@ -192,7 +192,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.24</version>
<version>1.25</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.miracl.milagro.amcl/milagro-crypto-java -->
Expand Down Expand Up @@ -236,7 +236,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<version>2.19.1</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<includes>
Expand All @@ -248,7 +248,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<version>2.19.1</version>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<includes>
Expand Down
48 changes: 44 additions & 4 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public class Channel implements Serializable {
private transient ScheduledExecutorService sweeperExecutorService;
private transient String blh = null;
private transient ServiceDiscovery serviceDiscovery;
private static final boolean asLocalhost = config.discoverAsLocalhost();

{
for (Peer.PeerRole peerRole : EnumSet.allOf(PeerRole.class)) {
Expand Down Expand Up @@ -1340,6 +1341,11 @@ public byte[][] getTLSIntermediateCerts() {
public Map<String, Orderer> getEndpointMap() {
return Collections.unmodifiableMap(Channel.this.ordererEndpointMap);
}

@Override
public Properties getProperties() {
return sdOrderer.getProperties();
}
});
}

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

@Override
public String getName() {
return sdEndorser.getName();
}

@Override
public Properties getProperties() {
Properties properties = new Properties();
if (asLocalhost) {
properties.put("hostnameOverride",
sdEndorser.getName().substring(0, sdEndorser.getName().lastIndexOf(':')));
}
return properties;
}
});
} else if (discoveryEndpoints.contains(sdEndorser.getEndpoint())) {

Expand Down Expand Up @@ -1450,6 +1470,7 @@ public void setServiceDiscoveryProperties(Properties serviceDiscoveryProperties)
}

public interface SDPeerAdditionInfo {
String getName();

String getMspId();

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

Map<String, Peer> getEndpointMap();

Properties getProperties();
}

public interface SDPeerAddition {
Expand Down Expand Up @@ -1555,6 +1577,8 @@ public interface SDOrdererAdditionInfo {

String getEndpoint();

Properties getProperties();

String getMspId();

Channel getChannel();
Expand Down Expand Up @@ -1613,7 +1637,7 @@ public SDOrdererDefaultAddition(Properties config) {
@Override
public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws InvalidArgumentException, ServiceDiscoveryException {

Properties properties = new Properties();
Properties properties = sdOrdererAdditionInfo.getProperties();
final String endpoint = sdOrdererAdditionInfo.getEndpoint();
final String mspid = sdOrdererAdditionInfo.getMspId();

Expand Down Expand Up @@ -1672,7 +1696,8 @@ public SDOPeerDefaultAddition(Properties config) {
@Override
public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentException, ServiceDiscoveryException {

Properties properties = new Properties();
Properties properties = sdPeerAddition.getProperties();
final String name = sdPeerAddition.getName();
final String endpoint = sdPeerAddition.getEndpoint();
final String mspid = sdPeerAddition.getMspId();

Expand Down Expand Up @@ -1713,12 +1738,12 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
properties.put("pemBytes", pemBytes);
}

peer = sdPeerAddition.getClient().newPeer(endpoint,
peer = sdPeerAddition.getClient().newPeer(name,
protocol + "//" + endpoint,
properties);

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

return peer;
}
Expand Down Expand Up @@ -4579,6 +4604,21 @@ public byte[][] getTLSIntermediateCerts() {
public Map<String, Peer> getEndpointMap() {
return Collections.unmodifiableMap(Channel.this.peerEndpointMap);
}

@Override
public String getName() {
return sdEndorser.getName();
}

@Override
public Properties getProperties() {
Properties properties = new Properties();
if (asLocalhost) {
properties.put("hostnameOverride",
sdEndorser.getName().substring(0, sdEndorser.getName().lastIndexOf(':')));
}
return properties;
}
});
}
endorsers.put(sdEndorser, epeer);
Expand Down
44 changes: 35 additions & 9 deletions src/main/java/org/hyperledger/fabric/sdk/ServiceDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -66,6 +67,7 @@ public class ServiceDiscovery {
private final TransactionContext transactionContext;
private final String channelName;
private volatile Map<String, SDChaindcode> chaindcodeMap = new HashMap<>();
private static final boolean asLocalhost = config.discoverAsLocalhost();

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

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

final SDOrderer sdOrderer = new SDOrderer(mspid, endpoint, lsdNetwork.getTlsCerts(mspid), lsdNetwork.getTlsIntermediateCerts(mspid));
Properties properties = new Properties();
if (asLocalhost) {
properties.put("hostnameOverride", l.getHost());
}

final SDOrderer sdOrderer = new SDOrderer(mspid, endpoint, lsdNetwork.getTlsCerts(mspid), lsdNetwork.getTlsIntermediateCerts(mspid), properties);

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

for (Protocol.Peer pp : peer.getPeersList()) {
SDEndorser ppp = new SDEndorser(pp, lsdNetwork.getTlsCerts(mspId), lsdNetwork.getTlsIntermediateCerts(mspId));
SDEndorser ppp = new SDEndorser(pp, lsdNetwork.getTlsCerts(mspId), lsdNetwork.getTlsIntermediateCerts(mspId), asLocalhost);

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

SDOrderer(String mspid, String endPoint, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts) {
SDOrderer(String mspid, String endPoint, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts, Properties properties) {
this.mspid = mspid;
this.endPoint = endPoint;
this.tlsCerts = tlsCerts;
this.tlsIntermediateCerts = tlsIntermediateCerts;
this.properties = properties;
}

public Collection<byte[]> getTlsIntermediateCerts() {
Expand All @@ -393,6 +403,10 @@ public String getMspid() {
public Collection<byte[]> getTlsCerts() {
return tlsCerts;
}

public Properties getProperties() {
return properties;
}
}

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

for (Protocol.Peer pp : peers.getPeersList()) {
SDEndorser ppp = new SDEndorser(pp, null, null);
SDEndorser ppp = new SDEndorser(pp, null, null, asLocalhost);
final String endPoint = ppp.getEndpoint();
SDEndorser nppp = sdNetwork.getEndorserByEndpoint(endPoint);
if (null == nppp) {
Expand Down Expand Up @@ -1096,19 +1110,23 @@ public static class SDEndorser {
private List<Message.Chaincode> chaincodesList;
// private final Protocol.Peer proto;
private String endPoint = null;
private String name = null;
private String mspid;
private long ledgerHeight = -1L;
private final Collection<byte[]> tlsCerts;
private final Collection<byte[]> tlsIntermediateCerts;
private final boolean asLocalhost;

SDEndorser() { // for testing only
tlsCerts = null;
tlsIntermediateCerts = null;
asLocalhost = false;
}

SDEndorser(Protocol.Peer peerRet, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts) {
SDEndorser(Protocol.Peer peerRet, Collection<byte[]> tlsCerts, Collection<byte[]> tlsIntermediateCerts, boolean asLocalhost) {
this.tlsCerts = tlsCerts;
this.tlsIntermediateCerts = tlsIntermediateCerts;
this.asLocalhost = asLocalhost;

parseEndpoint(peerRet);
parseLedgerHeight(peerRet);
Expand All @@ -1123,6 +1141,10 @@ Collection<byte[]> getTLSIntermediateCerts() {
return tlsIntermediateCerts;
}

public String getName() {
return name;
}

public String getEndpoint() {
return endPoint;
}
Expand Down Expand Up @@ -1151,9 +1173,13 @@ private String parseEndpoint(Protocol.Peer peerRet) throws InvalidProtocolBuffer
throw new RuntimeException(format("Error %s", "bad"));
}
Message.AliveMessage aliveMsg = gossipMessageMemberInfo.getAliveMsg();
endPoint = aliveMsg.getMembership().getEndpoint();
if (endPoint != null) {
endPoint = endPoint.toLowerCase().trim(); //makes easier on comparing.
name = aliveMsg.getMembership().getEndpoint();
if (name != null) {
if (asLocalhost) {
endPoint = "localhost" + name.substring(name.lastIndexOf(':'));
} else {
endPoint = name.toLowerCase().trim(); //makes easier on comparing.
}
}
} catch (InvalidProtocolBufferException e) {
throw new InvalidProtocolBufferRuntimeException(e);
Expand Down
Loading

0 comments on commit 96f5327

Please sign in to comment.