简体   繁体   中英

Can't invoke nodejs chaincode on Hyperledger Fabric - Amazon Managed Blockchain

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:

在此处输入图像描述

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM