use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Channel method queryChannels.
// ///////////////////////////////////////////////////////
// transactions order
Set<String> queryChannels(Peer peer) throws InvalidArgumentException, ProposalException {
checkPeer(peer);
if (!isSystemChannel()) {
throw new InvalidArgumentException("queryChannels should only be invoked on system channel.");
}
try {
TransactionContext context = getTransactionContext();
FabricProposal.Proposal q = QueryPeerChannelsBuilder.newBuilder().context(context).build();
SignedProposal qProposal = getSignedProposal(context, q);
Collection<ProposalResponse> proposalResponses = sendProposalToPeers(Collections.singletonList(peer), qProposal, context);
if (null == proposalResponses) {
throw new ProposalException(format("Peer %s channel query return with null for responses", peer.getName()));
}
if (proposalResponses.size() != 1) {
throw new ProposalException(format("Peer %s channel query expected one response but got back %d responses ", peer.getName(), proposalResponses.size()));
}
ProposalResponse proposalResponse = proposalResponses.iterator().next();
if (proposalResponse.getStatus() != ChaincodeResponse.Status.SUCCESS) {
throw new ProposalException(format("Failed exception message is %s, status is %d", proposalResponse.getMessage(), proposalResponse.getStatus().getStatus()));
}
FabricProposalResponse.ProposalResponse fabricResponse = proposalResponse.getProposalResponse();
if (null == fabricResponse) {
throw new ProposalException(format("Peer %s channel query return with empty fabric response", peer.getName()));
}
final Response fabricResponseResponse = fabricResponse.getResponse();
if (null == fabricResponseResponse) {
// not likely but check it.
throw new ProposalException(format("Peer %s channel query return with empty fabricResponseResponse", peer.getName()));
}
if (200 != fabricResponseResponse.getStatus()) {
throw new ProposalException(format("Peer %s channel query expected 200, actual returned was: %d. " + fabricResponseResponse.getMessage(), peer.getName(), fabricResponseResponse.getStatus()));
}
ChannelQueryResponse qr = ChannelQueryResponse.parseFrom(fabricResponseResponse.getPayload());
Set<String> ret = new HashSet<>(qr.getChannelsCount());
for (Query.ChannelInfo x : qr.getChannelsList()) {
ret.add(x.getChannelId());
}
return ret;
} catch (ProposalException e) {
throw e;
} catch (Exception e) {
throw new ProposalException(format("Query for peer %s channels failed. " + e.getMessage(), name), e);
}
}
use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Channel method getUpdateChannelConfigurationSignature.
/**
* Get signed byes of the update channel.
*
* @param updateChannelConfiguration
* @param signer
* @return
* @throws InvalidArgumentException
*/
public byte[] getUpdateChannelConfigurationSignature(UpdateChannelConfiguration updateChannelConfiguration, User signer) throws InvalidArgumentException {
userContextCheck(signer);
if (null == updateChannelConfiguration) {
throw new InvalidArgumentException("channelConfiguration is null");
}
try {
TransactionContext transactionContext = getTransactionContext(signer);
final ByteString configUpdate = ByteString.copyFrom(updateChannelConfiguration.getUpdateChannelConfigurationAsBytes());
ByteString sigHeaderByteString = getSignatureHeaderAsByteString(signer, transactionContext);
ByteString signatureByteSting = transactionContext.signByteStrings(new User[] { signer }, sigHeaderByteString, configUpdate)[0];
return ConfigSignature.newBuilder().setSignatureHeader(sigHeaderByteString).setSignature(signatureByteSting).build().toByteArray();
} catch (Exception e) {
throw new InvalidArgumentException(e);
} finally {
logger.debug("finally done");
}
}
use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Channel method joinPeer.
/**
* Join peer to channel
*
* @param orderer The orderer to get the genesis block.
* @param peer the peer to join the channel.
* @param peerOptions see {@link PeerOptions}
* @return
* @throws ProposalException
*/
public Channel joinPeer(Orderer orderer, Peer peer, PeerOptions peerOptions) throws ProposalException {
logger.debug(format("Channel %s joining peer %s, url: %s", name, peer.getName(), peer.getUrl()));
if (shutdown) {
throw new ProposalException(format("Channel %s has been shutdown.", name));
}
Channel peerChannel = peer.getChannel();
if (null != peerChannel && peerChannel != this) {
throw new ProposalException(format("Can not add peer %s to channel %s because it already belongs to channel %s.", peer.getName(), name, peerChannel.getName()));
}
if (genesisBlock == null && orderers.isEmpty()) {
ProposalException e = new ProposalException("Channel missing genesis block and no orderers configured");
logger.error(e.getMessage(), e);
}
try {
genesisBlock = getGenesisBlock(orderer);
logger.debug(format("Channel %s got genesis block", name));
// channel is not really created and this is targeted to system channel
final Channel systemChannel = newSystemChannel(client);
TransactionContext transactionContext = systemChannel.getTransactionContext();
FabricProposal.Proposal joinProposal = JoinPeerProposalBuilder.newBuilder().context(transactionContext).genesisBlock(genesisBlock).build();
logger.debug("Getting signed proposal.");
SignedProposal signedProposal = getSignedProposal(transactionContext, joinProposal);
logger.debug("Got signed proposal.");
// need to add peer.
addPeer(peer, peerOptions);
Collection<ProposalResponse> resp = sendProposalToPeers(new ArrayList<>(Collections.singletonList(peer)), signedProposal, transactionContext);
ProposalResponse pro = resp.iterator().next();
if (pro.getStatus() == ProposalResponse.Status.SUCCESS) {
logger.info(format("Peer %s joined into channel %s", peer.getName(), name));
} else {
removePeerInternal(peer);
throw new ProposalException(format("Join peer to channel %s failed. Status %s, details: %s", name, pro.getStatus().toString(), pro.getMessage()));
}
} catch (ProposalException e) {
removePeerInternal(peer);
logger.error(e);
throw e;
} catch (Exception e) {
peers.remove(peer);
logger.error(e);
throw new ProposalException(e.getMessage(), e);
}
return this;
}
use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Channel method sendInstallProposal.
/**
* Send install chaincode request proposal to the channel.
*
* @param installProposalRequest
* @param peers
* @return
* @throws ProposalException
* @throws InvalidArgumentException
*/
Collection<ProposalResponse> sendInstallProposal(InstallProposalRequest installProposalRequest, Collection<Peer> peers) throws ProposalException, InvalidArgumentException {
checkChannelState();
checkPeers(peers);
if (null == installProposalRequest) {
throw new InvalidArgumentException("InstallProposalRequest is null");
}
try {
TransactionContext transactionContext = getTransactionContext(installProposalRequest.getUserContext());
// Install will have no signing cause it's not really targeted to a channel.
transactionContext.verify(false);
transactionContext.setProposalWaitTime(installProposalRequest.getProposalWaitTime());
InstallProposalBuilder installProposalbuilder = InstallProposalBuilder.newBuilder();
installProposalbuilder.context(transactionContext);
installProposalbuilder.setChaincodeLanguage(installProposalRequest.getChaincodeLanguage());
installProposalbuilder.chaincodeName(installProposalRequest.getChaincodeName());
installProposalbuilder.chaincodePath(installProposalRequest.getChaincodePath());
installProposalbuilder.chaincodeVersion(installProposalRequest.getChaincodeVersion());
installProposalbuilder.setChaincodeSource(installProposalRequest.getChaincodeSourceLocation());
installProposalbuilder.setChaincodeInputStream(installProposalRequest.getChaincodeInputStream());
installProposalbuilder.setChaincodeMetaInfLocation(installProposalRequest.getChaincodeMetaInfLocation());
FabricProposal.Proposal deploymentProposal = installProposalbuilder.build();
SignedProposal signedProposal = getSignedProposal(transactionContext, deploymentProposal);
return sendProposalToPeers(peers, signedProposal, transactionContext);
} catch (Exception e) {
throw new ProposalException(e);
}
}
Aggregations