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;
}
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);
}
}
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);
}
}
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();
}
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);
}
}
Aggregations