介绍configtxlator工具 #
configtxlator工具提供了一个真正的无状态REST API,独立于SDK,以简化Hyperledger Fabric区块链网络中的配置任务。该工具可以在不同的等效数据表示/格式之间轻松转换。例如,在一种工具操作模式下,该工具在二进制protobuf格式之间执行转换为人类可读的JSON文本格式,反之亦然。此外,该工具可以根据两组不同配置事务之间的差异计算配置更新。
1、环境配置 #
运行官方测试网络,确保它正常运行,详情请见fabric环境搭建后面测试部分。
进入CLI容器,并使用容器内的以下命令检查对等版本:
docker exec -it cli /bin/bash
peer version
运行以下命令,确保JQ工具已在CLI容器中安装并正常工作:
jq --version
jq
运行以下命令,确保configtxlator工具可用,验证工具版本,在后台启动工具,并验证工具在后台、CLI容器内正确运行
configtxlator version
后台启动configtxlator并查看网络状态 (两行一起复制粘贴进去)
configtxlator start &
netstat -ap
2、检索当前配置 #
通过在CLI容器中运行以下命令来设置和验证以下环境变量:
export CHANNEL_NAME=mychannel
export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
生成第三个组织的证书文件及配置文件
文章路径主要看你test-network中脚本文件的路径做出修改,根据实际情况改
#生成证书文件 另开一个命令行进入test-network/addOrg3
cryptogen generate --config=org3-crypto.yaml --output="../organizations" #指定组织3的证书配置文件
#生成json格式的配置文件
configtxgen -printOrg Org3MSP > ../organizations/peerOrganizations/org3.example.com/org3.json
configtxgen -printOrg Org3MSP>./channel-artifacts/org3.json
#生成的配置文件需要放到cli中使用
3、组织注册 #
1、CLI容器中运行以下命令来检索当前配置的配置块 #
peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
报错:
2022-03-25 08:11:20.391 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2022-03-25 08:11:20.466 UTC [cli.common] readBlock -> INFO 002 Expect block, but got status: &{FORBIDDEN} Error: can’t read the block: &{FORBIDDEN}
解决办法:
将cli从组织二 切换到组织一 启动官方项目的时候 切换了组织 ,换回来。
2、将获得的二进制文件config_block.pb解码为文本json文件 #
configtxlator proto_decode --input config_block.pb --type common.Block > config_block.json
3、利用jq工具获取配置块json文件中的配置信息 #
jq .data.data[0].payload.data.config config_block.json > config.json
4、创建新的配置文件 #
jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ./organizations/peerOrganizations/org3.example.com/org3.json > updated_config.json
5、将原来的config.json和更新的配置文件update_config.json转换为二进制文件 #
configtxlator proto_encode --input config.json --type common.Config --output config.pb
configtxlator proto_encode --input updated_config.json --type common.Config --output updated_config.pb
6、计算更新对象 #
configtxlator compute_update --channel_id $CHANNEL_NAME --original config.pb --updated updated_config.pb --output config_update.pb
7、解码更新对象为json文件,并将其重新封装 #
#解码更新对象
configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate | jq . > config_update.json
#将更新对象重新封装
echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel","type":2}},"data":{"config_update":'$(cat config_update.json)'}}}' |jq .> config_update_as_envelope.json
#将重新封装的更新对象编码为二进制文件
configtxlator proto_encode --input config_update_as_envelope.json --type common.Envelope --output config_update_as_envelope.pb
8、提交更新对象到通道中 #
#配置org1环境
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
CORE_PEER_LOCALMSPID=Org1MSP
CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
#通过org1签名更新事务
peer channel signconfigtx -f config_update_as_envelope.pb
!
#配置org2环境
CORE_PEER_LOCALMSPID=Org2MSP
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
#通过org2提交更新事务
peer channel update -f config_update_as_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
9、检索新的配置块,检验是否更新成功 #
peer channel fetch config config_block_Org3MSP.pb -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
configtxlator proto_decode --input config_block_Org3MSP.pb --type common.Block > config_block_Org3MSP.json
grep Org3MSP config_block_Org3MSP.json
!
4、将组织节点添加到通道中 #
主要参照如何在已有组织中怎加节点第一部分,这里就不详细做出叙述了
docker-compose -f docker/docker-compose-org3.yaml up -d
在test-network/addOrg3里面有脚本,也有配置文件,自己改路径 不然会报错。
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org3MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp
export CORE_PEER_ADDRESS=localhost:11051
export CHANNEL_NAME=mychannel
export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
peer channel fetch oldest mychannel.block -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
peer lifecycle chaincode install basic.tar.gz
查询
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
测试完毕,关闭网络。
sudo ./network.sh down