Hyperledger Fabric学习笔记——学习byfn.sh脚本并手动搭建Fabric网络

本文主要通过学习官方提供的first-network案例中的byfn.sh脚本,手动搭建一下Fabric网络,了解搭建一个简单区块链网络的全过程。

1、销毁已有网络

如果之前运行过first-network,则首先销毁已有网络,并删除所有docker容器和各种已经创建的组件。

./byfn.sh down
docker rm -f $(docker ps -aq)
docker rmi -f $(docker images | grep fabcar | awk '{print $3}')

2、导入基本的环境变量

先导入两个环境变量:

  • 二进制文件所在目录,即configtxgen等工具所在目录
  • configtx.yaml文件所在目录,用于告知configtxgen工具
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=${PWD}

3、版本检查

byfn.sh中在checkPrereqs函数中还对docker容器和镜像等进行了版本检查,本文之前已经用正确的版本进行了配置,故此处略去版本检查。

4、为网络中的实体生成证书及密钥

使用cryptogen工具根据配置文件crypto-config.yaml为网络中的实体生成证书和密钥,放于crypto-config目录下,byfn.sh脚本中在完成这一步后,还调用了同一目录下的ccp-generate.sh脚本为Org1和Org2生成了CCP文件(供后器SDK使用),在此先忽略。

cryptogen generate --config=./crypto-config.yaml

5、生成四个配置组件

使用configtxgen工具借助configtx.yaml生成四个配置组件:genesis.block,channel.tx,Org1MSPanchors.tx,Org2MSPanchors.tx,并放置于channel-artifacts下。

configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
export CHANNEL_NAME=mychannel
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

6、启动docker容器(使用CAs与couchdb)

使用docker-compose基于yaml配置文件启动容器,建立Fabric网络。首先以环境变量的形式指定每个组织的CA的私钥,CA容器的yaml配置文件中会使用该变,在docker-compose命令中将参数设置为-d是不显示实时日志流。执行完后,可以使用docker ps命令查看已启动容器基本信息,包括一个cli,四个peer,四个couchdb,五个order和两个ca。最后一个命令可以用来查看网络信息。

export BYFN_CA1_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/org1.example.com/ca && ls *_sk)
export BYFN_CA2_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/org2.example.com/ca && ls *_sk)
docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml -f docker-compose-couch.yaml up -d

7、创建通道

byfn.sh脚本文件调用cli容器中scripts/script.sh脚本创建通道,将peer节点加入通道,并更新组织的锚节点。首先给出peer0.org1.example.com和peer0.org2.example.com的环境变量,后面需要不断地切换环境变量,以作为不同的peer节点执行相关操作。

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

 

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:9051

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

首先进入CLI容器终端,并以环境变量形式指定通道名称,然后使用peer channel create命令,将通道配置transaction发送给order节点以创建通道,若通道创建成功,将接收到来自order节点的通道的创世区块,以通道名称命名。

docker exec -it cli bash
export CHANNEL_NAME=mychannel
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -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、将节点加入通道

切换peer节点的环境变量,多次执行以下命令,将节点加入通道(这里仅加入了每个组织各一个节点,案例中将4个节点全部加入):

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 join -b $CHANNEL_NAME.block

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:8051
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 $CHANNEL_NAME.block

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:9051
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 $CHANNEL_NAME.block

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:10051
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 $CHANNEL_NAME.block

9、为每个组织指定其在当前通道中的锚节点

切换peer节点的环境变量,多次执行以下命令,为每个组织指定其在当前通道中的锚节点,每个组织至少有一个锚节点。

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/${CORE_PEER_LOCALMSPID}anchors.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

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:9051
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/${CORE_PEER_LOCALMSPID}anchors.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、运行成功截图如下:

 

你可能感兴趣的:(Hyperledger,Fabric)