问题:
我对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许可证标识符: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目录中命中了此命令。有人可以帮助我解决此问题并使它可在同行中安装吗?谢谢!!
答案 0 :(得分:1)
我没有使用此命令,而是使用脚本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/Admin@org1.example.com/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/Admin@org1.example.com/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进行讨论。
谢谢, 斯里拉姆