use of org.hyperledger.fabric.sdk.exception.ProposalException in project fabric-sdk-java by hyperledger.
the class Channel method queryBlockchainInfo.
/**
* query for chain information
*
* @param peers The peers to try send the request.
* @param userContext the user context.
* @return a {@link BlockchainInfo} object containing the chain info requested
* @throws InvalidArgumentException
* @throws ProposalException
*/
public BlockchainInfo queryBlockchainInfo(Collection<Peer> peers, User userContext) throws ProposalException, InvalidArgumentException {
checkChannelState();
checkPeers(peers);
User.userContextCheck(userContext);
try {
logger.debug("queryBlockchainInfo to peer " + " on channel " + name);
QuerySCCRequest querySCCRequest = new QuerySCCRequest(userContext);
querySCCRequest.setFcn(QuerySCCRequest.GETCHAININFO);
querySCCRequest.setArgs(name);
ProposalResponse proposalResponse = sendProposalSerially(querySCCRequest, peers);
return new BlockchainInfo(Ledger.BlockchainInfo.parseFrom(proposalResponse.getProposalResponse().getResponse().getPayload()));
} catch (Exception e) {
logger.error(e);
throw new ProposalException(e);
}
}
use of org.hyperledger.fabric.sdk.exception.ProposalException in project fabric-sdk-java by hyperledger.
the class NetworkConfigIT method moveAmount.
private static CompletableFuture<BlockEvent.TransactionEvent> moveAmount(HFClient client, Channel channel, ChaincodeID chaincodeID, String from, String to, String moveAmount, User user) throws Exception {
Collection<ProposalResponse> successful = new LinkedList<>();
Collection<ProposalResponse> failed = new LinkedList<>();
// /////////////
// / Send transaction proposal to all peers
TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
transactionProposalRequest.setChaincodeID(chaincodeID);
transactionProposalRequest.setFcn("move");
transactionProposalRequest.setArgs(from, to, moveAmount);
transactionProposalRequest.setProposalWaitTime(testConfig.getProposalWaitTime());
if (user != null) {
// specific user use that
transactionProposalRequest.setUserContext(user);
}
out("sending transaction proposal to all peers with arguments: move(%s,%s,%s)", from, to, moveAmount);
Collection<ProposalResponse> invokePropResp = channel.sendTransactionProposal(transactionProposalRequest);
for (ProposalResponse response : invokePropResp) {
if (response.getStatus() == Status.SUCCESS) {
out("Successful transaction proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName());
successful.add(response);
} else {
failed.add(response);
}
}
// 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) {
ProposalResponse firstTransactionProposalResponse = failed.iterator().next();
throw new ProposalException(format("Not enough endorsers for invoke(move %s,%s,%s):%d endorser error:%s. Was verified:%b", from, to, moveAmount, firstTransactionProposalResponse.getStatus().getStatus(), firstTransactionProposalResponse.getMessage(), firstTransactionProposalResponse.isVerified()));
}
out("Successfully received transaction proposal responses.");
// //////////////////////////
// Send transaction to orderer
out("Sending chaincode transaction(move %s,%s,%s) to orderer.", from, to, moveAmount);
if (user != null) {
return channel.sendTransaction(successful, user);
}
return channel.sendTransaction(successful);
}
use of org.hyperledger.fabric.sdk.exception.ProposalException in project fabric-sdk-java by hyperledger.
the class End2endAndBackAgainIT method moveAmount.
CompletableFuture<BlockEvent.TransactionEvent> moveAmount(HFClient client, Channel channel, ChaincodeID chaincodeID, String moveAmount, User user) {
try {
Collection<ProposalResponse> successful = new LinkedList<>();
Collection<ProposalResponse> failed = new LinkedList<>();
// /////////////
// / Send transaction proposal to all peers
TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
transactionProposalRequest.setChaincodeID(chaincodeID);
transactionProposalRequest.setFcn("move");
transactionProposalRequest.setArgs(new byte[][] { // test using bytes .. end2end uses Strings.
"a".getBytes(UTF_8), "b".getBytes(UTF_8), moveAmount.getBytes(UTF_8) });
transactionProposalRequest.setProposalWaitTime(testConfig.getProposalWaitTime());
if (user != null) {
// specific user use that
transactionProposalRequest.setUserContext(user);
}
out("sending transaction proposal to all peers with arguments: move(a,b,%s)", moveAmount);
Collection<ProposalResponse> invokePropResp = channel.sendTransactionProposal(transactionProposalRequest);
for (ProposalResponse response : invokePropResp) {
if (response.getStatus() == Status.SUCCESS) {
out("Successful transaction proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName());
successful.add(response);
} else {
failed.add(response);
}
}
out("Received %d transaction proposal responses. Successful+verified: %d . Failed: %d", invokePropResp.size(), successful.size(), failed.size());
if (failed.size() > 0) {
ProposalResponse firstTransactionProposalResponse = failed.iterator().next();
throw new ProposalException(format("Not enough endorsers for invoke(move a,b,%s):%d endorser error:%s. Was verified:%b", moveAmount, firstTransactionProposalResponse.getStatus().getStatus(), firstTransactionProposalResponse.getMessage(), firstTransactionProposalResponse.isVerified()));
}
out("Successfully received transaction proposal responses.");
// //////////////////////////
// Send transaction to orderer
out("Sending chaincode transaction(move a,b,%s) to orderer.", moveAmount);
if (user != null) {
return channel.sendTransaction(successful, user);
}
return channel.sendTransaction(successful);
} catch (Exception e) {
throw new CompletionException(e);
}
}
use of org.hyperledger.fabric.sdk.exception.ProposalException 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.exception.ProposalException 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;
}
Aggregations