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

Unable to query result from hyperledger fabric v1.0.0 blockchain

我使用 ./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"]}'

调用成功 return 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)。 要么是我没有正确调用,要么是查询错误...我不太确定

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#

你做到了:

return shim.Success(nil)

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

jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" fmt.Printf("Query Response:%s\n", jsonResp)

return shim.Success(Avalbytes)

在 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 错误的情况,这样您就会清楚地知道您执行的操作有误。所以不是 returning

    return shim.Success(nil)

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