Search in sources :

Example 26 with InvalidArgumentException

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

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

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

the class Channel method sendTransaction.

/**
 * Send transaction to one of a specified set of orderers with the specified user context.
 * IF there are no event hubs or eventing peers this future returns immediately completed
 * indicating that orderer has accepted the transaction only.
 *
 * @param proposalResponses
 * @param transactionOptions
 * @return Future allowing access to the result of the transaction invocation.
 */
public CompletableFuture<TransactionEvent> sendTransaction(Collection<ProposalResponse> proposalResponses, TransactionOptions transactionOptions) {
    try {
        if (null == transactionOptions) {
            throw new InvalidArgumentException("Parameter transactionOptions can't be null");
        }
        checkChannelState();
        User userContext = transactionOptions.userContext != null ? transactionOptions.userContext : client.getUserContext();
        userContextCheck(userContext);
        if (null == proposalResponses) {
            throw new InvalidArgumentException("sendTransaction proposalResponses was null");
        }
        List<Orderer> orderers = transactionOptions.orderers != null ? transactionOptions.orderers : new ArrayList<>(getOrderers());
        // make certain we have our own copy
        final List<Orderer> shuffeledOrderers = new ArrayList<>(orderers);
        if (transactionOptions.shuffleOrders) {
            Collections.shuffle(shuffeledOrderers);
        }
        if (config.getProposalConsistencyValidation()) {
            HashSet<ProposalResponse> invalid = new HashSet<>();
            int consistencyGroups = SDKUtils.getProposalConsistencySets(proposalResponses, invalid).size();
            if (consistencyGroups != 1 || !invalid.isEmpty()) {
                throw new IllegalArgumentException(format("The proposal responses have %d inconsistent groups with %d that are invalid." + " Expected all to be consistent and none to be invalid.", consistencyGroups, invalid.size()));
            }
        }
        List<FabricProposalResponse.Endorsement> ed = new LinkedList<>();
        FabricProposal.Proposal proposal = null;
        ByteString proposalResponsePayload = null;
        String proposalTransactionID = null;
        for (ProposalResponse sdkProposalResponse : proposalResponses) {
            ed.add(sdkProposalResponse.getProposalResponse().getEndorsement());
            if (proposal == null) {
                proposal = sdkProposalResponse.getProposal();
                proposalTransactionID = sdkProposalResponse.getTransactionID();
                proposalResponsePayload = sdkProposalResponse.getProposalResponse().getPayload();
            }
        }
        TransactionBuilder transactionBuilder = TransactionBuilder.newBuilder();
        Payload transactionPayload = transactionBuilder.chaincodeProposal(proposal).endorsements(ed).proposalResponsePayload(proposalResponsePayload).build();
        Envelope transactionEnvelope = createTransactionEnvelope(transactionPayload, userContext);
        NOfEvents nOfEvents = transactionOptions.nOfEvents;
        if (nOfEvents == null) {
            nOfEvents = NOfEvents.createNofEvents();
            Collection<Peer> eventingPeers = getEventingPeers();
            boolean anyAdded = false;
            if (!eventingPeers.isEmpty()) {
                anyAdded = true;
                nOfEvents.addPeers(eventingPeers);
            }
            Collection<EventHub> eventHubs = getEventHubs();
            if (!eventHubs.isEmpty()) {
                anyAdded = true;
                nOfEvents.addEventHubs(getEventHubs());
            }
            if (!anyAdded) {
                nOfEvents = NOfEvents.createNoEvents();
            }
        } else if (nOfEvents != NOfEvents.nofNoEvents) {
            StringBuilder issues = new StringBuilder(100);
            Collection<Peer> eventingPeers = getEventingPeers();
            nOfEvents.unSeenPeers().forEach(peer -> {
                if (peer.getChannel() != this) {
                    issues.append(format("Peer %s added to NOFEvents does not belong this channel. ", peer.getName()));
                } else if (!eventingPeers.contains(peer)) {
                    issues.append(format("Peer %s added to NOFEvents is not a eventing Peer in this channel. ", peer.getName()));
                }
            });
            nOfEvents.unSeenEventHubs().forEach(eventHub -> {
                if (!eventHubs.contains(eventHub)) {
                    issues.append(format("Eventhub %s added to NOFEvents does not belong this channel. ", eventHub.getName()));
                }
            });
            if (nOfEvents.unSeenEventHubs().isEmpty() && nOfEvents.unSeenPeers().isEmpty()) {
                issues.append("NofEvents had no Eventhubs added or Peer eventing services.");
            }
            String foundIssues = issues.toString();
            if (!foundIssues.isEmpty()) {
                throw new InvalidArgumentException(foundIssues);
            }
        }
        final boolean replyonly = nOfEvents == NOfEvents.nofNoEvents || (getEventHubs().isEmpty() && getEventingPeers().isEmpty());
        CompletableFuture<TransactionEvent> sret;
        if (replyonly) {
            // If there are no eventhubs to complete the future, complete it
            // immediately but give no transaction event
            logger.debug(format("Completing transaction id %s immediately no event hubs or peer eventing services found in channel %s.", proposalTransactionID, name));
            sret = new CompletableFuture<>();
        } else {
            sret = registerTxListener(proposalTransactionID, nOfEvents, transactionOptions.failFast);
        }
        logger.debug(format("Channel %s sending transaction to orderer(s) with TxID %s ", name, proposalTransactionID));
        boolean success = false;
        // Save last exception to report to user .. others are just logged.
        Exception lException = null;
        BroadcastResponse resp = null;
        Orderer failed = null;
        for (Orderer orderer : shuffeledOrderers) {
            if (failed != null) {
                logger.warn(format("Channel %s  %s failed. Now trying %s.", name, failed, orderer));
            }
            failed = orderer;
            try {
                if (null != diagnosticFileDumper) {
                    logger.trace(format("Sending to channel %s, orderer: %s, transaction: %s", name, orderer.getName(), diagnosticFileDumper.createDiagnosticProtobufFile(transactionEnvelope.toByteArray())));
                }
                resp = orderer.sendTransaction(transactionEnvelope);
                // no longer last exception .. maybe just failed.
                lException = null;
                if (resp.getStatus() == Status.SUCCESS) {
                    success = true;
                    break;
                } else {
                    logger.warn(format("Channel %s %s failed. Status returned %s", name, orderer, getRespData(resp)));
                }
            } catch (Exception e) {
                String emsg = format("Channel %s unsuccessful sendTransaction to orderer %s (%s)", name, orderer.getName(), orderer.getUrl());
                if (resp != null) {
                    emsg = format("Channel %s unsuccessful sendTransaction to orderer %s (%s).  %s", name, orderer.getName(), orderer.getUrl(), getRespData(resp));
                }
                logger.error(emsg);
                lException = new Exception(emsg, e);
            }
        }
        if (success) {
            logger.debug(format("Channel %s successful sent to Orderer transaction id: %s", name, proposalTransactionID));
            if (replyonly) {
                // just say we're done.
                sret.complete(null);
            }
            return sret;
        } else {
            String emsg = format("Channel %s failed to place transaction %s on Orderer. Cause: UNSUCCESSFUL. %s", name, proposalTransactionID, getRespData(resp));
            unregisterTxListener(proposalTransactionID);
            CompletableFuture<TransactionEvent> ret = new CompletableFuture<>();
            ret.completeExceptionally(lException != null ? new Exception(emsg, lException) : new Exception(emsg));
            return ret;
        }
    } catch (Exception e) {
        CompletableFuture<TransactionEvent> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;
    }
}
Also used : InstallProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder) Arrays(java.util.Arrays) GetConfigBlockBuilder(org.hyperledger.fabric.sdk.transaction.GetConfigBlockBuilder) MspConfig(org.hyperledger.fabric.protos.msp.MspConfig) BroadcastResponse(org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse) Header(org.hyperledger.fabric.protos.common.Common.Header) ConfigEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope) Future(java.util.concurrent.Future) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) Map(java.util.Map) ChannelHeader(org.hyperledger.fabric.protos.common.Common.ChannelHeader) QueryPeerChannelsBuilder(org.hyperledger.fabric.sdk.transaction.QueryPeerChannelsBuilder) ConfigSignature(org.hyperledger.fabric.protos.common.Configtx.ConfigSignature) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) Response(org.hyperledger.fabric.protos.peer.FabricProposalResponse.Response) EnumSet(java.util.EnumSet) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) SignedProposal(org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ConfigGroup(org.hyperledger.fabric.protos.common.Configtx.ConfigGroup) Set(java.util.Set) QueryInstalledChaincodesBuilder(org.hyperledger.fabric.sdk.transaction.QueryInstalledChaincodesBuilder) BlockingQueue(java.util.concurrent.BlockingQueue) ProcessedTransaction(org.hyperledger.fabric.protos.peer.FabricTransaction.ProcessedTransaction) Ab(org.hyperledger.fabric.protos.orderer.Ab) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) UpgradeProposalBuilder(org.hyperledger.fabric.sdk.transaction.UpgradeProposalBuilder) Status(org.hyperledger.fabric.protos.common.Common.Status) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) LogFactory(org.apache.commons.logging.LogFactory) HeaderType(org.hyperledger.fabric.protos.common.Common.HeaderType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConfigValue(org.hyperledger.fabric.protos.common.Configtx.ConfigValue) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) ConfigUpdateEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope) ProposalBuilder(org.hyperledger.fabric.sdk.transaction.ProposalBuilder) TransactionOptions.createTransactionOptions(org.hyperledger.fabric.sdk.Channel.TransactionOptions.createTransactionOptions) ArrayList(java.util.ArrayList) Block(org.hyperledger.fabric.protos.common.Common.Block) LinkedHashMap(java.util.LinkedHashMap) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) ProtoUtils(org.hyperledger.fabric.sdk.transaction.ProtoUtils) QueryInstantiatedChaincodesBuilder(org.hyperledger.fabric.sdk.transaction.QueryInstantiatedChaincodesBuilder) ObjectOutputStream(java.io.ObjectOutputStream) Payload(org.hyperledger.fabric.protos.common.Common.Payload) Utils.isNullOrEmpty(org.hyperledger.fabric.sdk.helper.Utils.isNullOrEmpty) Envelope(org.hyperledger.fabric.protos.common.Common.Envelope) Utils(org.hyperledger.fabric.sdk.helper.Utils) SeekInfo(org.hyperledger.fabric.protos.orderer.Ab.SeekInfo) Files(java.nio.file.Files) IOException(java.io.IOException) DiagnosticFileDumper(org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper) ChaincodeQueryResponse(org.hyperledger.fabric.protos.peer.Query.ChaincodeQueryResponse) TransactionBuilder(org.hyperledger.fabric.sdk.transaction.TransactionBuilder) File(java.io.File) Config(org.hyperledger.fabric.sdk.helper.Config) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) Paths(java.nio.file.Paths) BlockMetadata(org.hyperledger.fabric.protos.common.Common.BlockMetadata) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) SeekPosition(org.hyperledger.fabric.protos.orderer.Ab.SeekPosition) SeekSpecified(org.hyperledger.fabric.protos.orderer.Ab.SeekSpecified) User.userContextCheck(org.hyperledger.fabric.sdk.User.userContextCheck) ScheduledFuture(java.util.concurrent.ScheduledFuture) ProtoUtils.createSeekInfoEnvelope(org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope) ObjectInputStream(java.io.ObjectInputStream) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Collection(java.util.Collection) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) StandardOpenOption(java.nio.file.StandardOpenOption) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ChaincodeInfo(org.hyperledger.fabric.protos.peer.Query.ChaincodeInfo) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) JoinPeerProposalBuilder(org.hyperledger.fabric.sdk.transaction.JoinPeerProposalBuilder) Ledger(org.hyperledger.fabric.protos.common.Ledger) String.format(java.lang.String.format) Objects(java.util.Objects) ByteString(com.google.protobuf.ByteString) Metadata(org.hyperledger.fabric.protos.common.Common.Metadata) List(java.util.List) Pattern(java.util.regex.Pattern) FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) PeerRole(org.hyperledger.fabric.sdk.Peer.PeerRole) TransactionEvent(org.hyperledger.fabric.sdk.BlockEvent.TransactionEvent) InstantiateProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Hex(org.apache.commons.codec.binary.Hex) HashSet(java.util.HashSet) ChannelQueryResponse(org.hyperledger.fabric.protos.peer.Query.ChannelQueryResponse) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) Query(org.hyperledger.fabric.protos.peer.Query) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse) TimeUnit(java.util.concurrent.TimeUnit) LastConfig(org.hyperledger.fabric.protos.common.Common.LastConfig) Log(org.apache.commons.logging.Log) Collections(java.util.Collections) BroadcastResponse(org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) TransactionBuilder(org.hyperledger.fabric.sdk.transaction.TransactionBuilder) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ByteString(com.google.protobuf.ByteString) ConfigEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope) ConfigUpdateEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope) Envelope(org.hyperledger.fabric.protos.common.Common.Envelope) ProtoUtils.createSeekInfoEnvelope(org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope) TransactionEvent(org.hyperledger.fabric.sdk.BlockEvent.TransactionEvent) CompletableFuture(java.util.concurrent.CompletableFuture) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) HashSet(java.util.HashSet) LinkedList(java.util.LinkedList) 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) FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) Collection(java.util.Collection) Payload(org.hyperledger.fabric.protos.common.Common.Payload) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse)

Example 29 with InvalidArgumentException

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

the class Channel method initialize.

/**
 * Initialize the Channel.  Starts the channel. event hubs will connect.
 *
 * @return this channel.
 * @throws InvalidArgumentException
 * @throws TransactionException
 */
public Channel initialize() throws InvalidArgumentException, TransactionException {
    logger.debug(format("Channel %s initialize shutdown %b", name, shutdown));
    if (shutdown) {
        throw new InvalidArgumentException(format("Channel %s has been shutdown.", name));
    }
    if (isNullOrEmpty(name)) {
        throw new InvalidArgumentException("Can not initialize channel without a valid name.");
    }
    if (client == null) {
        throw new InvalidArgumentException("Can not initialize channel without a client object.");
    }
    userContextCheck(client.getUserContext());
    try {
        // put all MSP certs into cryptoSuite if this fails here we'll try again later.
        loadCACertificates();
    } catch (Exception e) {
        logger.warn(format("Channel %s could not load peer CA certificates from any peers.", name));
    }
    try {
        logger.debug(format("Eventque started %s", "" + eventQueueThread));
        for (EventHub eh : eventHubs) {
            // Connect all event hubs
            eh.connect(getTransactionContext());
        }
        for (Peer peer : getEventingPeers()) {
            peer.initiateEventing(getTransactionContext(), getPeersOptions(peer));
        }
        logger.debug(format("%d eventhubs initialized", getEventHubs().size()));
        // Manage transactions.
        registerTransactionListenerProcessor();
        logger.debug(format("Channel %s registerTransactionListenerProcessor completed", name));
        // Run the event for event messages from event hubs.
        startEventQue();
        this.initialized = true;
        logger.debug(format("Channel %s initialized", name));
        return this;
    // } catch (TransactionException e) {
    // logger.error(e.getMessage(), e);
    // throw e;
    } catch (Exception e) {
        TransactionException exp = new TransactionException(e);
        logger.error(exp.getMessage(), exp);
        throw exp;
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) 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 30 with InvalidArgumentException

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

the class Channel method queryTransactionByID.

/**
 * Query for a Fabric Transaction given its transactionID
 *
 * @param txID        the ID of the transaction
 * @param peers       the peers to try to send the request.
 * @param userContext the user context
 * @return a {@link TransactionInfo}
 * @throws ProposalException
 * @throws InvalidArgumentException
 */
public TransactionInfo queryTransactionByID(Collection<Peer> peers, String txID, User userContext) throws ProposalException, InvalidArgumentException {
    checkChannelState();
    checkPeers(peers);
    User.userContextCheck(userContext);
    if (txID == null) {
        throw new InvalidArgumentException("TxID parameter is null.");
    }
    TransactionInfo transactionInfo;
    try {
        logger.debug("queryTransactionByID with txID " + txID + "\n    from peer " + " on channel " + name);
        QuerySCCRequest querySCCRequest = new QuerySCCRequest(userContext);
        querySCCRequest.setFcn(QuerySCCRequest.GETTRANSACTIONBYID);
        querySCCRequest.setArgs(name, txID);
        ProposalResponse proposalResponse = sendProposalSerially(querySCCRequest, peers);
        return new TransactionInfo(txID, ProcessedTransaction.parseFrom(proposalResponse.getProposalResponse().getResponse().getPayload()));
    } catch (Exception e) {
        logger.error(e);
        throw new ProposalException(e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) 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

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