Hyperledger Fabric V1 0 Application using Java SDK Client for is waiting indefinitely when invoking chaincode

0 votes

I have my Hyperledger Fabric V1.0 network up and running by following the steps Building Your First Network. And now I am able to create channel, install/instatiate/invoke/query chaincode etc. Now I am trying to create some assets and query the same using Java SDK Client. I have created the following methods to invoke and query the chaincode from my java application.

void createChannel() throws InvalidArgumentException, TransactionException, IOException, ProposalException{
    Properties ordererProperties = getOrdererProperties("orderer.example.com");
    ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[] {5L, TimeUnit.MINUTES});
    ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTimeout", new Object[] {8L, TimeUnit.SECONDS});
    Orderer orderer = client.newOrderer("orderer.example.com", "grpcs://192.168.99.100:7050",ordererProperties);

    Properties peerProperties = getPeerProperties("peer0.org1.example.com"); //test properties for peer.. if any.
    if (peerProperties == null) {
        peerProperties = new Properties();
    }
    peerProperties.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
    Peer peer = client.newPeer("peer0.org1.example.com", "grpcs://192.168.99.100:7051",peerProperties);
    channel = client.newChannel("testchannel");
    channel.addOrderer(orderer);
    channel.addPeer(peer);
    channel.initialize();
}

void creteTransactionalProposal(){
    proposalRequest = client.newTransactionProposalRequest();
    final ChaincodeID chaincodeID = ChaincodeID.newBuilder()
            .setName("asset_test")
            .setVersion("1.0")
            .setPath("github.com/myuser/myfabricrepo/asset_chain")
            .build();

    proposalRequest.setChaincodeID(chaincodeID);
    proposalRequest.setFcn("set");
    proposalRequest.setProposalWaitTime(TimeUnit.SECONDS.toMillis(1));
    proposalRequest.setArgs(new String[]{"a1", "a1_val"});
}

void sendProposal() throws ProposalException, InvalidArgumentException, InterruptedException, ExecutionException{
    final Collection<ProposalResponse> responses = channel.sendTransactionProposal(proposalRequest);
    CompletableFuture<BlockEvent.TransactionEvent> txFuture = channel.sendTransaction(responses, client.getUserContext());
    BlockEvent.TransactionEvent event = txFuture.get();//waiting indefinitely
    System.out.println(event.toString());
    //query();
}

void query() throws InvalidArgumentException, ProposalException{
     final ChaincodeID chaincodeID = ChaincodeID.newBuilder()
                .setName(""asset_test"")
                .setVersion("1.0")
                .setPath("github.com/myuser/myfabricrepo/asset_chain")
                .build();

    QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest();
    queryByChaincodeRequest.setArgs(new String[] {"a1"});
    queryByChaincodeRequest.setFcn("get");
    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) {
            System.out.println("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();
            System.out.printf("\nQuery payload of b from peer %s returned %s", proposalResponse.getPeer().getName(),
                    payload);
            //assertEquals(payload, expect);
        }
    }
}

I am able to create Asset by calling

t.creteTransactionalProposal();
t.sendProposal();

But the line BlockEvent.TransactionEvent event = txFuture.get();makes the application in an indefinite waiting state even after the completion of the transaction commit to ledger. Why it is behaving like this?

Once I to force exit and run the query() method it is listing the asset.

Jul 16, 2018 in Blockchain by sabby
• 4,370 points
1,298 views

1 answer to this question.

0 votes

I ran into a similar issue to this, and many of the answers around the net are missing a key part of the code - assigning the EventHub to the channel. I added this before initializing the channel (which in this case would be in the createChannel mehtod), and my transactions were then processed successfully:

channel.addEventHub(client.newEventHub("eventhub0", "grpc://localhost:7053"));
answered Jul 16, 2018 by Perry
• 17,100 points

Related Questions In Blockchain

+1 vote
2 answers
0 votes
0 answers
+1 vote
2 answers

Where is custom chaincode path Hyperledger Fabric 1.0?

You have to have your chaincode at ...READ MORE

answered Jul 10, 2018 in Blockchain by slayer
• 29,370 points
2,980 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,003 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
2 answers
0 votes
1 answer

Hyperledger Fabric v1.0 setup on multiple machines

You would be able to host hyperledger fabric ...READ MORE

answered Oct 23, 2018 in Blockchain by Perry
• 17,100 points
1,502 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP