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