Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Fabric上编写链码时,我是否仅限于由链码存根定义的函数?_Javascript_Hyperledger Fabric_Blockchain_Hyperledger Chaincode - Fatal编程技术网

Javascript 在Fabric上编写链码时,我是否仅限于由链码存根定义的函数?

Javascript 在Fabric上编写链码时,我是否仅限于由链码存根定义的函数?,javascript,hyperledger-fabric,blockchain,hyperledger-chaincode,Javascript,Hyperledger Fabric,Blockchain,Hyperledger Chaincode,例如,当我使用VScode Hyperledger插件启动一个js项目时,我会得到如下智能合约: onst { Contract } = require('fabric-contract-api'); class MyAssetContract extends Contract { //****I added stuff */ async nameSomething(ctx, myAssetId, nameToGive){ const buffer = awa

例如,当我使用VScode Hyperledger插件启动一个js项目时,我会得到如下智能合约:

onst { Contract } = require('fabric-contract-api');

class MyAssetContract extends Contract {

    //****I added stuff */
    async nameSomething(ctx, myAssetId, nameToGive){
        const buffer = await ctx.stub.getState(myAssetId);
        if (!exists) {
            throw new Error(`The my asset ${myAssetId} does not exist`);
        }
        //NAME SOMETHING HERE?
    }

    async myAssetExists(ctx, myAssetId) {
        const buffer = await ctx.stub.getState(myAssetId);
        return (!!buffer && buffer.length > 0);
    }

    async createMyAsset(ctx, myAssetId, value) {
        const exists = await this.myAssetExists(ctx, myAssetId);
        if (exists) {
            throw new Error(`The my asset ${myAssetId} already exists`);
        }
        const asset = { value };
        const buffer = Buffer.from(JSON.stringify(asset));
        await ctx.stub.putState(myAssetId, buffer);
    }

    async readMyAsset(ctx, myAssetId) {
        const exists = await this.myAssetExists(ctx, myAssetId);
        if (!exists) {
            throw new Error(`The my asset ${myAssetId} does not exist`);
        }
        const buffer = await ctx.stub.getState(myAssetId);
        const asset = JSON.parse(buffer.toString());
        return asset;
    }

    async updateMyAsset(ctx, myAssetId, newValue) {
        const exists = await this.myAssetExists(ctx, myAssetId);
        if (!exists) {
            throw new Error(`The my asset ${myAssetId} does not exist`);
        }
        const asset = { value: newValue };
        const buffer = Buffer.from(JSON.stringify(asset));
        await ctx.stub.putState(myAssetId, buffer);
    }

    async deleteMyAsset(ctx, myAssetId) {
        const exists = await this.myAssetExists(ctx, myAssetId);
        if (!exists) {
            throw new Error(`The my asset ${myAssetId} does not exist`);
        }
        await ctx.stub.deleteState(myAssetId);
    }

}

module.exports = MyAssetContract;

正如你所看到的,我试图通过命名一些东西来添加我自己的函数。我来自以太坊,所以我习惯于定义变量并像传统程序一样命名它们。然而,我觉得我需要遵守存根定义的内容。是这样吗?如果是这种情况,账本是由Fabric的API更新的,还是我可以在合同中明确写入权限?

存根API提供了以下几点:

  • 通过put/get操作访问分类账状态
  • 访问事务上下文的便利函数
您可以在智能合约中自由实现任何逻辑,但如果您想从/向分类账读取和/或写入数据,则需要使用存根API

典型的函数将从分类账中读取一些状态(通常基于对函数的输入),运行一些逻辑(更改值,检查值),然后将新的和/或更新的状态写入分类账