byfn.sh
脚本。今天一起来学习如何创建属于自己的Fabric网络!
在byfn.sh
中创建网络的时候,第一步加载crypto-config.yaml
。该文件主要指定整个网络中的相关组织信息,我们稍加修改示例即可,最终结果如下,各个注释写的很详细的(老美的优点吧),稍微懂点英语就可以看懂了:
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:
# ---------------------------------------------------------------------------
# Orderer
# ---------------------------------------------------------------------------
- Name: Orderer
Domain: xidian.com
# ---------------------------------------------------------------------------
# "Specs" - See PeerOrgs below for complete description
# ---------------------------------------------------------------------------
Specs:
- Hostname: orderer
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
# ---------------------------------------------------------------------------
# Org1
# ---------------------------------------------------------------------------
- Name: Org1
Domain: org1.xidian.com
EnableNodeOUs: true
# ---------------------------------------------------------------------------
# "Specs"
# ---------------------------------------------------------------------------
# Uncomment this section to enable the explicit definition of hosts in your
# configuration. Most users will want to use Template, below
#
# Specs is an array of Spec entries. Each Spec entry consists of two fields:
# - Hostname: (Required) The desired hostname, sans the domain.
# - CommonName: (Optional) Specifies the template or explicit override for
# the CN. By default, this is the template:
#
# "{{.Hostname}}.{{.Domain}}"
#
# which obtains its values from the Spec.Hostname and
# Org.Domain, respectively.
# ---------------------------------------------------------------------------
# Specs:
# - Hostname: foo # implicitly "foo.org1.example.com"
# CommonName: foo27.org5.example.com # overrides Hostname-based FQDN set above
# - Hostname: bar
# - Hostname: baz
# ---------------------------------------------------------------------------
# "Template"
# ---------------------------------------------------------------------------
# Allows for the definition of 1 or more hosts that are created sequentially
# from a template. By default, this looks like "peer%d" from 0 to Count-1.
# You may override the number of nodes (Count), the starting index (Start)
# or the template used to construct the name (Hostname).
#
# Note: Template and Specs are not mutually exclusive. You may define both
# sections and the aggregate nodes will be created for you. Take care with
# name collisions
# ---------------------------------------------------------------------------
Template:
Count: 2
# Start: 5
# Hostname: {{.Prefix}}{{.Index}} # default
# ---------------------------------------------------------------------------
# "Users"
# ---------------------------------------------------------------------------
# Count: The number of user accounts _in addition_ to Admin
# ---------------------------------------------------------------------------
Users:
Count: 1
# ---------------------------------------------------------------------------
# Org2: See "Org1" for full specification
# ---------------------------------------------------------------------------
- Name: Org2
Domain: org2.xidian.com
EnableNodeOUs: true
Template:
Count: 2
Users:
Count: 1
该配置创建了OrdererOrgs和PeerOrgs两个组织信息,其中PeerOrgs中有两个org,每个org中有两个peer节点和一个user。
我们使用以前编译好的cryptogen
工具来生成,命令如下:
cryptogen generate --config=./crypto-config.yaml
结果如下:
执行完该命令后就会在当前目录crypto-config
文件夹,里边包括相关的结果及证书,我们可以用tree
命令看详细情况,展开三级哈,
再生成组织结构等文件后,我们还需要创世区块和通道。该文件的生成依然依靠yaml文件,而这些相关文件主要在configtx.yaml
文件,我们指定上一步生成文件的路径就可以了。
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
---
################################################################################
#
# Profile
#
# - Different configuration profiles may be encoded here to be specified
# as parameters to the configtxgen tool
#
################################################################################
Profiles:
TwoOrgsOrdererGenesis:
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Capabilities:
<<: *ApplicationCapabilities
################################################################################
#
# Section: Organizations
#
# - This section defines the different organizational identities which will
# be referenced later in the configuration.
#
################################################################################
Organizations:
# SampleOrg defines an MSP using the sampleconfig. It should never be used
# in production but may be used as a template for other definitions
- &OrdererOrg
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: OrdererOrg
# ID to load the MSP definition as
ID: OrdererMSP
# MSPDir is the filesystem path which contains the MSP configuration
MSPDir: crypto-config/ordererOrganizations/xidian.com/msp
- &Org1
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: Org1MSP
# ID to load the MSP definition as
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.xidian.com/msp
AnchorPeers:
# AnchorPeers defines the location of peers which can be used
# for cross org gossip communication. Note, this value is only
# encoded in the genesis block in the Application section context
- Host: peer0.org1.xidian.com
Port: 7051
- &Org2
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: Org2MSP
# ID to load the MSP definition as
ID: Org2MSP
MSPDir: crypto-config/peerOrganizations/org2.xidian.com/msp
AnchorPeers:
# AnchorPeers defines the location of peers which can be used
# for cross org gossip communication. Note, this value is only
# encoded in the genesis block in the Application section context
- Host: peer0.org2.xidian.com
Port: 7051
################################################################################
#
# SECTION: Orderer
#
# - This section defines the values to encode into a config transaction or
# genesis block for orderer related parameters
#
################################################################################
Orderer: &OrdererDefaults
# Orderer Type: The orderer implementation to start
# Available types are "solo" and "kafka"
OrdererType: solo
Addresses:
- orderer.xidian.com:7050
# Batch Timeout: The amount of time to wait before creating a batch
BatchTimeout: 2s
# Batch Size: Controls the number of messages batched into a block
BatchSize:
# Max Message Count: The maximum number of messages to permit in a batch
MaxMessageCount: 10
# Absolute Max Bytes: The absolute maximum number of bytes allowed for
# the serialized messages in a batch.
AbsoluteMaxBytes: 99 MB
# Preferred Max Bytes: The preferred maximum number of bytes allowed for
# the serialized messages in a batch. A message larger than the preferred
# max bytes will result in a batch larger than preferred max bytes.
PreferredMaxBytes: 512 KB
Kafka:
# Brokers: A list of Kafka brokers to which the orderer connects
# NOTE: Use IP:port notation
Brokers:
- 127.0.0.1:9092
# Organizations is the list of orgs which are defined as participants on
# the orderer side of the network
Organizations:
################################################################################
#
# SECTION: Application
#
# - This section defines the values to encode into a config transaction or
# genesis block for application related parameters
#
################################################################################
Application: &ApplicationDefaults
# Organizations is the list of orgs which are defined as participants on
# the application side of the network
Organizations:
################################################################################
#
# SECTION: Capabilities
#
# - This section defines the capabilities of fabric network. This is a new
# concept as of v1.1.0 and should not be utilized in mixed networks with
# v1.0.x peers and orderers. Capabilities define features which must be
# present in a fabric binary for that binary to safely participate in the
# fabric network. For instance, if a new MSP type is added, newer binaries
# might recognize and validate the signatures from this type, while older
# binaries without this support would be unable to validate those
# transactions. This could lead to different versions of the fabric binaries
# having different world states. Instead, defining a capability for a channel
# informs those binaries without this capability that they must cease
# processing transactions until they have been upgraded. For v1.0.x if any
# capabilities are defined (including a map with all capabilities turned off)
# then the v1.0.x peer will deliberately crash.
#
################################################################################
Capabilities:
# Channel capabilities apply to both the orderers and the peers and must be
# supported by both. Set the value of the capability to true to require it.
Global: &ChannelCapabilities
# V1.1 for Global is a catchall flag for behavior which has been
# determined to be desired for all orderers and peers running v1.0.x,
# but the modification of which would cause incompatibilities. Users
# should leave this flag set to true.
V1_1: true
# Orderer capabilities apply only to the orderers, and may be safely
# manipulated without concern for upgrading peers. Set the value of the
# capability to true to require it.
Orderer: &OrdererCapabilities
# V1.1 for Order is a catchall flag for behavior which has been
# determined to be desired for all orderers running v1.0.x, but the
# modification of which would cause incompatibilities. Users should
# leave this flag set to true.
V1_1: true
# Application capabilities apply only to the peer network, and may be safely
# manipulated without concern for upgrading orderers. Set the value of the
# capability to true to require it.
Application: &ApplicationCapabilities
# V1.1 for Application is a catchall flag for behavior which has been
# determined to be desired for all peers running v1.0.x, but the
# modification of which would cause incompatibilities. Users should
# leave this flag set to true.
V1_1: true
这一步使用到的工具为configtxgen
,关于详细使用情况我们用help即可查看,生成配置文件的命令如下:
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
相关命令:
export CHANNEL_NAME=mychannel
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
锚节点更新配置文件在通道创建之后用来更新组织中的锚节点,同样基于configtx.yaml
文件和configtxgen
工具,相关命令如下:
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
截止上一步我们的网络相关配置文件已经生成,下一步就是启动网络,启动网络主要用到的文件有:docker-compose-cli.yaml
、base/docker-compose-base.yaml
、``base/peer-base.yaml`
相关内容如下:
docker-compose-cli.yaml
文件内容:# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
volumes:
orderer.xidian.com:
peer0.org1.xidian.com:
peer1.org1.xidian.com:
peer0.org2.xidian.com:
peer1.org2.xidian.com:
networks:
byfn:
services:
orderer.xidian.com:
extends:
file: base/docker-compose-base.yaml
service: orderer.xidian.com
container_name: orderer.xidian.com
networks:
- byfn
peer0.org1.xidian.com:
container_name: peer0.org1.xidian.com
extends:
file: base/docker-compose-base.yaml
service: peer0.org1.xidian.com
networks:
- byfn
peer1.org1.xidian.com:
container_name: peer1.org1.xidian.com
extends:
file: base/docker-compose-base.yaml
service: peer1.org1.xidian.com
networks:
- byfn
peer0.org2.xidian.com:
container_name: peer0.org2.xidian.com
extends:
file: base/docker-compose-base.yaml
service: peer0.org2.xidian.com
networks:
- byfn
peer1.org2.xidian.com:
container_name: peer1.org2.xidian.com
extends:
file: base/docker-compose-base.yaml
service: peer1.org2.xidian.com
networks:
- byfn
cli:
container_name: cli
image: hyperledger/fabric-tools:$IMAGE_TAG
tty: true
stdin_open: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
#- CORE_LOGGING_LEVEL=DEBUG
- CORE_LOGGING_LEVEL=INFO
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org1.xidian.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.xidian.com/peers/peer0.org1.xidian.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.xidian.com/peers/peer0.org1.xidian.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.xidian.com/peers/peer0.org1.xidian.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.xidian.com/users/[email protected]/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./../chaincode/:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- orderer.xidian.com
- peer0.org1.xidian.com
- peer1.org1.xidian.com
- peer0.org2.xidian.com
- peer1.org2.xidian.com
networks:
- byfn
docker-compose-base.yaml
文件内容:# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
orderer.xidian.com:
container_name: orderer.xidian.com
image: hyperledger/fabric-orderer:$IMAGE_TAG
environment:
- ORDERER_GENERAL_LOGLEVEL=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ../crypto-config/ordererOrganizations/xidian.com/orderers/orderer.xidian.com/msp:/var/hyperledger/orderer/msp
- ../crypto-config/ordererOrganizations/xidian.com/orderers/orderer.xidian.com/tls/:/var/hyperledger/orderer/tls
- orderer.xidian.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
peer0.org1.xidian.com:
container_name: peer0.org1.xidian.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.org1.xidian.com
- CORE_PEER_ADDRESS=peer0.org1.xidian.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.xidian.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.xidian.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org1.xidian.com/peers/peer0.org1.xidian.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org1.xidian.com/peers/peer0.org1.xidian.com/tls:/etc/hyperledger/fabric/tls
- peer0.org1.xidian.com:/var/hyperledger/production
ports:
- 7051:7051
- 7053:7053
peer1.org1.xidian.com:
container_name: peer1.org1.xidian.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer1.org1.xidian.com
- CORE_PEER_ADDRESS=peer1.org1.xidian.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.xidian.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.xidian.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org1.xidian.com/peers/peer1.org1.xidian.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org1.xidian.com/peers/peer1.org1.xidian.com/tls:/etc/hyperledger/fabric/tls
- peer1.org1.xidian.com:/var/hyperledger/production
ports:
- 8051:7051
- 8053:7053
peer0.org2.xidian.com:
container_name: peer0.org2.xidian.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.org2.xidian.com
- CORE_PEER_ADDRESS=peer0.org2.xidian.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.xidian.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org2.xidian.com:7051
- CORE_PEER_LOCALMSPID=Org2MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org2.xidian.com/peers/peer0.org2.xidian.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org2.xidian.com/peers/peer0.org2.xidian.com/tls:/etc/hyperledger/fabric/tls
- peer0.org2.xidian.com:/var/hyperledger/production
ports:
- 9051:7051
- 9053:7053
peer1.org2.xidian.com:
container_name: peer1.org2.xidian.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer1.org2.xidian.com
- CORE_PEER_ADDRESS=peer1.org2.xidian.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.xidian.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.xidian.com:7051
- CORE_PEER_LOCALMSPID=Org2MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org2.xidian.com/peers/peer1.org2.xidian.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org2.xidian.com/peers/peer1.org2.xidian.com/tls:/etc/hyperledger/fabric/tls
- peer1.org2.xidian.com:/var/hyperledger/production
ports:
- 10051:7051
- 10053:7053
peer-base.yaml
文件内容# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
peer-base:
image: hyperledger/fabric-peer:$IMAGE_TAG
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_byfn
- CORE_LOGGING_LEVEL=INFO
#- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
用docker-compose来启动,相关命令:
docker-compose -f docker-compose-cli.yaml up
这个时候会报错如下:
WARNING: The IMAGE_TAG variable is not set. Defaulting to a blank string.
WARNING: The COMPOSE_PROJECT_NAME variable is not set. Defaulting to a blank string.
Creating network "test_byfn" with the default driver
ERROR: no such image: hyperledger/fabric-orderer:: invalid reference format
解决方案,是由于环境变量中base/docker-compose-base.yaml
没有相关内容,export
即可
export IMAGE_TAG=latest
然后重新运行即可,结果如下:
到此为止,我们的网络已经完成启动了。
通道的概念类似子网,通道提供一种通信机制,能够将Peer和Orderer连接在一起,形成一个具有保密性质的通信链路。
docker exec -it cli bash
export CHANNEL_NAME=mychannel
peer channel create -o orderer.xidian.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/xidian.com/orderers/orderer.xidian.com/msp/tlscacerts/tlsca.xidian.com-cert.pem
上图左边为后台信息,右边为容器信息。
peer channel join -b mychannel.block
上图为容器信息,下图为网络信息。
使用Org1的管理员身份更新
peer channel update -o orderer.xidian.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/xidian.com/orderers/orderer.xidian.com/msp/tlscacerts/tlsca.xidian.com-cert.pem
以Org2的管理员身份更新,因为在配置文件中所有相关配置文件的环境变量都默认Org1的管理员,要进行Org2管理员更新必须将相关的环境变量设置为Org2的,所有较上一步麻烦一点,相关操作如下
CORE_PEER_ADDRESS=peer0.org2.xidian.com:7051
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.xidian.com/peers/peer0.org2.xidian.com/tls/server.crt
CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.xidian.com/peers/peer0.org2.xidian.com/tls/server.key
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.xidian.com/peers/peer0.org2.xidian.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.xidian.com/users/[email protected]/msp
peer channel update -o orderer.xidian.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/xidian.com/orderers/orderer.xidian.com/msp/tlscacerts/tlsca.xidian.com-cert.pem