2016-07-20 17:23

如何在Invoke json响应中返回值

  • blockchain
  • hyperledger
  • json

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • donglu9896 donglu9896 5年前

    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


    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.

    点赞 评论 复制链接分享
  • dongpo9071 dongpo9071 5年前

    “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.

    点赞 评论 复制链接分享