Search in sources :

Example 1 with Envelope

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

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

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

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

the class Channel method parseConfigBlock.

protected void parseConfigBlock() throws TransactionException {
    Map<String, MSP> lmsps = msps;
    if (lmsps != null && !lmsps.isEmpty()) {
        return;
    }
    try {
        Block parseFrom = getConfigBlock(getShuffledPeers());
        // final Block configBlock = getConfigurationBlock();
        logger.debug(format("Channel %s Got config block getting MSP data and anchorPeers data", name));
        Envelope envelope = Envelope.parseFrom(parseFrom.getData().getData(0));
        Payload payload = Payload.parseFrom(envelope.getPayload());
        ConfigEnvelope configEnvelope = ConfigEnvelope.parseFrom(payload.getData());
        ConfigGroup channelGroup = configEnvelope.getConfig().getChannelGroup();
        Map<String, MSP> newMSPS = traverseConfigGroupsMSP("", channelGroup, new HashMap<>(20));
        msps = Collections.unmodifiableMap(newMSPS);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TransactionException(e);
    }
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ConfigEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope) Block(org.hyperledger.fabric.protos.common.Common.Block) Payload(org.hyperledger.fabric.protos.common.Common.Payload) 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) ConfigGroup(org.hyperledger.fabric.protos.common.Configtx.ConfigGroup) 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 Envelope

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

the class PeerEventServiceClient method peerVent.

// =========================================================
// Peer eventing
void peerVent(TransactionContext transactionContext) throws TransactionException {
    final Envelope envelope;
    try {
        Ab.SeekPosition.Builder start = Ab.SeekPosition.newBuilder();
        if (null != peerOptions.getNewest()) {
            start.setNewest(Ab.SeekNewest.getDefaultInstance());
        } else if (peerOptions.getStartEvents() != null) {
            start.setSpecified(Ab.SeekSpecified.newBuilder().setNumber(peerOptions.getStartEvents()));
        } else {
            start.setNewest(Ab.SeekNewest.getDefaultInstance());
        }
        // properties.
        envelope = createSeekInfoEnvelope(transactionContext, start.build(), Ab.SeekPosition.newBuilder().setSpecified(Ab.SeekSpecified.newBuilder().setNumber(peerOptions.getStopEvents()).build()).build(), SeekInfo.SeekBehavior.BLOCK_UNTIL_READY, clientTLSCertificateDigest);
        connectEnvelope(envelope);
    } catch (CryptoException e) {
        throw new TransactionException(e);
    }
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ProtoUtils.createSeekInfoEnvelope(org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope) Envelope(org.hyperledger.fabric.protos.common.Common.Envelope) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Aggregations

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