本案例依然基于First Network示例进行操作演示,Fisrt Network示例中已经存在org1、org2,现操作演示加入org3。
1.快速启动Fisrt Network
[root@huangzhenhua first-network]# cd /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network
[root@huangzhenhua first-network]# ./byfn.sh up
2.为org3生成通讯密钥
[root@huangzhenhua first-network]# ls
base channel-artifacts crypto-config docker-compose-cli.yaml docker-compose-couch.yaml docker-compose-e2e.yaml eyfn.sh README.md
byfn.sh configtx.yaml crypto-config.yaml docker-compose-couch-org3.yaml docker-compose-e2e-template.yaml docker-compose-org3.yaml org3-artifacts scripts
[root@huangzhenhua first-network]# cd org3-artifacts/
[root@huangzhenhua org3-artifacts]# ls
configtx.yaml org3-crypto.yaml
[root@huangzhenhua org3-artifacts]# ../../bin/cryptogen generate --config=./org3-crypto.yaml
org3.example.com
3.使用configtxgen工具将密钥导出到一个json文件中
[root@huangzhenhua org3-artifacts]# export FABRIC_CFG_PATH=$PWD && ../../bin/configtxgen -printOrg Org3MSP > ../channel-artifacts/org3.json
2018-03-30 15:50:57.522 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-03-30 15:50:57.569 CST [msp] getMspConfig -> INFO 002 Loading NodeOUs
4.复制共识服务节点密钥到org3密钥存放目录
huangzhenhua org3-artifacts]# cd ../ && cp -r crypto-config/ordererOrganizations org3-artifacts/crypto-config/
5.配置CLI docker容器环境
5.1.进入CLI容器
[root@huangzhenhua first-network]# docker exec -it cli bash
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# echo $ORDERER_CA && echo $CHANNEL_NAME
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
mychannel
5.2.在CLI容器安装jq工具
root@9e2f31f76519:/opt/gopath/src/github.com/hyperledger/fabric/peer# apt update && apt install -y jq
6.生成相应配置文件
6.1.获取orderer的block配置文件
root@d995c5e2873d:/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
查看是否生成config_block.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config_block.pb crypto log.txt mychannel.block scripts
6.2.通过configtxlator工具将config_block.pb配置文件转换为json配置文件
root@d995c5e2873d:/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
查看是否生成config.json配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.json config_block.pb crypto log.txt mychannel.block scripts
6.3.使用jq工具在org3.json配置文件中添加部分信息,并重新生成modified_config.json配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ./channel-artifacts/org3.json > modified_config.json
查看是否生成modified_config.json配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.json config_block.pb crypto log.txt modified_config.json mychannel.block scripts
6.4.通过configtxlator工具将config.json配置文件转换为config.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator proto_encode --input config.json --type common.Config --output config.pb
查看是否生成config.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.json config.pb config_block.pb crypto log.txt modified_config.json mychannel.block scripts
6.5.通过configtxlator工具将modified_config.json配置文件转换为modified_config.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator proto_encode --input modified_config.json --type common.Config --output modified_config.pb
查看是否生成config.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.json config.pb config_block.pb crypto log.txt modified_config.json modified_config.pb mychannel.block scripts
6.6.通过configtxlator工具计算config.pb、modified_config.pb配置增量变化,并生成org3_update.pb文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator compute_update --channel_id $CHANNEL_NAME --original config.pb --updated modified_config.pb --output org3_update.pb
查看是否生成org3_update.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.json config.pb config_block.pb crypto log.txt modified_config.json modified_config.pb mychannel.block org3_update.pb scripts
6.7.通过configtxlator工具将org3_update.pb配置文件转换为org3_update.json配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator proto_decode --input org3_update.pb --type common.ConfigUpdate | jq . > org3_update.json
查看是否生成org3_update.json配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.pb crypto modified_config.json mychannel.block org3_update.pb
config.json config_block.pb log.txt modified_config.pb org3_update.json scripts
6.8.在org3_update.json中添加部分信息,并生成新的配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":'$(cat org3_update.json)'}}}' | jq . > org3_update_in_envelope.json
查看是否生成org3_update_in_envelope.json配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.pb crypto modified_config.json mychannel.block org3_update.pb scripts
config.json config_block.pb log.txt modified_config.pb org3_update.json org3_update_in_envelope.json
6.9.通过configtxlator工具将org3_update_in_envelope.json配置文件转换为org3_update_in_envelope.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# configtxlator proto_encode --input org3_update_in_envelope.json --type common.Envelope --output org3_update_in_envelope.pb
查看是否生成org3_update_in_envelope.pb配置文件
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts config.pb crypto modified_config.json mychannel.block org3_update.pb org3_update_in_envelope.pb
config.json config_block.pb log.txt modified_config.pb org3_update.json org3_update_in_envelope.json scripts
7.更新org1和org2的配置信息
7.1.cli容器默认进入的是org1环境,更新org1的加签配置
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel signconfigtx -f org3_update_in_envelope.pb
7.2.切换到org2环境
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# export CORE_PEER_LOCALMSPID="Org2MSP"
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
7.3.更新org2的加签配置
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel update -f org3_update_in_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
8.org3正式加入区块链联盟
8.1.启动org3节点
[root@huangzhenhua first-network]# docker-compose -f docker-compose-org3.yaml up -d
8.2.进入到Org3cli容器,默认进入peer0节点
[root@huangzhenhua first-network]# docker exec -it Org3cli bash
8.3.设置环境变量
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# echo $ORDERER_CA && echo $CHANNEL_NAME
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
mychannel
8.4.获取orderer的mychannel配置文件
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel fetch 0 mychannel.block -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
8.5.org3的peer0节点加入到mychannel
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel join -b mychannel.block
8.6.切换到org3的peer1节点
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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
8.7.org3的peer1节点加入到mychannel
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel join -b mychannel.block
8.8.安装mycc智能合约
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/
9.切换到cli容器进行操作
9.1.重新进入到cli容器,安装mycc智能合约
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/
9.2.切换org1的peer0节点
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# export CORE_PEER_LOCALMSPID="Org1MSP"
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
9.3.在org1的peer0节点,安装mycc智能合约
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/
9.4.初始化a、b账户
root@d995c5e2873d:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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')"
10.切换到Org3cli容器进行操作
10.1.在org3的容器中查看a的账户是否正确
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
10.2.在org3的容器进行a、b转账操作
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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"]}'
10.3.查看转账后的a账户
root@770a5ef2b808:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'