无法使用Fabric Java SDK根据交易ID获取BlockInfo

时间:2018-12-12 07:27:59

标签: hyperledger-fabric

我正在使用Fabric Java SDK查询在Hyperledger fabric v1.3上运行的链码。 这是我尝试执行的步骤:

  • 创建 ChannelCLient 并使用方法 queryByChainCode(),我得到了ProposalResponse的集合。

  • 在每个ProposalResponse上,我都可以从方法getTransactionID()获取有效负载和transactionID。

  • 之后,使用从上一步获得的事务ID通过方法 queryBlockByTransactionID()查询BlockInfo。 因为我想获得包含我的交易的正确的区块哈希。

但是我收到错误消息500 无法获取txID的块...错误在索引中找不到条目

我的代码如下:

FabricClient fabClient = new FabricClient(adminUserContext);

        ChannelClient channelClient = fabClient.createChannelClient(Config.CHANNEL_NAME);

        Channel channel = channelClient.getChannel();
        Peer peer = fabClient.getInstance().newPeer(Config.ORG1_PEER_0, Config.ORG1_PEER_0_URL);
        EventHub eventHub = fabClient.getInstance().newEventHub("eventhub01", "grpc://localhost:7053");
        Orderer orderer = fabClient.getInstance().newOrderer(Config.ORDERER_NAME, Config.ORDERER_URL);
        channel.addPeer(peer);
        channel.addEventHub(eventHub);
        channel.addOrderer(orderer);
        channel.initialize();

        String transactionId = "";
        Logger.getLogger(QueryChaincode.class.getName()).log(Level.INFO, "Query ");
        Collection<ProposalResponse>  responsesQuery = channelClient.queryByChainCode(chaincode, "query", args);
        for (ProposalResponse pres : responsesQuery) {
            transactionId = pres.getTransactionID();
            String stringResponse = new String(pres.getChaincodeActionResponsePayload());
            Logger.getLogger(QueryChaincode.class.getName()).log(Level.INFO, stringResponse);
            System.out.println("transactionID is:" + transactionId + " chaincode id is: " + pres.getChaincodeID()
            + " peer name is: " + pres.getPeer().getName());
            result += stringResponse;
        }
        BlockInfo blockInfo = channel.queryBlockByTransactionID(transactionId);

有人可以帮助我如何根据交易ID查询冻结吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

BlockchainInfo blockchainInfo = channel.queryBlockchainInfo();
Long countBlock = blockchainInfo.getHeight();
blockInfoModel.setSize(countBlock.intValue());

for (int blockNumber = 0; blockNumber < countBlock; blockNumber++) {
    BlockInfo blockInfo = channel.queryBlockByNumber(blockNumber);

    blockInfoModel.getBlocks().add(new BlockInfoModel(
            String.valueOf(blockInfo.getBlockNumber()),
            blockInfo.getChannelId(),
            Hex.encodeHexString(blockInfo.getDataHash()),
            Hex.encodeHexString(blockInfo.getPreviousHash()),
            blockInfo.getTransactionCount(),
            queryDateTransactions(channel, blockInfo)
    ));

}