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