Davis Broda
Davis Broda

Reputation: 4135

How to return values in Invoke json response

I am trying to design a hyperledger chaincode, that is accessed through a web API, which passes json objects to the code. However, whenever I do an invoke method, I cannot actually return values to the user in the json response.

For instance, here is some sample code:

func (t *TLCChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
    //Do some stuff
    return []byte("Some string"), nil
}

And some sample code for returning an error

func (t *TLCChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
    //Try to do some stuff
    //Get some sort of error
    return nil, errors.New("someError")
}

however both of these return a message like this, with the message always being some random character string like below (I suspect a hash of some sort):

{
  "jsonrpc": "2.0",
  "result": {
    "status": "OK",
    "message": "1e1123e3-b484-4120-a28e-c3a8db384557"
  },
  "id": 11
}

As you can see, this response contains neither the response I returned (as in the first case), or the error I returned (in the second case). How would I go about getting the returned bytes, or the returned error into the returned json?

Edit: Please note that if I call an invoke method from another chaincode, it receives the correct return values. It's only when it's returned to the user that it fails to work properly.

Upvotes: 0

Views: 978

Answers (2)

Lesterpig
Lesterpig

Reputation: 218

If you need to get a return value as soon as the Invoke is processed (included in a block), your best bet is to use some events (for the moment I guess).

In your chaincode, just setup the event with:

func (stub *ChaincodeStub) SetEvent(name string, payload []byte) error

GoDoc

You may be able to listen for events in your application using the SDK or protobuf messages directly. I'm doing it like this on the developer preview; but it seems that the standard way to retrieve Invoke result is to poll the blockchain via Queries.

There is a related GitHub issue here.

Upvotes: 1

Sergey Balashevich
Sergey Balashevich

Reputation: 2101

“Invoke” is not a synchronous call. Peer generates this OK message immediately when it receives your Web request. Later, when Validation peers will try to generate new block, this “invoke” method will be executed together with other cached transactions.

In its turn chaincode-to-chaincode calls are synchronous and executed simultaneously.

As a workaround we use another Query request to check the status of this submitted Invoke. It would be great if anybody can propose better solution.

Upvotes: 2

Related Questions