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

本文是模拟是脚本createChannel.sh来创建通道,调用脚本createChannel.sh的参数为默认参数:

脚本调用:script/createChannel.sh "mychannel" 3 5 false

参数中的mychannel为通道名称

目录

1. 前提条件

(1)开启自己的测试网络

(2)拷贝配置文件

(3)创建目录channel-artifacts

2. 设置环境变量

3. 生成通道创世区块mychannel.block文件

4. 设置环境变量org1 并重置FABRIC_CFG_PATH

5. 创建通道

6. 将组织中peer节点加入通道

(1)将org1的peer节点加入通道

设置环境变量为org1

将节点加入通道

(2)将org2的peer节点加入通道

设置环境变量为org2

将节点加入通道

7. 设置锚节点

(1)设置org1中的锚节点

(2)设置org2中的锚节点

8. 生成创建通道脚本

(1)前提条件

(2)编辑脚本文件network-myself.sh

(3)测试 - 创建通道


1. 前提条件

(1)开启自己的测试网络

本文是接上篇文章启动好自己的测试网络条件下运行的,命令如下:

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

(2)拷贝配置文件

以下文件是配置文件及相关脚本文件:

[root@localhost test-network-myself]# mkdir configtx  #创建配置文件目录
[root@localhost test-network-myself]# cp ../test-network/configtx/configtx.yaml ./configtx/ #拷贝测试网络中的配置文件,用于创建通道创世区块使用
[root@localhost test-network-myself]# cp ../test-network/scripts/setAnchorPeer.sh ./scripts/  #拷贝测试网络中的配置文件,用于创建通道时设置锚节点使用
[root@localhost test-network-myself]# cp ../test-network/scripts/envVar.sh ./scripts/
[root@localhost test-network-myself]# cp ../test-network/scripts/configUpdate.sh ./scripts/
[root@localhost test-network-myself]# cp ../test-network/scripts/utils.sh ./scripts/

configtx.yaml文件指定新通道的通道配置,configtxgen工具使用configtx.yaml文件中定义的通道配置来创建通道配置,并将其写入protobuf格式,然后由Fabric读取。同时也会为系统通道创建完整的创世块。

setAnchorPeer.sh 设置锚节点脚本,在设置节点时使用。

envVar.sh、configUpdate.sh、utils.sh被setAnchorPeer.sh调用,因些一起拷贝过来。 

(3)创建目录channel-artifacts

[root@localhost test-network-myself]# mkdir channel-artifacts

2. 设置环境变量

设置排序节点的公钥、私钥、证书位置,设置peer节点证书位置

[root@localhost test-network-myself]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network-myself]# export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
[root@localhost test-network-myself]# export PEER0_ORG1_CA=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
[root@localhost test-network-myself]# export PEER0_ORG2_CA=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
[root@localhost test-network-myself]# export PEER0_ORG3_CA=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
[root@localhost test-network-myself]# export ORDERER_ADMIN_TLS_SIGN_CERT=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
[root@localhost test-network-myself]# export ORDERER_ADMIN_TLS_PRIVATE_KEY=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.key
[root@localhost test-network-myself]# export FABRIC_CFG_PATH=${PWD}/configtx  #configtx.yaml文件所在的目录
[root@localhost test-network-myself]# export BLOCKFILE="./channel-artifacts/mychannel.block"

FABRIC_CFG_PATH:configtx.yaml文件所在的目录 

BLOCKFILE:区块链文件位置

3. 生成通道创世区块mychannel.block文件

使用configtxgen工具通过配置文件./configtx/configtx.yaml来创建Orderer系统通道的创世块

[root@localhost test-network-myself]# configtxgen -profile TwoOrgsApplicationGenesis -outputBlock ./channel-artifacts/mychannel.block -channelID mychannel
2021-11-18 01:05:58.831 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-11-18 01:05:58.835 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: etcdraft
2021-11-18 01:05:58.835 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-18 01:05:58.835 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-18 01:05:58.837 CST [common.tools.configtxgen] doOutputBlock -> INFO 005 Generating genesis block
2021-11-18 01:05:58.837 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Creating application channel genesis block
2021-11-18 01:05:58.837 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block

4. 设置环境变量org1 并重置FABRIC_CFG_PATH

[root@localhost test-network-myself]# export FABRIC_CFG_PATH=$PWD/../config/
[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:7051

5. 创建通道

osnamdin channel命令与排序节点进行通信时需要使用TLS证书与私钥,在Fabric v2.3版本中创建通道时,直接创建应用通道,不再创建由排序服务管理的系统通道了。

[root@localhost test-network-myself]# osnadmin channel join --channelID mychannel --config-block ./channel-artifacts/mychannel.block -o localhost:7053 --ca-file "$ORDERER_CA" --client-cert "$ORDERER_ADMIN_TLS_SIGN_CERT" --client-key "$ORDERER_ADMIN_TLS_PRIVATE_KEY" 
Status: 201
{
	"name": "mychannel",
	"url": "/participation/v1/channels/mychannel",
	"consensusRelation": "consenter",
	"status": "active",
	"height": 1
}

6. 将组织中peer节点加入通道

(1)将org1的peer节点加入通道

设置环境变量为org1

此处调用envVar.sh脚本中的函数及参数 setGlobals 1,得到以下脚本

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:7051

将节点加入通道

[root@localhost test-network-myself]# peer channel join -b $BLOCKFILE
2021-11-18 01:19:10.301 CST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 01:19:10.467 CST [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

(2)将org2的peer节点加入通道

设置环境变量为org2

此处调用envVar.sh脚本中的函数及参数 setGlobals 2,得到以下脚本

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org2MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:9051

将节点加入通道

[root@localhost test-network-myself]# peer channel join -b $BLOCKFILE
2021-11-18 01:20:51.975 CST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 01:20:52.069 CST [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

7. 设置锚节点

组织的peer节点加入通道后,应至少选择一个peer节点成为锚节点。当前有2个组织,每个组织只有一个peer节点,因此为这2个peer节点设定锚节点。

(1)设置org1中的锚节点

docker exec命令中使用setAnchorPeer.sh脚本返回的信息作为参数部分,关于setAnchorPeer.sh脚本中执行的内容,将另起一篇介绍。

[root@localhost test-network-myself]# docker exec cli ./scripts/setAnchorPeer.sh 1 mychannel
+ peer channel fetch config config_block.pb -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Using organization 1
Fetching channel config for channel mychannel
Using organization 1
Fetching the most recent configuration block for the channel
2021-11-17 17:27:44.145 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-17 17:27:44.152 UTC [cli.common] readBlock -> INFO 002 Received block: 0
2021-11-17 17:27:44.153 UTC [channelCmd] fetch -> INFO 003 Retrieving last config block: 0
2021-11-17 17:27:44.153 UTC [cli.common] readBlock -> INFO 004 Received block: 0
+ configtxlator proto_decode --input config_block.pb --type common.Block
Decoding config block to JSON and isolating config to Org1MSPconfig.json
+ jq '.data.data[0].payload.data.config'
+ jq '.channel_group.groups.Application.groups.Org1MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org1.example.com","port": 7051}]},"version": "0"}}' Org1MSPconfig.json
Generating anchor peer update transaction for Org1 on channel mychannel
+ configtxlator proto_encode --input Org1MSPconfig.json --type common.Config
+ configtxlator proto_encode --input Org1MSPmodified_config.json --type common.Config
+ configtxlator compute_update --channel_id mychannel --original original_config.pb --updated modified_config.pb
+ configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate
+ jq .
++ cat config_update.json
+ echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":{' '"channel_id":' '"mychannel",' '"isolated_data":' '{},' '"read_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org1MSP":' '{' '"groups":' '{},' '"mod_policy":' '"",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '},' '"write_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org1MSP":' '{' '"groups":' '{},' '"mod_policy":' '"Admins",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"AnchorPeers":' '{' '"mod_policy":' '"Admins",' '"value":' '{' '"anchor_peers":' '[' '{' '"host":' '"peer0.org1.example.com",' '"port":' 7051 '}' ']' '},' '"version":' '"0"' '},' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"1"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '}}}}'
+ configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope
2021-11-17 17:27:44.416 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-17 17:27:44.426 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
Anchor peer set for org 'Org1MSP' on channel 'mychannel'

(2)设置org2中的锚节点

[root@localhost test-network-myself]# docker exec cli ./scripts/setAnchorPeer.sh 2 mychannel
+ peer channel fetch config config_block.pb -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Using organization 2
Fetching channel config for channel mychannel
Using organization 2
Fetching the most recent configuration block for the channel
2021-11-17 17:29:00.439 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-17 17:29:00.441 UTC [cli.common] readBlock -> INFO 002 Received block: 1
2021-11-17 17:29:00.441 UTC [channelCmd] fetch -> INFO 003 Retrieving last config block: 1
2021-11-17 17:29:00.442 UTC [cli.common] readBlock -> INFO 004 Received block: 1
Decoding config block to JSON and isolating config to Org2MSPconfig.json
+ configtxlator proto_decode --input config_block.pb --type common.Block
+ jq '.data.data[0].payload.data.config'
Generating anchor peer update transaction for Org2 on channel mychannel
+ jq '.channel_group.groups.Application.groups.Org2MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org2.example.com","port": 9051}]},"version": "0"}}' Org2MSPconfig.json
+ configtxlator proto_encode --input Org2MSPconfig.json --type common.Config
+ configtxlator proto_encode --input Org2MSPmodified_config.json --type common.Config
+ configtxlator compute_update --channel_id mychannel --original original_config.pb --updated modified_config.pb
+ configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate
+ jq .
++ cat config_update.json
+ echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":{' '"channel_id":' '"mychannel",' '"isolated_data":' '{},' '"read_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org2MSP":' '{' '"groups":' '{},' '"mod_policy":' '"",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '},' '"write_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org2MSP":' '{' '"groups":' '{},' '"mod_policy":' '"Admins",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"AnchorPeers":' '{' '"mod_policy":' '"Admins",' '"value":' '{' '"anchor_peers":' '[' '{' '"host":' '"peer0.org2.example.com",' '"port":' 9051 '}' ']' '},' '"version":' '"0"' '},' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"1"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '}}}}'
+ configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope
2021-11-17 17:29:00.665 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-17 17:29:00.676 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
Anchor peer set for org 'Org2MSP' on channel 'mychannel'

8. 生成创建通道脚本

(1)前提条件

保留本文步骤1中前提条件中生成的文件,当前目录存在的文件如所示:

[root@localhost test-network-myself]# tree 
.
├── channel-artifacts
│   └── mychannel.block
├── configtx
│   └── configtx.yaml        #本次新增文件:创建通道创世块使用
├── crypto-config.yaml
├── docker
│   └── docker-compose-test-net.yaml
├── network-myself.sh       #本次将做修改的文件:增加创建通道功能
├── organizations
│   ├── ccp-generate.sh
│   ├── ccp-template.json
│   └── ccp-template.yaml
├── scripts
│   ├── configUpdate.sh    #本次新增文件:被创建锚节点调用的脚本文件
│   ├── envVar.sh          #本次新增文件:设置环境变量、切换组织函数、验证执行命令结果函数等
│   ├── setAnchorPeer.sh   #本次新增文件:创建锚节点脚本文件
│   └── utils.sh           本次新增文件: 打印输出信息脚本文件
└── system-genesis-block

(2)编辑脚本文件network-myself.sh

此脚本为简化版本,没有过多的验证,即认为每个命令都会正常执行。

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

 回车,按键盘上的 i 键或 Insert 功能键,进入编辑状态,脚本修改内容如下:

#!/bin/bash
 
 
#开启自己的测试网络
function networkUpMyself() {
    # 关闭自己的测试网络
	networkDownMyself
 
	set -x
	# 生成组织及证书
	cryptogen generate --config=./crypto-config.yaml --output="organizations"
 
	# 执行ccp-generate.sh脚本,生成通用连通配置文件
	./organizations/ccp-generate.sh 
 
	# 启动网络
	export DOCKER_SOCK=/var/run/docker.sock
	docker-compose -f $COMPOSE_FILE_BASE up -d
 
	set +x
}

 
#关闭自己的测试网络
function networkDownMyself() {
    
	set -x
	
	#停止和删除容器、网络、卷、镜像
	export DOCKER_SOCK=/var/run/docker.sock
	docker-compose -f $COMPOSE_FILE_BASE down --volumes --remove-orphans
 
	#删除节点目录
	rm -rf ./organizations/*Organizations
	
	#删除block
	rm -rf ./system-genesis-block/*.block
 
	set +x
}


# 创建通道
function createChannelMyself() {
	# 通过是否存在目录organizations/peerOrganizations,判断是否启动了网络,若没有此目录,则启动网络
	if [ ! -d "organizations/peerOrganizations" ]; then
		networkUpMyself
	fi

	# 设置环境变量,设置排序节点的公钥、私钥、证书位置,设置peer节点证书位置
	export CORE_PEER_TLS_ENABLED=true
	export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
	export PEER0_ORG1_CA=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
	export PEER0_ORG2_CA=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
	export PEER0_ORG3_CA=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
	export ORDERER_ADMIN_TLS_SIGN_CERT=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
	export ORDERER_ADMIN_TLS_PRIVATE_KEY=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.key
	# configtx.yaml文件所在的目录 
	export FABRIC_CFG_PATH=${PWD}/configtx
	# 区块链文件位置
	export BLOCKFILE="./channel-artifacts/mychannel.block"

	# 生成创世区块mychannel.block文件,根据配置文件./configtx/configtx.yaml来创建Orderer系统通道的创世块
	configtxgen -profile TwoOrgsApplicationGenesis -outputBlock ./channel-artifacts/mychannel.block -channelID mychannel

	# 重置环境变量
	export FABRIC_CFG_PATH=$PWD/../config/

	# 设置环境变量为org1,调用envVar.sh脚本中的函数及参数 setGlobals 1
	export CORE_PEER_LOCALMSPID="Org1MSP"
	export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
	export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
	export CORE_PEER_ADDRESS=localhost:7051

	sleep 3

	# 创建通道
	osnadmin channel join --channelID mychannel --config-block ./channel-artifacts/mychannel.block -o localhost:7053 --ca-file "$ORDERER_CA" --client-cert "$ORDERER_ADMIN_TLS_SIGN_CERT" --client-key "$ORDERER_ADMIN_TLS_PRIVATE_KEY" 
	
	# 设置环境变量为org1,调用envVar.sh脚本中的函数及参数 setGlobals 1
	export CORE_PEER_LOCALMSPID="Org1MSP"
	export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
	export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
	export CORE_PEER_ADDRESS=localhost:7051

	sleep 3

	# 将org1的节点加入通道
	peer channel join -b $BLOCKFILE

	# 设置环境变量为org2,调用envVar.sh脚本中的函数及参数 setGlobals 2
	export CORE_PEER_LOCALMSPID="Org2MSP"
	export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
	export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
	export CORE_PEER_ADDRESS=localhost:9051

	sleep 3

	# 将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


}
 
# docker-compose yaml文件地址
COMPOSE_FILE_BASE=docker/docker-compose-test-net.yaml
 
#参数命令
MODE=$1
 
 
if [ "${MODE}" == "up" ]; then
	echo "开启自己的测试网络"
	networkUpMyself
elif [ "${MODE}" == "down" ]; then
	echo "关闭自己的测试网络"
	networkDownMyself
elif [ "${MODE}" == "createChannel" ]; then
	echo "创建通道"
	createChannelMyself
else
	echo "up 开启自己的测试网络"
	echo "down 关闭自己的测试网络"
	echo "createChannel 创建通道"
	exit 1
fi

脚本中增加了创建通道函数createChannelMyself

(3)测试 - 创建通道

[root@localhost test-network-myself]# ./network-myself.sh createChannel
创建通道
+ 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 peer0.org2.example.com ... done
Creating orderer.example.com    ... done
Creating peer0.org1.example.com ... done
Creating cli                    ... done
+ set +x
2021-11-18 16:40:38.920 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-11-18 16:40:38.927 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: etcdraft
2021-11-18 16:40:38.927 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-18 16:40:38.927 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-18 16:40:38.929 CST [common.tools.configtxgen] doOutputBlock -> INFO 005 Generating genesis block
2021-11-18 16:40:38.929 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Creating application channel genesis block
2021-11-18 16:40:38.929 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-18 16:40:45.016 CST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 16:40:45.028 CST [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2021-11-18 16:40:48.065 CST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 16:40:48.079 CST [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
+ peer channel fetch config config_block.pb -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Using organization 1
Fetching channel config for channel mychannel
Using organization 1
Fetching the most recent configuration block for the channel
2021-11-18 08:40:48.197 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 08:40:48.198 UTC [cli.common] readBlock -> INFO 002 Received block: 0
2021-11-18 08:40:48.199 UTC [channelCmd] fetch -> INFO 003 Retrieving last config block: 0
2021-11-18 08:40:48.199 UTC [cli.common] readBlock -> INFO 004 Received block: 0
Decoding config block to JSON and isolating config to Org1MSPconfig.json
+ configtxlator proto_decode --input config_block.pb --type common.Block
+ jq '.data.data[0].payload.data.config'
+ jq '.channel_group.groups.Application.groups.Org1MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org1.example.com","port": 7051}]},"version": "0"}}' Org1MSPconfig.json
Generating anchor peer update transaction for Org1 on channel mychannel
+ configtxlator proto_encode --input Org1MSPconfig.json --type common.Config
+ configtxlator proto_encode --input Org1MSPmodified_config.json --type common.Config
+ configtxlator compute_update --channel_id mychannel --original original_config.pb --updated modified_config.pb
+ configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate
+ jq .
++ cat config_update.json
+ echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":{' '"channel_id":' '"mychannel",' '"isolated_data":' '{},' '"read_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org1MSP":' '{' '"groups":' '{},' '"mod_policy":' '"",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '},' '"write_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org1MSP":' '{' '"groups":' '{},' '"mod_policy":' '"Admins",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"AnchorPeers":' '{' '"mod_policy":' '"Admins",' '"value":' '{' '"anchor_peers":' '[' '{' '"host":' '"peer0.org1.example.com",' '"port":' 7051 '}' ']' '},' '"version":' '"0"' '},' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"1"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '}}}}'
+ configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope
2021-11-18 08:40:48.369 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 08:40:48.379 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
Anchor peer set for org 'Org1MSP' on channel 'mychannel'
+ peer channel fetch config config_block.pb -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Using organization 2
Fetching channel config for channel mychannel
Using organization 2
Fetching the most recent configuration block for the channel
2021-11-18 08:40:48.557 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 08:40:48.566 UTC [cli.common] readBlock -> INFO 002 Received block: 1
2021-11-18 08:40:48.566 UTC [channelCmd] fetch -> INFO 003 Retrieving last config block: 1
2021-11-18 08:40:48.567 UTC [cli.common] readBlock -> INFO 004 Received block: 1
Decoding config block to JSON and isolating config to Org2MSPconfig.json
+ configtxlator proto_decode --input config_block.pb --type common.Block
+ jq '.data.data[0].payload.data.config'
Generating anchor peer update transaction for Org2 on channel mychannel
+ jq '.channel_group.groups.Application.groups.Org2MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org2.example.com","port": 9051}]},"version": "0"}}' Org2MSPconfig.json
+ configtxlator proto_encode --input Org2MSPconfig.json --type common.Config
+ configtxlator proto_encode --input Org2MSPmodified_config.json --type common.Config
+ configtxlator compute_update --channel_id mychannel --original original_config.pb --updated modified_config.pb
+ configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate
+ jq .
++ cat config_update.json
+ echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":{' '"channel_id":' '"mychannel",' '"isolated_data":' '{},' '"read_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org2MSP":' '{' '"groups":' '{},' '"mod_policy":' '"",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '},' '"write_set":' '{' '"groups":' '{' '"Application":' '{' '"groups":' '{' '"Org2MSP":' '{' '"groups":' '{},' '"mod_policy":' '"Admins",' '"policies":' '{' '"Admins":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Endorsement":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Readers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '},' '"Writers":' '{' '"mod_policy":' '"",' '"policy":' null, '"version":' '"0"' '}' '},' '"values":' '{' '"AnchorPeers":' '{' '"mod_policy":' '"Admins",' '"value":' '{' '"anchor_peers":' '[' '{' '"host":' '"peer0.org2.example.com",' '"port":' 9051 '}' ']' '},' '"version":' '"0"' '},' '"MSP":' '{' '"mod_policy":' '"",' '"value":' null, '"version":' '"0"' '}' '},' '"version":' '"1"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '},' '"mod_policy":' '"",' '"policies":' '{},' '"values":' '{},' '"version":' '"0"' '}' '}}}}'
+ configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope
2021-11-18 08:40:48.733 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-18 08:40:48.744 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
Anchor peer set for org 'Org2MSP' on channel 'mychannel'

上一篇: Fabric v2.3 手动模拟(一) - 测试网络./network.sh up命令创建自己的网络

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

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值