2017-08-08 100 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()將返回值的歷史的關鍵。