Search in sources :

Example 1 with ChannelHeader

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

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

the class Channel method getConfigurationBlock.

/**
 * Provide the Channel's latest raw Configuration Block.
 *
 * @return Channel configuration block.
 * @throws TransactionException
 */
private Block getConfigurationBlock() throws TransactionException {
    logger.debug(format("getConfigurationBlock for channel %s", name));
    try {
        Orderer orderer = getRandomOrderer();
        long lastConfigIndex = getLastConfigIndex(orderer);
        logger.debug(format("Last config index is %d", lastConfigIndex));
        Block configBlock = getBlockByNumber(lastConfigIndex);
        // Little extra parsing but make sure this really is a config block for this channel.
        Envelope envelopeRet = Envelope.parseFrom(configBlock.getData().getData(0));
        Payload payload = Payload.parseFrom(envelopeRet.getPayload());
        ChannelHeader channelHeader = ChannelHeader.parseFrom(payload.getHeader().getChannelHeader());
        if (channelHeader.getType() != HeaderType.CONFIG.getNumber()) {
            throw new TransactionException(format("Bad last configuration block type %d, expected %d", channelHeader.getType(), HeaderType.CONFIG.getNumber()));
        }
        if (!name.equals(channelHeader.getChannelId())) {
            throw new TransactionException(format("Bad last configuration block channel id %s, expected %s", channelHeader.getChannelId(), name));
        }
        if (null != diagnosticFileDumper) {
            logger.trace(format("Channel %s getConfigurationBlock returned %s", name, diagnosticFileDumper.createDiagnosticFile(String.valueOf(configBlock).getBytes())));
        }
        if (!logger.isTraceEnabled()) {
            logger.debug(format("Channel %s getConfigurationBlock returned", name));
        }
        return configBlock;
    } catch (TransactionException e) {
        logger.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TransactionException(e);
    }
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ChannelHeader(org.hyperledger.fabric.protos.common.Common.ChannelHeader) Block(org.hyperledger.fabric.protos.common.Common.Block) 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 3 with ChannelHeader

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

the class ProtoUtils method createChannelHeader.

// static CryptoSuite suite = null;
/*
     * createChannelHeader create chainHeader
     *
     * @param type                     header type. See {@link ChannelHeader.Builder#setType}.
     * @param txID                     transaction ID. See {@link ChannelHeader.Builder#setTxId}.
     * @param channelID                channel ID. See {@link ChannelHeader.Builder#setChannelId}.
     * @param epoch                    the epoch in which this header was generated. See {@link ChannelHeader.Builder#setEpoch}.
     * @param timeStamp                local time when the message was created. See {@link ChannelHeader.Builder#setTimestamp}.
     * @param chaincodeHeaderExtension extension to attach dependent on the header type. See {@link ChannelHeader.Builder#setExtension}.
     * @param tlsCertHash
     * @return a new chain header.
     */
public static ChannelHeader createChannelHeader(HeaderType type, String txID, String channelID, long epoch, Timestamp timeStamp, ChaincodeHeaderExtension chaincodeHeaderExtension, byte[] tlsCertHash) {
    if (isDebugLevel) {
        String tlschs = "";
        if (tlsCertHash != null) {
            tlschs = DatatypeConverter.printHexBinary(tlsCertHash);
        }
        logger.debug(format("ChannelHeader: type: %s, version: 1, Txid: %s, channelId: %s, epoch %d, clientTLSCertificate digest: %s", type.name(), txID, channelID, epoch, tlschs));
    }
    ChannelHeader.Builder ret = ChannelHeader.newBuilder().setType(type.getNumber()).setVersion(1).setTxId(txID).setChannelId(channelID).setTimestamp(timeStamp).setEpoch(epoch);
    if (null != chaincodeHeaderExtension) {
        ret.setExtension(chaincodeHeaderExtension.toByteString());
    }
    if (tlsCertHash != null) {
        ret.setTlsCertHash(ByteString.copyFrom(tlsCertHash));
    }
    return ret.build();
}
Also used : ChannelHeader(org.hyperledger.fabric.protos.common.Common.ChannelHeader) ByteString(com.google.protobuf.ByteString) Utils.logString(org.hyperledger.fabric.sdk.helper.Utils.logString) Utils.toHexString(org.hyperledger.fabric.sdk.helper.Utils.toHexString)

Example 4 with ChannelHeader

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

the class ProtoUtils method createSeekInfoEnvelope.

public static Envelope createSeekInfoEnvelope(TransactionContext transactionContext, SeekInfo seekInfo, byte[] tlsCertHash) throws CryptoException {
    ChannelHeader seekInfoHeader = createChannelHeader(Common.HeaderType.DELIVER_SEEK_INFO, transactionContext.getTxID(), transactionContext.getChannelID(), transactionContext.getEpoch(), transactionContext.getFabricTimestamp(), null, tlsCertHash);
    SignatureHeader signatureHeader = SignatureHeader.newBuilder().setCreator(transactionContext.getIdentity().toByteString()).setNonce(transactionContext.getNonce()).build();
    Common.Header seekHeader = Common.Header.newBuilder().setSignatureHeader(signatureHeader.toByteString()).setChannelHeader(seekInfoHeader.toByteString()).build();
    Payload seekPayload = Payload.newBuilder().setHeader(seekHeader).setData(seekInfo.toByteString()).build();
    return Envelope.newBuilder().setSignature(transactionContext.signByteString(seekPayload.toByteArray())).setPayload(seekPayload.toByteString()).build();
}
Also used : ChannelHeader(org.hyperledger.fabric.protos.common.Common.ChannelHeader) SignatureHeader(org.hyperledger.fabric.protos.common.Common.SignatureHeader) Payload(org.hyperledger.fabric.protos.common.Common.Payload) Common(org.hyperledger.fabric.protos.common.Common)

Aggregations

ChannelHeader (org.hyperledger.fabric.protos.common.Common.ChannelHeader)4 ByteString (com.google.protobuf.ByteString)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Envelope (org.hyperledger.fabric.protos.common.Common.Envelope)2 Payload (org.hyperledger.fabric.protos.common.Common.Payload)2 ConfigEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope)2 ConfigUpdateEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope)2 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)2 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)2 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)2 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)2 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)2 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)2 ProtoUtils.createSeekInfoEnvelope (org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope)2 Common (org.hyperledger.fabric.protos.common.Common)1 Block (org.hyperledger.fabric.protos.common.Common.Block)1