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

Commit 7f6dc28

Browse files
committed
FAB-5943 Update protos check on duplicate channel.
Change-Id: Ia717df5a99f8776a9a6481242eb330df1967ac73 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent c63dd83 commit 7f6dc28

File tree

6 files changed

+87
-44
lines changed

6 files changed

+87
-44
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private Channel(String name, HFClient hfClient, Orderer orderer, ChannelConfigur
193193
ByteString configUpdate = configUpdateEnv.getConfigUpdate();
194194

195195
sendUpdateChannel(configUpdate.toByteArray(), signers, orderer);
196-
// final ConfigUpdateEnvelope.Builder configUpdateEnvBuilder = configUpdateEnv.toBuilder();
196+
// final ConfigUpdateEnvelope.Builder configUpdateEnvBuilder = configUpdateEnv.toBuilder();`
197197

198198
//---------------------------------------
199199

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

30713071
blockListeners.clear();
30723072

3073+
client.removeChannel(this);
3074+
30733075
for (EventHub eh : getEventHubs()) {
30743076

30753077
try {
@@ -3100,6 +3102,8 @@ public synchronized void shutdown(boolean force) {
31003102
eventQueueThread.interrupt();
31013103
}
31023104
eventQueueThread = null;
3105+
3106+
client = null;
31033107
}
31043108

31053109
@Override

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
3333
import org.hyperledger.fabric.sdk.exception.ProposalException;
3434
import org.hyperledger.fabric.sdk.exception.TransactionException;
35+
import org.hyperledger.fabric.sdk.helper.Utils;
3536
import org.hyperledger.fabric.sdk.security.CryptoSuite;
3637

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

110111
public Channel newChannel(String name) throws InvalidArgumentException {
111112
clientCheck();
112-
logger.trace("Creating channel :" + name);
113-
Channel newChannel = Channel.createNewInstance(name, this);
114-
channels.put(name, newChannel);
115-
return newChannel;
113+
if (Utils.isNullOrEmpty(name)) {
114+
throw new InvalidArgumentException("Channel name can not be null or empty string.");
115+
}
116+
117+
synchronized (channels) {
118+
119+
if (channels.containsKey(name)) {
120+
throw new InvalidArgumentException(format("Channel by the name %s already exits", name));
121+
}
122+
logger.trace("Creating channel :" + name);
123+
Channel newChannel = Channel.createNewInstance(name, this);
124+
125+
channels.put(name, newChannel);
126+
return newChannel;
127+
128+
}
129+
116130
}
117131

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

133147
clientCheck();
134-
logger.trace("Creating channel :" + name);
135-
Channel newChannel = Channel.createNewInstance(name, this, orderer, channelConfiguration, channelConfigurationSignatures);
136-
channels.put(name, newChannel);
137-
return newChannel;
148+
if (Utils.isNullOrEmpty(name)) {
149+
throw new InvalidArgumentException("Channel name can not be null or empty string.");
150+
}
151+
152+
synchronized (channels) {
153+
154+
if (channels.containsKey(name)) {
155+
throw new InvalidArgumentException(format("Channel by the name %s already exits", name));
156+
}
157+
158+
logger.trace("Creating channel :" + name);
159+
160+
Channel newChannel = Channel.createNewInstance(name, this, orderer, channelConfiguration, channelConfigurationSignatures);
161+
162+
channels.put(name, newChannel);
163+
return newChannel;
164+
165+
}
166+
138167
}
139168

140169
/**
@@ -500,4 +529,9 @@ private void clientCheck() throws InvalidArgumentException {
500529

501530
}
502531

532+
void removeChannel(Channel channel) {
533+
synchronized (channels) {
534+
channels.remove(channel.getName());
535+
}
536+
}
503537
}

src/main/proto/ledger/rwset/rwset.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ message NsPvtReadWriteSet {
6262
message CollectionPvtReadWriteSet {
6363
string collection_name = 1;
6464
bytes rwset = 2; // Data model specific serialized proto message (e.g., kvrwset.KVRWSet for KV and Document data models)
65+
}
66+
67+
// CollectionProperty defines an element of a private data that corresponds
68+
// to a certain transaction and collection
69+
message CollectionCriteria {
70+
string channel = 1;
71+
string tx_id = 2;
72+
string collection = 3;
6573
}

src/test/java/org/hyperledger/fabric/sdk/ChannelTest.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545

4646
//CHECKSTYLE.ON: IllegalImport
4747

48-
49-
50-
5148
public class ChannelTest {
5249
private static HFClient hfclient = null;
5350
private static Channel shutdownChannel = null;
@@ -395,7 +392,7 @@ public void testChannelBadPeerDoesNotBelong() throws Exception {
395392

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

398-
createRunningChannel(peers);
395+
createRunningChannel("testChannelBadPeerDoesNotBelong", peers);
399396

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

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

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

414-
final Channel channel2 = createRunningChannel(null);
411+
final Channel channel2 = createRunningChannel("testChannelBadPeerDoesNotBelong2", null);
415412

416413
setField(peer, "channel", channel2);
417414

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

459456
}
460457

458+
@Test
459+
public void testTwoChannelsSameName() throws Exception {
460+
461+
thrown.expect(InvalidArgumentException.class);
462+
thrown.expectMessage("Channel by the name testTwoChannelsSameName already exits");
463+
464+
createRunningChannel("testTwoChannelsSameName", null);
465+
createRunningChannel("testTwoChannelsSameName", null);
466+
467+
}
468+
469+
static final String CHANNEL_NAME2 = "channel";
470+
461471
public static Channel createRunningChannel(Collection<Peer> peers) throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException {
462-
Channel channel = hfclient.newChannel("channel");
472+
Channel prevChannel = hfclient.getChannel(CHANNEL_NAME2);
473+
if (null != prevChannel) { //cleanup remove default channel.
474+
prevChannel.shutdown(false);
475+
}
476+
return createRunningChannel(CHANNEL_NAME2, peers);
477+
}
478+
479+
public static Channel createRunningChannel(String channelName, Collection<Peer> peers) throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException {
480+
481+
Channel channel = hfclient.newChannel(channelName);
463482
if (peers == null) {
464483
Peer peer = hfclient.newPeer("peer1", "grpc://localhost:22");
465484
channel.addPeer(peer);
@@ -480,13 +499,13 @@ public static Channel createRunningChannel(Collection<Peer> peers) throws Invali
480499
public void testChannelBadPeerDoesNotBelongJoin() throws Exception {
481500

482501
thrown.expect(ProposalException.class);
483-
thrown.expectMessage("Can not add peer peer2 to channel channel because it already belongs to channel channel");
502+
thrown.expectMessage("Can not add peer peer2 to channel testChannelBadPeerDoesNotBelongJoin because it already belongs to channel testChannelBadPeerDoesNotBelongJoin2");
484503

485-
final Channel channel = createRunningChannel(null);
504+
final Channel channel = createRunningChannel("testChannelBadPeerDoesNotBelongJoin", null);
486505

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

489-
createRunningChannel(peers);
508+
createRunningChannel("testChannelBadPeerDoesNotBelongJoin2", peers);
490509

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

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

530-
final Channel channel = hfclient.newChannel("del");
549+
final Channel channel = hfclient.newChannel("testChannelInitNullClient");
531550
setField(channel, "client", null);
532551

533552
channel.initialize();
@@ -746,5 +765,4 @@ private Unsafe getUnsafe() { //lets us throw undeclared exceptions.
746765
}
747766
}
748767

749-
750768
}

src/test/java/org/hyperledger/fabric/sdkintegration/End2endAndBackAgainIT.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.hyperledger.fabric.sdk.Peer;
4040
import org.hyperledger.fabric.sdk.ProposalResponse;
4141
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
42-
import org.hyperledger.fabric.sdk.SDKUtils;
4342
import org.hyperledger.fabric.sdk.TestConfigHelper;
4443
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
4544
import org.hyperledger.fabric.sdk.UpgradeProposalRequest;
@@ -258,13 +257,6 @@ void runChannel(HFClient client, Channel channel, SampleOrg sampleOrg, final int
258257
fail("Not enough endorsers for install :" + successful.size() + ". " + first.getMessage());
259258
}
260259

261-
// Check that all the proposals are consistent with each other. We should have only one set
262-
// where all the proposals above are consistent.
263-
Collection<Set<ProposalResponse>> proposalConsistencySets = SDKUtils.getProposalConsistencySets(responses);
264-
if (proposalConsistencySets.size() != 1) {
265-
fail(format("Expected only one set of consistent install proposal responses but got %d", proposalConsistencySets.size()));
266-
}
267-
268260
//////////////////
269261
// Upgrade chaincode to ***double*** our move results.
270262

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

320-
// Check that all the proposals are consistent with each other. We should have only one set
321-
// where the proposals above are consistent.
322-
proposalConsistencySets = SDKUtils.getProposalConsistencySets(responses2);
323-
if (proposalConsistencySets.size() != 1) {
324-
fail(format("Expected only one set of consistent upgrade proposal responses but got %d", proposalConsistencySets.size()));
325-
}
326-
327312
if (changeContext) {
328313
return channel.sendTransaction(successful, sampleOrg.getPeerAdmin()).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);
329314

@@ -451,13 +436,6 @@ CompletableFuture<BlockEvent.TransactionEvent> moveAmount(HFClient client, Chann
451436
}
452437
}
453438

454-
// Check that all the proposals are consistent with each other. We should have only one set
455-
// where all the proposals above are consistent.
456-
Collection<Set<ProposalResponse>> proposalConsistencySets = SDKUtils.getProposalConsistencySets(invokePropResp);
457-
if (proposalConsistencySets.size() != 1) {
458-
fail(format("Expected only one set of consistent move proposal responses but got %d", proposalConsistencySets.size()));
459-
}
460-
461439
out("Received %d transaction proposal responses. Successful+verified: %d . Failed: %d",
462440
invokePropResp.size(), successful.size(), failed.size());
463441
if (failed.size() > 0) {

src/test/java/org/hyperledger/fabric/sdkintegration/End2endIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ class ChaincodeEventCapture { //A test class to capture chaincode events
339339
}
340340
}
341341

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

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

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

0 commit comments

Comments
 (0)