参考链接:https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html
在上一篇文章通过 ./byfn.sh up
脚本自动执行了first-network,这里博主将参考byfn.sh
脚本里的内容手动部署first-network,这也有助于我们更好地理解Fabric网络的部署流程。
该实例中包括一个命令行客户端,一个orderer排序节点,两个组织(Org1和Org2),其中每个组织包含两个peer节点。
进入first-samples
目录,若之前运行过first-network,将网络关闭
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network
./byfn.sh down
检查bin
目录内是否存在所需的工具:
ls ../bin
若出现如下结果,则无问题:
configtxgen cryptogen fabric-ca-client idemixgen peer
configtxlator discover get-docker-images.sh orderer
使用cryptogen
工具,根据crypto-config.yaml
配置文件生成证书:
../bin/cryptogen generate --config=./crypto-config.yaml
执行成功后会出现如下的结果
org1.example.com
org2.example.com
同时当前目录中会生成crypto-config
文件夹,文件夹中包括ordererOrganizations
和peerOrganizations
两个文件夹,其中包含了排序服务组织机构的证书和Peer节点的证书。
生成排序服务创世区块
export FABRIC_CFG_PATH=$PWD
../bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
命令执行结果:
2019-04-18 20:04:37.630 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-04-18 20:04:37.658 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-04-18 20:04:37.658 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/dwm/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-04-18 20:04:37.683 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-04-18 20:04:37.683 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/dwm/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-04-18 20:04:37.685 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-04-18 20:04:37.685 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
成功后将会在当前目录中生成channel-artifacts
文件夹,并在该文件夹中生成排序服务创世区块genesis.block
。
export CHANNEL_NAME=mychannel
../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
命令执行结果:
2019-04-18 20:05:07.770 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-04-18 20:05:07.793 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/dwm/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-04-18 20:05:07.822 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-04-18 20:05:07.822 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/dwm/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-04-18 20:05:07.822 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-04-18 20:05:07.823 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
执行成功后将在channel-artifacts
文件夹中生成channel.tx
文件。
定义通道中Org1的锚节点
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
定义通道中Org2的锚节点
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
docker-compose -f docker-compose-cli.yaml up -d
命令执行完成后可执行:
docker ps -a
可以查看到如下内容:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fdacf7eb4ba1 hyperledger/fabric-tools:latest "/bin/bash" About a minute ago Up About a minute cli
5a8fde50d459 hyperledger/fabric-peer:latest "peer node start" 2 minutes ago Up About a minute 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.com
533657f47039 hyperledger/fabric-peer:latest "peer node start" 2 minutes ago Up About a minute 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com
caad6791c3a1 hyperledger/fabric-peer:latest "peer node start" 2 minutes ago Up About a minute 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com
43544e11c0d6 hyperledger/fabric-orderer:latest "orderer" 2 minutes ago Up About a minute 0.0.0.0:7050->7050/tcp orderer.example.com
70c2f36aaa91 hyperledger/fabric-peer:latest "peer node start" 2 minutes ago Up About a minute 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
我们首先进入容器:
docker exec -it cli bash
此时CLI中对应的为peer0.org1.example.com
的环境变量,查看docker-compose-cli.yaml
文件,可以看到如下环境变量的值:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
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 CHANNEL_NAME=mychannel
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
当出现如下结果时,通道创建成功:
2019-04-18 12:06:51.196 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-04-18 12:06:51.282 UTC [cli.common] readBlock -> INFO 002 Received block: 0
此时将能在容器的当前目录中得到该通道的创世区块mychannel.block
。
节点peer0.org1.example.com
加入通道
由docker-compose-cli.yaml
配置文件知CLI启动时对应为peer0.org1.example.com
的环境变量,故只需执行
peer channel join -b mychannel.block
便可将节点peer0.org1.example.com
加入通道中。
节点peer1.org1.example.com
加入通道
在命令行中输入如下命令修改CLI中的环境变量,使之对应节点peer1.org1.example.com
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt
接着执行
peer channel join -b mychannel.block
将节点peer1.org1.example.com
加入通道中。
节点peer0.org2.example.com
加入通道
在命令行中输入如下命令修改CLI中的环境变量,使之对应节点peer0.org2.example.com
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/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
接着执行
peer channel join -b mychannel.block
将节点peer0.org2.example.com
加入通道中。
节点peer1.org2.example.com
加入通道
在命令行中输入如下命令修改CLI中的环境变量,使之对应节点peer1.org2.example.com
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer1.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/peer1.org2.example.com/tls/ca.crt
接着执行
peer channel join -b mychannel.block
将节点peer1.org2.example.com
加入通道中。
至此实例中的4个peer节点均加入了通道中。
定义org1的锚节点
设置环境变量:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
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
更新锚节点:
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
定义org2的锚节点
设置环境变量:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/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
更新锚节点:
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
应用需要通过链码来和账本进行交互,因此应在每个需要执行和背书交易的peer节点上安装链码。该实例中安装的链码为fabric-samples/chaincode/chaincode_example02/go
目录下的chaincode_example02.go
在节点peer0.org1.example.com
上安装链码
设置环境变量:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
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
安装链码:
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
在节点peer1.org1.example.com
上安装链码
设置环境变量:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt
安装链码:
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
在节点peer0.org2.example.com
上安装链码
设置环境变量:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/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
安装链码:
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
在节点peer1.org2.example.com
上安装链码
设置环境变量:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer1.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/peer1.org2.example.com/tls/ca.crt
安装链码:
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
peer chaincode instantiate -o orderer.example.com:7050 --tls --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 $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
我们在实例化链码时也进行了初始化,即将a
赋值为100
,b
赋值为200
,可以通过下列命令对a
的值进行查询:
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
此时得到的结果为:
100
将命令后面的参数由a
改为b
,可以对b
的值进行查询。
我们可以通过调用如下命令,将a
的值转移10
给b
:
peer chaincode invoke -o orderer.example.com:7050 --tls --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 $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'
结果为:
2019-04-18 12:11:37.781 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
此时再对a
进行查询:
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
得到的结果为:
90
即链码调用成功。
peer0.org1.example.com
中查询链码上述查询和调用链码均是在节点peer1.org2.example.com
中进行的,我们将CLI中环境变量设置为与节点peer0.org1.example.com
对应,然后再该进行中进行查询:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
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
执行查询命令
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
得到结果为:
90
则first-network手动部署完成。