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

Commit 9224fa3

Browse files
committed
FAB-7702 GetConfigBlock
Change-Id: I8426196731995c514e729e217e054d8358813c7f Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 9bf5095 commit 9224fa3

File tree

3 files changed

+128
-6
lines changed

3 files changed

+128
-6
lines changed

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

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import org.hyperledger.fabric.sdk.helper.Config;
102102
import org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper;
103103
import org.hyperledger.fabric.sdk.helper.Utils;
104+
import org.hyperledger.fabric.sdk.transaction.GetConfigBlockBuilder;
104105
import org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder;
105106
import org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder;
106107
import org.hyperledger.fabric.sdk.transaction.JoinPeerProposalBuilder;
@@ -714,6 +715,52 @@ public Channel joinPeer(Peer peer, PeerOptions peerOptions) throws ProposalExcep
714715
return this;
715716
}
716717

718+
private Block getConfigBlock(Peer peer) throws ProposalException {
719+
720+
logger.debug(format("getConfigBlock for channel %s with peer %s, url: %s", name, peer.getName(), peer.getUrl()));
721+
722+
if (shutdown) {
723+
throw new ProposalException(format("Channel %s has been shutdown.", name));
724+
}
725+
726+
try {
727+
728+
final Channel systemChannel = newSystemChannel(client); //needs to be invoked on system channel
729+
730+
TransactionContext transactionContext = systemChannel.getTransactionContext();
731+
732+
FabricProposal.Proposal proposal = GetConfigBlockBuilder.newBuilder()
733+
.context(transactionContext)
734+
.channelId(name)
735+
.build();
736+
737+
logger.debug("Getting signed proposal.");
738+
SignedProposal signedProposal = getSignedProposal(transactionContext, proposal);
739+
logger.debug("Got signed proposal.");
740+
741+
Collection<ProposalResponse> resp = sendProposalToPeers(new ArrayList<>(Collections.singletonList(peer)),
742+
signedProposal, transactionContext);
743+
744+
ProposalResponse pro = resp.iterator().next();
745+
746+
if (pro.getStatus() == ProposalResponse.Status.SUCCESS) {
747+
logger.trace(format("getConfigBlock from peer %s on channel %s success", peer.getName(), name));
748+
return Block.parseFrom(pro.getProposalResponse().getResponse().getPayload().toByteArray());
749+
} else {
750+
throw new ProposalException(format("getConfigBlock for channel %s failed with peer %s. Status %s, details: %s",
751+
name, peer.getName(), pro.getStatus().toString(), pro.getMessage()));
752+
753+
}
754+
} catch (ProposalException e) {
755+
logger.error(format("getConfigBlock for channel %s failed with peer %s.", name, peer.getName()), e);
756+
throw e;
757+
} catch (Exception e) {
758+
logger.error(format("getConfigBlock for channel %s failed with peer %s.", name, peer.getName()), e);
759+
throw new ProposalException(e.getMessage(), e);
760+
}
761+
762+
}
763+
717764
/**
718765
* Removes the peer connection from the channel.
719766
* This does NOT unjoin the peer from from the channel.
@@ -1100,11 +1147,13 @@ protected void parseConfigBlock() throws TransactionException {
11001147

11011148
try {
11021149

1103-
final Block configBlock = getConfigurationBlock();
1150+
Block parseFrom = getConfigBlock(getRandomPeer());
1151+
1152+
// final Block configBlock = getConfigurationBlock();
11041153

11051154
logger.debug(format("Channel %s Got config block getting MSP data and anchorPeers data", name));
11061155

1107-
Envelope envelope = Envelope.parseFrom(configBlock.getData().getData(0));
1156+
Envelope envelope = Envelope.parseFrom(parseFrom.getData().getData(0));
11081157
Payload payload = Payload.parseFrom(envelope.getPayload());
11091158
ConfigEnvelope configEnvelope = ConfigEnvelope.parseFrom(payload.getData());
11101159
ConfigGroup channelGroup = configEnvelope.getConfig().getChannelGroup();
@@ -1114,9 +1163,6 @@ protected void parseConfigBlock() throws TransactionException {
11141163

11151164
// anchorPeers = Collections.unmodifiableSet(traverseConfigGroupsAnchors("", channelGroup, new HashSet<>()));
11161165

1117-
} catch (TransactionException e) {
1118-
logger.error(e.getMessage(), e);
1119-
throw e;
11201166
} catch (Exception e) {
11211167
logger.error(e.getMessage(), e);
11221168
throw new TransactionException(e);
@@ -1217,7 +1263,7 @@ private Block getConfigurationBlock() throws TransactionException {
12171263

12181264
public byte[] getChannelConfigurationBytes() throws TransactionException {
12191265
try {
1220-
final Block configBlock = getConfigurationBlock();
1266+
final Block configBlock = getConfigBlock(getRandomPeer());
12211267

12221268
Envelope envelopeRet = Envelope.parseFrom(configBlock.getData().getData(0));
12231269

@@ -1697,6 +1743,17 @@ private Peer getRandomLedgerQueryPeer() throws InvalidArgumentException {
16971743

16981744
}
16991745

1746+
private Peer getRandomPeer() throws InvalidArgumentException {
1747+
1748+
final ArrayList<Peer> randPicks = new ArrayList<>(getPeers()); //copy to avoid unlikely changes
1749+
1750+
if (randPicks.isEmpty()) {
1751+
throw new InvalidArgumentException("Channel " + name + " does not have any peers associated with it.");
1752+
}
1753+
1754+
return randPicks.get(RANDOM.nextInt(randPicks.size()));
1755+
}
1756+
17001757
private Orderer getRandomOrderer() throws InvalidArgumentException {
17011758

17021759
final ArrayList<Orderer> randPicks = new ArrayList<>(new HashSet<>(getOrderers())); //copy to avoid unlikely changes
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
*
3+
* Copyright 2016,2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
package org.hyperledger.fabric.sdk.transaction;
18+
19+
import java.nio.charset.StandardCharsets;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import com.google.protobuf.ByteString;
24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
26+
import org.hyperledger.fabric.sdk.exception.ProposalException;
27+
28+
public class GetConfigBlockBuilder extends CSCCProposalBuilder {
29+
private static final Log logger = LogFactory.getLog(GetConfigBlockBuilder.class);
30+
List<ByteString> argList = new ArrayList<>();
31+
32+
public GetConfigBlockBuilder channelId(String channelId) throws ProposalException {
33+
34+
if (channelId == null) {
35+
ProposalException exp = new ProposalException("Parameter channelId needs to be non-empty string .");
36+
logger.error(exp.getMessage(), exp);
37+
throw exp;
38+
}
39+
40+
argList.add(ByteString.copyFrom(channelId, StandardCharsets.UTF_8));
41+
42+
return this;
43+
}
44+
45+
private GetConfigBlockBuilder() {
46+
47+
argList.add(ByteString.copyFrom("GetConfigBlock", StandardCharsets.UTF_8));
48+
args(argList);
49+
50+
}
51+
52+
@Override
53+
public GetConfigBlockBuilder context(TransactionContext context) {
54+
super.context(context);
55+
return this;
56+
}
57+
58+
public static GetConfigBlockBuilder newBuilder() {
59+
return new GetConfigBlockBuilder();
60+
}
61+
62+
}
63+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ public void setup() {
190190

191191
//Let's add some additional verification...
192192

193+
client.setUserContext(sampleOrg.getPeerAdmin());
194+
193195
final byte[] modChannelBytes = fooChannel.getChannelConfigurationBytes();
194196

195197
//Now decode the new channel config bytes to json...

0 commit comments

Comments
 (0)