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

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
}
..
}
主站蜘蛛池模板: 苏尼特右旗| 双柏县| 平凉市| 田阳县| 阳谷县| 雅江县| 福鼎市| 宁海县| 石渠县| 彩票| 铜鼓县| 资兴市| 祁阳县| 无锡市| 大港区| 韶山市| 宿松县| 城固县| 鹤庆县| 大宁县| 通渭县| 珠海市| 靖州| 大厂| 蛟河市| 深水埗区| 永和县| 衡南县| 嘉义市| 文昌市| 广汉市| 西吉县| 临沧市| 县级市| 廊坊市| 赣榆县| 太谷县| 天等县| 汕头市| 灌云县| 常山县|