Hyperledger Fabric(七)------加入新的Org
开始接下来的步骤之前确保脚本执行没有问题
先启动连个2个Org的:
./byfn.sh up
加入第三个Org:
./eyfn.sh up
如果以上都没问题,最后会输出:
========= All GOOD, EYFN test execution completed ===========
下面就可以开始分步执行了
关于byfn的部分在上一篇博客里已经说的很清楚了,还不清楚的同学可以去看下。
开始之前先清理doker环境:
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
BYFN
先用脚本启动BYFN
./byfn.sh up
准备材料
-
用
cryptogen
生成需要的证书在
org3-artifacts
目录下,../../bin/cryptogen generate --config=./org3-crypto.yaml
-
生成加入Org3需要的配置文件(JSON格式)
export FABRIC_CFG_PATH=$PWD && ../../bin/configtxgen -printOrg Org3MSP > ../channel-artifacts/org3.json
-
将orderer的相关证书复制一份到Org3的目录下
cd ../ && cp -r crypto-config/ordererOrganizations org3-artifacts/crypto-config/
更新Channel
-
进入CLI:
docker exec -it cli bash
导入环境变量(orderer的tls证书和channel名字):
export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem && export CHANNEL_NAME=mychannel
-
接下来就是一系列pb文件和json文件的转换,比较繁琐,建议直接去看官方文档,我这里画了个转换的流程图作为参考.
到这一步我们应该得到了最终需要的文件
org3_update_in_envelope.pb
-
对这个文件进行签名,根据policy先前的两个Org都要签名.
peer0.Org1签名:
peer channel signconfigtx -f org3_update_in_envelope.pb
peer0.org2签名:
环境变量:
export CORE_PEER_LOCALMSPID="Org2MSP" export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected].example.com/msp export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
签名
peer channel signconfigtx -f org3_update_in_envelope.pb
-
更新channel
peer channel update -f org3_update_in_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
如果成功则会输出:
Successfully submitted channel update
文档中提到的
leading peer
选举我们配置文件里定义了动态选举.CORE_PEER_GOSSIP_USELEADERELECTION=true
CORE_PEER_GOSSIP_ORGLEADER=false
加入网络
启动Org3相关的容器:
docker-compose -f docker-compose-org3.yaml up -d
进入Org3CLI:
docker exec -it Org3cli bash
导入orderer的tls证书路径和channel名:
export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem && export CHANNEL_NAME=mychannel
-
用
peer channel fetch
取得genesis block
peer channel fetch 0 mychannel.block -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
将peer0.Org3加入网络:
peer channel join -b mychannel.block
-
将peer1.Org3加入网络:
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer1.org3.example.com/tls/ca.crt && export CORE_PEER_ADDRESS=peer1.org3.example.com:7051 peer channel join -b mychannel.block
install chaincode(这里安装在了peer1.Org3上):
peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/
-
打开新的终端进入CLI:
docker exec -it cli bash
export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected].example.com/msp export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
这时是peer0.Org1身份
install chaincode:
peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected].example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
这时是peer0.Org2身份
install chaincode:
peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/
-
更新
chaincode
了peer chaincode upgrade -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 2.0 -c '{"Args":["init","a","90","b","210"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"
这里指定了新的共识策略
-P "OR ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"
-
接下来就可以转账查询了
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'