fabric的组织配置信息一般是提前写在configtx.yaml文件中的,通过configtx.yaml生成系统创世区块和通道文件。而系统启动和通道创建是通过创世区块和通道文件进行的,因此要在fabric系统运行时添加组织则是非常困难的。
还好,fabric-1.0提供了configtxlator工具,为动态修改fabric通道信息提供可能。本文将介绍如何利用configtxlator为fabric-1.0网络动态添加组织。
运行fabric网络
本文利用fabric-sample项目中的first-network作为测试的示例。文档参考
下载fabric-sample
从github上克隆fabric-sample项目
$ git clone https://github.com/hyperledger/fabric-samples.git
$ cd fabric-samples
下载镜像和binary包
$ curl -sSL https://goo.gl/eYdRbX | bash
这一步可能需要翻墙,可以从这里下载,然后运行,或者
$ curl -sSL http://oui195df4.bkt.clouddn.com/download-bin.sh | bash
这一步会拉取fabric-1.0的镜像和binary包
生成证书和通道文件
为了测试添加组织,先利用cryptogen工具生成三个组织的证书。
修改first-network,添加org3
生成证书和通道文件
$ ./byfn.sh -m generate
证书文件在crypto-config文件夹里,通道文件在channel-artifacts文件夹中
启动两个组织的fabric网络
执行
$ ./byfn.sh -m up
启动包括两个组织的fabric网络,cli容器会执行scripts/script.sh脚本进行创建mychannel通道,加入通道,部署chaincode,调用和查询chaincode等。
在系统channel中添加org
参考configtxlator工具的文档。
启动configtxlator服务
$ configtxlator start
获取系统通道的配置
设置MSP为Orderer
$ export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/Admin\@example.com/msp/
$ export CORE_PEER_LOCALMSPID=OrdererMSP
获取通道
# peer channel fetch config config_block.pb -o orderer.example.com:7050 -c testchainid
生成添加组织的增量文件
- 将配置信息区块转换为可读的json文件
$ curl -X POST --data-binary @config_block.pb http://127.0.0.1:7059/protolator/decode/common.Block > config_block.json
- 获取config区域
$ jq .data.data[0].payload.data.config config_block.json > config.json
- 修改config.json,添加org3信息
这一步可以利用configtxgen工具生成org3的配置信息,并将配置信息添加到config.json文件中,修改后的文件保存为updated_config.json,部分内容如图:
- 将original config和updated config打包成proto文件
$ curl -X POST --data-binary @config.json http://127.0.0.1:7059/protolator/encode/common.Config > config.pb
和
$ curl -X POST --data-binary @updated_config.json http://127.0.0.1:7059/protolator/encode/common.Config > updated_config.pb
- 生成增量文件
$ curl -X POST -F original=@config.pb -F updated=@updated_config.pb http://127.0.0.1:7059/configtxlator/compute/update-from-configs -F channel=testchainid > config_updat