use of org.hyperledger.fabric.sdk.exception.ProposalException in project fabric-sdk-java by hyperledger.
the class Channel method sendProposalSerially.
// ////////////// Channel Block monitoring //////////////////////////////////
private ProposalResponse sendProposalSerially(TransactionRequest proposalRequest, Collection<Peer> peers) throws ProposalException {
ProposalException lastException = new ProposalException("ProposalRequest failed.");
for (Peer peer : peers) {
try {
Collection<ProposalResponse> proposalResponses = sendProposal(proposalRequest, Collections.singletonList(peer));
if (proposalResponses.isEmpty()) {
logger.warn(format("Proposal request to peer %s failed", peer));
}
ProposalResponse proposalResponse = proposalResponses.iterator().next();
ChaincodeResponse.Status status = proposalResponse.getStatus();
if (status.getStatus() < 400) {
return proposalResponse;
} else if (status.getStatus() > 499) {
// server error may work on other peer.
lastException = new ProposalException(format("Channel %s got exception on peer %s %d. %s ", name, peer, status.getStatus(), proposalResponse.getMessage()));
} else {
throw new ProposalException(format("Channel %s got exception on peer %s %d. %s ", name, peer, status.getStatus(), proposalResponse.getMessage()));
}
} catch (Exception e) {
lastException = new ProposalException(format("Channel %s failed proposal on peer %s %s", name, peer.getName(), e.getMessage()), e);
logger.warn(lastException.getMessage());
}
}
throw lastException;
}
use of org.hyperledger.fabric.sdk.exception.ProposalException in project fabric-sdk-java by hyperledger.
the class Channel method sendProposal.
private Collection<ProposalResponse> sendProposal(TransactionRequest proposalRequest, Collection<Peer> peers) throws InvalidArgumentException, ProposalException {
checkChannelState();
checkPeers(peers);
if (null == proposalRequest) {
throw new InvalidArgumentException("The proposalRequest is null");
}
if (Utils.isNullOrEmpty(proposalRequest.getFcn())) {
throw new InvalidArgumentException("The proposalRequest's fcn is null or empty.");
}
if (proposalRequest.getChaincodeID() == null) {
throw new InvalidArgumentException("The proposalRequest's chaincode ID is null");
}
proposalRequest.setSubmitted();
try {
TransactionContext transactionContext = getTransactionContext(proposalRequest.getUserContext());
transactionContext.verify(proposalRequest.doVerify());
transactionContext.setProposalWaitTime(proposalRequest.getProposalWaitTime());
// Protobuf message builder
ProposalBuilder proposalBuilder = ProposalBuilder.newBuilder();
proposalBuilder.context(transactionContext);
proposalBuilder.request(proposalRequest);
SignedProposal invokeProposal = getSignedProposal(transactionContext, proposalBuilder.build());
return sendProposalToPeers(peers, invokeProposal, transactionContext);
} catch (ProposalException e) {
throw e;
} catch (Exception e) {
ProposalException exp = new ProposalException(e);
logger.error(exp.getMessage(), exp);
throw exp;
}
}
use of org.hyperledger.fabric.sdk.exception.ProposalException in project fabric-sdk-java by hyperledger.
the class Channel method queryInstantiatedChaincodes.
/**
* Query peer for chaincode that has been instantiated
*
* @param peer The peer to query.
* @param userContext the user context.
* @return A list of ChaincodeInfo @see {@link ChaincodeInfo}
* @throws InvalidArgumentException
* @throws ProposalException
*/
public List<ChaincodeInfo> queryInstantiatedChaincodes(Peer peer, User userContext) throws InvalidArgumentException, ProposalException {
checkChannelState();
checkPeer(peer);
User.userContextCheck(userContext);
try {
TransactionContext context = getTransactionContext(userContext);
FabricProposal.Proposal q = QueryInstantiatedChaincodesBuilder.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();
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()));
}
ChaincodeQueryResponse chaincodeQueryResponse = ChaincodeQueryResponse.parseFrom(fabricResponseResponse.getPayload());
return chaincodeQueryResponse.getChaincodesList();
} 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 queryBlockByTransactionID.
/**
* query a peer in this channel for a Block by a TransactionID contained in the block
*
* @param peers the peer to try to send the request to
* @param txID the transactionID to query on
* @param userContext the user context.
* @return the {@link BlockInfo} for the Block containing the transaction
* @throws InvalidArgumentException
* @throws ProposalException
*/
public BlockInfo queryBlockByTransactionID(Collection<Peer> peers, String txID, User userContext) throws InvalidArgumentException, ProposalException {
checkChannelState();
checkPeers(peers);
User.userContextCheck(userContext);
if (txID == null) {
throw new InvalidArgumentException("TxID parameter is null.");
}
try {
logger.debug("queryBlockByTransactionID with txID " + txID + " \n " + " on channel " + name);
QuerySCCRequest querySCCRequest = new QuerySCCRequest(userContext);
querySCCRequest.setFcn(QuerySCCRequest.GETBLOCKBYTXID);
querySCCRequest.setArgs(name, txID);
ProposalResponse proposalResponse = sendProposalSerially(querySCCRequest, peers);
return new BlockInfo(Block.parseFrom(proposalResponse.getProposalResponse().getResponse().getPayload()));
} catch (InvalidProtocolBufferException e) {
throw new ProposalException(e);
}
}
use of org.hyperledger.fabric.sdk.exception.ProposalException in project fabric-sdk-java by hyperledger.
the class Channel method queryBlockByNumber.
/**
* query a peer in this channel for a Block by the blockNumber
*
* @param peers the peers to try and send the request to
* @param blockNumber index of the Block in the chain
* @param userContext the user context to use.
* @return the {@link BlockInfo} with the given blockNumber
* @throws InvalidArgumentException
* @throws ProposalException
*/
public BlockInfo queryBlockByNumber(Collection<Peer> peers, long blockNumber, User userContext) throws InvalidArgumentException, ProposalException {
checkChannelState();
checkPeers(peers);
userContextCheck(userContext);
try {
logger.debug("queryBlockByNumber with blockNumber " + blockNumber + " on channel " + name);
QuerySCCRequest querySCCRequest = new QuerySCCRequest(userContext);
querySCCRequest.setFcn(QuerySCCRequest.GETBLOCKBYNUMBER);
querySCCRequest.setArgs(name, Long.toUnsignedString(blockNumber));
ProposalResponse proposalResponse = sendProposalSerially(querySCCRequest, peers);
return new BlockInfo(Block.parseFrom(proposalResponse.getProposalResponse().getResponse().getPayload()));
} catch (InvalidProtocolBufferException e) {
logger.error(e);
throw new ProposalException(e);
}
}
Aggregations