总体大纲:
1为每个节点生成证书文件
2生成系统通道的创世区块
3生成通道配置信息
4生成锚节点配置信息
5启动节点
6根据通道配置文件创建通道生成应用通道创世区块
7每个节点加入通道
8为组织更新锚节点
9指定节点安装链码
10指定节点实例化链码
具体实现:
1 生成数字证书和密钥
cd /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network
../bin/cryptogen generate --config=./crypto-config.yaml
crypto-config.yaml 文件定义orderer组织 和Peer节点配置 生成的oederer组织个数 组织内的节点个数
2生成创世区块
../bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
configtx.yaml 文件定义通道配置信息、组织 peer节点的锚节点 多个可选的共识机制
3生成通道配置文件
../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
docker-compose -f docker-compose-cli.yaml up -d
执行docker ps,命令查看当前创建的镜像,如果orderer节点未启动 执行以下命令
docker-compose -f docker-compose-cli.yaml down --volumes --remove-orphans
docker rm -f $(docker ps -a | grep "hyperledger/*" | awk "{print \$1}")
docker volume prune
6进入cli后台操作
docker exec -it cli bash
7创建通道
peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
8 通道配置信息拷贝一份到 channel-artifacts文件下 cp mychannel.block ./channel-artifacts
。
此时网络中共有两个组织,每个组织下有两个节点
peer0.org1
peer1.org1
peer0.org2
peer1.org2
执行env | grep CORE
查看当前节点信息
peer0.org1加入通道
peer channel join -b channel-artifacts/mychannel.block
执行source scripts/utils.sh setGlobals 1 1
切换到peer1.org1,加入通道peer channel join -b channel-artifacts/mychannel.block
执行source scripts/utils.sh setGlobals 0 2
切换到peer0.org2,加入通道peer channel join -b channel-artifacts/mychannel.block
执行source scripts/utils.sh setGlobals 1 2
切换到peer1.org2,加入通道peer channel join -b channel-artifacts/mychannel.block
此时4个节点均加入通道
9更新锚节点
执行setGlobals 0 1
切换到peer0.org1
更新通道
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
执行setGlobals 0 2
切换到peer0.org2
更新通道
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
10 安装链码
执行source scripts/utils.sh setGlobals 0 1
切换到peer0.org1
安装链码
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
执行source scripts/utils.sh setGlobals 0 2
切换到peer0.org2
安装链码
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
执行docker images查看镜像
11 初始化链码
在org2.peer0上初始化链码并指定背书策略 需要两个组织同时背书才能完成交易
peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
在org1.peer0上查询验证
执行setGlobals 0 1
切换到peer0.org1验证
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
12 交易验证
根据背书策略需要两个组织共同背书才能完成交易
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
13 加入新的节点。在新节点上安装链码
在peer1.org2上安装链码
执行source scripts/utils.sh setGlobals 1 2
切换到peer1.org2
安装链码
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
peer channal list
peer chaincode list --installed
peer channel getinfo -c mychannel
peer channel fetch newest -c mychannel -o orderer.example.com
可以看到mychannel通道中共生成了5个区块(创世区块序号为0)
配置区块0:创世区块
配置区块1:组织一的锚节点更新
配置区块2:组织二的锚节点更新
普通区块3:实例化链码
普通区块4:调用链码