Search in sources :

Example 1 with TransactionContext

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

the class Channel method sendUpdateChannel.

private void sendUpdateChannel(byte[] configupdate, byte[][] signers, Orderer orderer) throws TransactionException, InvalidArgumentException {
    logger.debug(format("Channel %s sendUpdateChannel", name));
    checkOrderer(orderer);
    try {
        final long nanoTimeStart = System.nanoTime();
        int statusCode = 0;
        do {
            // Make sure we have fresh transaction context for each try just to be safe.
            TransactionContext transactionContext = getTransactionContext();
            ConfigUpdateEnvelope.Builder configUpdateEnvBuilder = ConfigUpdateEnvelope.newBuilder();
            configUpdateEnvBuilder.setConfigUpdate(ByteString.copyFrom(configupdate));
            for (byte[] signer : signers) {
                configUpdateEnvBuilder.addSignatures(ConfigSignature.parseFrom(signer));
            }
            // --------------
            // Construct Payload Envelope.
            final ByteString sigHeaderByteString = getSignatureHeaderAsByteString(transactionContext);
            final ChannelHeader payloadChannelHeader = ProtoUtils.createChannelHeader(HeaderType.CONFIG_UPDATE, transactionContext.getTxID(), name, transactionContext.getEpoch(), transactionContext.getFabricTimestamp(), null, null);
            final Header payloadHeader = Header.newBuilder().setChannelHeader(payloadChannelHeader.toByteString()).setSignatureHeader(sigHeaderByteString).build();
            final ByteString payloadByteString = Payload.newBuilder().setHeader(payloadHeader).setData(configUpdateEnvBuilder.build().toByteString()).build().toByteString();
            ByteString payloadSignature = transactionContext.signByteStrings(payloadByteString);
            Envelope payloadEnv = Envelope.newBuilder().setSignature(payloadSignature).setPayload(payloadByteString).build();
            BroadcastResponse trxResult = orderer.sendTransaction(payloadEnv);
            statusCode = trxResult.getStatusValue();
            logger.debug(format("Channel %s sendUpdateChannel %d", name, statusCode));
            if (statusCode == 404 || statusCode == 503) {
                // these we can retry..
                final long duration = TimeUnit.MILLISECONDS.convert(System.nanoTime() - nanoTimeStart, TimeUnit.NANOSECONDS);
                if (duration > CHANNEL_CONFIG_WAIT_TIME) {
                    // waited long enough .. throw an exception
                    String info = trxResult.getInfo();
                    if (null == info) {
                        info = "";
                    }
                    throw new TransactionException(format("Channel %s update error timed out after %d ms. Status value %d. Status %s. %s", name, duration, statusCode, trxResult.getStatus().name(), info));
                }
                try {
                    // try again sleep
                    Thread.sleep(ORDERER_RETRY_WAIT_TIME);
                } catch (InterruptedException e) {
                    TransactionException te = new TransactionException("update thread Sleep", e);
                    logger.warn(te.getMessage(), te);
                }
            } else if (200 != statusCode) {
                // Can't retry.
                String info = trxResult.getInfo();
                if (null == info) {
                    info = "";
                }
                throw new TransactionException(format("New channel %s error. StatusValue %d. Status %s. %s", name, statusCode, "" + trxResult.getStatus(), info));
            }
        } while (// try again
        200 != statusCode);
    } catch (TransactionException e) {
        logger.error(format("Channel %s error: %s", name, e.getMessage()), e);
        throw e;
    } catch (Exception e) {
        String msg = format("Channel %s error: %s", name, e.getMessage());
        logger.error(msg, e);
        throw new TransactionException(msg, e);
    }
}
Also used : ConfigUpdateEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope) BroadcastResponse(org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ByteString(com.google.protobuf.ByteString) 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) 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) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) Header(org.hyperledger.fabric.protos.common.Common.Header) ChannelHeader(org.hyperledger.fabric.protos.common.Common.ChannelHeader) ChannelHeader(org.hyperledger.fabric.protos.common.Common.ChannelHeader) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext)

Example 2 with TransactionContext

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

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

the class Channel method getChannelConfigurationSignature.

byte[] getChannelConfigurationSignature(ChannelConfiguration channelConfiguration, User signer) throws InvalidArgumentException {
    userContextCheck(signer);
    if (null == channelConfiguration) {
        throw new InvalidArgumentException("channelConfiguration is null");
    }
    try {
        Envelope ccEnvelope = Envelope.parseFrom(channelConfiguration.getChannelConfigurationAsBytes());
        final Payload ccPayload = Payload.parseFrom(ccEnvelope.getPayload());
        TransactionContext transactionContext = getTransactionContext(signer);
        final ConfigUpdateEnvelope configUpdateEnv = ConfigUpdateEnvelope.parseFrom(ccPayload.getData());
        final ByteString configUpdate = configUpdateEnv.getConfigUpdate();
        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 : ConfigUpdateEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope) 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) Payload(org.hyperledger.fabric.protos.common.Common.Payload) 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) 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 4 with TransactionContext

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

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

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