HyperLedger Fabric中的复杂数据类型

问题描述 投票:1回答:1

我阅读了有关HyperLedger的文档。但是,我找不到关于存储复杂数据类型的任何信息,因为这意味着我什至有可能。例如,假设我们有两个对象:作者和书。是否可以创建看起来像这样的智能合约? (以打字稿为例):


export class Book {
    public ISBN: string;
    public Title: string;
}


export class Author {
    public firstName: string;
    public lastName: string;
    public publishedBooks: Array<Book>;
}

如果是这样的话,查询在这种情况下将是什么样子。另一方面,如果不可能的话,如何在HyperLedger中建立这种数据关系的模型。

hyperledger-fabric
1个回答
1
投票

是的,您可以这样做。在智能合约中实施它,并使用Hyperledger指令查询分类帐。

例如,在Go中,您可以使用Shim PutState和GetState来确定具有ID的实体。如果实现像CouchDB这样的数据库,您甚至可以在数据库上执行更复杂和更丰富的查询。

[EDIT1]答案改进示例:这就是我在Go Chaincode中对此进行改进的方法

type V struct {
    Attribute string `json:"Attribute"`
    Function  string `json:"Function"`
    Value     string `json:"Value"`
}

type AV struct {
    Vs  []V      `json:"Vs"`
    CFs map[string]string `json:"CFs"`
}

如您所见,我将V结构用于Vs数组。这使我的数据集更加复杂,并且位于链码中。

[EDIT 2]通过查询和输入来回答改进:添加新实体非常容易。我的示例始终在GoLang中。发送一个JSON到链码(感谢SDK),然后将其解组:

var newEntity Entity
json.Unmarshal([]byte(args[0]), &newEntity)

现在使用PutState函数将给定新实体的ID(在我的情况下包含在JSON文件的id字段中)放入新实体:

entityAsBytes, _ := json.Marshal(newEntity)
err := APIstub.PutState(newEntity.Id, entityAsBytes)

到此您就完成了。如果现在要查询检索该ID的分类帐,则可以执行以下操作:

entityAsByte, err := APIstub.GetState(id)
return shim.Success(entityAsByte)
© www.soinside.com 2019 - 2024. All rights reserved.