我试图编写一个智能合约,该合约将多次使用相同的密钥名称(我存储其详细信息的人的姓名),并希望在查询名称时输出为该密钥名称创建的所有条目。
是否有可能在超级账本上这样做?如果是,那么您将如何编写查询函数
如果不是,您能否推荐一种替代方法来实现相同的结果?
我是超级账本的新手,考虑到我没有看到任何与此类似的链码示例,我不知道如何继续。
您需要做的是将值编码为 JSON 格式并将其存储为给定键编组,例如您可以定义一个带有切片的结构,每次使用新值更新/追加切片,编组为字节数组,然后保存入账本。
您从分类帐中读取字节数组的每次更新将其解组回结构体,使用所需信息进行更新并使用相同的密钥保存回来。
要检索更改历史记录,您可以使用以下方法之一 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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句