Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query ledger and historic data in Hyper-ledger Fabric

As ledger contains a sequence of transaction, How can I query the current state of ledger and Historic data from ledger.

like image 909
Manoj Indian Avatar asked Jul 10 '17 09:07

Manoj Indian


2 Answers

If you interested in history data in context of the chaincode, you could use ChaincodeStubInterface API, e.g.

// GetHistoryForKey returns a history of key values across time.
// For each historic key update, the historic value and associated
// transaction id and timestamp are returned. The timestamp is the
// timestamp provided by the client in the proposal header.
// GetHistoryForKey requires peer configuration
// core.ledger.history.enableHistoryDatabase to be true.
// The query is NOT re-executed during validation phase, phantom reads are
// not detected. That is, other committed transactions may have updated
// the key concurrently, impacting the result set, and this would not be
// detected at validation/commit time. Applications susceptible to this
// should therefore not use GetHistoryForKey as part of transactions that
// update ledger, and should limit use to read-only chaincode operations.
GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error)

which is capable to retrieve entire history for given key, you receiving back iterator, which will provide you insight on historical changes of the the given key, for example:

historyIer, err := stub.GetHistoryForKey(yourKeyIsHere)

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 about", string(modification.Value))
}

Also note, that you need to make sure history db is enabled, in core.yaml file part of the ledger section:

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
like image 112
Artem Barger Avatar answered Oct 19 '22 12:10

Artem Barger


You can use QSCC to query the ledger. It's a system chaincode that is used to query the peer. It has all kinds of queries such as get block by number, etc.

like image 31
yacovm Avatar answered Oct 19 '22 13:10

yacovm