hyperledger-fabric1.4

本篇博文为上篇博文“为通道添加新的组织”的可选部分。

更新通道配置包含Org3锚节点

因为Org1Org2已经在通道配置中定义了锚节点,所以Org3peer节点可以与Org1Org2peer节点建立gossip连接。同样,新添加的组织如Org3也应该在通道配置中定义自己的锚节点,让其它组织的新peer可以直接发现一个Org3peer

Org3-CLI继续,为了定义一个Org3锚节点对通道配置进行更新,该过程与上次的配置更新相似。

1.获取最新配置区块:
1
peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA

结果如下:

1
2
3
4
5
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
2019-10-24 02:55:14.866 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-10-24 02:55:14.883 UTC [cli.common] readBlock -> INFO 002 Received block: 7
2019-10-24 02:55:14.891 UTC [cli.common] readBlock -> INFO 003 Received block: 5
2019-10-24 02:55:14.892 UTC [channelCmd] fetch -> INFO 004 Retrieving last config block: 5
2. 文件转换与配置添加

configtxlator.pb文件转化为.json文件,同时jq工具去掉头部,元数据和签名。

1
configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config.json
1
2
3
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config.json
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
config.json config_block.pb crypto mychannel.block scripts

现在的config.json代表着将要被更新的最新的通道配置,再次使用jq工具更新配置JSON文件,添加Org3锚节点。

1
jq '.channel_group.groups.Application.groups.Org3MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org3.example.com","port": 11051}]},"version": "0"}}' config.json > modified_anchor_config.json
1
2
3
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# jq '.channel_group.groups.Application.groups.Org3MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org3.example.com","port": 11051}]},"version": "0"}}' config.json > modified_anchor_config.json
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
config.json config_block.pb crypto modified_anchor_config.json mychannel.block scripts

.json文件转换为 .pb文件,并计算增量:

1
2
3
configtxlator proto_encode --input config.json --type common.Config --output config.pb
configtxlator proto_encode --input modified_anchor_config.json --type common.Config --output modified_anchor_config.pb
configtxlator compute_update --channel_id $CHANNEL_NAME --original config.pb --updated modified_anchor_config.pb --output anchor_update.pb
1
2
3
4
5
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator proto_encode --input config.json --type common.Config --output config.pb
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator proto_encode --input modified_anchor_config.json --type common.Config --output modified_anchor_config.pb
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator compute_update --channel_id $CHANNEL_NAME --original config.pb --updated modified_anchor_config.pb --output anchor_update.pb
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
anchor_update.pb config.json config.pb config_block.pb crypto modified_anchor_config.json modified_anchor_config.pb mychannel.block scripts

anchor_update.pb转化为anchor_update.json:

1
configtxlator proto_decode --input anchor_update.pb --type common.ConfigUpdate | jq . > anchor_update.json

将更新封装在信封信息(envelope message)中,重新添加之前丢弃的头部,输出anchor_update_in_envelope.json文件:

1
echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":'$(cat anchor_update.json)'}}}' | jq . > anchor_update_in_envelope.json

anchor_update_in_envelope.json转化为anchor_update_in_envelope.pb

1
configtxlator proto_encode --input anchor_update_in_envelope.json --type common.Envelope --output anchor_update_in_envelope.pb

结果:

1
2
3
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
anchor_update.json anchor_update_in_envelope.json config.json config_block.pb modified_anchor_config.json mychannel.block
anchor_update.pb anchor_update_in_envelope.pb config.pb crypto modified_anchor_config.pb scripts

最后进行签名并提交更新,因为这只是对Org3的更新,所以只需要Org3在更新签名即可。由于当前环境身份为Org3身份,因此不需要切换身份。

1
peer channel update -f anchor_update_in_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
1
2
3
root@e6a67979d990:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel update -f anchor_update_in_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
2019-10-24 03:24:29.270 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-10-24 03:24:29.324 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update

orderer接收得到配置更新请求,添加一个配置更新区块。当所有peer接收到该区块,则会处理配置更新。

查看其中一个容器日志,当处理一个新区块中的配置交易时,可以看到gossip重新建立连接,代表配置更新已经成功应用了

1
docker logs -f peer0.org1.example.com
1
2
3
4
5
6
7
8
mit=4ms) commitHash=[742a4724235b86378598f9f77115dee0bac62859d79062a43de1a151b43cef81]
2019-10-24 03:24:29.329 UTC [gossip.privdata] StoreBlock -> INFO 2e1 [mychannel] Received block [8] from buffer
2019-10-24 03:24:29.370 UTC [gossip.gossip] JoinChan -> INFO 2e2 Joining gossip network of channel mychannel with 3 organizations
2019-10-24 03:24:29.370 UTC [gossip.gossip] learnAnchorPeers -> INFO 2e3 Learning about the configured anchor peers of Org2MSP for channel mychannel : [{peer0.org2.example.com 9051}]
2019-10-24 03:24:29.370 UTC [gossip.gossip] learnAnchorPeers -> INFO 2e4 Learning about the configured anchor peers of Org3MSP for channel mychannel : [{peer0.org3.example.com 11051}]
2019-10-24 03:24:29.370 UTC [gossip.gossip] learnAnchorPeers -> INFO 2e5 Learning about the configured anchor peers of Org1MSP for channel mychannel : [{peer0.org1.example.com 7051}]
2019-10-24 03:24:29.370 UTC [gossip.gossip] learnAnchorPeers -> INFO 2e6 Anchor peer with same endpoint, skipping connecting to myself
2019-10-24 03:24:29.413 UTC [committer.txvalidator] Validate -> INFO 2e7 [mychannel] Validated block [8] in 83ms

到目前为止,已经成功提交两个配置更新操作,包括添加Org3到通道中和为Org3定义锚节点。

最后更新: 2019年10月24日 14:06

原始链接: https://silence-linhl.github.io/blog/2019/10/24/addorg2/

× 请我吃糖~
打赏二维码