Skip to content

Commit

Permalink
FABJ-433 Fix return SD overrides.
Browse files Browse the repository at this point in the history
Return default implementation for SD peer orderer additions.

PS
02Fix testcase error.

Change-Id: Id9cf82210c3d63148694a3ef51feac6b7e5f0619
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed May 1, 2019
1 parent 4a2d7b3 commit 6c07d06
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 13 deletions.
70 changes: 57 additions & 13 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ public class Channel implements Serializable {
}
}

// public void clean(){
// channelEventQue = null;
// }
//
private Channel(String name, HFClient hfClient, Orderer orderer, ChannelConfiguration channelConfiguration, byte[][] signers) throws InvalidArgumentException, TransactionException {
this(name, hfClient, false);

Expand Down Expand Up @@ -1329,7 +1325,9 @@ public interface SDPeerAddition {
transient SDPeerAddition sdPeerAddition = null;

/**
* Set service discovery peer addition override.
* Set service discovery orderer addition override.
* <p>
* Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this.
*
* @param sdOrdererAddition
* @return
Expand All @@ -1340,10 +1338,32 @@ public SDOrdererAddition setSDOrdererAddition(SDOrdererAddition sdOrdererAdditio

this.sdOrdererAddition = sdOrdererAddition;

if (null == ret) {
ret = new SDOrdererDefaultAddition(getServiceDiscoveryProperties());
}

return ret;

}

/**
* Get current service discovery orderer addition override.
* <p>
* Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this.
*
* @return SDOrdererAddition
*/

public SDOrdererAddition getSDOrdererAddition() {

if (null == sdOrdererAddition) {
sdOrdererAddition = new SDOrdererDefaultAddition(getServiceDiscoveryProperties());
}

return sdOrdererAddition;

}

private static byte[] combineCerts(Collection<byte[]>... certCollections) throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
for (Collection<byte[]> certCollection : certCollections) {
Expand Down Expand Up @@ -1407,10 +1427,10 @@ public interface SDOrdererAddition {

private Properties serviceDiscoveryProperties = new Properties();

private static class SDOrdererDefaultAddition implements SDOrdererAddition {
private final Properties config;
public static class SDOrdererDefaultAddition implements SDOrdererAddition {
protected final Properties config;

SDOrdererDefaultAddition(Properties config) {
public SDOrdererDefaultAddition(Properties config) {
this.config = config == null ? new Properties() : (Properties) config.clone();

}
Expand Down Expand Up @@ -1464,10 +1484,10 @@ public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws In
}
}

private static class SDOPeerDefaultAddition implements SDPeerAddition {
private final Properties config;
public static class SDOPeerDefaultAddition implements SDPeerAddition {
protected final Properties config;

SDOPeerDefaultAddition(Properties config) {
public SDOPeerDefaultAddition(Properties config) {
this.config = config == null ? new Properties() : (Properties) config.clone();

}
Expand Down Expand Up @@ -1538,6 +1558,8 @@ static Object findClientProp(Properties config, final String prop, final String

/**
* Set service discovery peer addition override.
* <p>
* Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this.
*
* @param sdPeerAddition
* @return
Expand All @@ -1548,10 +1570,32 @@ public SDPeerAddition setSDPeerAddition(SDPeerAddition sdPeerAddition) {

this.sdPeerAddition = sdPeerAddition;

if (ret == null) {
ret = new SDOPeerDefaultAddition(getServiceDiscoveryProperties());
}

return ret;

}

/**
* Get current service discovery peer addition override.
* <p>
* Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this.
*
* @return SDOrdererAddition
*/

public SDPeerAddition getSDPeerAddition() {

if (null == sdPeerAddition) {
sdPeerAddition = new SDOPeerDefaultAddition(getServiceDiscoveryProperties());
}

return sdPeerAddition;

}

/**
* load the peer organizations CA certificates into the channel's trust store so that we
* can verify signatures from peer messages
Expand Down Expand Up @@ -3444,7 +3488,7 @@ public LifecycleApproveChaincodeDefinitionForMyOrgProposalResponse sendLifecycle
* Approve chaincode to be run on this peer's organization.
*
* @param lifecycleApproveChaincodeDefinitionForMyOrgRequest the request see {@link LifecycleApproveChaincodeDefinitionForMyOrgRequest}
* @param peers to send the request to.
* @param peers to send the request to.
* @return A {@link LifecycleApproveChaincodeDefinitionForMyOrgProposalResponse}
* @throws ProposalException
* @throws InvalidArgumentException
Expand Down Expand Up @@ -3534,7 +3578,7 @@ public Collection<LifecycleApproveChaincodeDefinitionForMyOrgProposalResponse> s
* Commit chaincode final approval to run on all organizations that have approved.
*
* @param lifecycleCommitChaincodeDefinitionRequest The request see {@link LifecycleCommitChaincodeDefinitionRequest}
* @param peers to send the request to.
* @param peers to send the request to.
* @return A {@link LifecycleCommitChaincodeDefinitionProposalResponse}
* @throws InvalidArgumentException
* @throws ProposalException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.hyperledger.fabric.sdkintegration;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
Expand All @@ -36,18 +37,22 @@
import org.hyperledger.fabric.sdk.ServiceDiscovery;
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
import org.hyperledger.fabric.sdk.TransactionRequest;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ServiceDiscoveryException;
import org.hyperledger.fabric.sdk.exception.TransactionEventException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.testutils.TestConfig;
import org.junit.Test;

import static java.lang.String.format;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
import static org.hyperledger.fabric.sdk.Channel.DiscoveryOptions.createDiscoveryOptions;
import static org.hyperledger.fabric.sdk.Channel.PeerOptions.createPeerOptions;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;

public class ServiceDiscoveryIT {
private static final TestConfig testConfig = TestConfig.getConfig();
Expand Down Expand Up @@ -107,13 +112,33 @@ public void setup() throws Exception {
foo.shutdown(false);
foo = client.deSerializeChannel(bytes);

//You can override the default implementation but these just reuse the default.
Channel.SDPeerAddition sdPeerAddition = foo.getSDPeerAddition();
assertNotNull(sdPeerAddition);
foo.setSDPeerAddition(sdPeerAddition);
assertSame(sdPeerAddition, foo.getSDPeerAddition());

// Just see if we can override and collect the orderers.
final ArrayList<Orderer> testCollectOrderers = new ArrayList();

Channel.SDOrdererAddition sdOrdererAddition = foo.setSDOrdererAddition(new Channel.SDOrdererDefaultAddition(sdprops) {
@Override
public Orderer addOrderer(Channel.SDOrdererAdditionInfo sdOrdererAdditionInfo) throws InvalidArgumentException, ServiceDiscoveryException {
Orderer ret = super.addOrderer(sdOrdererAdditionInfo);
testCollectOrderers.add(ret); // just see if our extended works.
return ret;
}
});
assertNotNull(sdOrdererAddition);

foo.initialize(); // initialize the channel.

Set<String> expect = new HashSet<>(Arrays.asList(protocol + "//orderer.example.com:7050")); //discovered orderer
for (Orderer orderer : foo.getOrderers()) {
expect.remove(orderer.getUrl());
}
assertTrue(expect.isEmpty());
assertEquals(foo.getOrderers().size(), testCollectOrderers.size());

final Collection<String> discoveredChaincodeNames = foo.getDiscoveredChaincodeNames();

Expand Down

0 comments on commit 6c07d06

Please sign in to comment.