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

976 Views Asked by At

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/instantiate/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.

1

There are 1 best solutions below

0
On

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