Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k points)

I am using IBM bluemix blockchain service to try out some smart contract logic for my asset sharing demo. 

Is there any way to query the asset modified history in hyperledger fabric network. 

I have checked with documentations for both fabric 0.6 and 1.0 versions, but I can find only the stub.pushState(key,value_json) and stub.getState(key) to interact width the ledger.

But using stub.getState(key), I can fetch only the latest entry of the key, but how can I fetch and display the series of changes/modifications written for the same key. I have iterated through the block using {peeraddress}/Block/getBlock/{Block}, but I am getting the encrypted transaction payloads only since its security is on. I am not getting the idea to display the history of asset modifications for the same key.

Please suggest me the correct way to do this. 

Thanks in advance

1 Answer

0 votes
by (29.5k points)
edited by

Hi, please refer to the code below to use GetHistoryForKey() API :

historyIter, err := stub.GetHistoryForKey(key)

    if err != nil {
        errMsg := fmt.Sprintf("[ERROR] cannot retrieve history for key <%s>, due to %s", key, err)
        fmt.Println(errMsg)
        return shim.Error(errMsg)
    }

    for historyIter.HasNext() {
        modification, err := historyIer.Next()
        if err != nil {
            errMsg := fmt.Sprintf("[ERROR] cannot read record modification for key %s, id <%s>, due to %s", key, err)
            fmt.Println(errMsg)
            return shim.Error(errMsg)
        }
        fmt.Println("Returning information about", string(modification.Value))

}

for more clarifications I'd recommend you to read about API description

There is a lot more to learn than this. Enroll now in Blockchain Online Course to learn more.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...