如何在Hyperledger Composer中查询或获取其他链代码数据?

时间:2017-07-04 08:40:40

标签: hyperledger hyperledger-composer

从Composer构建的当前运行的业务网络/逻辑,如何使用composer app / javascript函数查询或从另一个业务网络(链代码)获取数据?

Fabric的JIRA表明至少可以在同一个频道内查询 https://jira.hyperledger.org/browse/FAB-1788

2 个答案:

答案 0 :(得分:0)

目前,Composer还没有任何与其他商业网络进行通信的直接方式。业务网络可以向外部系统发出事件,因此您可以创建中间客户端应用程序以立即执行某些操作。 跟踪此要求存在问题 https://github.com/hyperledger/composer/issues/898

答案 1 :(得分:0)

我最近构建了一些用于hyperledger-composer(v0.13)应用程序的代码,该应用程序监视链代码事件,以便在创建块信息时显示它们。该代码是关于区块链的新Zero To Blockhain教程的一部分。代码位于第5章&中的queryBlockchain.js文件中。以下。用于查找此文件的文件夹结构是:

Chapter 05
  ↳ controller
     ↳restapi
       router.js
        ↳features
           ↳composer
             autoLoad.js
             hlcAdmin.js
             queryBlockChain.js <=== You want this file for blockchain events
             Z2B_Services.js <=== and this file for socket communications with the browser
             Z2B_Utilities.js 
             ↳creds

并且它使用作曲家安装提供的凭据,这些凭据存储在creds文件夹中。

在参考Jonathan的评论时,上面引用的实际代码访问任何业务网络,而不仅仅是连接超级边界编写器的网络。在结构级别,您需要访问有效的keyStore,通道名称,对等请求URL和目标网络的订购者URL。代码引用代码如下:

    var channel = {};
    var client = null;
    var wallet_path = path.join(__dirname, 'creds');
    Promise.resolve().then(() => {
        //
        // As of 9/28/2017 there is a known and unresolved bug in HyperLedger Fabric
        // https://github.com/hyperledger/composer/issues/957
        // this requires that the file location for the wallet for Fabric version 1.0 be in the following location: 
        // {HOME}/.hfc-key-store
        // therefore the wallet location will be ignored and any private keys required to enroll a user in this process 
        // must be located in {HOME}/.hfc-key-store
        // this is currently managed for you in the installation exec by copying the private key for PeerAdmin to this location
        //
        console.log("Create a client and set the wallet location");
        // hfc = require('fabric-client');
        client = new hfc();
        return hfc.newDefaultKeyValueStore({ path: wallet_path })
        .then((wallet) => {
            client.setStateStore(wallet);
            // const config = require('../../../env.json');
            // which contains the following and is set in this
            // example to support the hyperledger fabric dev environment:
            // {
            //     "composer":
            //     {
            //         "connectionProfile": "hlfv1",
            //         "network": "zerotoblockchain-network",
            //         "adminID": "admin",
            //         "adminPW": "adminpw",
            //         "PeerAdmin": "PeerAdmin",
            //         "PeerPW": "randomString",
            //         "NS": "org.acme.Z2BTestNetwork"
            //     },
            //     "fabric":
            //     {
            //         "user": "queryUser",
            //         "eventURL": "grpc://localhost:7053",
            //         "channelName": "composerchannel",
            //         "keyValStore": ".composer-credentials/PeerAdmin",
            //         "wallet_store": "creds",
            //         "peer": "peer0.org1.example.com",
            //         "peerRequestURL": "grpc://localhost:7051",
            //         "peerEventURL": "grpc://localhost:7053",
            //         "ordererURL" : "grpc://localhost:7050",
            //         "caURL": "http://localhost:7054"
            //     }
            // }
            // 
            return client.getUserContext(config.composer.PeerAdmin, true);})
            .then((user) => {
                if (user === null || user === undefined || user.isEnrolled() === false) 
                    {console.error("User not defined, or not enrolled - error");}
                    channel = client.newChannel(config.fabric.channelName);
                    channel.addPeer(client.newPeer(config.fabric.peerRequestURL));
                    channel.addOrderer(client.newOrderer(config.fabric.ordererURL)); 
                 })
                .then(() => {
                    return channel.queryInfo()
                    .then((blockchainInfo) => {
                        if (blockchainInfo) {
                            res.send({"result": "success", "currentHash": blockchainInfo.currentBlockHash.toString("hex"), blockchain: blockchainInfo});
                        } else {
                            console.log('response_payload is null');
                            res.send({"result": "uncertain", "message": 'response_payload is null'});
                        }
                    })
                    .catch((_err) => {
                        console.log("queryInfo failed with _err = ", _err);
                        res.send({"result": "failed", "message": _err.message});
                    });     
                });
        });
}