Search in sources :

Example 1 with SignedProposal

use of org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal 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 2 with SignedProposal

use of org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal 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 3 with SignedProposal

use of org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal 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 4 with SignedProposal

use of org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal 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 5 with SignedProposal

use of org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal in project fabric-sdk-java by hyperledger.

the class Channel method getConfigBlock.

private Block getConfigBlock(List<Peer> peers) throws ProposalException {
    if (shutdown) {
        throw new ProposalException(format("Channel %s has been shutdown.", name));
    }
    if (peers.isEmpty()) {
        throw new ProposalException("No peers go get config block");
    }
    TransactionContext transactionContext = null;
    SignedProposal signedProposal = null;
    try {
        transactionContext = getTransactionContext();
        // can't verify till we get the config block.
        transactionContext.verify(false);
        FabricProposal.Proposal proposal = GetConfigBlockBuilder.newBuilder().context(transactionContext).channelId(name).build();
        logger.debug("Getting signed proposal.");
        signedProposal = getSignedProposal(transactionContext, proposal);
        logger.debug("Got signed proposal.");
    } catch (Exception e) {
        throw new ProposalException(e);
    }
    ProposalException lastException = new ProposalException(format("getConfigBlock for channel %s failed.", name));
    for (Peer peer : peers) {
        try {
            Collection<ProposalResponse> resp = sendProposalToPeers(new ArrayList<>(Collections.singletonList(peer)), signedProposal, transactionContext);
            if (!resp.isEmpty()) {
                ProposalResponse pro = resp.iterator().next();
                if (pro.getStatus() == ProposalResponse.Status.SUCCESS) {
                    logger.trace(format("getConfigBlock from peer %s on channel %s success", peer.getName(), name));
                    return Block.parseFrom(pro.getProposalResponse().getResponse().getPayload().toByteArray());
                } else {
                    lastException = new ProposalException(format("getConfigBlock for channel %s failed with peer %s.  Status %s, details: %s", name, peer.getName(), pro.getStatus().toString(), pro.getMessage()));
                    logger.warn(lastException.getMessage());
                }
            } else {
                logger.warn(format("Got empty proposals from %s", peer));
            }
        } catch (Exception e) {
            lastException = new ProposalException(format("getConfigBlock for channel %s failed with peer %s.", name, peer.getName()), e);
            logger.warn(lastException.getMessage());
        }
    }
    throw lastException;
}
Also used : FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) 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)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 StatusRuntimeException (io.grpc.StatusRuntimeException)9 IOException (java.io.IOException)9 ExecutionException (java.util.concurrent.ExecutionException)9 TimeoutException (java.util.concurrent.TimeoutException)9 SignedProposal (org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal)9 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)9 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)9 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)9 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)9 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)9 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)9 TransactionContext (org.hyperledger.fabric.sdk.transaction.TransactionContext)9 FabricProposal (org.hyperledger.fabric.protos.peer.FabricProposal)7 FabricProposalResponse (org.hyperledger.fabric.protos.peer.FabricProposalResponse)5 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 ChannelQueryResponse (org.hyperledger.fabric.protos.peer.Query.ChannelQueryResponse)3