官术网_书友最值得收藏!

  • Hyperledger Cookbook
  • Xun (Brian) Wu Chuanfeng Zhang Andrew Zhang
  • 166字
  • 2021-06-24 14:59:39

The query and getHistory functions

ChaincodeStubInterface provides GetState, query functions. We can call this functions by passing assetId. This will trigger chaincode to get the corresponding result.

The getHistory function is used to view the records returned from the transaction history; all records are associated with assetId. Each record contains a related transaction ID and timestamp. With the timestamp, we know when the asset status was updated in the past.

Once the data is saved to blockchain, the application needs to query the chaincode data to check the OrgAsset information, shown as follows:

func (c *AssetMgr) getHistory(stub shim.ChaincodeStubInterface, args []string) pb.Response {
type AuditHistory struct {
TxId string `json:"txId"`
Value OrgAsset `json:"value"`
}
var history []AuditHistory
var orgAsset OrgAsset
assetId := args[0]
// Get History
resultsIterator, err := stub.GetHistoryForKey(assetId)
defer resultsIterator.Close()
for resultsIterator.HasNext() {
historyData, err := resultsIterator.Next()
var tx AuditHistory
tx.TxId = historyData.TxId
json.Unmarshal(historyData.Value, &orgAsset)
tx.Value = orgAsset //copy orgAsset over
history = append(history, tx) //add this tx to the list
}
..
}
主站蜘蛛池模板: 合阳县| 江城| 若尔盖县| 灌云县| 双城市| 大洼县| 清原| 曲阜市| 盘锦市| 台中市| 建阳市| 友谊县| 太原市| 大竹县| 庆元县| 青铜峡市| 洪雅县| 惠东县| 宜兰县| 汪清县| 宁南县| 尚义县| 江西省| 福贡县| 根河市| 溧水县| 临邑县| 阜康市| 金川县| 麦盖提县| 宝坻区| 大田县| 兰坪| 三河市| 迭部县| 图木舒克市| 苏尼特左旗| 庐江县| 松滋市| 安溪县| 深水埗区|