概述
我们已经创建好了application channel, 并且连接了peer。接下来发布一个chaincode。
chaincode发布的基本步骤为:
- 打包chaincode
- 安装chaincode
- 批准chaincode
- 提交chaincode到channel
1. 打包chaincode
chaincode需要打包成一定的格式
我们使用一个fabric-samples项目中提供的[chaincode]()
首先跳转到目录
cd fabric-samples/asset-transfer-basic/chaincode-go
下载项目需要的依赖到vendor目录:
go mod vendor
打包文件:
peer lifecycle chaincode package hello.tar.gz --path ./chaincode-go --lang golang --label hello_1.0
打包生成了hello.tar.gz文件
2. 安装chaincode
- 注意:接下来的动作同样需要本组织的Admin账户,相关指令都在我们上一节提到的cli容器中执行
首先是把chaincode的代码安装到具体的peer中。
并不是所有的peer都需要安装chaincode, 只有需要运行chaincode的peer才安装
首先把hello.tar.gz从本地拷贝到cli容器中
docker cp hello.tar.gz Container:PATH
安装chaincode:
# peer lifecycle chaincode install ./hello.tar.gz 2021-10-13 05:54:54.779 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nJhello_1.0:646c5f97e59c9734cd7738bdd0dac07cfa5d65a5840a3a4656da350416ef7faf\022\thello_1.0" > 2021-10-13 05:54:54.779 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: hello_1.0:646c5f97e59c9734cd7738bdd0dac07cfa5d65a5840a3a4656da350416ef7faf
查询下刚才安装的chaincode
peer lifecycle chaincode queryinstalled
3. 批准chaincode
安装了chaincode还不算完。还得组织上批准chaincode的定义,即chaincode definition。
一般情况,大部分的组织都得批准该定义,这个chaincode才有效。具体策略取决于/Channel/Application/LifecycleEndorsment。
需要注意的是,批准定义的是组织,和本组织的peer是否安装了该chaincode没关系
- 注意:该过程需要Organization Administrator来执行,必须得提前配置好变量CORE_PEER_MSPCONFIGPATH
运行指令:
peer lifecycle chaincode approveformyorg -o orderer1.orgA:7050 --channelID channel1 --name hello --version 1.0 --package-id hello_1.0:646c5f97e59c9734cd7738bdd0dac07cfa5d65a5840a3a4656da350416ef7faf --sequence 1 --tls --cafile /etc/hyperledger/fabric/tls/root-orgA.crt
- -o 指定orderer地址
- –package-id, 通过peer lifecycle chaincode queryinstalled可以获得
- –sequence,指定chaincode的更新次数,第一次提交为1,比如说修改过chaincode,就变成2,依此类推。已经commit了的chaincode,sequence的值可以通过”peer lifecycle chaincode querycommitted”命令查到
- –cafile,指定本组织的根TLS证书
4. 提交chaincode到channel
大部分的组织批准后,其中任意一个peer把chaincode正式提交到channel,chaincode就生效了
- 注意:该过程需要Organization Administrator来执行,必须得提前配置好变量CORE_PEER_MSPCONFIGPATH
首先看一下有多少组织批准了指定的chaincode
peer lifecycle chaincode checkcommitreadiness --channelID channel1 --name hello --version 1.0 --sequence 1 --output json
在其中一台peer上运行:
peer lifecycle chaincode commit -o orderer1.orgA:7050 --channelID channel1 --name hello --version 1.0 --sequence 1 --tls --cafile /etc/hyperledger/fabric/tls/root-orgA.crt --peerAddresses peer1.orgA:8050 --tlsRootCertFiles /etc/hyperledger/fabric/tls/root-orgA.crt --peerAddresses peer1.orgB:8051 --tlsRootCertFiles /etc/hyperledger/fabric/tls/root-orgB.crt
- –peerAddress指定了多个peer的地址,这个是根据策略决定的,如果是Majority策略,则需要指定大多数组织中的peer, 本项目中,指定一个就足够了
- –tlsRootCertFiles指定能够验证对应peerTLS证书的根证书,一般而言,设置为组织的根证书就妥
查询刚提交的chaincode:
peer lifecycle chaincode querycommitted --channelID channel1 --name hello
常见错误
在安装chaincode的时候,遇到错误
dial unix /var/run/docker.sock: connect: no such file or directory
这是因为container想要访问dockerd的端口失败造成的。
需要注意两点:
- CORE_VM_ENDPOINT这个变量要配置对。一般是unix:///var/run/docker.sock。如果还是不行,则要通过ip addr查看网络名为docker0的ip地址,设置为对应的值,比如:http://172.17.0.1:2375。
- 要在volume那儿设置- /var/run/docker.sock:/var/run/docker.sock。才可以访问主机的sock地址
Next
部署好chaincode后,我们调用一个试试呢?
异次元传送门:[调用chaincode]()
ref
- https://hyperledger-fabric.readthedocs.io/en/release-2.2/chaincode_lifecycle.html
- https://hyperledger-fabric.readthedocs.io/en/release-2.2/deploy_chaincode.html
- https://fabricdocs.readthedocs.io/en/latest/Setup/Network-setup.html
发表回复