Search in sources :

Example 6 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException 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 7 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class Channel method queryInstalledChaincodes.

List<ChaincodeInfo> queryInstalledChaincodes(Peer peer) throws InvalidArgumentException, ProposalException {
    checkPeer(peer);
    if (!isSystemChannel()) {
        throw new InvalidArgumentException("queryInstalledChaincodes should only be invoked on system channel.");
    }
    try {
        TransactionContext context = getTransactionContext();
        FabricProposal.Proposal q = QueryInstalledChaincodesBuilder.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 : 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) 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) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse)

Example 8 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class Channel method sendUpgradeProposal.

/**
 * Send Upgrade proposal proposal to upgrade chaincode to a new version.
 *
 * @param upgradeProposalRequest
 * @param peers                  the specific peers to send to.
 * @return Collection of proposal responses.
 * @throws ProposalException
 * @throws InvalidArgumentException
 */
public Collection<ProposalResponse> sendUpgradeProposal(UpgradeProposalRequest upgradeProposalRequest, Collection<Peer> peers) throws InvalidArgumentException, ProposalException {
    checkChannelState();
    checkPeers(peers);
    if (null == upgradeProposalRequest) {
        throw new InvalidArgumentException("Upgradeproposal is null");
    }
    try {
        TransactionContext transactionContext = getTransactionContext(upgradeProposalRequest.getUserContext());
        // transactionContext.verify(false);  // Install will have no signing cause it's not really targeted to a channel.
        transactionContext.setProposalWaitTime(upgradeProposalRequest.getProposalWaitTime());
        UpgradeProposalBuilder upgradeProposalBuilder = UpgradeProposalBuilder.newBuilder();
        upgradeProposalBuilder.context(transactionContext);
        upgradeProposalBuilder.argss(upgradeProposalRequest.getArgs());
        upgradeProposalBuilder.chaincodeName(upgradeProposalRequest.getChaincodeName());
        upgradeProposalBuilder.chaincodePath(upgradeProposalRequest.getChaincodePath());
        upgradeProposalBuilder.chaincodeVersion(upgradeProposalRequest.getChaincodeVersion());
        upgradeProposalBuilder.chaincodEndorsementPolicy(upgradeProposalRequest.getChaincodeEndorsementPolicy());
        SignedProposal signedProposal = getSignedProposal(transactionContext, upgradeProposalBuilder.build());
        return sendProposalToPeers(peers, signedProposal, transactionContext);
    } catch (Exception e) {
        throw new ProposalException(e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) UpgradeProposalBuilder(org.hyperledger.fabric.sdk.transaction.UpgradeProposalBuilder) 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 9 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class Channel method sendProposalToPeers.

private Collection<ProposalResponse> sendProposalToPeers(Collection<Peer> peers, SignedProposal signedProposal, TransactionContext transactionContext) throws InvalidArgumentException, ProposalException {
    checkPeers(peers);
    if (transactionContext.getVerify()) {
        try {
            loadCACertificates();
        } catch (Exception e) {
            throw new ProposalException(e);
        }
    }
    class Pair {

        private final Peer peer;

        private final Future<FabricProposalResponse.ProposalResponse> future;

        private Pair(Peer peer, Future<FabricProposalResponse.ProposalResponse> future) {
            this.peer = peer;
            this.future = future;
        }
    }
    List<Pair> peerFuturePairs = new ArrayList<>();
    for (Peer peer : peers) {
        logger.debug(format("Channel %s send proposal to peer %s at url %s", name, peer.getName(), peer.getUrl()));
        if (null != diagnosticFileDumper) {
            logger.trace(format("Sending to channel %s, peer: %s, proposal: %s", name, peer.getName(), diagnosticFileDumper.createDiagnosticProtobufFile(signedProposal.toByteArray())));
        }
        Future<FabricProposalResponse.ProposalResponse> proposalResponseListenableFuture;
        try {
            proposalResponseListenableFuture = peer.sendProposalAsync(signedProposal);
        } catch (Exception e) {
            proposalResponseListenableFuture = new CompletableFuture<>();
            ((CompletableFuture) proposalResponseListenableFuture).completeExceptionally(e);
        }
        peerFuturePairs.add(new Pair(peer, proposalResponseListenableFuture));
    }
    Collection<ProposalResponse> proposalResponses = new ArrayList<>();
    for (Pair peerFuturePair : peerFuturePairs) {
        FabricProposalResponse.ProposalResponse fabricResponse = null;
        String message;
        int status = 500;
        final String peerName = peerFuturePair.peer.getName();
        try {
            fabricResponse = peerFuturePair.future.get(transactionContext.getProposalWaitTime(), TimeUnit.MILLISECONDS);
            message = fabricResponse.getResponse().getMessage();
            status = fabricResponse.getResponse().getStatus();
            logger.debug(format("Channel %s got back from peer %s status: %d, message: %s", name, peerName, status, message));
            if (null != diagnosticFileDumper) {
                logger.trace(format("Got back from channel %s, peer: %s, proposal response: %s", name, peerName, diagnosticFileDumper.createDiagnosticProtobufFile(fabricResponse.toByteArray())));
            }
        } catch (InterruptedException e) {
            message = "Sending proposal to " + peerName + " failed because of interruption";
            logger.error(message, e);
        } catch (TimeoutException e) {
            message = format("Sending proposal to " + peerName + " failed because of timeout(%d milliseconds) expiration", transactionContext.getProposalWaitTime());
            logger.error(message, e);
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof Error) {
                String emsg = "Sending proposal to " + peerName + " failed because of " + cause.getMessage();
                // wrapped in exception to get full stack trace.
                logger.error(emsg, new Exception(cause));
                throw (Error) cause;
            } else {
                if (cause instanceof StatusRuntimeException) {
                    message = format("Sending proposal to " + peerName + " failed because of: gRPC failure=%s", ((StatusRuntimeException) cause).getStatus());
                } else {
                    message = format("Sending proposal to " + peerName + " failed because of: %s", cause.getMessage());
                }
                // wrapped in exception to get full stack trace.
                logger.error(message, new Exception(cause));
            }
        }
        ProposalResponse proposalResponse = new ProposalResponse(transactionContext.getTxID(), transactionContext.getChannelID(), status, message);
        proposalResponse.setProposalResponse(fabricResponse);
        proposalResponse.setProposal(signedProposal);
        proposalResponse.setPeer(peerFuturePair.peer);
        if (fabricResponse != null && transactionContext.getVerify()) {
            proposalResponse.verify(client.getCryptoSuite());
        }
        proposalResponses.add(proposalResponse);
    }
    return proposalResponses;
}
Also used : ArrayList(java.util.ArrayList) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ByteString(com.google.protobuf.ByteString) 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) CompletableFuture(java.util.concurrent.CompletableFuture) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) Future(java.util.concurrent.Future) ScheduledFuture(java.util.concurrent.ScheduledFuture) CompletableFuture(java.util.concurrent.CompletableFuture) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class Channel method sendInstantiationProposal.

/**
 * Send instantiate request to the channel. Chaincode is created and initialized.
 *
 * @param instantiateProposalRequest
 * @param peers
 * @return responses from peers.
 * @throws InvalidArgumentException
 * @throws ProposalException
 */
public Collection<ProposalResponse> sendInstantiationProposal(InstantiateProposalRequest instantiateProposalRequest, Collection<Peer> peers) throws InvalidArgumentException, ProposalException {
    checkChannelState();
    if (null == instantiateProposalRequest) {
        throw new InvalidArgumentException("InstantiateProposalRequest is null");
    }
    instantiateProposalRequest.setSubmitted();
    checkPeers(peers);
    try {
        TransactionContext transactionContext = getTransactionContext(instantiateProposalRequest.getUserContext());
        transactionContext.setProposalWaitTime(instantiateProposalRequest.getProposalWaitTime());
        InstantiateProposalBuilder instantiateProposalbuilder = InstantiateProposalBuilder.newBuilder();
        instantiateProposalbuilder.context(transactionContext);
        instantiateProposalbuilder.argss(instantiateProposalRequest.getArgs());
        instantiateProposalbuilder.chaincodeName(instantiateProposalRequest.getChaincodeName());
        instantiateProposalbuilder.chaincodeType(instantiateProposalRequest.getChaincodeLanguage());
        instantiateProposalbuilder.chaincodePath(instantiateProposalRequest.getChaincodePath());
        instantiateProposalbuilder.chaincodeVersion(instantiateProposalRequest.getChaincodeVersion());
        instantiateProposalbuilder.chaincodEndorsementPolicy(instantiateProposalRequest.getChaincodeEndorsementPolicy());
        instantiateProposalbuilder.setTransientMap(instantiateProposalRequest.getTransientMap());
        FabricProposal.Proposal instantiateProposal = instantiateProposalbuilder.build();
        SignedProposal signedProposal = getSignedProposal(transactionContext, instantiateProposal);
        return sendProposalToPeers(peers, signedProposal, transactionContext);
    } catch (Exception e) {
        throw new ProposalException(e);
    }
}
Also used : FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) InstantiateProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) 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)

Aggregations

InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)38 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)26 IOException (java.io.IOException)22 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)22 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)21 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)17 StatusRuntimeException (io.grpc.StatusRuntimeException)16 ExecutionException (java.util.concurrent.ExecutionException)16 TimeoutException (java.util.concurrent.TimeoutException)16 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)16 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)16 ByteString (com.google.protobuf.ByteString)11 TransactionContext (org.hyperledger.fabric.sdk.transaction.TransactionContext)11 FabricProposalResponse (org.hyperledger.fabric.protos.peer.FabricProposalResponse)9 SignedProposal (org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal)8 ProtoUtils.getSignatureHeaderAsByteString (org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString)7 FabricProposal (org.hyperledger.fabric.protos.peer.FabricProposal)6 CertificateException (java.security.cert.CertificateException)5 BroadcastResponse (org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse)5 ArrayList (java.util.ArrayList)4