Search in sources :

Example 1 with Block

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

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

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

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

the class Channel method getLastConfigIndex.

private long getLastConfigIndex(Orderer orderer) throws TransactionException, InvalidProtocolBufferException {
    Block latestBlock = getLatestBlock(orderer);
    BlockMetadata blockMetadata = latestBlock.getMetadata();
    Metadata metaData = Metadata.parseFrom(blockMetadata.getMetadata(1));
    LastConfig lastConfig = LastConfig.parseFrom(metaData.getValue());
    return lastConfig.getIndex();
}
Also used : LastConfig(org.hyperledger.fabric.protos.common.Common.LastConfig) BlockMetadata(org.hyperledger.fabric.protos.common.Common.BlockMetadata) BlockMetadata(org.hyperledger.fabric.protos.common.Common.BlockMetadata) Metadata(org.hyperledger.fabric.protos.common.Common.Metadata) Block(org.hyperledger.fabric.protos.common.Common.Block)

Example 5 with Block

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

Aggregations

Block (org.hyperledger.fabric.protos.common.Common.Block)7 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 IOException (java.io.IOException)5 ExecutionException (java.util.concurrent.ExecutionException)5 TimeoutException (java.util.concurrent.TimeoutException)5 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)5 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)5 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)5 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)5 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)5 ArrayList (java.util.ArrayList)3 Envelope (org.hyperledger.fabric.protos.common.Common.Envelope)3 Payload (org.hyperledger.fabric.protos.common.Common.Payload)3 ConfigEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope)3 ConfigUpdateEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope)3 DeliverResponse (org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)3 SeekInfo (org.hyperledger.fabric.protos.orderer.Ab.SeekInfo)3 SeekPosition (org.hyperledger.fabric.protos.orderer.Ab.SeekPosition)3