從Composer構建的當前運行的業務網絡/邏輯,如何使用composer app/javascript函數查詢或從其他商業網絡(chaincode)獲取數據?如何在Hyperledger Composer中查詢或獲取其他鏈式代碼數據?
面料的JIRA給出了同一通道內至少進行查詢時可用 https://jira.hyperledger.org/browse/FAB-1788
從Composer構建的當前運行的業務網絡/邏輯,如何使用composer app/javascript函數查詢或從其他商業網絡(chaincode)獲取數據?如何在Hyperledger Composer中查詢或獲取其他鏈式代碼數據?
面料的JIRA給出了同一通道內至少進行查詢時可用 https://jira.hyperledger.org/browse/FAB-1788
作曲目前沒有任何直接的方式尚未與其他業務網絡進行通信。業務網絡可以將事件發送到外部系統,以便您現在可以創建中間客戶端應用程序來執行某些操作。 有一個問題在 https://github.com/hyperledger/composer/issues/898
跟蹤這個要求我最近建立了一些代碼與監控chaincode事件,這樣可以爲他們創建顯示塊信息的hyperledger作曲家(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的評論,上面引用的實際代碼訪問任何業務網絡,而不僅僅是連接了hyperledger作曲者的網絡。在結構級別,您需要訪問有效的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});
});
});
});
}
這並不回答關於從現有的企業網絡查詢等業務網絡(chaincodes)問題 –
上面提到的實際代碼訪問任何一個企業網絡,而不僅僅是一個到hyperledger作曲家被附上。在結構級別,您需要訪問有效的keyStore,通道名稱,對等請求URL和目標網絡的訂購者URL。 –