in src/main/java/com/lambdajavablockchain/service/ManagedBlockchainService.java [360:410]
public void invokeChaincode(HFClient hfClient, Channel channel, String chainCodeName, String functionName,
String[] arguments) throws ManagedBlockchainServiceException, InvalidArgumentException {
if (channel == null || hfClient == null) {
log.error("Channel/Client not initialized. Run ManagedBlockchainService.initChannel() first");
throw new ManagedBlockchainServiceException("Channel/Client not initialized!");
}
// Set chaincdoe name, function and arguments
ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(chainCodeName).build();
TransactionProposalRequest invokeRequest = hfClient.newTransactionProposalRequest();
invokeRequest.setChaincodeID(chaincodeID);
invokeRequest.setFcn(functionName);
invokeRequest.setArgs(arguments);
invokeRequest.setProposalWaitTime(2000);
Collection<ProposalResponse> successful = new LinkedList<>();
Collection<ProposalResponse> failed = new LinkedList<>();
try {
// Send transaction proposal to all peers
Collection<ProposalResponse> responses = channel.sendTransactionProposal(invokeRequest);
// Process responses from transaction proposal
for (ProposalResponse response : responses) {
String stringResponse = new String(response.getChaincodeActionResponsePayload());
log.info("Invoke status:" + response.getStatus() + " result:" + stringResponse);
if (response.getStatus() == ChaincodeResponse.Status.SUCCESS) {
log.info("Received successful transaction proposal response txId:"
+ response.getTransactionID() + " from peer: " + response.getPeer().getName());
successful.add(response);
} else {
failed.add(response);
log.error("Received unsuccessful transaction proposal response");
}
}
if (failed.size() > 0) {
log.error("Failed to send Proposal and receive successful proposal responses");
throw new RuntimeException("Proposal error");
}
// Send transaction to Orderer
CompletableFuture<BlockEvent.TransactionEvent> cf = channel.sendTransaction(responses);
CompletableFuture<Void> future = cf
.thenAccept((s) -> log.info("Invoke Completed. Block nb:" + s.getBlockEvent().getBlockNumber()));
} catch (ProposalException ex) {
log.error("Proposal exception " + ex.getMessage());
throw new RuntimeException("Proposal exception ", ex);
}
}