如何在链代码中执行调用函数时返回事务id,时间戳?

时间:2017-11-22 07:29:35

标签: blockchain hyperledger-fabric

我需要在每次调用函数调用后在客户端接口上返回事务id,时间戳的指导。

我发现stub.GetTxID()用于获取事务id,但是peer.response只接受一个参数,所以我无法在客户端接口上返回TxID。

2 个答案:

答案 0 :(得分:1)

您可以创建一个响应对象来捕获相关信息,将其编组为json并将其返回,如下所示:

 type ChaincodeResponse struct {
      txID string
      time *timestamp.Timestamp
 }

然后

// rest of the invoke code skipped, here is
// the relevant part:

resp, err := json.Marshal(ChaincodeResponse{
      txID: stub.GetTxID(),
      time: stub.GetTxTimestamp(),
})

// return json representation of relevant information
// in response
return shim.Success(resp)

答案 1 :(得分:0)

目前,我正在做一些事情,要求对我们所有的交易都加盖时间戳。我根据您上面的代码尝试了一些操作,但我认为该API自2017年以来已经有了很大发展。

当前,我在要放入分类帐的所有内容中添加一个created: stub.GetTxTimestamp()字段,然后稍后在任何查询中读取它们。尽管我想知道时间戳是否已经生成并存储,因此使它变得不必要-您是否知道时间戳是否仍自动存储在分类帐上的每个项目上?