【问题标题】:How to get all transaction history against a chaincode in Hyperledger fabric如何根据 Hyperledger 结构中的链码获取所有交易历史记录
【发布时间】:2016-11-07 13:38:04
【问题描述】:

我能够在 Hyperledger(结构实现)中进行交易。我想通过传递用户的密钥来查看用户发起的所有事务及其有效负载详细信息。

例如:

A transfers 10 units to B
A transfers 5 units to C
D transfers 8 units to A

当我传递 A 的密钥时,fabric 必须向我提供 A 的所有交易。 有什么办法吗?或者我应该使用哪个 Fabric API 函数调用?

【问题讨论】:

    标签: hyperledger hyperledger-fabric


    【解决方案1】:

    /chain/blocks/{Block} 端点在指定的区块中携带有序的交易列表

    使用/chain 端点获取链的高度(块数),然后使用/chain/blocks/{Block} REST 端点从每个块中检索交易。

    【讨论】:

    • 谢谢,但我通过维护 Marc Campora 建议的索引方法解决了这个问题,这样我可以一次获得所有交易信息,这并不意味着你的答案是错误的,而是你的方法我必须循环获取每笔交易的数据。
    【解决方案2】:

    您可以在您的链码中开发适当的索引和查询功能。

    对于每个事务,您将其详细信息存储在内部键/值存储 (stub.PutState) 中,并以用户为键并返回与查询中的用户关联的所有事务 (stub.GetState)。

    【讨论】:

    • 我认为应该有一个功能可以在一次调用中至少查看所有事务,但是 Fabric 提供了 REST API GET/transactions/{tx_UUID},使用它我们一次只能看到一个事务时间。例如比特币区块链已经提供了。
    • 这个答案用于获取状态。您可以访问此链接以获取记录的历史记录gist.github.com/mrvijaycode/9692319998605372848bb85b37650aab
    【解决方案3】:

    最好最简单的方法是使用shim包函数

    GetHistoryForKey(key string)

    正如文档所说:

    链码可以调用 GetHistoryForKey 函数来返回跨时间的键值历史记录。 GetHistoryForKey 旨在用于只读查询。

    【讨论】:

    【解决方案4】:

    如果有人需要 Java SDK 和 go 链码组合。给你

    在这里回答similar question

    Java 代码

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

    你去 chancode 实现如下

    去代码

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

    }

    如果您有任何疑问,请告诉我。

    【讨论】:

      【解决方案5】:

      如果你使用 composer-client,你可以简单地使用 Historian 命令。

       var historian = await businessNetworkConnection.getHistorian();
       historian.getAll().then(historianRecords => console.log(historianRecords));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-27
        • 2014-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多