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

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
}
..
}
主站蜘蛛池模板: 吴堡县| 合山市| 河西区| 永康市| 新河县| 梓潼县| 温泉县| 嫩江县| 灌阳县| 定远县| 清丰县| 大港区| 静宁县| 伊川县| 和龙市| 科技| 什邡市| 天台县| 永康市| 和田市| 沅陵县| 邵阳市| 万载县| 利津县| 台前县| 阿勒泰市| 黄冈市| 唐河县| 汪清县| 延吉市| 五原县| 桦甸市| 尚义县| 顺义区| 清远市| 新蔡县| 莲花县| 云南省| 红安县| 山阴县| 屯昌县|