Search in sources :

Example 16 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext 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 17 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.

the class Channel method getUpdateChannelConfigurationSignature.

/**
 * Get signed byes of the update channel.
 *
 * @param updateChannelConfiguration
 * @param signer
 * @return
 * @throws InvalidArgumentException
 */
public byte[] getUpdateChannelConfigurationSignature(UpdateChannelConfiguration updateChannelConfiguration, User signer) throws InvalidArgumentException {
    userContextCheck(signer);
    if (null == updateChannelConfiguration) {
        throw new InvalidArgumentException("channelConfiguration is null");
    }
    try {
        TransactionContext transactionContext = getTransactionContext(signer);
        final ByteString configUpdate = ByteString.copyFrom(updateChannelConfiguration.getUpdateChannelConfigurationAsBytes());
        ByteString sigHeaderByteString = getSignatureHeaderAsByteString(signer, transactionContext);
        ByteString signatureByteSting = transactionContext.signByteStrings(new User[] { signer }, sigHeaderByteString, configUpdate)[0];
        return ConfigSignature.newBuilder().setSignatureHeader(sigHeaderByteString).setSignature(signatureByteSting).build().toByteArray();
    } catch (Exception e) {
        throw new InvalidArgumentException(e);
    } finally {
        logger.debug("finally done");
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) 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)

Example 18 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext 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 19 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext 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

TransactionContext (org.hyperledger.fabric.sdk.transaction.TransactionContext)19 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)13 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 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)13 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)13 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)13 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)13 FabricProposal (org.hyperledger.fabric.protos.peer.FabricProposal)10 SignedProposal (org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal)9 InstallProposalBuilder (org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder)7 ByteString (com.google.protobuf.ByteString)6 File (java.io.File)5 FabricProposalResponse (org.hyperledger.fabric.protos.peer.FabricProposalResponse)5 Test (org.junit.Test)5 BroadcastResponse (org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse)4