From 96f532746be2608a55371e7098497e54974a2739 Mon Sep 17 00:00:00 2001 From: andrew-coleman Date: Mon, 7 Oct 2019 15:28:27 +0100 Subject: [PATCH] =?UTF-8?q?FGJ-33=20=E2=80=98As=20Localhost=E2=80=99=20opt?= =?UTF-8?q?ion=20for=20discovery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .classpath | 43 +++++++++++++++++ .project | 29 +++++++++++ pom.xml | 18 +++---- .../org/hyperledger/fabric/sdk/Channel.java | 48 +++++++++++++++++-- .../fabric/sdk/ServiceDiscovery.java | 44 +++++++++++++---- .../hyperledger/fabric/sdk/helper/Config.java | 9 ++-- .../hyperledger/fabric/sdk/ChannelTest.java | 10 ++++ 7 files changed, 176 insertions(+), 25 deletions(-) create mode 100644 .classpath create mode 100644 .project diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..56e6d3b4 --- /dev/null +++ b/.classpath @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 00000000..d4e2cd76 --- /dev/null +++ b/.project @@ -0,0 +1,29 @@ + + + fabric-sdk-java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + net.sf.eclipsecs.core.CheckstyleBuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + net.sf.eclipsecs.core.CheckstyleNature + + diff --git a/pom.xml b/pom.xml index cd060770..c22e0426 100644 --- a/pom.xml +++ b/pom.xml @@ -28,10 +28,10 @@ fabric-sdk-java-1.0 - 1.20.0 - 3.7.1 - 1.61 - 4.5.8 + 1.23.0 + 3.10.0 + 1.63 + 4.5.10 3.0.1 true 8.1.7.v20160121 @@ -94,12 +94,12 @@ io.netty netty-tcnative-boringssl-static - 2.0.25.Final + 2.0.26.Final io.netty netty-codec-http2 - 4.1.35.Final + 4.1.41.Final @@ -192,7 +192,7 @@ org.yaml snakeyaml - 1.24 + 1.25 @@ -236,7 +236,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M3 + 2.19.1 ${surefireArgLine} @@ -248,7 +248,7 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M3 + 2.19.1 ${failsafeArgLine} diff --git a/src/main/java/org/hyperledger/fabric/sdk/Channel.java b/src/main/java/org/hyperledger/fabric/sdk/Channel.java index 2e0f7cb0..6b58ee93 100644 --- a/src/main/java/org/hyperledger/fabric/sdk/Channel.java +++ b/src/main/java/org/hyperledger/fabric/sdk/Channel.java @@ -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)) { @@ -1340,6 +1341,11 @@ public byte[][] getTLSIntermediateCerts() { public Map getEndpointMap() { return Collections.unmodifiableMap(Channel.this.ordererEndpointMap); } + + @Override + public Properties getProperties() { + return sdOrderer.getProperties(); + } }); } @@ -1418,6 +1424,20 @@ public Map 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())) { @@ -1450,6 +1470,7 @@ public void setServiceDiscoveryProperties(Properties serviceDiscoveryProperties) } public interface SDPeerAdditionInfo { + String getName(); String getMspId(); @@ -1489,6 +1510,7 @@ default byte[] getAllTLSCerts() throws ServiceDiscoveryException { Map getEndpointMap(); + Properties getProperties(); } public interface SDPeerAddition { @@ -1555,6 +1577,8 @@ public interface SDOrdererAdditionInfo { String getEndpoint(); + Properties getProperties(); + String getMspId(); Channel getChannel(); @@ -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(); @@ -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(); @@ -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; } @@ -4579,6 +4604,21 @@ public byte[][] getTLSIntermediateCerts() { public Map 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); diff --git a/src/main/java/org/hyperledger/fabric/sdk/ServiceDiscovery.java b/src/main/java/org/hyperledger/fabric/sdk/ServiceDiscovery.java index 604d5c9b..db7727e2 100644 --- a/src/main/java/org/hyperledger/fabric/sdk/ServiceDiscovery.java +++ b/src/main/java/org/hyperledger/fabric/sdk/ServiceDiscovery.java @@ -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; @@ -66,6 +67,7 @@ public class ServiceDiscovery { private final TransactionContext transactionContext; private final String channelName; private volatile Map chaindcodeMap = new HashMap<>(); + private static final boolean asLocalhost = config.discoverAsLocalhost(); ServiceDiscovery(Channel channel, Collection serviceDiscoveryPeers, TransactionContext transactionContext) { this.serviceDiscoveryPeers = serviceDiscoveryPeers; @@ -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) { @@ -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); } @@ -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) { @@ -370,12 +378,14 @@ public static class SDOrderer { private final Collection tlsCerts; private final Collection tlsIntermediateCerts; private final String endPoint; + private final Properties properties; - SDOrderer(String mspid, String endPoint, Collection tlsCerts, Collection tlsIntermediateCerts) { + SDOrderer(String mspid, String endPoint, Collection tlsCerts, Collection tlsIntermediateCerts, Properties properties) { this.mspid = mspid; this.endPoint = endPoint; this.tlsCerts = tlsCerts; this.tlsIntermediateCerts = tlsIntermediateCerts; + this.properties = properties; } public Collection getTlsIntermediateCerts() { @@ -393,6 +403,10 @@ public String getMspid() { public Collection getTlsCerts() { return tlsCerts; } + + public Properties getProperties() { + return properties; + } } Map discoverEndorserEndpoints(TransactionContext transactionContext, List> chaincodeNames) throws ServiceDiscoveryException { @@ -520,7 +534,7 @@ Map discoverEndorserEndpoints(TransactionContext transacti List 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) { @@ -1096,19 +1110,23 @@ public static class SDEndorser { private List chaincodesList; // private final Protocol.Peer proto; private String endPoint = null; + private String name = null; private String mspid; private long ledgerHeight = -1L; private final Collection tlsCerts; private final Collection tlsIntermediateCerts; + private final boolean asLocalhost; SDEndorser() { // for testing only tlsCerts = null; tlsIntermediateCerts = null; + asLocalhost = false; } - SDEndorser(Protocol.Peer peerRet, Collection tlsCerts, Collection tlsIntermediateCerts) { + SDEndorser(Protocol.Peer peerRet, Collection tlsCerts, Collection tlsIntermediateCerts, boolean asLocalhost) { this.tlsCerts = tlsCerts; this.tlsIntermediateCerts = tlsIntermediateCerts; + this.asLocalhost = asLocalhost; parseEndpoint(peerRet); parseLedgerHeight(peerRet); @@ -1123,6 +1141,10 @@ Collection getTLSIntermediateCerts() { return tlsIntermediateCerts; } + public String getName() { + return name; + } + public String getEndpoint() { return endPoint; } @@ -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); diff --git a/src/main/java/org/hyperledger/fabric/sdk/helper/Config.java b/src/main/java/org/hyperledger/fabric/sdk/helper/Config.java index 25423759..5683443c 100644 --- a/src/main/java/org/hyperledger/fabric/sdk/helper/Config.java +++ b/src/main/java/org/hyperledger/fabric/sdk/helper/Config.java @@ -57,7 +57,6 @@ public class Config { public static final String ORDERER_WAIT_TIME = "org.hyperledger.fabric.sdk.orderer.ordererWaitTimeMilliSecs"; public static final String PEER_EVENT_REGISTRATION_WAIT_TIME = "org.hyperledger.fabric.sdk.peer.eventRegistration.wait_time"; public static final String PEER_EVENT_RETRY_WAIT_TIME = "org.hyperledger.fabric.sdk.peer.retry_wait_time"; - public static final String PEER_EVENT_RECONNECTION_WARNING_RATE = "org.hyperledger.fabric.sdk.peer.reconnection_warning_rate"; public static final String GENESISBLOCK_WAIT_TIME = "org.hyperledger.fabric.sdk.channel.genesisblock_wait_time"; /** @@ -102,6 +101,7 @@ public class Config { public static final String SERVICE_DISCOVER_FREQ_SECONDS = "org.hyperledger.fabric.sdk.service_discovery.frequency_sec"; public static final String SERVICE_DISCOVER_WAIT_TIME = "org.hyperledger.fabric.sdk.service_discovery.discovery_wait_time"; + public static final String SERVICE_DISCOVER_AS_LOCALHOST = "org.hyperledger.fabric.sdk.service_discovery.as_localhost"; public static final String LIFECYCLE_CHAINCODE_ENDORSEMENT_PLUGIN = "org.hyperledger.fabric.sdk.lifecycle.chaincode_endorsement_plugin"; //ORG_HYPERLEDGER_FABRIC_SDK_LIFECYCLE_CHAINCODE_ENDORSEMENT_PLUGIN @@ -144,7 +144,6 @@ private Config() { defaultProperty(ORDERER_WAIT_TIME, "10000"); defaultProperty(PEER_EVENT_REGISTRATION_WAIT_TIME, "5000"); defaultProperty(PEER_EVENT_RETRY_WAIT_TIME, "500"); - defaultProperty(GENESISBLOCK_WAIT_TIME, "5000"); /** * This will NOT complete any transaction futures time out and must be kept WELL above any expected future timeout @@ -193,11 +192,11 @@ private Config() { * Miscellaneous settings */ defaultProperty(PROPOSAL_CONSISTENCY_VALIDATION, "true"); - defaultProperty(PEER_EVENT_RECONNECTION_WARNING_RATE, "50"); defaultProperty(SERVICE_DISCOVER_FREQ_SECONDS, "120"); defaultProperty(SERVICE_DISCOVER_WAIT_TIME, "5000"); + defaultProperty(SERVICE_DISCOVER_AS_LOCALHOST, "false"); defaultProperty(LIFECYCLE_CHAINCODE_ENDORSEMENT_PLUGIN, DEFAULT_NULL); defaultProperty(LIFECYCLE_CHAINCODE_VALIDATION_PLUGIN, DEFAULT_NULL); defaultProperty(LIFECYCLE_INITREQUIREDDEFAULT, DEFAULT_NULL); @@ -474,6 +473,10 @@ public int getServiceDiscoveryWaitTime() { return Integer.parseInt(getProperty(SERVICE_DISCOVER_WAIT_TIME)); } + public boolean discoverAsLocalhost() { + return Boolean.parseBoolean(getProperty(SERVICE_DISCOVER_AS_LOCALHOST)); + } + public String getAsymmetricKeyType() { return getProperty(ASYMMETRIC_KEY_TYPE); } diff --git a/src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java b/src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java index d36cc429..5c19748f 100644 --- a/src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java +++ b/src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java @@ -644,6 +644,16 @@ public byte[][] getTLSIntermediateCerts() { public Map getEndpointMap() { return new HashMap<>(); } + + @Override + public String getName() { + return discoveredEndpoint[0]; + } + + @Override + public Properties getProperties() { + return properties1; + } }; Peer peer = sd.sdPeerAddition.addPeer(sdPeerAdditionInfo);