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
• 27,620 points
1,210 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

answered Jun 29, 2018 by Kalgi
• 37,420 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
• 67,140 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. To know more, look at the example here: https://fabric-sdk-node.github.io/Channel.html#queryTransaction
0 votes

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

GetHistoryForKey(key string)
answered Apr 25 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
• 67,140 points
228 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,010 points
697 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
• 67,140 points
26 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
• 67,140 points
190 views
0 votes
1 answer
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
• 27,620 points
55 views
+1 vote
1 answer