Search in sources :

Example 11 with ProposalException

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);
    }
}
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 12 with ProposalException

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);
}
Also used : TransactionProposalRequest(org.hyperledger.fabric.sdk.TransactionProposalRequest) Set(java.util.Set) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) ProposalResponse(org.hyperledger.fabric.sdk.ProposalResponse) LinkedList(java.util.LinkedList)

Example 13 with ProposalException

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);
    }
}
Also used : TransactionProposalRequest(org.hyperledger.fabric.sdk.TransactionProposalRequest) CompletionException(java.util.concurrent.CompletionException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) ProposalResponse(org.hyperledger.fabric.sdk.ProposalResponse) LinkedList(java.util.LinkedList) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) CompletionException(java.util.concurrent.CompletionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) MalformedURLException(java.net.MalformedURLException)

Example 14 with ProposalException

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);
    }
}
Also used : ChannelQueryResponse(org.hyperledger.fabric.protos.peer.Query.ChannelQueryResponse) Query(org.hyperledger.fabric.protos.peer.Query) SignedProposal(org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal) 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) 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) 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) HashSet(java.util.HashSet)

Example 15 with ProposalException

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;
}
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

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