这里介绍Chaincode的查询功能, 从前面的介绍得知, Fabric SDK提供了多种查询功能, 具体包括Channel, Block, Transaction, Chaincode 等信息, 而这个功能, 都是通过Channel对象实现的。
因此, 首先创建channel对象。
// setup the fabric networkvar 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"); }
关于queryByChaincode方法的具体文档见:。其他关于Channel的文档见: 。
此外, 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的状态。