use of org.hyperledger.fabric.protos.common.Common.ChannelHeader 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.protos.common.Common.ChannelHeader 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.ChannelHeader in project fabric-sdk-java by hyperledger.
the class ProtoUtils method createChannelHeader.
// static CryptoSuite suite = null;
/*
* createChannelHeader create chainHeader
*
* @param type header type. See {@link ChannelHeader.Builder#setType}.
* @param txID transaction ID. See {@link ChannelHeader.Builder#setTxId}.
* @param channelID channel ID. See {@link ChannelHeader.Builder#setChannelId}.
* @param epoch the epoch in which this header was generated. See {@link ChannelHeader.Builder#setEpoch}.
* @param timeStamp local time when the message was created. See {@link ChannelHeader.Builder#setTimestamp}.
* @param chaincodeHeaderExtension extension to attach dependent on the header type. See {@link ChannelHeader.Builder#setExtension}.
* @param tlsCertHash
* @return a new chain header.
*/
public static ChannelHeader createChannelHeader(HeaderType type, String txID, String channelID, long epoch, Timestamp timeStamp, ChaincodeHeaderExtension chaincodeHeaderExtension, byte[] tlsCertHash) {
if (isDebugLevel) {
String tlschs = "";
if (tlsCertHash != null) {
tlschs = DatatypeConverter.printHexBinary(tlsCertHash);
}
logger.debug(format("ChannelHeader: type: %s, version: 1, Txid: %s, channelId: %s, epoch %d, clientTLSCertificate digest: %s", type.name(), txID, channelID, epoch, tlschs));
}
ChannelHeader.Builder ret = ChannelHeader.newBuilder().setType(type.getNumber()).setVersion(1).setTxId(txID).setChannelId(channelID).setTimestamp(timeStamp).setEpoch(epoch);
if (null != chaincodeHeaderExtension) {
ret.setExtension(chaincodeHeaderExtension.toByteString());
}
if (tlsCertHash != null) {
ret.setTlsCertHash(ByteString.copyFrom(tlsCertHash));
}
return ret.build();
}
use of org.hyperledger.fabric.protos.common.Common.ChannelHeader in project fabric-sdk-java by hyperledger.
the class ProtoUtils method createSeekInfoEnvelope.
public static Envelope createSeekInfoEnvelope(TransactionContext transactionContext, SeekInfo seekInfo, byte[] tlsCertHash) throws CryptoException {
ChannelHeader seekInfoHeader = createChannelHeader(Common.HeaderType.DELIVER_SEEK_INFO, transactionContext.getTxID(), transactionContext.getChannelID(), transactionContext.getEpoch(), transactionContext.getFabricTimestamp(), null, tlsCertHash);
SignatureHeader signatureHeader = SignatureHeader.newBuilder().setCreator(transactionContext.getIdentity().toByteString()).setNonce(transactionContext.getNonce()).build();
Common.Header seekHeader = Common.Header.newBuilder().setSignatureHeader(signatureHeader.toByteString()).setChannelHeader(seekInfoHeader.toByteString()).build();
Payload seekPayload = Payload.newBuilder().setHeader(seekHeader).setData(seekInfo.toByteString()).build();
return Envelope.newBuilder().setSignature(transactionContext.signByteString(seekPayload.toByteArray())).setPayload(seekPayload.toByteString()).build();
}
Aggregations