如何在CLI容器之外查询Hyperledger Fabric区块链数据? [英] How to query Hyperledger Fabric blockchain data outside CLI container?
问题描述
我正在使用Fabric-sample中的./byfn.sh脚本来启动我的区块链. 我想从区块链的PHP网站调用和查询数据.
I am using the ./byfn.sh script from the fabric-sample to start my blockchain. I would like to invoke and query data from a PHP website from the blockchain.
我确实知道我只能使用CLI容器中的API调用或使用JavaSDK/NodeSDK,RESTapi.
I do know that I can only use the API call from CLI container or use a JavaSDK/NodeSDK, RESTapi.
如何以最简单的方式从区块链查询数据? 我正在查看NodeSDK和RestApi,但没有太多指南可以帮助我,因此是这个问题. 很抱歉出现新手问题.
How can I query data from the blockchain the easiest way? I am looking at NodeSDK and RestApi but I don't see much guides out there to help me, hence this question. Sorry for the newbie questions.
谢谢!
推荐答案
调用Hyperledger Fabric对等方并与之交互的正确方法是通过SDK,这是可用的SDK的列表:
The proper way to call and interact with Hyperledger Fabric peers is via SDK, here is the list of available SDK's:
- Java SDK
- GoLang SDK
- NodeJS SDK
- Python SDK
更新
如@ christo4ferris所建议,还有一个附加项目:
As suggested by @christo4ferris there is an additional project:
(您可能对此感兴趣的人)
(Which might be the one you are interested in)
例如,您可以使用golang SDK并执行以下操作:
For example you can use golang SDK and do following:
-
读取配置文件
Read configuration file
var err error
conf, err := config.InitConfig("config.yaml")
if err != nil {
fmt.Println(err)
return
}
var err error
conf, err := config.InitConfig("config.yaml")
if err != nil {
fmt.Println(err)
return
}
初始化结构客户端
cl := fabricclient.NewClient(conf)
bccspFactory.InitFactories(conf.CSPConfig())
cl.SetCryptoSuite(bccspFactory.GetDefault())
cl := fabricclient.NewClient(conf)
bccspFactory.InitFactories(conf.CSPConfig())
cl.SetCryptoSuite(bccspFactory.GetDefault())
加载客户端证书和密钥
privKey := filepath.Join(conf.CryptoConfigPath(), "path to key")
pubKey := filepath.Join(conf.CryptoConfigPath(), "path to cert")
privKey := filepath.Join(conf.CryptoConfigPath(), "path to key")
pubKey := filepath.Join(conf.CryptoConfigPath(), "path to cert")
读取并设置MSP ID
Read and setup MSP ID
mspID, err := conf.MspID("org1")
if err != nil {
fmt.Println(err)
return
}
mspID, err := conf.MspID("org1")
if err != nil {
fmt.Println(err)
return
}
将客户端用户设置为上下文
Setup client user into the context
user, err := fabapi.NewPreEnrolledUser(conf, privKey, pubKey, "user1", mspID, cl.GetCryptoSuite())
if err != nil {
fmt.Println(err)
return
}
cl.SetUserContext(user)
user, err := fabapi.NewPreEnrolledUser(conf, privKey, pubKey, "user1", mspID, cl.GetCryptoSuite())
if err != nil {
fmt.Println(err)
return
}
cl.SetUserContext(user)
设置订购节点
ordererConf, err := conf.OrdererConfig("orderer0")
if err != nil {
fmt.Println(err)
return
}
ordererConf, err := conf.OrdererConfig("orderer0")
if err != nil {
fmt.Println(err)
return
}
o, err := orderer.NewOrderer(fmt.Sprintf("%s:%d", ordererConf.Host, ordererConf.Port),
filepath.Join(conf.CryptoConfigPath(), "path to orderer cert"),
"orderer.example.com", conf)
if err != nil {
fmt.Println(err)
return
}
o, err := orderer.NewOrderer(fmt.Sprintf("%s:%d", ordererConf.Host, ordererConf.Port),
filepath.Join(conf.CryptoConfigPath(), "path to orderer cert"),
"orderer.example.com", conf)
if err != nil {
fmt.Println(err)
return
}
设置和初始化频道以及认可的对等方
Setup and initialize channel and the endorsing peer
peers, err := conf.PeersConfig("org1")
if err != nil {
fmt.Println(err)
return
}
peers, err := conf.PeersConfig("org1")
if err != nil {
fmt.Println(err)
return
}
p, err := peer.NewPeer(fmt.Sprintf("%s:%d", peers[0].Host, peers[0].Port), conf)
if err != nil {
fmt.Println(err)
}
p, err := peer.NewPeer(fmt.Sprintf("%s:%d", peers[0].Host, peers[0].Port), conf)
if err != nil {
fmt.Println(err)
}
ch, err := cl.NewChannel("mychannel")
if err != nil {
fmt.Println(err)
return
}
ch, err := cl.NewChannel("mychannel")
if err != nil {
fmt.Println(err)
return
}
ch.AddOrderer(o)
ch.AddPeer(p)
ch.SetPrimaryPeer(p)
cl.SaveUserToStateStore(user, true)
ch.AddOrderer(o)
ch.AddPeer(p)
ch.SetPrimaryPeer(p)
cl.SaveUserToStateStore(user, true)
准备交易建议请求并发送并提交订购
Prepare transaction proposal request send it and submit for ordering
txRequest := apitxn.ChaincodeInvokeRequest{
Targets: []apitxn.ProposalProcessor{p},
Fcn: "myFCN",
Args: []string{"myargs"},
TransientMap: map[string][]byte{},
ChaincodeID: "helloworld",
}
txRequest := apitxn.ChaincodeInvokeRequest{
Targets: []apitxn.ProposalProcessor{p},
Fcn: "myFCN",
Args: []string{"myargs"},
TransientMap: map[string][]byte{},
ChaincodeID: "helloworld",
}
proposalResponse, _, err := ch.SendTransactionProposal(txRequest)
if err != nil {
fmt.Println(err)
return
}
proposalResponse, _, err := ch.SendTransactionProposal(txRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%v\n", proposalResponse[0].ProposalResponse)
tx, err := ch.CreateTransaction(proposalResponse)
if err != nil {
fmt.Println(err)
return
}
tx, err := ch.CreateTransaction(proposalResponse)
if err != nil {
fmt.Println(err)
return
}
txResponse, err := ch.SendTransaction(tx)
if err != nil {
fmt.Println(err)
return
}
txResponse, err := ch.SendTransaction(tx)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(txResponse[0])
在所有SDK上都可以使用相同的方法.
Pretty same way this will work across all SDK's.
这篇关于如何在CLI容器之外查询Hyperledger Fabric区块链数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!