Skip to content

Commit

Permalink
FAB-5943 Update protos check on duplicate channel.
Browse files Browse the repository at this point in the history
Change-Id: Ia717df5a99f8776a9a6481242eb330df1967ac73
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Aug 28, 2017
1 parent c63dd83 commit 7f6dc28
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 44 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private Channel(String name, HFClient hfClient, Orderer orderer, ChannelConfigur
ByteString configUpdate = configUpdateEnv.getConfigUpdate();

sendUpdateChannel(configUpdate.toByteArray(), signers, orderer);
// final ConfigUpdateEnvelope.Builder configUpdateEnvBuilder = configUpdateEnv.toBuilder();
// final ConfigUpdateEnvelope.Builder configUpdateEnvBuilder = configUpdateEnv.toBuilder();`

//---------------------------------------

Expand Down Expand Up @@ -3070,6 +3070,8 @@ public synchronized void shutdown(boolean force) {

blockListeners.clear();

client.removeChannel(this);

for (EventHub eh : getEventHubs()) {

try {
Expand Down Expand Up @@ -3100,6 +3102,8 @@ public synchronized void shutdown(boolean force) {
eventQueueThread.interrupt();
}
eventQueueThread = null;

client = null;
}

@Override
Expand Down
50 changes: 42 additions & 8 deletions src/main/java/org/hyperledger/fabric/sdk/HFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric.sdk.security.CryptoSuite;

import static java.lang.String.format;
Expand Down Expand Up @@ -109,10 +110,23 @@ public static HFClient createNewInstance() {

public Channel newChannel(String name) throws InvalidArgumentException {
clientCheck();
logger.trace("Creating channel :" + name);
Channel newChannel = Channel.createNewInstance(name, this);
channels.put(name, newChannel);
return newChannel;
if (Utils.isNullOrEmpty(name)) {
throw new InvalidArgumentException("Channel name can not be null or empty string.");
}

synchronized (channels) {

if (channels.containsKey(name)) {
throw new InvalidArgumentException(format("Channel by the name %s already exits", name));
}
logger.trace("Creating channel :" + name);
Channel newChannel = Channel.createNewInstance(name, this);

channels.put(name, newChannel);
return newChannel;

}

}

/**
Expand All @@ -131,10 +145,25 @@ public Channel newChannel(String name) throws InvalidArgumentException {
public Channel newChannel(String name, Orderer orderer, ChannelConfiguration channelConfiguration, byte[]... channelConfigurationSignatures) throws TransactionException, InvalidArgumentException {

clientCheck();
logger.trace("Creating channel :" + name);
Channel newChannel = Channel.createNewInstance(name, this, orderer, channelConfiguration, channelConfigurationSignatures);
channels.put(name, newChannel);
return newChannel;
if (Utils.isNullOrEmpty(name)) {
throw new InvalidArgumentException("Channel name can not be null or empty string.");
}

synchronized (channels) {

if (channels.containsKey(name)) {
throw new InvalidArgumentException(format("Channel by the name %s already exits", name));
}

logger.trace("Creating channel :" + name);

Channel newChannel = Channel.createNewInstance(name, this, orderer, channelConfiguration, channelConfigurationSignatures);

channels.put(name, newChannel);
return newChannel;

}

}

/**
Expand Down Expand Up @@ -500,4 +529,9 @@ private void clientCheck() throws InvalidArgumentException {

}

void removeChannel(Channel channel) {
synchronized (channels) {
channels.remove(channel.getName());
}
}
}
8 changes: 8 additions & 0 deletions src/main/proto/ledger/rwset/rwset.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ message NsPvtReadWriteSet {
message CollectionPvtReadWriteSet {
string collection_name = 1;
bytes rwset = 2; // Data model specific serialized proto message (e.g., kvrwset.KVRWSet for KV and Document data models)
}

// CollectionProperty defines an element of a private data that corresponds
// to a certain transaction and collection
message CollectionCriteria {
string channel = 1;
string tx_id = 2;
string collection = 3;
}
40 changes: 29 additions & 11 deletions src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@

//CHECKSTYLE.ON: IllegalImport




public class ChannelTest {
private static HFClient hfclient = null;
private static Channel shutdownChannel = null;
Expand Down Expand Up @@ -395,7 +392,7 @@ public void testChannelBadPeerDoesNotBelong() throws Exception {

Collection<Peer> peers = Arrays.asList((Peer[]) new Peer[] {hfclient.newPeer("peer2", "grpc://localhost:22")});

createRunningChannel(peers);
createRunningChannel("testChannelBadPeerDoesNotBelong", peers);

channel.sendInstantiationProposal(hfclient.newInstantiationProposalRequest(), peers);

Expand All @@ -411,7 +408,7 @@ public void testChannelBadPeerDoesNotBelong2() throws Exception {

Peer peer = channel.getPeers().iterator().next();

final Channel channel2 = createRunningChannel(null);
final Channel channel2 = createRunningChannel("testChannelBadPeerDoesNotBelong2", null);

setField(peer, "channel", channel2);

Expand Down Expand Up @@ -458,8 +455,30 @@ public void testChannelBadPeerCollectionNull() throws Exception {

}

@Test
public void testTwoChannelsSameName() throws Exception {

thrown.expect(InvalidArgumentException.class);
thrown.expectMessage("Channel by the name testTwoChannelsSameName already exits");

createRunningChannel("testTwoChannelsSameName", null);
createRunningChannel("testTwoChannelsSameName", null);

}

static final String CHANNEL_NAME2 = "channel";

public static Channel createRunningChannel(Collection<Peer> peers) throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException {
Channel channel = hfclient.newChannel("channel");
Channel prevChannel = hfclient.getChannel(CHANNEL_NAME2);
if (null != prevChannel) { //cleanup remove default channel.
prevChannel.shutdown(false);
}
return createRunningChannel(CHANNEL_NAME2, peers);
}

public static Channel createRunningChannel(String channelName, Collection<Peer> peers) throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException {

Channel channel = hfclient.newChannel(channelName);
if (peers == null) {
Peer peer = hfclient.newPeer("peer1", "grpc://localhost:22");
channel.addPeer(peer);
Expand All @@ -480,13 +499,13 @@ public static Channel createRunningChannel(Collection<Peer> peers) throws Invali
public void testChannelBadPeerDoesNotBelongJoin() throws Exception {

thrown.expect(ProposalException.class);
thrown.expectMessage("Can not add peer peer2 to channel channel because it already belongs to channel channel");
thrown.expectMessage("Can not add peer peer2 to channel testChannelBadPeerDoesNotBelongJoin because it already belongs to channel testChannelBadPeerDoesNotBelongJoin2");

final Channel channel = createRunningChannel(null);
final Channel channel = createRunningChannel("testChannelBadPeerDoesNotBelongJoin", null);

Collection<Peer> peers = Arrays.asList((Peer[]) new Peer[] {hfclient.newPeer("peer2", "grpc://localhost:22")});

createRunningChannel(peers);
createRunningChannel("testChannelBadPeerDoesNotBelongJoin2", peers);

//Peer joining channel when it belongs to another channel.

Expand Down Expand Up @@ -527,7 +546,7 @@ public void testChannelInitNullClient() throws Exception {
thrown.expect(InvalidArgumentException.class);
thrown.expectMessage("Can not initialize channel without a client object.");

final Channel channel = hfclient.newChannel("del");
final Channel channel = hfclient.newChannel("testChannelInitNullClient");
setField(channel, "client", null);

channel.initialize();
Expand Down Expand Up @@ -746,5 +765,4 @@ private Unsafe getUnsafe() { //lets us throw undeclared exceptions.
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.ProposalResponse;
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
import org.hyperledger.fabric.sdk.SDKUtils;
import org.hyperledger.fabric.sdk.TestConfigHelper;
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
import org.hyperledger.fabric.sdk.UpgradeProposalRequest;
Expand Down Expand Up @@ -258,13 +257,6 @@ void runChannel(HFClient client, Channel channel, SampleOrg sampleOrg, final int
fail("Not enough endorsers for install :" + successful.size() + ". " + first.getMessage());
}

// Check that all the proposals are consistent with each other. We should have only one set
// where all the proposals above are consistent.
Collection<Set<ProposalResponse>> proposalConsistencySets = SDKUtils.getProposalConsistencySets(responses);
if (proposalConsistencySets.size() != 1) {
fail(format("Expected only one set of consistent install proposal responses but got %d", proposalConsistencySets.size()));
}

//////////////////
// Upgrade chaincode to ***double*** our move results.

Expand Down Expand Up @@ -317,13 +309,6 @@ void runChannel(HFClient client, Channel channel, SampleOrg sampleOrg, final int
+ successful.size() + ". " + first.getMessage());
}

// Check that all the proposals are consistent with each other. We should have only one set
// where the proposals above are consistent.
proposalConsistencySets = SDKUtils.getProposalConsistencySets(responses2);
if (proposalConsistencySets.size() != 1) {
fail(format("Expected only one set of consistent upgrade proposal responses but got %d", proposalConsistencySets.size()));
}

if (changeContext) {
return channel.sendTransaction(successful, sampleOrg.getPeerAdmin()).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);

Expand Down Expand Up @@ -451,13 +436,6 @@ CompletableFuture<BlockEvent.TransactionEvent> moveAmount(HFClient client, Chann
}
}

// Check that all the proposals are consistent with each other. We should have only one set
// where all the proposals above are consistent.
Collection<Set<ProposalResponse>> proposalConsistencySets = SDKUtils.getProposalConsistencySets(invokePropResp);
if (proposalConsistencySets.size() != 1) {
fail(format("Expected only one set of consistent move proposal responses but got %d", proposalConsistencySets.size()));
}

out("Received %d transaction proposal responses. Successful+verified: %d . Failed: %d",
invokePropResp.size(), successful.size(), failed.size());
if (failed.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ class ChaincodeEventCapture { //A test class to capture chaincode events
}
}

SDKUtils.getProposalConsistencySets(responses);
// }
out("Received %d install proposal responses. Successful+verified: %d . Failed: %d", numInstallProposal, successful.size(), failed.size());

Expand Down Expand Up @@ -443,7 +442,9 @@ policy OR(Org1MSP.member, Org2MSP.member) meaning 1 signature from someone in ei
}

// Check that all the proposals are consistent with each other. We should have only one set
// where all the proposals above are consistent.
// where all the proposals above are consistent. Note the when sending to Orderer this is done automatically.
// Shown here as an example that applications can invoke and select.
// See org.hyperledger.fabric.sdk.proposal.consistency_validation config property.
Collection<Set<ProposalResponse>> proposalConsistencySets = SDKUtils.getProposalConsistencySets(transactionPropResp);
if (proposalConsistencySets.size() != 1) {
fail(format("Expected only one set of consistent proposal responses but got %d", proposalConsistencySets.size()));
Expand Down

0 comments on commit 7f6dc28

Please # to comment.