如何在Hyper-ledger Fabric中查询分类帐和历史数据

时间:2017-07-10 09:38:39

标签: blockchain hyperledger-fabric

由于分类帐包含一系列交易,如何查询分类帐的当前状态和分类帐中的历史数据。

2 个答案:

答案 0 :(得分:2)

如果您对链码上下文中的历史数据感兴趣,可以使用ChaincodeStubInterface API,例如

// 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)

能够检索给定键的完整历史记录,您接收回迭代器,它将为您提供有关给定键的历史更改的信息,例如:

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))
}

另请注意,您需要在分类帐部分的core.yaml文件部分中确保启用了历史数据库:

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

答案 1 :(得分:1)

您可以使用QSCC查询分类帐。 它是用于查询对等方的系统链代码。 它有各种各样的查询,比如get by number等。