Search in sources :

Example 1 with ProposalException

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;
}
Also used : ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Example 2 with ProposalException

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;
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) InstallProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder) UpgradeProposalBuilder(org.hyperledger.fabric.sdk.transaction.UpgradeProposalBuilder) ProposalBuilder(org.hyperledger.fabric.sdk.transaction.ProposalBuilder) JoinPeerProposalBuilder(org.hyperledger.fabric.sdk.transaction.JoinPeerProposalBuilder) InstantiateProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder) SignedProposal(org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Example 3 with ProposalException

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);
    }
}
Also used : BroadcastResponse(org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) Response(org.hyperledger.fabric.protos.peer.FabricProposalResponse.Response) ChaincodeQueryResponse(org.hyperledger.fabric.protos.peer.Query.ChaincodeQueryResponse) ChannelQueryResponse(org.hyperledger.fabric.protos.peer.Query.ChannelQueryResponse) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse) ChaincodeQueryResponse(org.hyperledger.fabric.protos.peer.Query.ChaincodeQueryResponse) FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) SignedProposal(org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Example 4 with ProposalException

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);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse)

Example 5 with ProposalException

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);
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse)

Aggregations

ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)20 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)16 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)16 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)14 StatusRuntimeException (io.grpc.StatusRuntimeException)13 IOException (java.io.IOException)13 ExecutionException (java.util.concurrent.ExecutionException)13 TimeoutException (java.util.concurrent.TimeoutException)13 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)13 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)13 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)13 FabricProposalResponse (org.hyperledger.fabric.protos.peer.FabricProposalResponse)12 SignedProposal (org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal)9 TransactionContext (org.hyperledger.fabric.sdk.transaction.TransactionContext)9 FabricProposal (org.hyperledger.fabric.protos.peer.FabricProposal)7 ByteString (com.google.protobuf.ByteString)3 BroadcastResponse (org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse)3 DeliverResponse (org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)3 Response (org.hyperledger.fabric.protos.peer.FabricProposalResponse.Response)3 ChaincodeQueryResponse (org.hyperledger.fabric.protos.peer.Query.ChaincodeQueryResponse)3