Skip to content

Commit

Permalink
FAB-7702 GetConfigBlock
Browse files Browse the repository at this point in the history
Change-Id: I8426196731995c514e729e217e054d8358813c7f
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Jan 22, 2018
1 parent 9bf5095 commit 9224fa3
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import org.hyperledger.fabric.sdk.helper.Config;
import org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric.sdk.transaction.GetConfigBlockBuilder;
import org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder;
import org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder;
import org.hyperledger.fabric.sdk.transaction.JoinPeerProposalBuilder;
Expand Down Expand Up @@ -714,6 +715,52 @@ public Channel joinPeer(Peer peer, PeerOptions peerOptions) throws ProposalExcep
return this;
}

private Block getConfigBlock(Peer peer) throws ProposalException {

logger.debug(format("getConfigBlock for channel %s with peer %s, url: %s", name, peer.getName(), peer.getUrl()));

if (shutdown) {
throw new ProposalException(format("Channel %s has been shutdown.", name));
}

try {

final Channel systemChannel = newSystemChannel(client); //needs to be invoked on system channel

TransactionContext transactionContext = systemChannel.getTransactionContext();

FabricProposal.Proposal proposal = GetConfigBlockBuilder.newBuilder()
.context(transactionContext)
.channelId(name)
.build();

logger.debug("Getting signed proposal.");
SignedProposal signedProposal = getSignedProposal(transactionContext, proposal);
logger.debug("Got signed proposal.");

Collection<ProposalResponse> resp = sendProposalToPeers(new ArrayList<>(Collections.singletonList(peer)),
signedProposal, transactionContext);

ProposalResponse pro = resp.iterator().next();

if (pro.getStatus() == ProposalResponse.Status.SUCCESS) {
logger.trace(format("getConfigBlock from peer %s on channel %s success", peer.getName(), name));
return Block.parseFrom(pro.getProposalResponse().getResponse().getPayload().toByteArray());
} else {
throw new ProposalException(format("getConfigBlock for channel %s failed with peer %s. Status %s, details: %s",
name, peer.getName(), pro.getStatus().toString(), pro.getMessage()));

}
} catch (ProposalException e) {
logger.error(format("getConfigBlock for channel %s failed with peer %s.", name, peer.getName()), e);
throw e;
} catch (Exception e) {
logger.error(format("getConfigBlock for channel %s failed with peer %s.", name, peer.getName()), e);
throw new ProposalException(e.getMessage(), e);
}

}

/**
* Removes the peer connection from the channel.
* This does NOT unjoin the peer from from the channel.
Expand Down Expand Up @@ -1100,11 +1147,13 @@ protected void parseConfigBlock() throws TransactionException {

try {

final Block configBlock = getConfigurationBlock();
Block parseFrom = getConfigBlock(getRandomPeer());

// final Block configBlock = getConfigurationBlock();

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

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

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

} catch (TransactionException e) {
logger.error(e.getMessage(), e);
throw e;
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new TransactionException(e);
Expand Down Expand Up @@ -1217,7 +1263,7 @@ private Block getConfigurationBlock() throws TransactionException {

public byte[] getChannelConfigurationBytes() throws TransactionException {
try {
final Block configBlock = getConfigurationBlock();
final Block configBlock = getConfigBlock(getRandomPeer());

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

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

}

private Peer getRandomPeer() throws InvalidArgumentException {

final ArrayList<Peer> randPicks = new ArrayList<>(getPeers()); //copy to avoid unlikely changes

if (randPicks.isEmpty()) {
throw new InvalidArgumentException("Channel " + name + " does not have any peers associated with it.");
}

return randPicks.get(RANDOM.nextInt(randPicks.size()));
}

private Orderer getRandomOrderer() throws InvalidArgumentException {

final ArrayList<Orderer> randPicks = new ArrayList<>(new HashSet<>(getOrderers())); //copy to avoid unlikely changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
*
* Copyright 2016,2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.hyperledger.fabric.sdk.transaction;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import com.google.protobuf.ByteString;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.fabric.sdk.exception.ProposalException;

public class GetConfigBlockBuilder extends CSCCProposalBuilder {
private static final Log logger = LogFactory.getLog(GetConfigBlockBuilder.class);
List<ByteString> argList = new ArrayList<>();

public GetConfigBlockBuilder channelId(String channelId) throws ProposalException {

if (channelId == null) {
ProposalException exp = new ProposalException("Parameter channelId needs to be non-empty string .");
logger.error(exp.getMessage(), exp);
throw exp;
}

argList.add(ByteString.copyFrom(channelId, StandardCharsets.UTF_8));

return this;
}

private GetConfigBlockBuilder() {

argList.add(ByteString.copyFrom("GetConfigBlock", StandardCharsets.UTF_8));
args(argList);

}

@Override
public GetConfigBlockBuilder context(TransactionContext context) {
super.context(context);
return this;
}

public static GetConfigBlockBuilder newBuilder() {
return new GetConfigBlockBuilder();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ public void setup() {

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

client.setUserContext(sampleOrg.getPeerAdmin());

final byte[] modChannelBytes = fooChannel.getChannelConfigurationBytes();

//Now decode the new channel config bytes to json...
Expand Down

0 comments on commit 9224fa3

Please sign in to comment.