我正在尝试编写一个智能合约,该合约将使用相同的密钥名称(我正在存储其详细信息的人的姓名)多次,并希望在查询时输出为该密钥名称生成的所有条目名字。
是否有可能在hyperledger上这样做?如果是,那么您将如何编写查询函数?如果不是,您是否可以推荐另一种方法来实现相同的结果?
我是超级棒的新手,不知道如何继续,因为我没有看到任何与此类似的链码的例子。
答案 0 :(得分:1)
您需要做的是将值编码为JSON格式并将其存储为给定键的封送,例如,您可以使用切片定义结构,每次使用新值更新/附加切片,编组到字节数组然后保存到分类帐中。
您从分类帐中读取字节数组的每次更新都会将其解组为struct,使用所需信息进行更新并使用相同的密钥保存。
要检索更改历史记录,您可以使用ChaincodeStubInterface
// Chaincode interface must be implemented by all chaincodes. The fabric runs
// the transactions by calling these functions as specified.
type ChaincodeStubInterface interface {
// Other methods omitted
// 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)
}