2017-09-02 55 views
0

我的鏈代碼存在問題,因爲我無法查詢所有數據(過去的記錄)並顯示出來。無法使用Hyperledger結構查詢所有歷史/計數數據v1.0.0 chaincode

我希望做的是,如果同樣的uniqueID變量在。

對於計數器鍵,與UNIQUEID增值我能得到正確的查詢,添加計數器。

目前,我可以從blockchain獲得奇異條目的數據,當我跑這條命令:

peer chaincode query -C food -n food_ccv01 -c '{"Args":["queryFoodInfo","1","123456789"]}' 

使用"123456789"作爲一個獨特的ID和「1」的計數器,結合他們給了我唯一的入口

但是,我無法使用此「123456789」+計數器來提取以前輸入區塊鏈的所有數據。

我該怎麼辦?或者,還有更好的方法?

我不知道爲什麼我的計數器不能作爲整數被初始化,我使用的是字符串現在...

這是我chaincode.

回答

1

我不認爲你需要附加您的與反鍵,而不是你可以簡單地繼續更新相同的密鑰,並查詢所有更新過,因爲你可以使用下面的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) 

如沿着這些線路的東西應該爲你做的工作:

func queryFoodFullInfo(stub shim.ChaincodeStubInterface, args []string) pb.Response { 
    fmt.Println("Entering Query Food information") 

    // Assuming food key is at zero index 
    historyIer, err := stub.GetHistoryForKey(args[0]) 

    if err != nil { 
     errMsg := fmt.Sprintf("[ERROR] cannot retrieve history of food record with id <%s>, due to %s", args[0], err) 
     fmt.Println(errMsg) 
     return shim.Error(errMsg) 
    } 

    result := make([]FavouritefoodInfo, 0) 
    for historyIer.HasNext() { 
     modification, err := historyIer.Next() 
     if err != nil { 
      errMsg := fmt.Sprintf("[ERROR] cannot read food record modification, id <%s>, due to %s", args[0], err) 
      fmt.Println(errMsg) 
      return shim.Error(errMsg) 
     } 
     var food FavouritefoodInfo 
     json.Unmarshal(modification.Value, &food) 
     result = append(result, food) 
    } 

    outputAsBytes, _ := json.Marshal(&result)     
    return shim.Success(outputAsBytes) 
} 

追隨你的初始路徑,你可能會喜歡探索range query功能:

// GetStateByRange returns a range iterator over a set of keys in the 
// ledger. The iterator can be used to iterate over all keys 
// between the startKey (inclusive) and endKey (exclusive). 
// The keys are returned by the iterator in lexical order. Note 
// that startKey and endKey can be empty string, which implies unbounded range 
// query on start or end. 
// Call Close() on the returned StateQueryIteratorInterface object when done. 
// The query is re-executed during validation phase to ensure result set 
// has not changed since transaction endorsement (phantom reads detected). 
GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error) 

marbles example

+0

謝謝!我設法使用GetHistoryForKey函數訪問所有以前的記錄。 :)我非常感謝你的幫助!謝謝! 我在哪裏可以在hyperleder面料中尋找更多這樣的功能來改善我的鏈碼? –

+0

對一些編譯錯誤'返回shim.Success([]字節(json.Marshal(結果))))' 並用 'outputAsBytes取代,ERR:= json.Marshal(結果) \t返回墊片。成功(outputAsBytes)' –

+0

感謝您的更新將編輯我的答案,以體現這一點。 –