2017-09-02 1 views
0

J'ai un problème dans ma chaincode car je ne peux pas interroger toutes les données (enregistrements passés) et les afficher.Impossible d'interroger toutes les données d'historique/de comptage avec Hyperledger fabric v1.0.0 chaincode

Ce que j'espérais faire était d'ajouter un compteur si la même variable uniqueID est entré au clavier.

Avec le compteur, en ajoutant à la valeur uniqueID je peux obtenir la requête droite.

Actuellement, je pourrais obtenir des données d'entrée unique du blockchain quand je couru cette commande:

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

Utilisation du "123456789" comme un identifiant unique et « 1 », comme le compteur, en les combinant me donne une entrée unique

Cependant, je ne peux pas utiliser ce compteur "123456789" + pour extraire TOUTES les données précédemment entrées dans la blockchain.

Comment puis-je faire à ce sujet? ou y a-t-il un meilleur moyen?

Je ne sais pas pourquoi mon compteur ne peut pas être initialisé comme un entier, je suis en utilisant une chaîne pour l'instant ...

Ceci est mon chaincode.

Répondre

1

Je ne pense pas que vous devez ajouter votre clé avec un compteur, vous pouvez tout simplement au lieu de continuer à mettre à jour et même clé pour interroger toutes les mises à jour depuis toujours, vous pouvez utiliser l'API suivante:

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

par exemple, quelque chose le long de ces lignes devrait faire un travail pour vous:

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

Après votre chemin initial que vous aimeriez probablement explorer les capacités 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) 

Voir marbles example.

+0

Merci! J'ai réussi à accéder à tous mes enregistrements passés en utilisant la fonction GetHistoryForKey. :) J'apprécie beaucoup votre aide! Je vous remercie! Où puis-je chercher plus d'une telle fonction dans le tissu hyperleder pour améliorer ma chaincode? –

+0

eu quelques erreurs de compilation sur 'retour shim.Success ([] octet (json.Marshal (résultat))))' et remplacé par 'outputAsBytes, err: = json.Marshal (et résultat) \t cale de retour. Succès (outputAsBytes) ' –

+0

Merci pour la mise à jour éditera ma réponse pour refléter cela. –