Java Code Examples for org.hyperledger.fabric.sdk.QueryByChaincodeRequest#setFcn()
The following examples show how to use
org.hyperledger.fabric.sdk.QueryByChaincodeRequest#setFcn() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelClient.java From blockchain-application-using-fabric-java-sdk with Apache License 2.0 | 6 votes |
/** * Query by chaincode. * * @param chaincodeName * @param functionName * @param args * @return * @throws InvalidArgumentException * @throws ProposalException */ public Collection<ProposalResponse> queryByChainCode(String chaincodeName, String functionName, String[] args) throws InvalidArgumentException, ProposalException { Logger.getLogger(ChannelClient.class.getName()).log(Level.INFO, "Querying " + functionName + " on channel " + channel.getName()); QueryByChaincodeRequest request = fabClient.getInstance().newQueryProposalRequest(); ChaincodeID ccid = ChaincodeID.newBuilder().setName(chaincodeName).build(); request.setChaincodeID(ccid); request.setFcn(functionName); if (args != null) request.setArgs(args); Collection<ProposalResponse> response = channel.queryByChaincode(request); return response; }
Example 2
Source File: End2endLifecycleIT.java From fabric-sdk-java with Apache License 2.0 | 6 votes |
void executeVerifyByQuery(HFClient client, Channel channel, String chaincodeName, String expect) throws ProposalException, InvalidArgumentException { out("Now query chaincode for the value of b."); QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setArgs("b"); queryByChaincodeRequest.setFcn("query"); queryByChaincodeRequest.setChaincodeName(chaincodeName); Collection<ProposalResponse> queryProposals = channel.queryByChaincode(queryByChaincodeRequest, channel.getPeers()); for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != ProposalResponse.Status.SUCCESS) { fail("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); } else { String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); out("Query payload of b from peer %s returned %s", proposalResponse.getPeer().getName(), payload); assertEquals(expect, payload); } } }
Example 3
Source File: ChaincodeManager.java From fabric-net-server with Apache License 2.0 | 5 votes |
/** * 查询智能合约 * * @param fcn * 方法名 * @param args * 参数数组 * @return * @throws InvalidArgumentException * @throws ProposalException * @throws IOException * @throws TransactionException * @throws CryptoException * @throws InvalidKeySpecException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException */ public Map<String, String> query(String fcn, String[] args) throws InvalidArgumentException, ProposalException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, CryptoException, TransactionException, IOException { Map<String, String> resultMap = new HashMap<>(); String payload = ""; QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setArgs(args); queryByChaincodeRequest.setFcn(fcn); queryByChaincodeRequest.setChaincodeID(chaincodeID); Map<String, byte[]> tm2 = new HashMap<>(); tm2.put("HyperLedgerFabric", "QueryByChaincodeRequest:JavaSDK".getBytes(UTF_8)); tm2.put("method", "QueryByChaincodeRequest".getBytes(UTF_8)); queryByChaincodeRequest.setTransientMap(tm2); Collection<ProposalResponse> queryProposals = channel.queryByChaincode(queryByChaincodeRequest, channel.getPeers()); for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != ProposalResponse.Status.SUCCESS) { log.debug("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); resultMap.put("code", "error"); resultMap.put("data", "Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); } else { payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); log.debug("Query payload from peer: " + proposalResponse.getPeer().getName()); log.debug("TransactionID: " + proposalResponse.getTransactionID()); log.debug("" + payload); resultMap.put("code", "success"); resultMap.put("data", payload); resultMap.put("txid", proposalResponse.getTransactionID()); } } return resultMap; }
Example 4
Source File: End2endAndBackAgainIT.java From fabric_sdk_java_study with Apache License 2.0 | 5 votes |
private void queryChaincodeForExpectedValue(HFClient client, Channel channel, final String expect, ChaincodeID chaincodeID) { out("Now query chaincode on channel %s for the value of b expecting to see: %s", channel.getName(), expect); QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setArgs(new String[] {"query", "b"}); queryByChaincodeRequest.setFcn("invoke"); queryByChaincodeRequest.setChaincodeID(chaincodeID); Collection<ProposalResponse> queryProposals; try { queryProposals = channel.queryByChaincode(queryByChaincodeRequest); } catch (Exception e) { throw new CompletionException(e); } for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != Status.SUCCESS) { fail("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); } else { String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); out("Query payload of b from peer %s returned %s", proposalResponse.getPeer().getName(), payload); assertEquals(payload, expect); } } }
Example 5
Source File: PrivateDataIT.java From fabric-sdk-java with Apache License 2.0 | 5 votes |
private void queryChaincodeForExpectedValue(HFClient client, Channel channel, final String expect, ChaincodeID chaincodeID) { out("Now query chaincode %s on channel %s for the value of b expecting to see: %s", chaincodeID, channel.getName(), expect); Collection<ProposalResponse> queryProposals; try { QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setFcn("query"); queryByChaincodeRequest.setChaincodeID(chaincodeID); Map<String, byte[]> tmap = new HashMap<>(); tmap.put("B", "b".getBytes(UTF_8)); // test using bytes as args. End2end uses Strings. queryByChaincodeRequest.setTransientMap(tmap); queryProposals = channel.queryByChaincode(queryByChaincodeRequest); } catch (Exception e) { throw new CompletionException(e); } for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != Status.SUCCESS) { fail("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); } else { String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); out("Query payload of b from peer %s returned %s", proposalResponse.getPeer().getName(), payload); assertEquals(format("Failed compare on channel %s chaincode id %s expected value:'%s', but got:'%s'", channel.getName(), chaincodeID, expect, payload), expect, payload); } } }
Example 6
Source File: NetworkConfigIT.java From fabric-sdk-java with Apache License 2.0 | 5 votes |
private static String queryChaincodeForCurrentValue(HFClient client, Channel channel, ChaincodeID chaincodeID) { out("Now query chaincode on channel %s for the current value of b", channel.getName()); QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setArgs("b"); queryByChaincodeRequest.setFcn("query"); queryByChaincodeRequest.setChaincodeID(chaincodeID); Collection<ProposalResponse> queryProposals; try { queryProposals = channel.queryByChaincode(queryByChaincodeRequest); } catch (Exception e) { throw new CompletionException(e); } String expect = null; for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != Status.SUCCESS) { fail("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); } else { String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); out("Query payload of b from peer %s returned %s", proposalResponse.getPeer().getName(), payload); if (expect != null) { assertEquals(expect, payload); } else { expect = payload; } } } return expect; }
Example 7
Source File: End2endAndBackAgainIT.java From fabric-sdk-java with Apache License 2.0 | 5 votes |
private void queryChaincodeForExpectedValue(HFClient client, Channel channel, final String expect, ChaincodeID chaincodeID) { out("Now query chaincode %s on channel %s for the value of b expecting to see: %s", chaincodeID, channel.getName(), expect); QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setArgs("b".getBytes(UTF_8)); // test using bytes as args. End2end uses Strings. queryByChaincodeRequest.setFcn("query"); queryByChaincodeRequest.setChaincodeID(chaincodeID); Collection<ProposalResponse> queryProposals; try { queryProposals = channel.queryByChaincode(queryByChaincodeRequest); } catch (Exception e) { throw new CompletionException(e); } for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != Status.SUCCESS) { fail("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); } else { String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); out("Query payload of b from peer %s returned %s", proposalResponse.getPeer().getName(), payload); assertEquals(format("Failed compare on channel %s chaincode id %s expected value:'%s', but got:'%s'", channel.getName(), chaincodeID, expect, payload), expect, payload); } } }
Example 8
Source File: QueryBlock.java From fabric-jdbc-connector with Apache License 2.0 | 4 votes |
public String queryChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs) { try { checkConfig(); ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(chaincodename).build(); logger.info("Channel Name is " + channel.getName()); logger.debug(String.format("Querying chaincode %s and function %s with arguments %s", chaincodename, chaincodeFunction, Arrays.asList(chaincodeArgs).toString())); QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setArgs(chaincodeArgs); queryByChaincodeRequest.setFcn(chaincodeFunction); queryByChaincodeRequest.setChaincodeID(chaincodeID); Map<String, byte[]> tm2 = new HashMap<>(); tm2.put("HyperLedgerFabric", "QueryByChaincodeRequest:JavaSDK".getBytes(UTF_8)); tm2.put("method", "QueryByChaincodeRequest".getBytes(UTF_8)); queryByChaincodeRequest.setTransientMap(tm2); logger.debug("Chaincode request args:- " + queryByChaincodeRequest.getArgs().toString()); Collection<ProposalResponse> queryProposals = channel.queryByChaincode(queryByChaincodeRequest, channel.getPeers()); for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != ProposalResponse.Status.SUCCESS) { String errorMsg = "Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified(); logger.debug(errorMsg); throw new BlkchnException(errorMsg); } else { return proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); } } } catch (Exception e) { logger.error("QueryBlock | queryChaincode | " + e.getMessage()); throw new BlkchnException(e); } throw new BlkchnException("Caught an exception while quering chaincode"); }
Example 9
Source File: ChaincodeServiceImpl.java From balance-transfer-java with Apache License 2.0 | 4 votes |
/** * queries the chaincode takes input chaincode name, function and args * returns payload from blockchain as string */ public String queryChaincode(String name, String chaincodeFunction, String[] chaincodeArgs) { try { checkConfig(); chaincodeID = getChaincodeId(name); Channel channel = reconstructChannel(); logger.debug("Now query chaincode for the value of b."); logger.debug(chaincodeFunction); QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest(); queryByChaincodeRequest.setArgs(chaincodeArgs); queryByChaincodeRequest.setFcn(chaincodeFunction); queryByChaincodeRequest.setChaincodeID(chaincodeID); Map<String, byte[]> tm2 = new HashMap<>(); tm2.put("HyperLedgerFabric", "QueryByChaincodeRequest:JavaSDK".getBytes(UTF_8)); tm2.put("method", "QueryByChaincodeRequest".getBytes(UTF_8)); queryByChaincodeRequest.setTransientMap(tm2); Collection<ProposalResponse> queryProposals = channel.queryByChaincode(queryByChaincodeRequest, channel.getPeers()); for (ProposalResponse proposalResponse : queryProposals) { if (!proposalResponse.isVerified() || proposalResponse.getStatus() != ProposalResponse.Status.SUCCESS) { logger.debug("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified()); } else { String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8(); return payload; } } } catch (Exception e) { logger.error("ChaincodeServiceImpl | queryChaincode | " +e.getMessage()); } return "Caught an exception while quering chaincode"; }