无法从hyperledger fabric v1.0.0区块链查询结果

时间:2017-08-13 13:02:58

标签: hyperledger hyperledger-fabric

我使用./byfn.sh

创建了一个简单的结构区块链

启动网络后docker exe -it cli bash

我设法安装并实例化我的链代码而没有任何错误。 然后我用这个命令调用我的链码

> peer chaincode invoke -o orderer.example.com:7050  --tls
> $CORE_PEER_TLS_ENABLED --cafile
> /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
> -C ethos -n ethos_ccv100 -c '{"Args":["CreatePatientInfo","123456", "22", "175","74","133","37","Eggs","Fever", "Wei Quan", "Tsu",
> "11April1995","tsuweiquan@gmail.com","96259561", "SINGAPOREAN",
> "Chinese", "Buddist", "Single","13Aug2017"]}'

Invurn成功返回200。

但是,当我运行查询以获取数据时,没有输出。

peer chaincode query -C ethos -n ethos_ccv100 -c '{"Args["queryPatientInfo","123456"]}'

peer chaincode query -C ethos -n ethos_ccv100 -c '{"function":"queryPatientInfo","Args":["123456"]}'

这是我的chaincode (pastebin link)。 要么我没有正确调用或错误地查询...我不太确定

my containers

root@6b6ec9e233e3:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C ethos -n ethos_ccv100 -v 0.2 -c '{"Args":["queryPatientInfo","S9511924G"]}' -r 2017-08-13 12:48:32.066 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2017-08-13 12:48:32.066 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2017-08-13 12:48:32.066 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2017-08-13 12:48:32.067 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2017-08-13 12:48:32.067 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A98070A6A08031A0B08A095C1CC0510...74496E666F0A09533935313139323447 2017-08-13 12:48:32.067 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 4920E5394B2048EC5629886A51A0F022BED4803495C9FC08B5AB62A1463B92BD Query Result (Raw): 2017-08-13 12:48:32.073 UTC [main] main -> INFO 007 Exiting..... root@6b6ec9e233e3:/opt/gopath/src/github.com/hyperledger/fabric/peer#

2 个答案:

答案 0 :(得分:2)

你做了:

  

返回shim.Success(无)

虽然您需要做的是将一些数据放入其中:

  

jsonResp:=“{\”Name \“:\”“+ A +”\“,\”Amount \“:\”“+   string(Avalbytes)+“\”}“fmt.Printf(”查询响应:%s \ n“,jsonResp)

     

返回shim.Success(Avalbytes)

答案 1 :(得分:1)

在Hyperledger Fabric v1.0.0中,链代码应确认以下API:

// Chaincode interface must be implemented by all chaincodes. The fabric runs
// the transactions by calling these functions as specified.
type Chaincode interface {
    // Init is called during Instantiate transaction after the chaincode container
    // has been established for the first time, allowing the chaincode to
    // initialize its internal data
    Init(stub ChaincodeStubInterface) pb.Response

    // Invoke is called to update or query the ledger in a proposal transaction.
    // Updated state variables are not committed to the ledger until the
    // transaction is committed.
    Invoke(stub ChaincodeStubInterface) pb.Response
}

注意

Query(stub shim.ChaincodeStubInterface) pb.Response

不是它的一部分,因此,为了能够从分类账中查询状态,您需要在Invoke内明确地将调度添加到此函数,例如:

func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    function, args := stub.GetFunctionAndParameters()
    if function == "CreatePatientInfo" {
        return CreatePatientInfo(stub, args)
    } else if function == "queryPatientInfo" {
        return t.Query(stub)
    }
    return shim.Success(nil)
}

为防止混淆,建议您处理意外功能名称并返回错误的情况,这样您就会清楚地看到您执行了错误操作。所以而不是返回

    return shim.Success(nil)

DO

    return shim.Error(fmt.Errorf("Wrong function name, %s", function))