开发环境
操作系统:Ubuntu 18.04.1 LTS
软件工具版本:
root@ubuntu:/home/wjl/nodejs/sample2/fabric-samples-1.3.0/balance-transfer# node -v
v8.9.4
root@ubuntu:/home/wjl/nodejs/sample2/fabric-samples-1.3.0/balance-transfer# npm -v
5.6.0
root@ubuntu:/home/wjl/nodejs/sample2/fabric-samples-1.3.0/balance-transfer# docker -v
Docker version 18.06.1-ce, build e68fc7a
root@ubuntu:/home/wjl/nodejs/sample2/fabric-samples-1.3.0/balance-transfer# docker-compose -v
docker-compose version 1.23.1, build b02f1306
软件工具的版本取决于项目目录下的package.json
"engines": {
"node": ">=8.9.4 <9.0",
"npm": ">=5.6.0 <6.0"
},
如果使用虚拟机,不能在Ubuntu和Windows的共享文件夹操作示例,会导致文件权限问题。要把官方示例下载到ubuntu本机的文件夹中。
下载fabric官方的示例
下载v1.3.0版本,不同的版本,对node,npm的要求会有不同,所以需指定版本
地址:https://github.com/hyperledger/fabric-samples/archive/v1.3.0.tar.gz
在终端输入
wget https://github.com/hyperledger/fabric-samples/archive/v1.3.0.tar.gz
tar -zxvf v1.3.0.tar.gz
然后
ls
可看到fabric-samples-1.3.0
然后执行,进入项目目录
cd fabric-samples-1.3.0/balance-transfer
执行 ./runApp.sh
进入balance-transfer项目目录
直接执行
./runApp.sh
出现如下错误
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/wjl/nodejs/sample2/fabric-samples-1.3.0/balance-transfer/node_modules/dtrace-provider/build'
gyp ERR! System Linux 4.15.0-39-generic
gyp ERR! command "/opt/node-v8.9.4-linux-x64/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/wjl/nodejs/sample2/fabric-samples-1.3.0/balance-transfer/node_modules/dtrace-provider
gyp ERR! node -v v8.9.4
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
说明node_modules下载出现错误
需要设置上网代理,然后手动npm安装node_modules
设置上网代理(搭梯子)
export http_proxy=http://192.168.10.158:1080
在192.168.10.158,开启上网代理,并启用本地端口1080,允许来自局域网的连接
测试代理设置是否成功
curl google.com
出现如下信息
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
说明已经可以访问google.com,也就是搭梯子成功
手动npm安装
删除项目目录下的node_modules
rm -r node_modules
需要设置上网代理,然后执行命令
npm install --unsafe-perm=true
启动nodejs的服务端
在启动服务端前需要删除上网代理
export -n http_proxy
执行命令
PORT=4000 node app
即可成功启动服务端
[2018-11-18 23:59:07.860] [INFO] SampleWebApp - ****************** SERVER STARTED ************************
[2018-11-18 23:59:07.862] [INFO] SampleWebApp - *************** http://localhost:4000 ******************
(node:34938) DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition instead
说明启动成功
使用 ./testAPIs.sh进行接口测试
在项目目录执行
./testAPIs.sh
POST request Enroll on Org1 ...
{"success":true,"secret":"","message":"Jim enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDI2NDk4NzksInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1NDI2MTM4Nzl9.nEsR5xVG5l3DmLBhanCIq1KPQq1t0YDQV6udyoluD94"}
ORG1 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDI2NDk4NzksInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1NDI2MTM4Nzl9.nEsR5xVG5l3DmLBhanCIq1KPQq1t0YDQV6udyoluD94
POST request Enroll on Org2 ...
{"success":true,"secret":"","message":"Barry enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDI2NDk4ODEsInVzZXJuYW1lIjoiQmFycnkiLCJvcmdOYW1lIjoiT3JnMiIsImlhdCI6MTU0MjYxMzg4MX0.qjaSpwtITLz0TGCpSf_-rt8FMKDhoBwyYDvW6Gz8Hwk"}
ORG2 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDI2NDk4ODEsInVzZXJuYW1lIjoiQmFycnkiLCJvcmdOYW1lIjoiT3JnMiIsImlhdCI6MTU0MjYxMzg4MX0.qjaSpwtITLz0TGCpSf_-rt8FMKDhoBwyYDvW6Gz8Hwk
POST request Create channel ...
{"success":true,"message":"Channel 'mychannel' created Successfully"}
POST request Join channel on Org1
{"success":true,"message":"Successfully joined peers in organization Org1 to the channel:mychannel"}
POST request Join channel on Org2
{"success":true,"message":"Successfully joined peers in organization Org2 to the channel:mychannel"}
POST Install chaincode on Org1
{"success":true,"message":"Successfully installed chaincode"}
POST Install chaincode on Org2
{"success":true,"message":"Successfully installed chaincode"}
POST instantiate chaincode on Org1
POST invoke chaincode on peers of Org1 and Org2
说明可以实现部分功能,但是instantiate chaincode on Org1,出现错误
手动post链码初始化
接下来,使用手动命令行进行链码的初始化,需要拷贝org1的token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDI2NDk4NzksInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1NDI2MTM4Nzl9.nEsR5xVG5l3DmLBhanCIq1KPQq1t0YDQV6udyoluD94
curl -s -X POST
http://localhost:4000/channels/mychannel/chaincodes
-H “authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDI2NDk4NzksInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1NDI2MTM4Nzl9.nEsR5xVG5l3DmLBhanCIq1KPQq1t0YDQV6udyoluD94”
-H “content-type: application/json”
-d “{
“chaincodeName”:“mycc”,
“chaincodeVersion”:“v0”,
“chaincodeType”: “$LANGUAGE”,
“args”:[“a”,“100”,“b”,“200”]
}”
执行结果
{"success":true,"message":"Successfully instantiate chaincode in organization Org1 to the channel 'mychannel'"}
说明链码初始化成功,之后进行查询验证
手动post查询已初始化链码
拷贝org1的token,执行命令
curl -s -X GET
“http://localhost:4000/channels/mychannel/chaincodes?peer=peer0.org1.example.com”
-H “authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDI2NDk4NzksInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1NDI2MTM4Nzl9.nEsR5xVG5l3DmLBhanCIq1KPQq1t0YDQV6udyoluD94”
-H “content-type: application/json”
执行后返回结果
["name: mycc, version: v0, path: github.com/example_cc/go"]
说明查询成功