我可以将对象传递给Hyperledger Fabric事务吗? [英] Can I pass objects to a Hyperledger Fabric transaction?

查看:85
本文介绍了我可以将对象传递给Hyperledger Fabric事务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 fabcar示例的网络设置.自定义了链码和文件夹结构,所有这些似乎都可以编译并正常运行了.但是现在,我试图找出是否可以使用函数Contract.submitTransaction()(位于

I have a network set up that is based on the fabcar example. Customized the chaincode and the folder structure, and it all seems to compile and fire up just fine. But now, I am trying to find out whether I can use function Contract.submitTransaction() (found in invoke.ts and covered in the docs) or a more applicable function to pass somewhat more complex arguments for custom type Shipment which is based on type Car of the original example. In the example, Car is simply a flat type of strings, which you can simply pass to Contract.submitTransaction(), which takes only string arguments, like so:

await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom');

具有由多个子类型"组成的类型Shipment时,会变得更加困难:

With a type Shipment composed of multiple "sub-types", that gets a little harder:

import { TransitStatusEnum } from "../enum/transitstatusenum";
import { FreightContainer } from "./freightcontainer";
import { Company } from "./company";

export class Shipment {
    public docType?: string;
    public id: string;
    public status: TransitStatusEnum;
    public containers: FreightContainer[];
    public shipper?: Company;
    private totalShipmentValue?: number = 0;

    constructor() {
        if (!this.containers) {
            console.log("Shipment does not (yet) contain any containers, shipmentValue is 0");            
            return;
        }

        for (let container of this.containers) {
            this.totalShipmentValue += container.transitAgreement.cargoValue;
        }
    }
}

在下面,您将找到Contract.submitTransaction()应该调用的函数,而不是CreateCar():

Below you'll find the function that Contract.submitTransaction() should invoke instead of CreateCar():

public async createShipment(ctx: Context, id: string, status: TransitStatusEnum, shipper: Company, containers?: FreightContainer[]) {
    console.info('============= START : Create Shipment ===========');

    const shipment: Shipment = {
        docType: 'shipment',
        id,
        status,
        containers,
        shipper
    };

    await ctx.stub.putState(id, Buffer.from(JSON.stringify(shipment)));
    console.info('============= END : Create Shipment ===========');
}

我可以为这些自定义类型创建工厂,并根据 string 值生成类型,而不是传递给createShipment()的类型,或者传递字符串化对象(对象/数组) ,更多的对象/数组).但是我想知道是否真的有必要(尤其是后者,这让我感到畏惧). docs 仅提及此Contract.submitTransaction()是一种向区块链提交交易的方式.

I could make factories for these self-defined types and have the types generated based on string values instead of types passed to createShipment(), or pass a stringified object (of objects/arrays, of more objects/arrays). But I'd like to know if (especially the latter, which gives me chills) is truly necessary. The docs only mention this Contract.submitTransaction() function as a way to submit a transaction to the blockchain.

我应该采用我的工厂解决方案吗?我可以使用另一个功能来使用类型提交事务吗?还是这不是我应该构造链代码的方式,应该考虑完全简化它吗?

推荐答案

对于Hyperledger Fabric支持的每种语言,对象是不同的.例如,存在可用于JavaScript(Node.js),Java,Go和Python的SDK.此外,链码本身可以用JavaScript,Java或Go编写.

An object is different for each language that Hyperledger Fabric supports. For example, there are SDKs available for JavaScript (Node.js), Java, Go and Python. Furthermore the chaincode itself could be written in either JavaScript, Java or Go.

为保持语言中立,参数的内部表示形式只是一个字节数组,该数组在参见比较).但是,您的应用程序和链码都需要支持它.

To stay language neutral, the internal representation of the arguments is just an array of bytes, which is defined in the protobuf specification which Fabric uses for internal communication. Fabric SDKs also tend to toss in a conversion to a string definition. Your only option is to work with bytes or strings. You could use your own custom parsing or for more advanced use, you could use some type of serialization protocol (see comparison). However, both your application and chaincode needs to support it.

当您需要发送对象时,我建议为您的案例创建一个链码函数,例如createShipment,该函数接受构造对象所需的尽可能多的参数.如果对象本身包含对象数组(FreightContainer),则可以将其注册为对象本身(createFreightContainer),其中对象包含Shipment的ID.

When you need to send objects, I would recommend creating a chaincode function such as createShipment for your case, which accepts as many arguments as needed to construct the object. If the object itself contains an array of objects (FreightContainer) you could register this as an object itself (createFreightContainer) which contains an ID to the Shipment.

这篇关于我可以将对象传递给Hyperledger Fabric事务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆