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

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
}
..
}
主站蜘蛛池模板: 郴州市| 垦利县| 舟曲县| 栖霞市| 金川县| 吴忠市| 宁城县| 淮安市| 信阳市| 普兰店市| 桐柏县| 全州县| 河源市| 中方县| 夹江县| 都匀市| 会泽县| 邵武市| 大埔县| 阳信县| 平谷区| 健康| 大化| 海南省| 博客| 三原县| 黔西县| 开化县| 越西县| 和龙市| 波密县| 寿宁县| 嘉黎县| 揭东县| 元氏县| 安义县| 洞口县| 东乌| 铜陵市| 北流市| 类乌齐县|