2017-08-08 55 views
1

我正在使用IBM bluemix blockchain service为我的资产共享演示尝试一些智能合约逻辑。

无论如何查询hyperledger结构网络中的资产修改历史记录。

我与单证检查两个织物0.6和1.0版本,但我能找到仅 stub.pushState(键,value_json) stub.getState(键)相互作用宽度分类账。
但是使用 stub.getState(key),我只能获取该键的最新条目,但是如何获取并显示为同一个键编写的一系列更改/修改。 我已经使用{peeraddress}/Block/getBlock/{Block}迭代了整个块,但我只是在加密的事务有效负载自安全开启后才获取。我没有想到为相同的密钥显示资产修改的历史记录。如何在超级结构结构中获取资产修改历史记录


请告诉我正确的方法来做到这一点。

预先感谢

+0

实际上这个{peeraddress}/Block/getBlock/{Block}正在使用什么?该块返回可以做什么。由于我在这里使用的加密/解密逻辑并不是很专业,所以这个疑问仍然存在。可以建议任何人..?我在其api文档中找不到更多内容。 – Girish007

回答

4

您可以使用GetHistoryForKey() API如下:

historyIter, err := stub.GetHistoryForKey(key) 

    if err != nil { 
     errMsg := fmt.Sprintf("[ERROR] cannot retrieve history for key <%s>, due to %s", key, err) 
     fmt.Println(errMsg) 
     return shim.Error(errMsg) 
    } 

    for historyIter.HasNext() { 
     modification, err := historyIer.Next() 
     if err != nil { 
      errMsg := fmt.Sprintf("[ERROR] cannot read record modification for key %s, id <%s>, due to %s", key, err) 
      fmt.Println(errMsg) 
      return shim.Error(errMsg) 
     } 
     fmt.Println("Returning information about", string(modification.Value)) 
    } 

Here is the link的接口API描述:

// 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) 

如果你想检查变化的历史不链码的上下文可以使用QSCC(查询系统Chaincode),它提供以下功能:

// These are function names from Invoke first parameter 
const (
    GetChainInfo  string = "GetChainInfo" 
    GetBlockByNumber string = "GetBlockByNumber" 
    GetBlockByHash  string = "GetBlockByHash" 
    GetTransactionByID string = "GetTransactionByID" 
    GetBlockByTxID  string = "GetBlockByTxID" 
) 
+0

感谢评论和例子:)我使用https://github.com/hyperledger/fabric/blob/v0.6/core/chaincode/shim/interfaces.go跟随fabricv0.6,现在升级到v1。 0。 – Girish007

1

Fabric FAQ,答chaincode API GetHistoryForKey()将返回值的历史的关键。