Search in sources :

Example 1 with TransactionException

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

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

the class Channel method getLatestBlock.

private Block getLatestBlock(Orderer orderer) throws TransactionException {
    logger.debug(format("getConfigurationBlock for channel %s", name));
    SeekPosition seekPosition = SeekPosition.newBuilder().setNewest(Ab.SeekNewest.getDefaultInstance()).build();
    SeekInfo seekInfo = SeekInfo.newBuilder().setStart(seekPosition).setStop(seekPosition).setBehavior(SeekInfo.SeekBehavior.BLOCK_UNTIL_READY).build();
    ArrayList<DeliverResponse> deliverResponses = new ArrayList<>();
    seekBlock(seekInfo, deliverResponses, orderer);
    DeliverResponse blockresp = deliverResponses.get(1);
    Block latestBlock = blockresp.getBlock();
    if (latestBlock == null) {
        throw new TransactionException(format("newest block for channel %s fetch bad deliver returned null:", name));
    }
    logger.trace(format("Received latest  block for channel %s, block no:%d", name, latestBlock.getHeader().getNumber()));
    return latestBlock;
}
Also used : SeekInfo(org.hyperledger.fabric.protos.orderer.Ab.SeekInfo) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) SeekPosition(org.hyperledger.fabric.protos.orderer.Ab.SeekPosition) ArrayList(java.util.ArrayList) Block(org.hyperledger.fabric.protos.common.Common.Block) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)

Example 3 with TransactionException

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

the class Channel method updateChannelConfiguration.

/**
 * Update channel with specified channel configuration
 *
 * @param updateChannelConfiguration Channel configuration
 * @param signers                    signers
 * @param orderer                    The specific orderer to use.
 * @throws TransactionException
 * @throws InvalidArgumentException
 */
public void updateChannelConfiguration(UpdateChannelConfiguration updateChannelConfiguration, Orderer orderer, byte[]... signers) throws TransactionException, InvalidArgumentException {
    checkChannelState();
    checkOrderer(orderer);
    try {
        final long startLastConfigIndex = getLastConfigIndex(orderer);
        logger.trace(format("startLastConfigIndex: %d. Channel config wait time is: %d", startLastConfigIndex, CHANNEL_CONFIG_WAIT_TIME));
        sendUpdateChannel(updateChannelConfiguration.getUpdateChannelConfigurationAsBytes(), signers, orderer);
        long currentLastConfigIndex = -1;
        final long nanoTimeStart = System.nanoTime();
        // Try to wait to see the channel got updated but don't fail if we don't see it.
        do {
            currentLastConfigIndex = getLastConfigIndex(orderer);
            if (currentLastConfigIndex == startLastConfigIndex) {
                final long duration = TimeUnit.MILLISECONDS.convert(System.nanoTime() - nanoTimeStart, TimeUnit.NANOSECONDS);
                if (duration > CHANNEL_CONFIG_WAIT_TIME) {
                    logger.warn(format("Channel %s did not get updated last config after %d ms, Config wait time: %d ms. startLastConfigIndex: %d, currentLastConfigIndex: %d ", name, duration, CHANNEL_CONFIG_WAIT_TIME, startLastConfigIndex, currentLastConfigIndex));
                    // waited long enough ..
                    // just bail don't throw exception.
                    currentLastConfigIndex = startLastConfigIndex - 1L;
                } else {
                    try {
                        // try again sleep
                        Thread.sleep(ORDERER_RETRY_WAIT_TIME);
                    } catch (InterruptedException e) {
                        TransactionException te = new TransactionException("update channel thread Sleep", e);
                        logger.warn(te.getMessage(), te);
                    }
                }
            }
            logger.trace(format("currentLastConfigIndex: %d", currentLastConfigIndex));
        } while (currentLastConfigIndex == startLastConfigIndex);
    } 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 : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) 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 4 with TransactionException

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

the class Channel method getBlockByNumber.

private Block getBlockByNumber(final long number) throws TransactionException {
    logger.trace(format("getConfigurationBlock for channel %s", name));
    try {
        logger.trace(format("Last config index is %d", number));
        SeekSpecified seekSpecified = SeekSpecified.newBuilder().setNumber(number).build();
        SeekPosition seekPosition = SeekPosition.newBuilder().setSpecified(seekSpecified).build();
        SeekInfo seekInfo = SeekInfo.newBuilder().setStart(seekPosition).setStop(seekPosition).setBehavior(SeekInfo.SeekBehavior.BLOCK_UNTIL_READY).build();
        ArrayList<DeliverResponse> deliverResponses = new ArrayList<>();
        seekBlock(seekInfo, deliverResponses, getRandomOrderer());
        DeliverResponse blockresp = deliverResponses.get(1);
        Block retBlock = blockresp.getBlock();
        if (retBlock == null) {
            throw new TransactionException(format("newest block for channel %s fetch bad deliver returned null:", name));
        }
        int dataCount = retBlock.getData().getDataCount();
        if (dataCount < 1) {
            throw new TransactionException(format("Bad config block data count %d", dataCount));
        }
        logger.trace(format("Received  block for channel %s, block no:%d, transaction count: %d", name, retBlock.getHeader().getNumber(), retBlock.getData().getDataCount()));
        return retBlock;
    } catch (TransactionException e) {
        logger.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TransactionException(e);
    }
}
Also used : SeekInfo(org.hyperledger.fabric.protos.orderer.Ab.SeekInfo) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) SeekPosition(org.hyperledger.fabric.protos.orderer.Ab.SeekPosition) SeekSpecified(org.hyperledger.fabric.protos.orderer.Ab.SeekSpecified) ArrayList(java.util.ArrayList) Block(org.hyperledger.fabric.protos.common.Common.Block) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse) 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 TransactionException

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

Aggregations

TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)15 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)11 StatusRuntimeException (io.grpc.StatusRuntimeException)10 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)10 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 IOException (java.io.IOException)9 ExecutionException (java.util.concurrent.ExecutionException)9 TimeoutException (java.util.concurrent.TimeoutException)9 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)9 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)9 Block (org.hyperledger.fabric.protos.common.Common.Block)6 ArrayList (java.util.ArrayList)5 Envelope (org.hyperledger.fabric.protos.common.Common.Envelope)5 DeliverResponse (org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)5 ProtoUtils.createSeekInfoEnvelope (org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope)5 ConfigEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope)4 ConfigUpdateEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope)4 ByteString (com.google.protobuf.ByteString)3 ManagedChannel (io.grpc.ManagedChannel)3