getHistory

As its name indicates, the gethistory function reads a claim of all historical values records for a key, as well as the TxId and claim value.

We first define the AuditHistory struct, which has TxId and value. GetHistoryForKey returns the list of results with resultsIterator, which contains all historical transaction records. We iterate through these records and add them to an array of AuditHistory. Later, we convert it to JSON byte and send the data back as a response, as follows:

func (c *ClaimContract) getHistory(stub shim.ChaincodeStubInterface, args []string) pb.Response {
type AuditHistory struct {
TxId string `json:"txId"`
Value Claim `json:"value"`
}
var history []AuditHistory
var claim Claim
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
claimId := args[0]
fmt.Printf("- start getHistoryForClaim: %s ", claimId)


// Get History
resultsIterator, err := stub.GetHistoryForKey(claimId)
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()


for resultsIterator.HasNext() {
historyData, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
var tx AuditHistory
tx.TxId = historyData.TxId
json.Unmarshal(historyData.Value, &claim)
tx.Value = claim //copy claim over
history = append(history, tx) //add this tx to the list
}
fmt.Printf("- getHistoryForClaim returning: %s", history)


//change to array of bytes
historyAsBytes, _ := json.Marshal(history) //convert to array of bytes
return shim.Success(historyAsBytes)
}

This covers our issuance claim chaincode. We will learn about Hyperledger Fabric configuration in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset