use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Channel method sendUpgradeProposal.
/**
* Send Upgrade proposal proposal to upgrade chaincode to a new version.
*
* @param upgradeProposalRequest
* @param peers the specific peers to send to.
* @return Collection of proposal responses.
* @throws ProposalException
* @throws InvalidArgumentException
*/
public Collection<ProposalResponse> sendUpgradeProposal(UpgradeProposalRequest upgradeProposalRequest, Collection<Peer> peers) throws InvalidArgumentException, ProposalException {
checkChannelState();
checkPeers(peers);
if (null == upgradeProposalRequest) {
throw new InvalidArgumentException("Upgradeproposal is null");
}
try {
TransactionContext transactionContext = getTransactionContext(upgradeProposalRequest.getUserContext());
// transactionContext.verify(false); // Install will have no signing cause it's not really targeted to a channel.
transactionContext.setProposalWaitTime(upgradeProposalRequest.getProposalWaitTime());
UpgradeProposalBuilder upgradeProposalBuilder = UpgradeProposalBuilder.newBuilder();
upgradeProposalBuilder.context(transactionContext);
upgradeProposalBuilder.argss(upgradeProposalRequest.getArgs());
upgradeProposalBuilder.chaincodeName(upgradeProposalRequest.getChaincodeName());
upgradeProposalBuilder.chaincodePath(upgradeProposalRequest.getChaincodePath());
upgradeProposalBuilder.chaincodeVersion(upgradeProposalRequest.getChaincodeVersion());
upgradeProposalBuilder.chaincodEndorsementPolicy(upgradeProposalRequest.getChaincodeEndorsementPolicy());
SignedProposal signedProposal = getSignedProposal(transactionContext, upgradeProposalBuilder.build());
return sendProposalToPeers(peers, signedProposal, transactionContext);
} catch (Exception e) {
throw new ProposalException(e);
}
}
use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Channel method getConfigBlock.
private Block getConfigBlock(List<Peer> peers) throws ProposalException {
if (shutdown) {
throw new ProposalException(format("Channel %s has been shutdown.", name));
}
if (peers.isEmpty()) {
throw new ProposalException("No peers go get config block");
}
TransactionContext transactionContext = null;
SignedProposal signedProposal = null;
try {
transactionContext = getTransactionContext();
// can't verify till we get the config block.
transactionContext.verify(false);
FabricProposal.Proposal proposal = GetConfigBlockBuilder.newBuilder().context(transactionContext).channelId(name).build();
logger.debug("Getting signed proposal.");
signedProposal = getSignedProposal(transactionContext, proposal);
logger.debug("Got signed proposal.");
} catch (Exception e) {
throw new ProposalException(e);
}
ProposalException lastException = new ProposalException(format("getConfigBlock for channel %s failed.", name));
for (Peer peer : peers) {
try {
Collection<ProposalResponse> resp = sendProposalToPeers(new ArrayList<>(Collections.singletonList(peer)), signedProposal, transactionContext);
if (!resp.isEmpty()) {
ProposalResponse pro = resp.iterator().next();
if (pro.getStatus() == ProposalResponse.Status.SUCCESS) {
logger.trace(format("getConfigBlock from peer %s on channel %s success", peer.getName(), name));
return Block.parseFrom(pro.getProposalResponse().getResponse().getPayload().toByteArray());
} else {
lastException = new ProposalException(format("getConfigBlock for channel %s failed with peer %s. Status %s, details: %s", name, peer.getName(), pro.getStatus().toString(), pro.getMessage()));
logger.warn(lastException.getMessage());
}
} else {
logger.warn(format("Got empty proposals from %s", peer));
}
} catch (Exception e) {
lastException = new ProposalException(format("getConfigBlock for channel %s failed with peer %s.", name, peer.getName()), e);
logger.warn(lastException.getMessage());
}
}
throw lastException;
}
use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Channel method sendInstantiationProposal.
/**
* Send instantiate request to the channel. Chaincode is created and initialized.
*
* @param instantiateProposalRequest
* @param peers
* @return responses from peers.
* @throws InvalidArgumentException
* @throws ProposalException
*/
public Collection<ProposalResponse> sendInstantiationProposal(InstantiateProposalRequest instantiateProposalRequest, Collection<Peer> peers) throws InvalidArgumentException, ProposalException {
checkChannelState();
if (null == instantiateProposalRequest) {
throw new InvalidArgumentException("InstantiateProposalRequest is null");
}
instantiateProposalRequest.setSubmitted();
checkPeers(peers);
try {
TransactionContext transactionContext = getTransactionContext(instantiateProposalRequest.getUserContext());
transactionContext.setProposalWaitTime(instantiateProposalRequest.getProposalWaitTime());
InstantiateProposalBuilder instantiateProposalbuilder = InstantiateProposalBuilder.newBuilder();
instantiateProposalbuilder.context(transactionContext);
instantiateProposalbuilder.argss(instantiateProposalRequest.getArgs());
instantiateProposalbuilder.chaincodeName(instantiateProposalRequest.getChaincodeName());
instantiateProposalbuilder.chaincodeType(instantiateProposalRequest.getChaincodeLanguage());
instantiateProposalbuilder.chaincodePath(instantiateProposalRequest.getChaincodePath());
instantiateProposalbuilder.chaincodeVersion(instantiateProposalRequest.getChaincodeVersion());
instantiateProposalbuilder.chaincodEndorsementPolicy(instantiateProposalRequest.getChaincodeEndorsementPolicy());
instantiateProposalbuilder.setTransientMap(instantiateProposalRequest.getTransientMap());
FabricProposal.Proposal instantiateProposal = instantiateProposalbuilder.build();
SignedProposal signedProposal = getSignedProposal(transactionContext, instantiateProposal);
return sendProposalToPeers(peers, signedProposal, transactionContext);
} catch (Exception e) {
throw new ProposalException(e);
}
}
use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class Peer method reconnectPeerEventServiceClient.
void reconnectPeerEventServiceClient(final PeerEventServiceClient failedPeerEventServiceClient, final Throwable throwable) {
if (shutdown) {
logger.debug("Not reconnecting PeerEventServiceClient shutdown ");
return;
}
PeerEventingServiceDisconnected ldisconnectedHandler = disconnectedHandler;
if (null == ldisconnectedHandler) {
// just wont reconnect.
return;
}
TransactionContext ltransactionContext = transactionContext;
if (ltransactionContext == null) {
logger.warn("Not reconnecting PeerEventServiceClient no transaction available ");
return;
}
final TransactionContext fltransactionContext = ltransactionContext.retryTransactionSameContext();
final ExecutorService executorService = getExecutorService();
final PeerOptions peerOptions = null != failedPeerEventServiceClient.getPeerOptions() ? failedPeerEventServiceClient.getPeerOptions() : PeerOptions.createPeerOptions();
if (executorService != null && !executorService.isShutdown() && !executorService.isTerminated()) {
executorService.execute(() -> ldisconnectedHandler.disconnected(new PeerEventingServiceDisconnectEvent() {
@Override
public BlockEvent getLatestBLockReceived() {
return lastBlockEvent;
}
@Override
public long getLastConnectTime() {
return lastConnectTime;
}
@Override
public long getReconnectCount() {
return reconnectCount;
}
@Override
public Throwable getExceptionThrown() {
return throwable;
}
@Override
public void reconnect(Long startBLockNumber) throws TransactionException {
logger.trace("reconnecting startBLockNumber" + startBLockNumber);
++reconnectCount;
if (startBLockNumber == null) {
peerOptions.startEventsNewest();
} else {
peerOptions.startEvents(startBLockNumber);
}
PeerEventServiceClient lpeerEventingClient = new PeerEventServiceClient(Peer.this, new Endpoint(url, properties), properties, peerOptions);
lpeerEventingClient.connect(fltransactionContext);
peerEventingClient = lpeerEventingClient;
}
}));
}
}
use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.
the class ChannelTest method testProposalBuilderWithOutMetaInf.
@Test
public void testProposalBuilderWithOutMetaInf() throws Exception {
InstallProposalBuilder installProposalBuilder = InstallProposalBuilder.newBuilder();
installProposalBuilder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
installProposalBuilder.chaincodePath("github.com/example_cc");
installProposalBuilder.setChaincodeSource(new File(SAMPLE_GO_CC));
installProposalBuilder.chaincodeName("example_cc.go");
installProposalBuilder.chaincodeVersion("1");
Channel channel = hfclient.newChannel("testProposalBuilderWithOutMetaInf");
TransactionContext transactionContext = new TransactionContext(channel, getMockUser("rick", "rickORG"), CryptoSuite.Factory.getCryptoSuite());
installProposalBuilder.context(transactionContext);
// Build it get the proposal. Then unpack it to see if it's what we expect.
FabricProposal.Proposal proposal = installProposalBuilder.build();
FabricProposal.ChaincodeProposalPayload chaincodeProposalPayload = FabricProposal.ChaincodeProposalPayload.parseFrom(proposal.getPayload());
Chaincode.ChaincodeInvocationSpec chaincodeInvocationSpec = Chaincode.ChaincodeInvocationSpec.parseFrom(chaincodeProposalPayload.getInput());
Chaincode.ChaincodeSpec chaincodeSpec = chaincodeInvocationSpec.getChaincodeSpec();
Chaincode.ChaincodeInput input = chaincodeSpec.getInput();
Chaincode.ChaincodeDeploymentSpec chaincodeDeploymentSpec = Chaincode.ChaincodeDeploymentSpec.parseFrom(input.getArgs(1));
ByteString codePackage = chaincodeDeploymentSpec.getCodePackage();
ArrayList tarBytesToEntryArrayList = tarBytesToEntryArrayList(codePackage.toByteArray());
ArrayList<String> expect = new ArrayList(Arrays.asList(new String[] { "src/github.com/example_cc/example_cc.go" }));
assertArrayListEquals("Tar in Install Proposal's codePackage does not have expected entries. ", expect, tarBytesToEntryArrayList);
}
Aggregations