Javascript 在Fabric上编写链码时,我是否仅限于由链码存根定义的函数?
例如,当我使用VScode Hyperledger插件启动一个js项目时,我会得到如下智能合约: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
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操作访问分类账状态
- 访问事务上下文的便利函数