问题描述
我一直在使用 Fabric 测试网络为我的项目研究 Hyperledger Fabric。 在我成功地试验了 FabCar 示例后,我想尝试使用 Javascript 编写我自己的类似于 FabCar 链码的链码。
首先,我根据在 youtube 上看到的 javascript 链码教程实现了一个简单的属性类。
'use strict';
class Property {
constructor(id,name,signature) {
this.ID = id;
this.name = name;
this.signature = signature;
}
static from(bufferOrjson){
if (Buffer.isBuffer(bufferOrjson)){
if (!bufferOrjson.length){
return;
}
bufferOrjson = JSON.parse(bufferOrjson.toString('utf8'));
}
return Object.assign(new property(),bufferOrjson);
}
toBuffer() {
return Buffer.from(JSON.stringify(this))
}
}
export default {Property};
然后,我尝试实现与 Fabcar 类似的 Chaincode,但使用我作为对象创建的 Property 类。
'use strict';
import { Contract } from 'fabric-contract-api';
import { Property } from 'lib/property.js';
class FabProperty extends Contract {
async InitLedger(ctx) {
console.info('============= START : Initialize Ledger ===========');
const properties = [
new Property('p1','J House','adsd'),new Property('p2','12 Condo','4xes'),new Property('p3','U Mansion','man123')
];
for (let i = 0; i < properties.length; i++) {
await ctx.stub.putState(properties[i].ID,properties[i].toBuffer());
console.info('Added <--> ',properties[i].name);
}
console.info('============= END : Initialize Ledger ===========');
}
async createProperty(ctx,id,signature) {
const property = new Property(id,signature);
ctx.stub.putState(id,property.toBuffer());
return property.toBuffer();
}
async readProperty(ctx,id) {
const propertyJSON = await ctx.stub.getState(id);
if (!propertyJSON || propertyJSON.length === 0) {
throw new Error(`The Property ${id} does not exist`);
}
return propertyJSON.toString();
}
async propertyExists(ctx,id) {
const assetJSON = await ctx.stub.getState(id);
return assetJSON && assetJSON.length > 0;
}
async updateProperty(ctx,signature) {
const exists = await this.propertyExists(ctx,id);
if (!exists) {
throw new Error(`The Property ${id} does not exist`);
}
const updateProperty = new Property(id,signature);
return ctx.stub.putState(id,updateProperty.toBuffer())
}
async deleteProperty(ctx,id) {
const exists = await this.propertyExists(ctx,id);
if (!exists) {
throw new Error(`The Property ${id} does not exist`);
}
return ctx.stub.deleteState(id);
}
}
module.exports = FabProperty;
之后,我尝试使用类似于部署 fabcar 的 shell 脚本的 shell 脚本来部署此链代码。
但我无法部署我的链码并收到此错误。
现在,我怀疑我的 Javascript Chaincode 中可能存在错误,因为这是我第一次使用 Javascript 语言编写程序。我在 VSCode 中编写代码,因此没有警告或代码建议。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)