如何在Invoke json响应中返回值

如何在Invoke 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.

我正在尝试设计通过网络API访问的超级账本链码,该API将json对象传递给代码 。 但是,无论何时执行调用方法,我都无法在json响应中实际将值返回给用户。 p>

例如,下面是一些示例代码: p>

  func(t * TLCChaincode)调用(存根* shim.ChaincodeStub,函数字符串)  ,args [] string)([] byte,error){
 //做一些事情
返回[] byte(“ Some string”),nil 
} 
  code>  pre> 
  
 

以及一些用于返回错误的示例代码 p>

  func(t * TLCChaincode)调用(存根* shim.ChaincodeStub,函数字符串,参数[]字符串)(  [] byte,error){
 //尝试做一些事情
 //得到某种错误
返回nil,errors.New(“ someError”)
} 
  code>  pre  > 
 
 

但是它们都返回这样的消息,消息总是像下面这样的一些随机字符串(我怀疑是某种哈希): p>

   {
“ jsonrpc”:“ 2.0”,
“ result”:{
“ status”:“ OK”,
“ message”:“ 1e1123e3-b484-4120-a28e-c3a8db384557” 
  },
“ id”:11 
} 
  code>  pre> 
 
 

如您所见,该响应都不包含我返回的响应(在第一种情况下), 或我返回的错误(在 第二种情况)。 我将如何获取返回的字节或返回的错误到返回的json中? p>

编辑:请注意,如果我从另一个链码中调用一个invoke方法,它将收到正确的返回值。 只是当它返回给用户时,它才能正常工作。 p> div>

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.

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