【发布时间】:2017-12-13 23:38:24
【问题描述】:
由于账本包含一系列交易,我如何从账本中查询账本的当前状态和历史数据。
【问题讨论】:
标签: blockchain hyperledger-fabric
由于账本包含一系列交易,我如何从账本中查询账本的当前状态和历史数据。
【问题讨论】:
标签: blockchain hyperledger-fabric
如果您对链码上下文中的历史数据感兴趣,可以使用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
【讨论】:
您可以使用QSCC 查询账本。 这是一个用于查询对等点的系统链码。 它具有各种查询,例如按数字获取块等。
【讨论】: