1

Following AWS's first steps on how to set up a blockchain network, I got stuck at step 7[1] where to package/install/approve/commit a chaincode. As a personal challenge, I tried using the asset-transfer-basic/chaincode-typescript project [2].

This is the project folder structure (where lib is the node_modules folder, but renamed, so it is included in the final package):

chaincode-typescript/  
  ├─ dist/  
  ├─ lib/  
  ├─ src/  
  ├─ tslint.json  
  ├─ tsconfig.json  
  ├─ package.json  
  ├─ package-lock.json

This is the project package.json (note the start script):

{
    "name": "asset-transfer-basic",
    "version": "1.0.0",
    "description": "Asset Transfer Basic contract implemented in TypeScript",
    "main": "dist/index.js",
    "typings": "dist/index.d.ts",
    "engines": {
        "node": ">=12",
        "npm": ">=5"
    },
    "scripts": {
        "lint": "tslint -c tslint.json 'src/**/*.ts'",
        "pretest": "npm run lint",
        "test": "nyc mocha -r ts-node/register src/**/*.spec.ts",
        "start": "NODE_PATH=lib node dist/index.js",
        "build": "tsc",
        "build:watch": "tsc -w",
        "prepublishOnly": "npm run build"
    },
    "engineStrict": true,
    "author": "Hyperledger",
    "license": "Apache-2.0",
    "dependencies": {
        "fabric-contract-api": "^2.0.0",
        "fabric-shim": "^2.0.0"
    },
    "devDependencies": {
        "@types/chai": "^4.1.7",
        "@types/mocha": "^5.2.5",
        "@types/node": "^10.12.10",
        "@types/sinon": "^5.0.7",
        "@types/sinon-chai": "^3.2.1",
        "chai": "^4.2.0",
        "mocha": "^5.2.0",
        "nyc": "^14.1.1",
        "sinon": "^7.1.1",
        "sinon-chai": "^3.3.0",
        "ts-node": "^7.0.1",
        "tslint": "^5.11.0",
        "typescript": "^3.1.6"
    }
}

these are the commands I ran and their outputs:

docker exec cli peer lifecycle chaincode package package.tar.gz --lang node --label package_3 --path ./fabric-samples/asset-transfer-basic/chaincode-typescript/
docker exec cli peer lifecycle chaincode install package.tar.gz

2022-07-30 23:09:05.476 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nJpackage_3:5a51d2bc0ef40de3c4ad1f2080456004c39e26670f2a9048796496eaef7b88b4\022\tpackage_3" > 
2022-07-30 23:09:05.488 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: package_3:5a51d2bc0ef40de3c4ad1f2080456004c39e26670f2a9048796496eaef7b88b4
export CC_PACKAGE_ID=package_3:5a51d2bc0ef40de3c4ad1f2080456004c39e26670f2a9048796496eaef7b88b4
docker exec cli peer lifecycle chaincode queryinstalled

Installed chaincodes on peer:
Package ID: package_2:1fbb720f16271e9ae647736e012ca6ff8d5f6c6bf80ca7696668bd1c6a89b8e2, Label: package_2
Package ID: package_3:5a51d2bc0ef40de3c4ad1f2080456004c39e26670f2a9048796496eaef7b88b4, Label: package_3
Package ID: package_1:e73c64b62ebe74c1997117b037df2dd51d7aaacb26c877f726f03f7c568c4af9, Label: package_1
docker exec cli peer lifecycle chaincode approveformyorg --orderer $ORDERER --tls --cafile /opt/home/managedblockchain-tls-chain.pem --channelID supply-chain --name package3 --version v0 --sequence 1 --package-id $CC_PACKAGE_ID

2022-07-30 23:10:36.764 UTC [chaincodeCmd] ClientWait -> INFO 001 txid [a2634acda7acbc220ca0b78b6f46a5c7f202ac498541139f2209d412c49ed20b] committed with status (VALID) at nd-<node_id>.m-<member_id>.n-<network_id>.managedblockchain.us-east-1.amazonaws.com:30003
docker exec cli peer lifecycle chaincode checkcommitreadiness --orderer $ORDERER --tls --cafile /opt/home/managedblockchain-tls-chain.pem --channelID supply-chain --name package3 --version v0 --sequence 1

Chaincode definition for chaincode 'package3', version 'v0', sequence '1' on channel 'supply-chain' approval status by org:
m-<member_id>: true
docker exec cli peer lifecycle chaincode commit --orderer $ORDERER --tls --cafile /opt/home/managedblockchain-tls-chain.pem --channelID supply-chain --name package3 --version v0 --sequence 1

2022-07-30 23:12:17.742 UTC [chaincodeCmd] ClientWait -> INFO 001 txid [e8745d021b24567caeebac0fbbf0a969b66e77f817e78ebba735af6b7c32b43f] committed with status (VALID) at nd-<node_id>.m-<member_id>.n-<network_id>.managedblockchain.us-east-1.amazonaws.com:30003
docker exec cli peer lifecycle chaincode querycommitted -C supply-chain

Committed chaincode definitions on channel 'supply-chain':
Name: package2, Version: v0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
Name: package3, Version: v0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
Name: package, Version: v0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
docker exec cli peer chaincode invoke --tls --cafile /opt/home/managedblockchain-tls-chain.pem --channelID supply-chain --name package3 -c '{"function":"InitLedger","Args":[]}'

2022-07-30 23:13:30.906 UTC [chaincodeCmd] InitCmdFactory -> INFO 001 Retrieved channel (supply-chain) orderer endpoint: nd-<node_id>.m-<member_id>.n-<network_id>.managedblockchain.us-east-1.amazonaws.com:30001
Error: endorsement failure during invoke. response: status:500 message:"error in simulation: failed to execute transaction 2034113c1b27abebdee846465ba3609f2dd8d2a58984eb6684c2895989ea4443: could not launch chaincode package_3:5a51d2bc0ef40de3c4ad1f2080456004c39e26670f2a9048796496eaef7b88b4: chaincode registration failed: container exited with 0"

The last log says that the chaincode package was not registered. But when I go to the logs in CloudWatch to see what went wrong calling the InitLedger function, I get this:

enter image description here

Ricardo Passos
  • 373
  • 1
  • 3
  • 10
  • It appears that your logs do not contain any errors that would indicate the code failed to run? Have you tested this chaincode using the local BYFN (build your first network) method defined in Fabric Samples to eliminate an issue with the chaincode itself? – Forrest Aug 01 '22 at 18:23
  • I believe all chaincode that uses the high level `fabric-contract-api` library needs to start with the "fabric-chaincode-node start" script rather than calling a function from index.js. This could be causing the problem. There is a known issue with Node.js chaincode on Amazon Managed Blockchain where this start script may not work properly. Let me know if you face this issue. – Forrest Aug 01 '22 at 18:27
  • @Forrest Yes, I tested this chaincode locally and it seems to be working. In the AMB context, I packaged and commited the same chaincode. This time, in the `start` script I used `fabric-chaincode-node start` as you recommended, like this: `NODE_PATH=lib fabric-chaincode-node start`. But when I invoked the `InitLedger` method, I got this NPM error on CloudWatch: `sh: fabric-chaincode-node: not found`. I also tried to prefix the command with `npx`, like this: `NODE_PATH=lib npx fabric-chaincode-node start` but no luck either; it triggers a time out error. – Ricardo Passos Aug 02 '22 at 17:42

1 Answers1

0

I came to the conclusion that AMB service doesn't support the fabric-contract-api and fabric-chaincode-node but supports fabric-shim.

What I've tried as an attempt to make it work:
1 - Install the latest version of fabric-contract-api and fabric-shim (v2.2.3 at this moment); I didn't notice any different error messages or success when invoking functions.

2 - Since the node_modules folder isn't packaged and installed into the peers, I've changed its name to lib; then, in the package.json, I changed the start script to: NODE_PATH=lib node contract.js; this way the project dependencies are included in the final package; it didn't work because fabric-contract-api requires the fabric-chaincode-node start in the start script, and since I renamed the node_modules folder, this script can't be found anywhere by the Node.js runtime.

3 - Following the same strategy as above (changing the node_modules folder name), I tried to include the lib/.bin folder in the $PATH, like this: export PATH=$PATH:./lib/.bin/ fabric-chaincode-node start; I got a sh: fabric-chaincode-node: Permission denied error when tried to invoke a function.

On the other hand, fabric-shim seems to work just fine (v2.2.3).

Ricardo Passos
  • 373
  • 1
  • 3
  • 10
  • 1
    Thanks for updating on this. This is a known issue and I will report back once a long-term fix is in place. For now, `fabric-shim` bundled with the above method will work. Thanks for your patience! – Forrest Aug 03 '22 at 18:32