2017-07-03 28 views
1

我试图编写一个智能合约,该合约将使用多次使用相同密钥名称(我存储的详细信息的人员的姓名)并希望所有条目查询名称时要输出的键名。
是否有可能在超级主机上这样做?如果是,那么你将如何编写查询函数
如果不是,你能否推荐一种替代方法来实现相同的结果?
我是新来hyperledger,不知道如何进行考虑我没有看到任何类似链码的例子。在Hyperledger中使用相同的密钥名称创建多个条目

+0

(如果不是)一个解决方案,我认为是走在名作为参数,并附加一些后缀为它例如Name.mobile_no.x,其中x通过查询区块链中的名称直到没有条目出现数字添加的名称,而比最后一个具有类似条目的数字多1。任何更好的想法? – Snorlaxative

回答

1

您需要做的是将值编码为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) 


} 
+0

谢谢!我还希望在分类帐中包含更改属性的全部历史记录,这是我的案例中的有用信息(以电话号码为例)。任何方式来实现呢? – Snorlaxative

+0

我已经更新了我的答案,考虑使用GetHistoryForKey API。 –

+0

谢谢!完全解决了这个问题! – Snorlaxative

相关问题