Querying past transaction data from Hyperledger Fabric

0 votes
A ledger contains many transactions in it. How can I query the present and past transaction data from the ledger?
Jun 29, 2018 in Blockchain by digger
• 26,740 points
6,108 views

3 answers to this question.

0 votes

You can use the following system chaincode to query the ledger. It is used to query the peer and it also has a 
number of queries related to blocks.

https://github.com/hyperledger/fabric/tree/master/core/scc/qscc

Hope this helps!

Get your Blockchain certification today to learn more about this technology.

Thanks.

answered Jun 29, 2018 by Kalgi
• 52,350 points
0 votes

To get the historic data of the chaincode, you can use the ChaincodeStubInterface API.

This retrieves entire histroy for a given key. It is iterative and so you can see the changes made for the 
given key.

Look at the following example:

historyIer, err := stub.GetHistoryForKey(yourKey)

if err != nil {
    fmt.Println(errMsg)
    return shim.Error(errMsg)
}

if historyIer.HasNext() {
    modification, err := historyIer.Next()
    if err != nil {
        fmt.Println(errMsg)
        return shim.Error(errMsg)
    }
    fmt.Println("Returning information related to", string(modification.Value))
}


To make this work, you have the set the value of "enableHistoryDatabase" in core.yaml under the ledger section
as true as shown below:

ledger:

 history:
    # enableHistoryDatabase - options are true or false
    # Indicates if the history of key updates should be stored.
    # All history 'index' will be stored in goleveldb, regardless if using
    # CouchDB or alternate database for the state.
    enableHistoryDatabase: true

answered Jun 29, 2018 by Omkar
• 69,220 points
Can we query ledger using the transaction ID?
You can not query ledger using transaction ID with shim API but you can do it at application level using SDK.
0 votes

You can use shim function and query it using the transaction key. 

GetHistoryForKey(key string)
answered Apr 26, 2019 by Shaheen

Related Questions In Blockchain

0 votes
1 answer

Hyperledger Fabric: How to retrieve result from Fabric Transaction?

invokeChainCode(securityContext, functionName, args, options) { ...READ MORE

answered Nov 8, 2018 in Blockchain by Omkar
• 69,220 points
1,885 views
0 votes
1 answer
+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 17,100 points
2,956 views
+1 vote
1 answer
0 votes
1 answer

Hyperledger Fabric 1.0 how to maintain privacy from OSNs?

Yes, OSNs can see all the transaction ...READ MORE

answered Jul 2, 2018 in Blockchain by Omkar
• 69,220 points
864 views
0 votes
1 answer

In Hyperledger fabric, how does committer validate the transaction?

Endorsing and committing are just two functions ...READ MORE

answered Jul 3, 2018 in Blockchain by Omkar
• 69,220 points
1,243 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,003 views
+1 vote
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP