2017-09-02 21 views
0

すべてのデータ(過去のレコード)を照会できず、表示することができないためチェーンコードに問題があります。私がやることを望んでいたものHyperledgerファブリックv1.0.0チェーンコードですべての履歴/カウントデータを照会できません

は同じuniqueID変数がキー入力された場合にカウンタを追加することでした。

カウンターで、私は右のクエリを得ることができますユニークID値を追加します。それらを組み合わせ、

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

カウンタとしてユニークなIDとして"123456789"と「1」を使用して私に一意のエントリを与える:私は、このコマンドを実行したときに

現在、私はblockchainから特異エントリデータを得ることができます

しかし、この "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関数を使用して過去のすべてのレコードにアクセスすることができました。 :)私はあなたの助けに大きく感謝します!ありがとうございました! 私はチェーンコードを改善するために、ハイパーレイヤーファブリックでこのような機能をもっと探していますか? = json.Marshal(&結果) \t戻りシム: –

+0

は '返しshim.Success([]バイト(json.Marshal(結果))))' と 'outputAsBytes、ERRに置き換えるにいくつかのコンパイルエラーを持っていました。成功(outputAsBytes) ' –

+0

ありがとうございました。これを反映するために私の答えを編集します。 –

関連する問題