在 Hyperledger 中使用相同的键名创建多个条目
Making multiple entries with the same key name in Hyperledger
我试图编写一个智能合约,它会多次使用相同的密钥名称(我正在存储其详细信息的人的姓名),并希望在以下情况下输出为该密钥名称创建的所有条目查询名称。
是否有可能在 hyperledger 上这样做?如果是,那么您将如何编写查询函数
如果不是,您能否推荐一种替代方法来实现相同的结果?
我是 hyperledger 的新手,考虑到我没有看到任何与此类似的链码示例,我不知道如何继续。
你需要做的是将值编码为 JSON 格式并将其存储为给定键,例如你可以定义一个带有切片的结构,每次 update/append 切片使用新值,编组为字节数组,然后保存到分类帐中。
您从账本中读取字节数组的每次更新都会将其解组回结构,使用所需信息进行更新并使用相同的密钥保存回来。
要检索更改历史记录,您可以使用 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)
}
我试图编写一个智能合约,它会多次使用相同的密钥名称(我正在存储其详细信息的人的姓名),并希望在以下情况下输出为该密钥名称创建的所有条目查询名称。
是否有可能在 hyperledger 上这样做?如果是,那么您将如何编写查询函数
如果不是,您能否推荐一种替代方法来实现相同的结果?
我是 hyperledger 的新手,考虑到我没有看到任何与此类似的链码示例,我不知道如何继续。
你需要做的是将值编码为 JSON 格式并将其存储为给定键,例如你可以定义一个带有切片的结构,每次 update/append 切片使用新值,编组为字节数组,然后保存到分类帐中。
您从账本中读取字节数组的每次更新都会将其解组回结构,使用所需信息进行更新并使用相同的密钥保存回来。
要检索更改历史记录,您可以使用 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)
}