你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

Fabric v2.3 手动模拟(三)- 创建通道./network.sh createChannel命令 - 设置锚节点

2021-11-23 7:19:22

目录

1. 前提条件

(1)注释设置锚节点命令

(2)关闭网络

(3)开启网络

(4)创建通道

2. 设置org1中的锚节点

(1)进入client客户端

(2)设置环境变量

(3)获取通道配置

(4)将配置块protobuf格式转成json格式

(5)将锚节点添加至配置文件中

(6)设置环境变量

(7)将原始和修改的通道配置都转换回protobuf格式

(8)将配置更新包装在交易Envelope中

(9)更新通道配置添加Peer 锚节点

3. 设置org2中的锚节点

4. 将注释锚节点的命令解开


在上一篇创建通道中,创建通道最后一步是设置锚节点,是通过执行

docker exec cli ./scripts/setAnchorPeer.sh 1 mychannel 完成对组织org1中的锚节点设置,这里调用./scripts/setAnchorPeer.sh脚本文件,下面对将模拟脚本中的内容进行锚节点设置。

1. 前提条件

注释掉设置锚节点命令,关闭网络,重新开启网络并创建通道。

(1)注释设置锚节点命令

[root@localhost test-network-myself]# vi network-myself.sh

回车,按键盘上的 i 键或 Insert 功能键,进入编辑状态,注释设置锚节点,只显示如注释部分,内容如下:

	# 将org2节点加入通道
	peer channel join -b $BLOCKFILE
 
	# 设置组织org1的锚节点,docker exec命令中使用setAnchorPeer.sh脚本返回的信息作为参数部分
	#docker exec cli ./scripts/setAnchorPeer.sh 1 mychannel
 
	# 设置组织org2的锚节点,docker exec命令中使用setAnchorPeer.sh脚本返回的信息作为参数部分
	#docker exec cli ./scripts/setAnchorPeer.sh 2 mychannel

按ESC,输入:wq保存退出

(2)关闭网络

[root@localhost test-network-myself]# ./network-myself.sh down
关闭自己的测试网络
+ export DOCKER_SOCK=/var/run/docker.sock
+ DOCKER_SOCK=/var/run/docker.sock
+ docker-compose -f docker/docker-compose-test-net.yaml down --volumes --remove-orphans
Stopping cli                    ... done
Stopping peer0.org2.example.com ... done
Stopping orderer.example.com    ... done
Stopping peer0.org1.example.com ... done
Removing cli                    ... done
Removing peer0.org2.example.com ... done
Removing orderer.example.com    ... done
Removing peer0.org1.example.com ... done
Removing network fabric_test
Removing volume docker_orderer.example.com
Removing volume docker_peer0.org1.example.com
Removing volume docker_peer0.org2.example.com
+ rm -rf ./organizations/ordererOrganizations ./organizations/peerOrganizations
+ rm -rf ./system-genesis-block/genesis.block
+ set +x

(3)开启网络

[root@localhost test-network-myself]# ./network-myself.sh up
开启自己的测试网络
+ export DOCKER_SOCK=/var/run/docker.sock
+ DOCKER_SOCK=/var/run/docker.sock
+ docker-compose -f docker/docker-compose-test-net.yaml down --volumes --remove-orphans
Removing network fabric_test
WARNING: Network fabric_test not found.
Removing volume docker_orderer.example.com
WARNING: Volume docker_orderer.example.com not found.
Removing volume docker_peer0.org1.example.com
WARNING: Volume docker_peer0.org1.example.com not found.
Removing volume docker_peer0.org2.example.com
WARNING: Volume docker_peer0.org2.example.com not found.
+ rm -rf './organizations/*Organizations'
+ rm -rf './system-genesis-block/*.block'
+ set +x
+ cryptogen generate --config=./crypto-config.yaml --output=organizations
org1.example.com
org2.example.com
+ ./organizations/ccp-generate.sh
+ export DOCKER_SOCK=/var/run/docker.sock
+ DOCKER_SOCK=/var/run/docker.sock
+ docker-compose -f docker/docker-compose-test-net.yaml up -d
Creating network "fabric_test" with the default driver
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
Creating orderer.example.com    ... done
Creating peer0.org2.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli                    ... done
+ set +x

(4)创建通道

此时创建通道中没有设置锚节点

[root@localhost test-network-myself]# ./network-myself.sh createChannel
创建通道
2021-11-22 18:55:14.649 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-11-22 18:55:14.655 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: etcdraft
2021-11-22 18:55:14.655 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 Orderer.EtcdRaft.Options unset, setting to tick_interval:"500ms" election_tick:10 heartbeat_tick:1 max_inflight_blocks:5 snapshot_interval_size:16777216 
2021-11-22 18:55:14.655 CST [common.tools.configtxgen.localconfig] Load -> INFO 004 Loaded configuration: /home/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network-myself/configtx/configtx.yaml
2021-11-22 18:55:14.657 CST [common.tools.configtxgen] doOutputBlock -> INFO 005 Generating genesis block
2021-11-22 18:55:14.657 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Creating application channel genesis block
2021-11-22 18:55:14.658 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
Status: 201
{
	"name": "mychannel",
	"url": "/participation/v1/channels/mychannel",
	"consensusRelation": "consenter",
	"status": "active",
	"height": 1
}

2021-11-22 18:55:20.760 CST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-22 18:55:20.774 CST [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2021-11-22 18:55:23.810 CST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-22 18:55:23.824 CST [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

2. 设置org1中的锚节点

(1)进入client客户端

[root@localhost test-network-myself]# docker exec -it cli /bin/bash  #进入客户端
bash-5.1# ls  #查看客户端目录
organizations  scripts

(2)设置环境变量

bash-5.1# export ORG=1    #传递的参数
bash-5.1# export CHANNEL_NAME=mychannel   #传递的参数
bash-5.1# export CORE_PEER_TLS_ENABLED=true
bash-5.1# export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
bash-5.1# export PEER0_ORG1_CA=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
bash-5.1# export PEER0_ORG2_CA=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
bash-5.1# export PEER0_ORG3_CA=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
bash-5.1# export ORDERER_ADMIN_TLS_SIGN_CERT=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
bash-5.1# export ORDERER_ADMIN_TLS_PRIVATE_KEY=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.key
bash-5.1# export CORE_PEER_LOCALMSPID="Org1MSP"
bash-5.1# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
bash-5.1# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
bash-5.1# export CORE_PEER_ADDRESS=localhost:7051
bash-5.1# export OUTPUT=${CORE_PEER_LOCALMSPID}config.json
bash-5.1# export HOST="peer0.org1.example.com"
bash-5.1# export PORT=7051

(3)获取通道配置

bash-5.1# peer channel fetch config config_block.pb -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile "$ORDERER_CA"
2021-11-22 10:57:38.039 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-22 10:57:38.040 UTC [cli.common] readBlock -> INFO 002 Received block: 0
2021-11-22 10:57:38.041 UTC [channelCmd] fetch -> INFO 003 Retrieving last config block: 0
2021-11-22 10:57:38.041 UTC [cli.common] readBlock -> INFO 004 Received block: 0

由于最新的通道配置块是通道创世块,因此该通道的命令返回块0

(4)将配置块protobuf格式转成json格式

bash-5.1# configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config >"${OUTPUT}"
bash-5.1# ls
Org1MSPconfig.json  config_block.pb     organizations       scripts

(5)将锚节点添加至配置文件中

bash-5.1# jq '.channel_group.groups.Application.groups.'${CORE_PEER_LOCALMSPID}'.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "'$HOST'","port": '$PORT'}]},"version": "0"}}' ${CORE_PEER_LOCALMSPID}config.json > ${CORE_PEER_LOCALMSPID}modified_config.json
bash-5.1# ls
Org1MSPconfig.json           organizations
Org1MSPmodified_config.json  scripts
config_block.pb

(6)设置环境变量

bash-5.1# export CHANNEL=mychannel
bash-5.1# export ORIGINAL=${CORE_PEER_LOCALMSPID}config.json
bash-5.1# export MODIFIED=${CORE_PEER_LOCALMSPID}modified_config.json
bash-5.1# export OUTPUT=${CORE_PEER_LOCALMSPID}anchors.tx

(7)将原始和修改的通道配置都转换回protobuf格式

并计算它们之间的差异

bash-5.1# configtxlator proto_encode --input "${ORIGINAL}" --type common.Config >original_config.pb
onfig.pb --updated modified_config.pb >config_update.pbbash-5.1# configtxlator proto_encode --input "${MODIFIED}" --type common.Config >modified_config.pb
bash-5.1# configtxlator compute_update --channel_id "${CHANNEL}" --original original_config.pb --updated modified_config.pb >config_update.pb
bash-5.1# ls
Org1MSPconfig.json           modified_config.pb
Org1MSPmodified_config.json  organizations
config_block.pb              original_config.pb
config_update.pb             scripts

(8)将配置更新包装在交易Envelope中

为了创建通道配置更新交易,将配置更新包装在交易Envelope中

bash-5.1# configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate >config_update.json
bash-5.1# configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope >"${OUTPUT}"
configtxlator: error: open config_update_in_envelope.json: no such file or directory, try --help
bash-5.1# ls
Org1MSPanchors.tx            config_update.pb
Org1MSPconfig.json           modified_config.pb
Org1MSPmodified_config.json  organizations
config_block.pb              original_config.pb
config_update.json           scripts

(9)更新通道配置添加Peer 锚节点

bash-5.1# peer channel update -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c $CHANNEL_NAME -f ${CORE_PEER_LOCALMSPID}anchors.tx --tls --cafile "$ORDERER_CA" >&log.txt

3. 设置org2中的锚节点

设置org2使用的环境变量,执行步骤与设置org1中的锚节点一样,此处不再赘述。

4. 将注释锚节点的命令解开

[root@localhost test-network-myself]# vi network-myself.sh

回车,按键盘上的 i 键或 Insert 功能键,进入编辑状态,注释设置锚节点,只显示如注释部分,内容如下:

	# 将org2节点加入通道
	peer channel join -b $BLOCKFILE
 
	# 设置组织org1的锚节点,docker exec命令中使用setAnchorPeer.sh脚本返回的信息作为参数部分
	docker exec cli ./scripts/setAnchorPeer.sh 1 mychannel
 
	# 设置组织org2的锚节点,docker exec命令中使用setAnchorPeer.sh脚本返回的信息作为参数部分
	docker exec cli ./scripts/setAnchorPeer.sh 2 mychannel

按ESC,输入:wq保存退出

上一篇:Fabric v2.3 手动模拟(二)- 创建通道./network.sh createChannel命令