Hyperledger Fabric中如何实现Chaincode的查询功能

这篇文章主要介绍了Hyperledger Fabric中如何实现Chaincode的查询功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

Fabric SDK提供了多种查询功能, 具体包括Channel, Block, Transaction, Chaincode 等信息, 而这个功能, 都是通过Channel对象实现的。

因此, 首先创建channel对象。

// setup the fabric network
var channel = fabric_client.newChannel('mychannel');
var peer = fabric_client.newPeer('grpc://localhost:7051');
channel.addPeer(peer);

然后调用channel的方法实现相应的查询及其其他功能。例如使用queryByChaincode来查询Blockchain中存储的数据。

        // queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],
        // queryAllCars chaincode function - requires no arguments , ex: args: [''],
        const request = {
                //targets : --- letting this default to the peers assigned to the channel
                chaincodeId: 'fabcar',
                fcn: 'queryAllCars',
                args: ['']
        };

        // send the query proposal to the peer
        return channel.queryByChaincode(request);
}).then((query_responses) => {
        console.log("Query has completed, checking results");
        // query_responses could have more than one  results if there multiple peers were used as targets
        if (query_responses && query_responses.length == 1) {
                if (query_responses[0] instanceof Error) {
                        console.error("error from query = ", query_responses[0]);
                } else {
                        console.log("Response is ", query_responses[0].toString());
                }
        } else {
                console.log("No payloads were returned from query");
        }

此外, Channel 还提供了几个比较常用的用于查询的方法:

queryBlock(blockNumber, target, useAdmin, skipDecode)

queryBlockByHash(block, target, useAdmin, skipDecode)

queryBlockByTxID(tx_id, target, useAdmin, skipDecode)

queryTransaction(tx_id, target, useAdmin, skipDecode)

queryInstantiatedChaincodes(target, useAdmin)

使用以上几个方法, 可以帮助查看Blockchain的状态。

感谢你能够认真阅读完这篇文章,希望小编分享的“Hyperledger Fabric中如何实现Chaincode的查询功能”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注行业资讯频道,更多相关知识等着你来学习!