dongtingxiao4697 2019-08-13 02:51
浏览 97

如何在Hyperledger Fabric中解组交易有效负载

I need to fetch transaction fields from a transaction in the ledger. I'm able to successfully query the transaction from ledger by invoking qscc chaincode but unable to parse the fetched transaction to retrieve transaction fields. I want to unmarshal the transaction content into a structure with all the fields. With the below code, I'm getting error "failed to unmarshal channel header". Appreciate your help.

chainCodeArgs := toChaincodeArgs("GetTransactionByID", "myc", tid)
response := stub.InvokeChaincode("qscc", chainCodeArgs, "myc")
fmt.Printf("****************")
if response.Status != shim.OK {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload)
fmt.Printf(errStr)
return "", fmt.Errorf(errStr)
}

respStr := fmt.Sprintf("successfully queried qscc chaincode. Got response: %s", response.Payload)
fmt.Printf(respStr)

payload, err := utils.UnmarshalPayload(response.Payload)
if err != nil {
return "", fmt.Errorf("Error when unmarshalling transaction payload response")
}
fmt.Printf("++++++++++++++++++++++")
fmt.Println(payload)

if payload.Header == nil {
return "", fmt.Errorf("error getting txID from header: payload header is nil")
}

chdr, err := utils.UnmarshalChannelHeader(payload.GetHeader().ChannelHeader)
if err != nil {
return "", fmt.Errorf("failed to unmarshal channel header")
}

fmt.Printf("++++++++++++++++++++++")
fmt.Printf(chdr.TxId)

Error screenshot:

enter image description here

  • 写回答

1条回答 默认 最新

  • doushi8187 2019-08-15 15:21
    关注

    the response payload is in []bytes

    you can use string(response.Payload)

    Above code is equivalent to .toString("utf8")

    ------ or ------

    USE

    var data interface{}
    
    err = json.Unmarshal(responseBody, &data)
    
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算