How to fetch asset modification history in hyperledger fabric

后端 未结 4 920
独厮守ぢ
独厮守ぢ 2021-01-07 13:33

I am using IBM bluemix blockchain service to tryout some smart contract logic for my asset sharing demo.

Is there anyway to query the asset modified history in hyp

4条回答
  •  猫巷女王i
    2021-01-07 14:04

    IF you need. Java SDk and go chaincode combination. Here is the example

    Java code

    public List getUFOHistory(String key) throws Exception {
        String[] args = { key };
        Logger.getLogger(QueryChaincode.class.getName()).log(Level.INFO, "UFO communication history - " + args[0]);
    
        Collection responses1Query = ucc.getChannelClient().queryByChainCode("skynetchaincode", "getHistoryForUFO", args);
        String stringResponse = null;
        ArrayList newArrayList = new ArrayList<>();
        for (ProposalResponse pres : responses1Query) {
            stringResponse = new String(pres.getChaincodeActionResponsePayload());
            Logger.getLogger(QueryChaincode.class.getName()).log(Level.INFO, stringResponse);
            newArrayList = gson.fromJson(stringResponse, new TypeToken>() {
            }.getType());
        }
        if (null == stringResponse)
            stringResponse = "Not able to find any ufo communication history";
        return newArrayList;
    }
    

    and you go chancode implemetation is as follows

    Go code

    func (t *SmartContract) getHistoryForUFO(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
    
        if len(args) < 1 {
                return shim.Error("Incorrect number of arguments. Expecting 1")
        }
    
        ufoId := args[0]
        resultsIterator, err := APIstub.GetHistoryForKey(ufoId)
        if err != nil {
                return shim.Error(err.Error())
        }
        defer resultsIterator.Close()
    
        var buffer bytes.Buffer
        buffer.WriteString("[")
    
        bArrayMemberAlreadyWritten := false
        for resultsIterator.HasNext() {
                response, err := resultsIterator.Next()
                if err != nil {
                        return shim.Error(err.Error())
                }
                // Add a comma before array members, suppress it for the first array member
                if bArrayMemberAlreadyWritten == true {
                        buffer.WriteString(",")
                }
                buffer.WriteString("{\"TxId\":")
                buffer.WriteString("\"")
                buffer.WriteString(response.TxId)
                buffer.WriteString("\"")
    
                buffer.WriteString(", \"Value\":")
                // if it was a delete operation on given key, then we need to set the
                //corresponding value null. Else, we will write the response.Value
                //as-is (as the Value itself a JSON)
                if response.IsDelete {
                        buffer.WriteString("null")
                } else {
                        buffer.WriteString(string(response.Value))
                }
    
                buffer.WriteString(", \"Timestamp\":")
                buffer.WriteString("\"")
                buffer.WriteString(time.Unix(response.Timestamp.Seconds, int64(response.Timestamp.Nanos)).String())
                buffer.WriteString("\"")
    
                buffer.WriteString(", \"IsDelete\":")
                buffer.WriteString("\"")
                buffer.WriteString(strconv.FormatBool(response.IsDelete))
                buffer.WriteString("\"")
    
                buffer.WriteString("}")
                bArrayMemberAlreadyWritten = true
        }
        buffer.WriteString("]")
    
        fmt.Printf("- History returning:\n%s\n", buffer.String())
        return shim.Success(buffer.Bytes())
    }
    

    There you go. Have fun.

提交回复
热议问题