Skip to content

Instantly share code, notes, and snippets.

@thackerronak
Last active February 18, 2024 11:19
Show Gist options
  • Save thackerronak/ac80f3a55fc0c77410f5b58ec59908c8 to your computer and use it in GitHub Desktop.
Save thackerronak/ac80f3a55fc0c77410f5b58ec59908c8 to your computer and use it in GitHub Desktop.
NodeJS Chaincode Deploy and Test
start Docker
cd /fabric-samples/basic-network /basic network
Edit docker-compose.yml change line no 72 comment and 73 uncomment. //prod to devp network
Edit start.sh add ‘cli’ in 15 no. line (docker-compose -f docker-compose.yml up -d ca.example.com orderer.example.com peer0.org1.example.com couchdb cli)
./start.sh //run start script to up network
docker logs -f peer0.org1.example.com //log (1 terminal)
docker exec -it cli bash //peer (2 terminal)
cd /fabric-samples/chaincode/hyperledger/fabric/peer/nodefiles (3 terminal)
Add chaincode node file package.json and mycc.js
npm install
CORE_CHAINCODE_ID_NAME="mycc:v0" node --inspect mycc.js --peer.address grpc://0.0.0.0:7052 //configured chaincode (everything fine you will get ‘Successfully established communication with peer node. State transferred to "ready"’ )
Go to terminal 2
peer chaincode install -l node -n mycc -v v0 -p nodefiles(directory name where we have put chaincode files)
CORE_PEER_LOCALMSPID="Org1MSP"
peer chaincode instantiate -l node -n mycc -v v0 -C mychannel -c '{"args":["init","1","2"]}'
peer chaincode invoke -n mycc -C mychannel -c '{"Args":["get","1"]}'
peer chaincode invoke -n mycc -C mychannel -c '{"Args":["set","1","1"]}'
peer chaincode invoke -n mycc -C mychannel -c '{"Args":["get","1"]}'
const shim = require('fabric-shim');
const util = require('util');
var Asset = class {
async Init(stub) {
let ret = stub.getFunctionAndParameters();
let params = ret.params;
if (params.length != 2) {
return shim.error("Incorrect number of arguments. Expecting 2");
}
let A = params[0];
let B = params[1];
try {
await stub.putState(A, Buffer.from(B));
return shim.success(Buffer.from("success"));
} catch (e) {
return shim.error(e);
}
}
async Invoke(stub) {
let ret = stub.getFunctionAndParameters();
let params = ret.params;
let fn = ret.fcn;
if (fn === 'set') {
var result = await this.setValues(stub, params);
if(result)
return shim.success(Buffer.from("success"));
} else {
var result = await this.getValues(stub, params);
if(result)
return shim.success(Buffer.from(result.toString()));
}
if (!result) {
return shim.error('Failed to get asset');
}
}
async setValues(stub, args) {
if (args.length != 2) {
return shim.error("Incorrect number of arguments. Expecting 2");
}
try {
return await stub.putState(args[0], Buffer.from(args[1]));
} catch (e) {
return shim.error(e);
}
}
async getValues(stub, args) {
if (args.length != 1) {
return shim.error("Incorrect number of arguments. Expecting 1");
}
try {
return await stub.getState(args[0]);
} catch (e) {
return shim.error(e);
}
}
}
shim.start(new Asset());
{
"name": "asset",
"version": "1.0.0",
"description": "hyperledger fabric blockchain",
"engines": {
"node": ">=8.4.0",
"npm": ">=5.3.0"
},
"scripts": {
"start": "node mycc.js"
},
"engine-strict": true,
"author": "",
"license": "Apache-2.0",
"dependencies": {
"fabric-shim": "unstable"
}
}
@svel91
Copy link

svel91 commented Mar 12, 2019

Hi any solution for the above issue ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment