Search in sources :

Example 6 with SignedProposal

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

Example 7 with SignedProposal

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

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

Example 9 with SignedProposal

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

the class Channel method sendInstallProposal.

/**
 * Send install chaincode request proposal to the channel.
 *
 * @param installProposalRequest
 * @param peers
 * @return
 * @throws ProposalException
 * @throws InvalidArgumentException
 */
Collection<ProposalResponse> sendInstallProposal(InstallProposalRequest installProposalRequest, Collection<Peer> peers) throws ProposalException, InvalidArgumentException {
    checkChannelState();
    checkPeers(peers);
    if (null == installProposalRequest) {
        throw new InvalidArgumentException("InstallProposalRequest is null");
    }
    try {
        TransactionContext transactionContext = getTransactionContext(installProposalRequest.getUserContext());
        // Install will have no signing cause it's not really targeted to a channel.
        transactionContext.verify(false);
        transactionContext.setProposalWaitTime(installProposalRequest.getProposalWaitTime());
        InstallProposalBuilder installProposalbuilder = InstallProposalBuilder.newBuilder();
        installProposalbuilder.context(transactionContext);
        installProposalbuilder.setChaincodeLanguage(installProposalRequest.getChaincodeLanguage());
        installProposalbuilder.chaincodeName(installProposalRequest.getChaincodeName());
        installProposalbuilder.chaincodePath(installProposalRequest.getChaincodePath());
        installProposalbuilder.chaincodeVersion(installProposalRequest.getChaincodeVersion());
        installProposalbuilder.setChaincodeSource(installProposalRequest.getChaincodeSourceLocation());
        installProposalbuilder.setChaincodeInputStream(installProposalRequest.getChaincodeInputStream());
        installProposalbuilder.setChaincodeMetaInfLocation(installProposalRequest.getChaincodeMetaInfLocation());
        FabricProposal.Proposal deploymentProposal = installProposalbuilder.build();
        SignedProposal signedProposal = getSignedProposal(transactionContext, deploymentProposal);
        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) InstallProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder) 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

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