Search in sources :

Example 1 with Header

use of org.hyperledger.fabric.protos.common.Common.Header 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 Header

use of org.hyperledger.fabric.protos.common.Common.Header in project fabric-sdk-java by hyperledger.

the class ProposalResponse method getChaincodeID.

// public ByteString getPayload() {
// return proposalResponse.getPayload();
// }
/**
 * Chaincode ID that was executed.
 *
 * @return See {@link ChaincodeID}
 * @throws InvalidArgumentException
 */
public ChaincodeID getChaincodeID() throws InvalidArgumentException {
    try {
        if (chaincodeID == null) {
            Header header = Header.parseFrom(proposal.getHeader());
            Common.ChannelHeader channelHeader = Common.ChannelHeader.parseFrom(header.getChannelHeader());
            ChaincodeHeaderExtension chaincodeHeaderExtension = ChaincodeHeaderExtension.parseFrom(channelHeader.getExtension());
            chaincodeID = new ChaincodeID(chaincodeHeaderExtension.getChaincodeId());
        }
        return chaincodeID;
    } catch (Exception e) {
        throw new InvalidArgumentException(e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) Header(org.hyperledger.fabric.protos.common.Common.Header) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) Common(org.hyperledger.fabric.protos.common.Common) ChaincodeHeaderExtension(org.hyperledger.fabric.protos.peer.FabricProposal.ChaincodeHeaderExtension)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 Header (org.hyperledger.fabric.protos.common.Common.Header)2 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)2 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)2 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)2 ByteString (com.google.protobuf.ByteString)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Common (org.hyperledger.fabric.protos.common.Common)1 ChannelHeader (org.hyperledger.fabric.protos.common.Common.ChannelHeader)1 Envelope (org.hyperledger.fabric.protos.common.Common.Envelope)1 ConfigEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope)1 ConfigUpdateEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope)1 BroadcastResponse (org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse)1 ChaincodeHeaderExtension (org.hyperledger.fabric.protos.peer.FabricProposal.ChaincodeHeaderExtension)1 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)1 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)1 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)1