问题:
我是Hyperledger面料的新手。我在fabric-samples的chaincode文件夹中创建了一个chaincode。这就是我的文件结构的样子。
在lib文件夹中,我签了合同。这就是它的样子。
"use strict";
const { Contract } = require("fabric-contract-api");
const util = require("util");
class Landreg extends Contract {
async initLeger(ctx) {
console.info("============= START : Initialize Ledger ===========");
const lands = [
{
location: "Kottawa",
owner: "Tharindu",
size: "1 acres",
sale: false
},
{
location: "Bandaragama",
owner: "Shanilka",
size: "15 hectare",
sale: false
}
];
for (let i = 0; i < lands.length; i++) {
lands[i].docType = "land";
await ctx.stub.putState(
"LAND" + i,
Buffer.from(JSON.stringify(lands[i]))
);
console.info("Added <--> ", lands[i]);
}
console.info("============= END : Initialize Ledger ===========");
}
async queryLand(ctx, landNumber) {
const landAsBytes = await ctx.stub.getState(landNumber); // get the land from chaincode state
if (!landAsBytes || landAsBytes.length === 0) {
throw new Error(`${landNumber} does not exist`);
}
console.log(landAsBytes.toString());
return landAsBytes.toString();
}
async createLand(ctx, landNumber, location, owner, size) {
console.info("============= START : Create Land ===========");
const land = {
location,
docType: "land",
size,
owner,
sale: "false"
};
await ctx.stub.putState(landNumber, Buffer.from(JSON.stringify(land)));
console.info("============= END : Create Land ===========");
}
async queryAlllands(ctx) {
const startKey = "LAND0";
const endKey = "LAND999";
const iterator = await ctx.stub.getStateByRange(startKey, endKey);
const allResults = [];
while (true) {
const res = await iterator.next();
if (res.value && res.value.value.toString()) {
console.log(res.value.value.toString("utf8"));
const Key = res.value.key;
let Record;
try {
Record = JSON.parse(res.value.value.toString("utf8"));
} catch (err) {
console.log(err);
Record = res.value.value.toString("utf8");
}
allResults.push({ Key, Record });
}
if (res.done) {
console.log("end of data");
await iterator.close();
console.info(allResults);
return JSON.stringify(allResults);
}
}
}
async changeLandOwner(ctx, landNumber, newOwner) {
console.info("============= START : changeLandOwner ===========");
const landAsBytes = await ctx.stub.getState(landNumber); // get the land from chaincode state
if (!landAsBytes || landAsBytes.length === 0) {
throw new Error(`${landNumber} does not exist`);
}
const land = JSON.parse(landAsBytes.toString());
land.owner = newOwner;
await ctx.stub.putState(landNumber, Buffer.from(JSON.stringify(land)));
console.info("============= END : changeLandOwner ===========");
}
async makeLandForSale(ctx, landNumber) {
console.info("============= START : makeLandForSale ===========");
const landASBytes = await ctx.stub.getState(landNumber);
if (!landASBytes || landASBytes.length === 0) {
throw new Error(`${landNumber} does not exit`);
}
const land = JSON.parse(landASBytes.toString());
land.sale = "true";
await ctx.stub.putState(landNumber, Buffer.from(JSON.stringify(land)));
console.info("============= END : makeLandForSale ===========");
}
}
module.exports = Landreg;
这就是我的index.js的样子。
/ * * SPDX-License-Identifier:Apache-2.0 * /
“严格使用”;
const Landreg = require(“./ lib / landreg”);
module.exports.Landreg = Landreg; module.exports.contracts = [Landreg];
我点击$(npm bin)/ fabric-chaincode-node --peer.address localhost:7052。但是这给我留下了这样的错误。
$ $(npm bin)/fabric-chaincode-node --peer.address localhost:7052
cli.js <command>
Commands:
cli.js start [options] Start an empty chaincode
Options:
--help Show help [boolean]
-v, --version Show version number [boolean]
Not enough non-option arguments: got 0, need at least 1
我在landreg目录中点击了这个命令。有人可以帮助我解决这个问题并使其可以在同行中安装吗?谢谢!!
我没有使用此命令,而是使用脚本startFabric.sh在对等端安装。执行此行后,请使用以下docker exec脚本运行。
docker-compose -f ./docker-compose.yml up -d cli
一个接一个地执行这些行。
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp" cli peer chaincode install -n landreg -v 1.0 -p "/opt/gopath/src/github.com/strains/javascript" -l "node"
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n landreg -l "node" -v 1.0 -c '{"Args":[]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
请确保您的服务名称应与chaincode父文件夹匹配。这就是分类帐如何安装服务。我把landreg作为名字。如果它在fabcar下,请按原样给出。
我自己学习并使用Fabric Node SDK成功安装了一个工作示例。我们可以通过Skype讨论。
谢谢,Sriram