@SuppressWarnings("unchecked") @Test public void testInstantiateChaincode() throws ClassNotFoundException, SQLException, InvalidArgumentException{ PowerMockito.mockStatic(HFClient.class); when(HFClient.createNewInstance()).thenReturn(mockClient); Channel mockChannel = mock(Channel.class); when(mockClient.newChannel(anyString())).thenReturn(mockChannel); InstantiateProposalRequest mockInstantiateProposalRequest = mock(InstantiateProposalRequest.class); when(mockClient.newInstantiationProposalRequest()).thenReturn(mockInstantiateProposalRequest); String configPath = "src/test/resources/blockchain-query"; Class.forName("com.impetus.fabric.jdbc.FabricDriver"); QueryBlock qb = new QueryBlock(configPath,"mychannel", null, null); qb.setChannel(); String chaincodeName ="chncodefunc"; String version = "1.0"; String goPath = "/home/impetus/IdeaProjects/fabric-jdbc-driver/src/test/resources/blockchain-query/"; CompletableFuture<BlockEvent.TransactionEvent> mockCompletableFutureTEvent = new CompletableFuture<BlockEvent.TransactionEvent>(); when(mockChannel.sendTransaction(any(ArrayList.class),anyCollection())).thenReturn(mockCompletableFutureTEvent); try { qb.instantiateChaincode(chaincodeName,version,goPath,"testFunction",new String[] {"a","b","5","10"}, null); } catch(BlkchnException blkEx){ if(!blkEx.getMessage().contains("java.util.concurrent.TimeoutException")) { assert(false); } } assert(true); }
public String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath, String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); try { checkConfig(); Collection<Orderer> orderers = channel.getOrderers(); InstantiateProposalRequest instantiateProposalRequest = getInstantiateProposalRequest(chaincodeName, chainCodeVersion, chainCodePath, chaincodeFunction, chaincodeArgs, endorsers); Map<String, byte[]> tm = new HashMap<>(); tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8)); tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8)); instantiateProposalRequest.setTransientMap(tm); logger.info( "Sending instantiateProposalRequest to all peers with arguments:" + Arrays.asList(chaincodeArgs)); responses = channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers()); for (ProposalResponse response : responses) { if (response.isVerified() && response.getStatus() == ProposalResponse.Status.SUCCESS) { successful.add(response); logger.info(String.format("Succesful instantiate proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); } else { failed.add(response); } } logger.info(String.format("Received %d instantiate proposal responses. Successful+verified: %d . Failed: %d", responses.size(), successful.size(), failed.size())); if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Chaincode instantiation failed , reason " + "Not enough endorsers for instantiate :" + successful.size() + "endorser failed with " + first.getMessage() + ". Was verified:" + first.isVerified(); throw new BlkchnException(errMsg); } logger.info("Sending instantiateTransaction to orderer"); logger.info("orderers", orderers); channel.sendTransaction(successful, orderers) .thenApply( transactionEvent -> { logger.info("transaction event is valid", transactionEvent.isValid()); logger.info(String.format("Finished instantiate transaction with transaction id %s", transactionEvent.getTransactionID())); return null; }).exceptionally(e -> { if (e instanceof TransactionEventException) { BlockEvent.TransactionEvent te = ((TransactionEventException) e).getTransactionEvent(); if (te != null) { logger.info(String.format("Transaction with txid %s failed. %s", te.getTransactionID(), e)); } } String errMsg = String.format(" failed with %s exception %s", e.getClass().getName(), e); logger.info(errMsg); throw new BlkchnException(errMsg); }).get(conf.getTransactionWaitTime(), TimeUnit.MILLISECONDS); return "Chaincode instantiated Successfully"; } catch (Exception e) { String errMsg = "QueryBlock | instantiateChaincode |" + e; logger.error(errMsg); throw new BlkchnException(errMsg, e); } }
QueryBlock { public String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath, String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); try { checkConfig(); Collection<Orderer> orderers = channel.getOrderers(); InstantiateProposalRequest instantiateProposalRequest = getInstantiateProposalRequest(chaincodeName, chainCodeVersion, chainCodePath, chaincodeFunction, chaincodeArgs, endorsers); Map<String, byte[]> tm = new HashMap<>(); tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8)); tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8)); instantiateProposalRequest.setTransientMap(tm); logger.info( "Sending instantiateProposalRequest to all peers with arguments:" + Arrays.asList(chaincodeArgs)); responses = channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers()); for (ProposalResponse response : responses) { if (response.isVerified() && response.getStatus() == ProposalResponse.Status.SUCCESS) { successful.add(response); logger.info(String.format("Succesful instantiate proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); } else { failed.add(response); } } logger.info(String.format("Received %d instantiate proposal responses. Successful+verified: %d . Failed: %d", responses.size(), successful.size(), failed.size())); if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Chaincode instantiation failed , reason " + "Not enough endorsers for instantiate :" + successful.size() + "endorser failed with " + first.getMessage() + ". Was verified:" + first.isVerified(); throw new BlkchnException(errMsg); } logger.info("Sending instantiateTransaction to orderer"); logger.info("orderers", orderers); channel.sendTransaction(successful, orderers) .thenApply( transactionEvent -> { logger.info("transaction event is valid", transactionEvent.isValid()); logger.info(String.format("Finished instantiate transaction with transaction id %s", transactionEvent.getTransactionID())); return null; }).exceptionally(e -> { if (e instanceof TransactionEventException) { BlockEvent.TransactionEvent te = ((TransactionEventException) e).getTransactionEvent(); if (te != null) { logger.info(String.format("Transaction with txid %s failed. %s", te.getTransactionID(), e)); } } String errMsg = String.format(" failed with %s exception %s", e.getClass().getName(), e); logger.info(errMsg); throw new BlkchnException(errMsg); }).get(conf.getTransactionWaitTime(), TimeUnit.MILLISECONDS); return "Chaincode instantiated Successfully"; } catch (Exception e) { String errMsg = "QueryBlock | instantiateChaincode |" + e; logger.error(errMsg); throw new BlkchnException(errMsg, e); } } }
QueryBlock { public String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath, String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); try { checkConfig(); Collection<Orderer> orderers = channel.getOrderers(); InstantiateProposalRequest instantiateProposalRequest = getInstantiateProposalRequest(chaincodeName, chainCodeVersion, chainCodePath, chaincodeFunction, chaincodeArgs, endorsers); Map<String, byte[]> tm = new HashMap<>(); tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8)); tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8)); instantiateProposalRequest.setTransientMap(tm); logger.info( "Sending instantiateProposalRequest to all peers with arguments:" + Arrays.asList(chaincodeArgs)); responses = channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers()); for (ProposalResponse response : responses) { if (response.isVerified() && response.getStatus() == ProposalResponse.Status.SUCCESS) { successful.add(response); logger.info(String.format("Succesful instantiate proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); } else { failed.add(response); } } logger.info(String.format("Received %d instantiate proposal responses. Successful+verified: %d . Failed: %d", responses.size(), successful.size(), failed.size())); if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Chaincode instantiation failed , reason " + "Not enough endorsers for instantiate :" + successful.size() + "endorser failed with " + first.getMessage() + ". Was verified:" + first.isVerified(); throw new BlkchnException(errMsg); } logger.info("Sending instantiateTransaction to orderer"); logger.info("orderers", orderers); channel.sendTransaction(successful, orderers) .thenApply( transactionEvent -> { logger.info("transaction event is valid", transactionEvent.isValid()); logger.info(String.format("Finished instantiate transaction with transaction id %s", transactionEvent.getTransactionID())); return null; }).exceptionally(e -> { if (e instanceof TransactionEventException) { BlockEvent.TransactionEvent te = ((TransactionEventException) e).getTransactionEvent(); if (te != null) { logger.info(String.format("Transaction with txid %s failed. %s", te.getTransactionID(), e)); } } String errMsg = String.format(" failed with %s exception %s", e.getClass().getName(), e); logger.info(errMsg); throw new BlkchnException(errMsg); }).get(conf.getTransactionWaitTime(), TimeUnit.MILLISECONDS); return "Chaincode instantiated Successfully"; } catch (Exception e) { String errMsg = "QueryBlock | instantiateChaincode |" + e; logger.error(errMsg); throw new BlkchnException(errMsg, e); } } QueryBlock(String configPath, String channel, String username, String secret); }
QueryBlock { public String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath, String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); try { checkConfig(); Collection<Orderer> orderers = channel.getOrderers(); InstantiateProposalRequest instantiateProposalRequest = getInstantiateProposalRequest(chaincodeName, chainCodeVersion, chainCodePath, chaincodeFunction, chaincodeArgs, endorsers); Map<String, byte[]> tm = new HashMap<>(); tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8)); tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8)); instantiateProposalRequest.setTransientMap(tm); logger.info( "Sending instantiateProposalRequest to all peers with arguments:" + Arrays.asList(chaincodeArgs)); responses = channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers()); for (ProposalResponse response : responses) { if (response.isVerified() && response.getStatus() == ProposalResponse.Status.SUCCESS) { successful.add(response); logger.info(String.format("Succesful instantiate proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); } else { failed.add(response); } } logger.info(String.format("Received %d instantiate proposal responses. Successful+verified: %d . Failed: %d", responses.size(), successful.size(), failed.size())); if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Chaincode instantiation failed , reason " + "Not enough endorsers for instantiate :" + successful.size() + "endorser failed with " + first.getMessage() + ". Was verified:" + first.isVerified(); throw new BlkchnException(errMsg); } logger.info("Sending instantiateTransaction to orderer"); logger.info("orderers", orderers); channel.sendTransaction(successful, orderers) .thenApply( transactionEvent -> { logger.info("transaction event is valid", transactionEvent.isValid()); logger.info(String.format("Finished instantiate transaction with transaction id %s", transactionEvent.getTransactionID())); return null; }).exceptionally(e -> { if (e instanceof TransactionEventException) { BlockEvent.TransactionEvent te = ((TransactionEventException) e).getTransactionEvent(); if (te != null) { logger.info(String.format("Transaction with txid %s failed. %s", te.getTransactionID(), e)); } } String errMsg = String.format(" failed with %s exception %s", e.getClass().getName(), e); logger.info(errMsg); throw new BlkchnException(errMsg); }).get(conf.getTransactionWaitTime(), TimeUnit.MILLISECONDS); return "Chaincode instantiated Successfully"; } catch (Exception e) { String errMsg = "QueryBlock | instantiateChaincode |" + e; logger.error(errMsg); throw new BlkchnException(errMsg, e); } } QueryBlock(String configPath, String channel, String username, String secret); Config getConf(); String getAdminName(); void setAdminName(String adminName); String getChannelName(); void setChannelName(String channelName); void setChannel(); Channel getChannel(); void checkConfig(); synchronized void registerUser(String uname, String secret, String affiliation); synchronized HyperUser enroll(); Channel reconstructChannel(); Channel reconstructChannel(List<Peer> peers, HFClient client); String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath); String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); DataFrame invokeChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String queryChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String upgradeChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); Long getChannelHeight(); }
QueryBlock { public String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath, String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); try { checkConfig(); Collection<Orderer> orderers = channel.getOrderers(); InstantiateProposalRequest instantiateProposalRequest = getInstantiateProposalRequest(chaincodeName, chainCodeVersion, chainCodePath, chaincodeFunction, chaincodeArgs, endorsers); Map<String, byte[]> tm = new HashMap<>(); tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8)); tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8)); instantiateProposalRequest.setTransientMap(tm); logger.info( "Sending instantiateProposalRequest to all peers with arguments:" + Arrays.asList(chaincodeArgs)); responses = channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers()); for (ProposalResponse response : responses) { if (response.isVerified() && response.getStatus() == ProposalResponse.Status.SUCCESS) { successful.add(response); logger.info(String.format("Succesful instantiate proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); } else { failed.add(response); } } logger.info(String.format("Received %d instantiate proposal responses. Successful+verified: %d . Failed: %d", responses.size(), successful.size(), failed.size())); if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Chaincode instantiation failed , reason " + "Not enough endorsers for instantiate :" + successful.size() + "endorser failed with " + first.getMessage() + ". Was verified:" + first.isVerified(); throw new BlkchnException(errMsg); } logger.info("Sending instantiateTransaction to orderer"); logger.info("orderers", orderers); channel.sendTransaction(successful, orderers) .thenApply( transactionEvent -> { logger.info("transaction event is valid", transactionEvent.isValid()); logger.info(String.format("Finished instantiate transaction with transaction id %s", transactionEvent.getTransactionID())); return null; }).exceptionally(e -> { if (e instanceof TransactionEventException) { BlockEvent.TransactionEvent te = ((TransactionEventException) e).getTransactionEvent(); if (te != null) { logger.info(String.format("Transaction with txid %s failed. %s", te.getTransactionID(), e)); } } String errMsg = String.format(" failed with %s exception %s", e.getClass().getName(), e); logger.info(errMsg); throw new BlkchnException(errMsg); }).get(conf.getTransactionWaitTime(), TimeUnit.MILLISECONDS); return "Chaincode instantiated Successfully"; } catch (Exception e) { String errMsg = "QueryBlock | instantiateChaincode |" + e; logger.error(errMsg); throw new BlkchnException(errMsg, e); } } QueryBlock(String configPath, String channel, String username, String secret); Config getConf(); String getAdminName(); void setAdminName(String adminName); String getChannelName(); void setChannelName(String channelName); void setChannel(); Channel getChannel(); void checkConfig(); synchronized void registerUser(String uname, String secret, String affiliation); synchronized HyperUser enroll(); Channel reconstructChannel(); Channel reconstructChannel(List<Peer> peers, HFClient client); String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath); String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); DataFrame invokeChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String queryChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String upgradeChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); Long getChannelHeight();  }