How to get transaction history on a particular key in Hyperledger Fabric?

Multi tool use
Multi tool use


How to get transaction history on a particular key in Hyperledger Fabric?



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?




1 Answer
1



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.





Thanks Artem, it did solve the issue... Just one point for my clarity, when I looked in the CouchDB it does have the updated version of data then where does SHIM API gets the history data from...
– Ashwani Kumar
Jul 3 at 5:48






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

9p9Mu,0I7motfI,pwHStZj,9hbj evf90lSWRM
L Cg SG,b,QR 08,o zu U

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications