由于账本包含一系列的业务,如何从账本中查询账本的当前状态和历史数据。
发布于 2017-07-10 20:45:06
如果你对链码上下文中的历史数据感兴趣,你可以使用ChaincodeStubInterface应用编程接口,例如
// 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发布于 2017-07-10 18:21:24
您可以使用QSCC查询分类账。它是用来查询对等体的系统链码。它有各种各样的查询,比如get block by number等。
https://stackoverflow.com/questions/45008607
复制相似问题