Tsu Wei Quan
Tsu Wei Quan

Reputation: 345

Unable to query result from hyperledger fabric v1.0.0 blockchain

I have created a simple fabric blockchain using the ./byfn.sh

After i start the network and docker exe -it cli bash

I managed to install and instantiate my chaincode without any errors. I then invoke my chaincode with this command

> 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","[email protected]","96259561", "SINGAPOREAN",
> "Chinese", "Buddist", "Single","13Aug2017"]}'

Invoke was successful with a return 200.

However when i run a query to get the data back, there was no output.

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

or

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

This is my chaincode (pastebin link). Either i did not invoke properly or query wrongly... i am not too sure

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#

Upvotes: 1

Views: 595

Answers (2)

Artem Barger
Artem Barger

Reputation: 41232

In Hyperledger Fabric v1.0.0 chaincode should confirm following 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
}

NOTE

Query(stub shim.ChaincodeStubInterface) pb.Response

Is not a part of it, therefore to be able to query for states from the ledger you need to explicitly add dispatch to this function inside the Invoke, e.g.:

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)
}

Also to prevent confusion it's advised to handle the case where you get unexpected function name and return an error, so you will get clear indication that you performing something wrong. So instead of returning

    return shim.Success(nil)

do

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

Upvotes: 1

yacovm
yacovm

Reputation: 5140

You did:

return shim.Success(nil)

While what you need to do is to put some data inside:

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

return shim.Success(Avalbytes)

Upvotes: 2

Related Questions