例如,当我使用 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 是否更新了账本,或者我可以在合约中明确写入权限?