如何在Hyperledger结构中获取针对链代码的所有事务历史记录

问题描述 投票:8回答:5

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

例如:

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

当我通过A的钥匙时,面料必须向我提供A的所有交易。有什么办法吗?或者我应该使用哪个结构API函数调用?

hyperledger hyperledger-fabric
5个回答
4
投票

您可以在链代码中开发正确的索引和查询功能。

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


5
投票

/chain/blocks/{Block}端点携带指定块中的有序事务列表。

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


2
投票

最好和最简单的方法是使用垫片包功能

GetHistoryForKey(键字符串)

正如文件所说:

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


1
投票

如果有人需要Java SDk并去链代码组合。你去吧

在这里回答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;
}

你去链码实现如下

去代码

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

}

如果你提问,请告诉我。


0
投票

如果您使用的是composer-client,则只需使用Historian命令即可。

 var historian = await businessNetworkConnection.getHistorian();
 historian.getAll().then(historianRecords => console.log(historianRecords));
© www.soinside.com 2019 - 2024. All rights reserved.