【问题标题】:How to query ledger and historic data in Hyper-ledger Fabric如何在 Hyper-ledger Fabric 中查询账本和历史数据
【发布时间】:2017-12-13 23:38:24
【问题描述】:

由于账本包含一系列交易,我如何从账本中查询账本的当前状态和历史数据。

【问题讨论】:

    标签: blockchain hyperledger-fabric


    【解决方案1】:

    如果您对链码上下文中的历史数据感兴趣,可以使用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
    

    【讨论】:

    • @Hi Artem:我们可以根据交易ID查询账本吗?
    • shim API 无法查询账本中的 txID,而在应用程序级别,您可以使用 SDK 来完成,请参见此处的示例:fabric-sdk-node.github.io/Channel.html#queryTransaction
    • 你好,请问historyDB是用来存储key的修改历史的吗?对吗?
    • @Wandy 是的,是的。
    • @ArtemBarger GetHistoryForKey 从哪里提取数据?由于 World State 不存储历史数据,它是直接从区块链中获取的吗?
    【解决方案2】:

    您可以使用QSCC 查询账本。 这是一个用于查询对等点的系统链码。 它具有各种查询,例如按数字获取块等。

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 2016-02-18
      相关资源
      最近更新 更多