完成 Fabric 的安装与初步尝试后,需要我们进一步的对 Fabric 的部署做了解。由于在生产环境下,无法对链码错误进行有效定位。故而本节首先完成 Fabric 开发环境的部署,可以由程序员手动执行链码,而非提交给 Fabric 去执行,从log日志中可以有效定位问题。另外,本节完成链码的整体操作流程。enjoy!
1 基础 chaincode 编写
- 在 chaincode 文件夹中创建nodejs环境
# 1.进入 chaincode 目录
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/chaincode
# 2.创建我们自己的chaincode目录
mkdir helloworldcc
# 3.初始化npm
npm init
# 4.下载fabric 链码的必要依赖
npm install fabric-shim
# 5.编写链码,并保存至 helloworld.js 文件中
# 6.修改 package.json 文件,指定容器启动时候执行的js程序
vim package.json
"scripts": {
"start": "node helloworld.js"
},
- 编写链码 helloworld.js
//导入环境依赖
const shim = require('fabric-shim');
const Chaincode = class{
//链码初始化操作
async Init(stub){
//获取当前方法的名字和参数
var ret = stub.getFunctionAndParameters();
var args = ret.params;
var a = args[0];
var aValue = args[1];
var b = args[2];
var bValue = args[3];
await stub.putState(a,Buffer.from(aValue));
await stub.putState(b,Buffer.from(bValue));
return shim.success(Buffer.from('chaincod init successs'));
}
async Invoke(stub){
let ret = stub.getFunctionAndParameters();
let fcn = this[ret.fcn];
return fcn(stub,ret.params);
}
//查询操作
async query(stub,args){
let a = args[0];
let balance = await stub.getState(a);
return shim.success(balance);
}
};
shim.start(new Chaincode());
2 基础开发环境搭建
- 基础网络配置
# 根据fabric提供的基础网络环境配置网络
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/
cp -r basic-network helloworld-network
- dev开发环境配置,可以看到chaincode的log信息,方便定位错误
vim docker-compose.yml
- 修改 start.sh 将 cli 客户端也启动
vim start.sh
- docker 容器已全部正常启动
- dev模式下,手动将chaincode注册给peer
# CORE_CHAINCODE_ID_NAME 表示链码的名字和版本,根据链码安装时的参数指定
# peer.address grpc 根据当前ifconfig得到的docker0的ip地址指定,7052为dev端口
CORE_CHAINCODE_ID_NAME="helloworld:v1.0" npm start -- --peer.address grpc://172.17.0.1:7052
3 cli 链码操作
- 进入 cli docker 容器
docker exec -it cli bash
- 安装链码
CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp peer chaincode install -l node -n helloworld -v v1.0 -p /opt/gopath/src/github.com/helloworldcc/
- result
2022-12-05 08:13:17.211 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2022-12-05 08:13:17.211 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2022-12-05 08:13:17.227 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
- 实例化链码
CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp peer chaincode instantiate -l node -n helloworld -v v1.0 -C mychannel -c '{"args":["init","zzh","100","czbk","100"]}' -o 172.17.0.1:7050
- result
2022-12-05 08:15:00.436 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2022-12-05 08:15:00.436 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
- 测试链码调用
CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp peer chaincode invoke -n helloworld -C mychannel -c '{"args":["query","zzh"]}' -o 172.17.0.1:7050
- result
2022-12-05 08:15:45.203 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 payload:"100"
- Dev日志的信息记录
root@iZ2ze1b5riita5dqa4o7fkZ:/opt/go-project/src/github.com/hyperledger/fabric/scripts/fabric-samples/chaincode/helloworldcc# CORE_CHAINCODE_ID_NAME="helloworld:v1.0" npm start -- --peer.address grpc://172.17.0.1:7052
> helloworldcc@1.0.0 start
> node helloworld.js --peer.address grpc://172.17.0.1:7052
2022-12-05T08:12:56.299Z info [c-api:lib/chaincode.js] Registering with peer grpc://172.17.0.1:7052 as chaincode "helloworld:v1.0"
2022-12-05T08:12:56.318Z info [c-api:lib/handler.js] Successfully registered with peer node. State transferred to "established"
2022-12-05T08:12:56.318Z info [c-api:lib/handler.js] Successfully established communication with peer node. State transferred to "ready"
2022-12-05T08:15:00.445Z info [c-api:lib/handler.js] [mychannel-be6b0906] Calling chaincode Init() succeeded. Sending COMPLETED message back to peer
2022-12-05T08:15:45.201Z info [c-api:lib/handler.js] [mychannel-00ca098e] Calling chaincode Invoke() succeeded. Sending COMPLETED message back to peer