org.hyperledger.fabric.sdk.InstantiateProposalRequest Java Examples

The following examples show how to use org.hyperledger.fabric.sdk.InstantiateProposalRequest. 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: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
public static Collection<ProposalResponse> instantiateProposal(HFClient client, Channel channel, ChaincodeID chaincodeID,
                                                               TransactionRequest.Type chaincodeLang, Long proposalTimeout) throws InvalidArgumentException, ProposalException {
    InstantiateProposalRequest instantiateProposalRequest = client.newInstantiationProposalRequest();
    instantiateProposalRequest.setProposalWaitTime(proposalTimeout);//time in milliseconds
    instantiateProposalRequest.setChaincodeID(chaincodeID);
    instantiateProposalRequest.setChaincodeLanguage(chaincodeLang);
    instantiateProposalRequest.setFcn("init");
    instantiateProposalRequest.setArgs(new String[]{});

    // I do not know the purpose of transient map works for.
    Map<String, byte[]> transientMap = new HashMap<>();
    transientMap.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8));
    transientMap.put("method", "InstantiateProposalRequest".getBytes(UTF_8));
    instantiateProposalRequest.setTransientMap(transientMap);
    return channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers());
}
 
Example #2
Source File: QueryBlock.java    From fabric-jdbc-connector with Apache License 2.0 6 votes vote down vote up
private InstantiateProposalRequest getInstantiateProposalRequest(String chaincodeName, String chainCodeVersion,
        String chainCodePath, String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers) throws InvalidArgumentException {
    client.setUserContext(userOrg.getPeerAdmin());
    ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(chaincodeName).setVersion(chainCodeVersion)
            .setPath(chainCodePath).build();
    InstantiateProposalRequest instantiateProposalRequest = client.newInstantiationProposalRequest();
    instantiateProposalRequest.setProposalWaitTime(conf.getProposalWaitTime());
    instantiateProposalRequest.setChaincodeID(chaincodeID);
    instantiateProposalRequest.setFcn(chaincodeFunction);
    instantiateProposalRequest.setArgs(chaincodeArgs);
    if(endorsers != null) {
        ChaincodeEndorsementPolicy policy = getChaincodeEndorsementPolicy(endorsers);
        instantiateProposalRequest.setChaincodeEndorsementPolicy(policy);
    }
    return instantiateProposalRequest;
}
 
Example #3
Source File: QueryBlockTest.java    From fabric-jdbc-connector with Apache License 2.0 5 votes vote down vote up
@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);// .thenReturn(mockCompletableFutureTEvent);

    try {
        qb.instantiateChaincode(chaincodeName,version,goPath,"testFunction",new String[] {"a","b","5","10"}, null);
    }
    catch(BlkchnException blkEx){
        //Do Nothing for Java Concurrent Error
        if(!blkEx.getMessage().contains("java.util.concurrent.TimeoutException")) {

            assert(false);
        }
    }

    assert(true);
}
 
Example #4
Source File: QueryBlockTest.java    From fabric-jdbc-connector with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testInvokeChaincode() throws ClassNotFoundException, SQLException, InvalidArgumentException, ProposalException{

    PowerMockito.mockStatic(HFClient.class);
    when(HFClient.createNewInstance()).thenReturn(mockClient);

    Channel mockChannel = mock(Channel.class);
    when(mockClient.newChannel(anyString())).thenReturn(mockChannel);
    when(mockClient.newPeer(anyString(), anyString(), any())).thenCallRealMethod();


    InstantiateProposalRequest mockInstantiateProposalRequest = mock(InstantiateProposalRequest.class);
    when(mockClient.newInstantiationProposalRequest()).thenReturn(mockInstantiateProposalRequest);

    TransactionProposalRequest mockTransactionProposalRequest = mock(TransactionProposalRequest.class);
    when(mockClient.newTransactionProposalRequest()).thenReturn(mockTransactionProposalRequest);

    Collection<ProposalResponse> mockProposalResponsesList = new ArrayList<ProposalResponse>();
    ProposalResponse mockProposalResponses = mock(ProposalResponse.class);
    when(mockProposalResponses.getStatus()).thenReturn(ProposalResponse.Status.SUCCESS);
    Peer mkpeer = mock(Peer.class);
    when(mockProposalResponses.getPeer()).thenReturn(mkpeer);
    mockProposalResponsesList.add(mockProposalResponses);
    mockProposalResponsesList.add(mockProposalResponses);

    when(mockChannel.sendTransactionProposal(any(TransactionProposalRequest.class),anyCollectionOf(Peer.class))).thenReturn(mockProposalResponsesList);


    PowerMockito.mockStatic(SDKUtils.class);

    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";

    when(SDKUtils.getProposalConsistencySets(anyCollection())).thenReturn(new ArrayList<>());

    CompletableFuture<BlockEvent.TransactionEvent> mockCompletableFutureTEvent = new CompletableFuture<BlockEvent.TransactionEvent>();//{mockTranEvent};
    when(mockChannel.sendTransaction(any(ArrayList.class))).thenReturn(mockCompletableFutureTEvent);// .thenReturn(mockCompletableFutureTEvent);

    try {
        qb.invokeChaincode(chaincodeName, "testFunction", new String[]{"a", "b", "5", "10"});

    }catch(BlkchnException blkEx){
        //Do Nothing for Java concurrent Error
        if(!(blkEx.getMessage().contains("java.util.concurrent.TimeoutException"))) {
            assert(false);
        }
    }
    assert(true);

}
 
Example #5
Source File: QueryBlockTest.java    From fabric-jdbc-connector with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testInvokeNoPeerInfo() throws ClassNotFoundException, SQLException, InvalidArgumentException, ProposalException {
    PowerMockito.mockStatic(HFClient.class);
    when(HFClient.createNewInstance()).thenReturn(mockClient);

    Channel mockChannel = mock(Channel.class);
    when(mockClient.newChannel(anyString())).thenReturn(mockChannel);
    when(mockClient.newPeer(anyString(), anyString(), any())).thenCallRealMethod();


    InstantiateProposalRequest mockInstantiateProposalRequest = mock(InstantiateProposalRequest.class);
    when(mockClient.newInstantiationProposalRequest()).thenReturn(mockInstantiateProposalRequest);

    TransactionProposalRequest mockTransactionProposalRequest = mock(TransactionProposalRequest.class);
    when(mockClient.newTransactionProposalRequest()).thenReturn(mockTransactionProposalRequest);

    Collection<ProposalResponse> mockProposalResponsesList = new ArrayList<ProposalResponse>();
    ProposalResponse mockProposalResponses = mock(ProposalResponse.class);
    when(mockProposalResponses.getStatus()).thenReturn(ProposalResponse.Status.SUCCESS);
    Peer mkpeer = mock(Peer.class);
    when(mockProposalResponses.getPeer()).thenReturn(mkpeer);
    mockProposalResponsesList.add(mockProposalResponses);
    mockProposalResponsesList.add(mockProposalResponses);

    when(mockChannel.sendTransactionProposal(any(TransactionProposalRequest.class),anyCollectionOf(Peer.class))).thenReturn(mockProposalResponsesList);


    PowerMockito.mockStatic(SDKUtils.class);

    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 ="chncodefuncNoPeerInfo";

    when(SDKUtils.getProposalConsistencySets(anyCollection())).thenReturn(new ArrayList<>());

    CompletableFuture<BlockEvent.TransactionEvent> mockCompletableFutureTEvent = new CompletableFuture<BlockEvent.TransactionEvent>();//{mockTranEvent};
    when(mockChannel.sendTransaction(any(ArrayList.class))).thenReturn(mockCompletableFutureTEvent);// .thenReturn(mockCompletableFutureTEvent);

    DataFrame df = qb.invokeChaincode(chaincodeName, "testFunction", new String[]{"a", "b", "5", "10"});
    assertEquals(df.getData().size(), 1);
    List<Object> row = df.getData().get(0);
    assertEquals(false, Boolean.parseBoolean(row.get(1).toString()));
    assertEquals("Endorsing peer information not provided for chaincode chncodefuncNoPeerInfo", row.get(3).toString());
}
 
Example #6
Source File: ChannelClient.java    From blockchain-application-using-fabric-java-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * Instantiate chaincode.
 * 
 * @param chaincodeName
 * @param version
 * @param chaincodePath
 * @param language
 * @param functionName
 * @param functionArgs
 * @param policyPath
 * @return
 * @throws InvalidArgumentException
 * @throws ProposalException
 * @throws ChaincodeEndorsementPolicyParseException
 * @throws IOException
 */
public Collection<ProposalResponse> instantiateChainCode(String chaincodeName, String version, String chaincodePath,
		String language, String functionName, String[] functionArgs, String policyPath)
		throws InvalidArgumentException, ProposalException, ChaincodeEndorsementPolicyParseException, IOException {
	Logger.getLogger(ChannelClient.class.getName()).log(Level.INFO,
			"Instantiate proposal request " + chaincodeName + " on channel " + channel.getName()
					+ " with Fabric client " + fabClient.getInstance().getUserContext().getMspId() + " "
					+ fabClient.getInstance().getUserContext().getName());
	InstantiateProposalRequest instantiateProposalRequest = fabClient.getInstance()
			.newInstantiationProposalRequest();
	instantiateProposalRequest.setProposalWaitTime(180000);
	ChaincodeID.Builder chaincodeIDBuilder = ChaincodeID.newBuilder().setName(chaincodeName).setVersion(version)
			.setPath(chaincodePath);
	ChaincodeID ccid = chaincodeIDBuilder.build();
	Logger.getLogger(ChannelClient.class.getName()).log(Level.INFO,
			"Instantiating Chaincode ID " + chaincodeName + " on channel " + channel.getName());
	instantiateProposalRequest.setChaincodeID(ccid);
	if (language.equals(Type.GO_LANG.toString()))
		instantiateProposalRequest.setChaincodeLanguage(Type.GO_LANG);
	else
		instantiateProposalRequest.setChaincodeLanguage(Type.JAVA);

	instantiateProposalRequest.setFcn(functionName);
	instantiateProposalRequest.setArgs(functionArgs);
	Map<String, byte[]> tm = new HashMap<>();
	tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8));
	tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8));
	instantiateProposalRequest.setTransientMap(tm);

	if (policyPath != null) {
		ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
		chaincodeEndorsementPolicy.fromYamlFile(new File(policyPath));
		instantiateProposalRequest.setChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);
	}

	Collection<ProposalResponse> responses = channel.sendInstantiationProposal(instantiateProposalRequest);
	CompletableFuture<TransactionEvent> cf = channel.sendTransaction(responses);
	
	Logger.getLogger(ChannelClient.class.getName()).log(Level.INFO,
			"Chaincode " + chaincodeName + " on channel " + channel.getName() + " instantiation " + cf);
	return responses;
}