Hyperledger织物:使用GethistoryFromkey与Golang

发布于 2025-02-04 14:07:15 字数 2282 浏览 4 评论 0原文

我试图在我的Golang Chaincode中实现GethistoryFromkey(),但我一直在获得当前的令牌状态(而不是历史性的)。

  1. 我的Hyperledger Fabric网络(最新版本)对于此问题非常基本。我正在使用 fabcar chaincode,来自 golang 和SDK节点的织物样本。

  2. 呼叫启动后,我正在用 createcar (carnumber:“ 121”,...)创建一辆新车。 carnumber 在这种情况下是关键。

  3. 下一步是与 changecarowner (“ 121”,“ newowner”)

    的交易
  4. ,现在我想获得这辆车的历史记录。因此,我正在实施以下功能。

      func(s *smartContract)GethistoryForKeyAcciduuidamo(CTX Contractapi.TransactionContextInterface,carnumber string)(字符串,错误){
    
     历史记录,err:= ctx.getStub()。gethistoryforkey(carnumber)
    
     如果err!= nil {
         返回“ 0”,fmt.errorf(“按键加载历史记录的错误”,carnumber)
     }
    
     如果historiter.hasnext(){
         修改,err:= history.next()
         如果err!= nil {
             返回“ 0”,fmt.errorf(“通过键入键入历史记录的错误”,carnumber)
         }
         返回字符串(修改。值),零
     } 别的 {
         返回“ 0”,fmt.errorf(“获取历史博犬的错误”,carnumber)
     }
     

    }

结果:{“ make”:“ toyota”,“型号”:“ rav4”,“ color”:“灰色”,“所有者”:“ newowner”}

这不是一个历史悠久的汽车标记,但是在将Car121的所有者更改为人物之后,

我如何从创建汽车中获取历史性交易数据?

编辑: 这是我的新功能,但仍然是相同的行为。

func(S *SmartContract)GetAssethistory(CTX Contractapi.TransactionContextInterface,帐户字符串,carnumber uint64)([] HistoryQueryResult,错误){

historyKey, err := ctx.GetStub().CreateCompositeKey(Prefix, []string{owner, strconv.FormatUint(carNumber, 10)})
resultsIterator, err := ctx.GetStub().GetHistoryForKey(historyKey)
defer resultsIterator.Close()

var records []HistoryQueryResult
for resultsIterator.HasNext() {
    response, err := resultsIterator.Next()
    if err != nil {
        return nil, err
    }

    var asset Asset
    if len(response.Value) > 0 {
        err = json.Unmarshal(response.Value, &asset)
        if err != nil {
            return nil, err
        }
    } else {
        asset = Asset{Account: account}
    }

    timestamp, err := ptypes.Timestamp(response.Timestamp)
    if err != nil {
        return nil, err
    }

    record := HistoryQueryResult{
        TxId:      response.TxId,
        Timestamp: timestamp,
        Record:    &asset,
        IsDelete:  response.IsDelete,
    }
    records = append(records, record)
}

return records, nil

}

I was trying to implement GetHistoryFromKey() in my Golang Chaincode, but I just get the current token state all the time (and not historic).

  1. My Hyperledger Fabric network (latest version) is very basic for this issue. I am using the fabcar chaincode from fabric-samples in Golang and SDK Node.

  2. After calling InitLedger, I was creating a new car with CreateCar(CarNumber: "121", ...). CarNumber is key in this situation.

  3. Next step is a transaction with ChangeCarOwner("121", "NewOwner")

  4. Now I want to get the history of this car. Therefore I was implementing following function.

     func (s *SmartContract) GetHistoryForKeyAccIDUUIDAMO(ctx contractapi.TransactionContextInterface, carNumber string) (string, error) {
    
     historyIter, err := ctx.GetStub().GetHistoryForKey(carNumber)
    
     if err != nil {
         return "0", fmt.Errorf("Error in loading History by Key", carNumber)
     }
    
     if historyIter.HasNext() {
         modification, err := historyIter.Next()
         if err != nil {
             return "0", fmt.Errorf("Error in getting History by Key in Iteration", carNumber)
         }
         return string(modification.Value), nil
     } else {
         return "0", fmt.Errorf("Error in getting HistorybyKey", carNumber)
     }
    

    }

Result: {"make":"Toyota","model":"Rav4","colour":"Grey","owner":"NewOwner"}

This is not a historic car token, but the car after changing the owner of Car121 to PersonX

How can I get historic transaction data from the creation of car?

EDIT:
Here is my new function, but it is still the same behaviour.

func (s *SmartContract) GetAssetHistory(ctx contractapi.TransactionContextInterface, account string, carNumber uint64) ([]HistoryQueryResult, error) {

historyKey, err := ctx.GetStub().CreateCompositeKey(Prefix, []string{owner, strconv.FormatUint(carNumber, 10)})
resultsIterator, err := ctx.GetStub().GetHistoryForKey(historyKey)
defer resultsIterator.Close()

var records []HistoryQueryResult
for resultsIterator.HasNext() {
    response, err := resultsIterator.Next()
    if err != nil {
        return nil, err
    }

    var asset Asset
    if len(response.Value) > 0 {
        err = json.Unmarshal(response.Value, &asset)
        if err != nil {
            return nil, err
        }
    } else {
        asset = Asset{Account: account}
    }

    timestamp, err := ptypes.Timestamp(response.Timestamp)
    if err != nil {
        return nil, err
    }

    record := HistoryQueryResult{
        TxId:      response.TxId,
        Timestamp: timestamp,
        Record:    &asset,
        IsDelete:  response.IsDelete,
    }
    records = append(records, record)
}

return records, nil

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2025-02-11 14:07:16

您只是在读取历史记录迭代器中的第一个值,因此您只是看到与该键关联的值之一。要查看所有历史价值,您需要迭代所有这些价值,也许将它们收集到一个切片中。请参阅此处的示例:

https://github.com/hyperledger/fabric-samples/blob/8ca50df4ffec311E59451C2A7EBE210D9E6F0004/

You are just reading the first value from the history iterator so you are seeing just one of the values that has been associated with that key. To see all the historic values you need to iterate over all of them, perhaps collecting them into a slice. See here for an example:

https://github.com/hyperledger/fabric-samples/blob/8ca50df4ffec311e59451c2a7ebe210d9e6f0004/asset-transfer-ledger-queries/chaincode-go/asset_transfer_ledger_chaincode.go#L389-L433

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文