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

关于queryByChaincode方法的具体文档见:Hyperledger Fabric SDK for node.js Class: Channel。其他关于Channel的文档见: Hyperledger Fabric SDK for node.js Class: 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的状态。

转载于:https://my.oschina.net/kingwjb/blog/1861496

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值