I have a Hyperledger Fabric network with multiple peers with corresponding StateDB (CouchDB) and I have given CORE_LEDGER_HISTORY_ENABLEHISTORYDATABASE=true in all peer configuration under docker compose file. The idea that I shall be able to retrieve the update history of a record basis the key. The key here is a trade number and whenever any attribute of Trade gets updated, I do see update happening in CouchDB as I see increase in revision value. Now, in my chaincode function, have added below snippet of code (sample code for now)
_, args := stub.GetFunctionAndParameters()
tradeNumber := args[1]
historyIer, err := stub.GetHistoryForKey(tradeNumber)
if err != nil {
fmt.Println(err.Error())
return shim.Error(err.Error())
}
if historyIer.HasNext() {
modification, err := historyIer.Next()
if err != nil {
fmt.Println(err.Error())
return shim.Error(err.Error())
}
fmt.Println("Returning information about", string(modification.Value))
} return shim.Success(nil)
However, everytime I check chaincode container, it prints only the last value of Trade than the number of updates it has gone through, can anybody please let me know what wrong am I doing here?
Well, the problem that using the history iterator only once instead of actually iterating over it.
Instead of:
if historyIer.HasNext() {
modification, err := historyIer.Next()
if err != nil {
fmt.Println(err.Error())
return shim.Error(err.Error())
}
fmt.Println("Returning information about", string(modification.Value))
}
You need to do:
for historyIer.HasNext() {
modification, err := historyIer.Next()
if err != nil {
fmt.Println(err.Error())
return shim.Error(err.Error())
}
fmt.Println("Returning information about", string(modification.Value))
}
which is going to iterate over all key updates and print values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With