一、fabric环境准备
1. 软件环境
docker、docker-compose、golang
2. docker镜像拉取
docker pull hyperledger/fabric-peer:x86_64-1.0.0 docker pull hyperledger/fabric-orderer:x86_64-1.0.0 docker pull hyperledger/fabric-baseos:x86_64-0.3.1 docker pull hyperledger/fabric-tools:x86_64-1.0.0
二、 fabric源码拉取
1. 拉取fabric源码
源码库版本切换到 release-1.0(刚接触,先按教程学习,减少弯路)
拉取目的是编译 cryptogen、 configtxgen两个工具
cryptogen:主要目的是生成相关的证书
configtxgen:主要目的是生成创世区块和通道配置相关的内容
拉取fabric源码
go get方式
go get -u -v github.com/hyperledger/fabric
git方式
mkdir -p $GOPATH/src/github.com/hyperledger cd -p $GOPATH/src/github.com/hyperledger git clone https://github.com/hyperledger/fabric.git
切换到release-1.0版本
git checkout release-1.0
2. 安装cryptogen:
进入fabric目录
cd common/configtx/tool/configtxgen/
go install #安装
碰到错误,我用vmwre创建的centos7虚拟机,没有安装gcc,错误如下:exec: "gcc": executable file not found in $PATH
安装gcc编译器
yum -y install gcc gcc-c++ kernel-devel
然后go install又出现错误:
# github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11 ../../../../vendor/github.com/miekg/pkcs11/pkcs11.go:29:18: fatal error: ltdl.h: No such file or directory #include <ltdl.h> ^ compilation terminated.
解决办法,安装libtool
yum -y install libtool-ltdl-devel
再次执行 go install 成功安装
3. 安装configtxgen
进入/fabric/common/tools/cryptogen
执行go install命令
PS:mac下可能报错 ....github.com/miekg/pkcs11....., 可以使用 go install --tags=nopkcs11 忽略
两个工具都被安装到GOPATH的bin目录下
下面进入fabric-samples项目搭建第一个网络
三、 fabric-samples项目
这个项目里面,官方提供了一些例子,帮助我们快速的建立一个fabric网络,在官方提供的例子里,建立我们的第一个网络是非常简单的
1. 源码拉取及切换到release-1.0分支
git checkout release-1.0
里面有很多例子,首先看first-network
drwxr-xr-x. 5 root root 253 Apr 14 23:03 . drwxr-xr-x. 11 root root 257 Apr 14 23:03 .. drwxr-xr-x. 2 root root 60 Apr 14 23:03 base -rwxr-xr-x. 1 root root 15108 Apr 14 23:03 byfn.sh drwxr-xr-x. 2 root root 22 Apr 14 20:20 channel-artifacts -rw-r--r--. 1 root root 5013 Apr 14 23:03 configtx.yaml -rw-r--r--. 1 root root 3858 Apr 14 23:03 crypto-config.yaml -rw-r--r--. 1 root root 3015 Apr 14 23:03 docker-compose-cli.yaml -rw-r--r--. 1 root root 4604 Apr 14 23:03 docker-compose-couch.yaml -rw-r--r--. 1 root root 2883 Apr 14 20:20 docker-compose-e2e-template.yaml -rw-r--r--. 1 root root 42 Apr 14 20:20 .env -rw-r--r--. 1 root root 335 Apr 14 20:20 README.md drwxr-xr-x. 2 root root 23 Apr 14 23:03 scripts
注释:
base # 目录下是多个compose的公共服务 byfn.sh # 启动脚本 channel-artifacts configtx.yaml # 根据之前生成的两个工具生成相应的配置文件,放入到channel-artifacts crypto-config.yaml # 根据之前生成的两个工具生成相应的配置文件,放到哪儿??? docker-compose-cli.yaml # 启动网络 docker-compose-couch.yaml # 启动网络 docker-compose-e2e-template.yaml # 启动网络 .env # 存储的环境变量 README.md scripts # 目录下存储的是测试脚本,例如创建通道、加入通道、安装链码、实例化链码、和链码做一些交互的工作
2. byfn.sh脚本命令参数
./byfn.sh -h # 查看帮助
[root@chow first-network]# ./byfn.sh -h Usage: byfn.sh -m up|down|restart|generate [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-i <imagetag>] byfn.sh -h|--help (print this message) -m <mode> - one of 'up', 'down', 'restart' or 'generate' - 'up' - bring up the network with docker-compose up - 'down' - clear the network with docker-compose down - 'restart' - restart the network - 'generate' - generate required certificates and genesis block -c <channel name> - channel name to use (defaults to "mychannel") -t <timeout> - CLI timeout duration in microseconds (defaults to 10000) -d <delay> - delay duration in seconds (defaults to 3) -f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml) -s <dbtype> - the database backend to use: goleveldb (default) or couchdb -i <imagetag> - pass the image tag to launch the network using the tag: 1.0.1, 1.0.2, 1.0.3, 1.0.4 (defaults to latest) Typically, one would first generate the required certificates and genesis block, then bring up the network. e.g.: byfn.sh -m generate -c mychannel byfn.sh -m up -c mychannel -s couchdb byfn.sh -m up -c mychannel -s couchdb -i 1.0.6 byfn.sh -m down -c mychannel Taking all defaults: byfn.sh -m generate byfn.sh -m up byfn.sh -m down
说明:
up、 down 、 restart:是提供的docker-compose启动、停止、重启命令选项
generate:根据之前编译的两个工具,生成相应的证书以及创世区块
-c:channel通道的名字
-t:客户端超时时间,默认10s
-s:数据库引擎的选择,默认goleveldb
创建网络流程
1. 构建配置文件:byfn.sh -m generate (根据前面提到的configtx.yaml和crypto-config.yaml生成peer节点以及oderer节点相关的MSP证书以及创世区块等配置)
2. 启动网络:byfn.sh -m up
3. 停止网络:byfn.sh -m down
3. 执行 ./byfn.sh -m generate -c testchannel,脚本做了哪些操作
proceeding ... /root/go/bin/cryptogen # 首先根据cryptogen工具生成了参与主体的MSP证书,下面有两个主体:组织一和组织二 ########################################################## ##### Generate certificates using cryptogen tool ######### ########################################################## org1.example.com # 组织一 org2.example.com # 组织二 /root/go/bin/configtxgen # 根据 configtxgen生成创世区块以及通道的配置文件,最后生成了两个组织的锚节点,锚节点是每个组织对外的一个节点, ########################################################## ######### Generating Orderer Genesis block ############## ########################################################## 创世区块 2020-04-14 23:53:06.015 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-04-14 23:53:06.037 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block 2020-04-14 23:53:06.038 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block ################################################################# ### Generating channel configuration transaction 'channel.tx' ### ################################################################# 通道配置文件 2020-04-14 23:53:06.048 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-04-14 23:53:06.052 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx 2020-04-14 23:53:06.052 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx ################################################################# ####### Generating anchor peer update for Org1MSP ########## ################################################################# 组织一锚节点 2020-04-14 23:53:06.061 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-04-14 23:53:06.065 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 2020-04-14 23:53:06.066 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update ################################################################# ####### Generating anchor peer update for Org2MSP ########## ################################################################# 组织二锚节点 2020-04-14 23:53:06.075 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-04-14 23:53:06.079 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 2020-04-14 23:53:06.079 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
4.crypto-config.yaml配置文件是如何设置的
# Copyright IBM Corp. All Rights Reserved. # # SPDX-License-Identifier: Apache-2.0 # # --------------------------------------------------------------------------- # "OrdererOrgs" - Definition of organizations managing orderer nodes # --------------------------------------------------------------------------- OrdererOrgs: # 首先设置了orderer的一些信息 # --------------------------------------------------------------------------- # Orderer # --------------------------------------------------------------------------- - Name: Orderer # orderer名字 Domain: example.com # orderer根域名 # --------------------------------------------------------------------------- # "Specs" - See PeerOrgs below for complete description # --------------------------------------------------------------------------- Specs: - Hostname: orderer # 这里的配置代表只有一个orderer # --------------------------------------------------------------------------- # "PeerOrgs" - Definition of organizations managing peer nodes # --------------------------------------------------------------------------- PeerOrgs: # peer组织的信息 # --------------------------------------------------------------------------- # Org1 # --------------------------------------------------------------------------- - Name: Org1 Domain: org1.example.com # --------------------------------------------------------------------------- # "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 # 代表有两个peer # 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.example.com Template: Count: 2 Users: Count: 1
5.所生成的配置文件所放到的位置
fabric-samples/first-network/crypto-config
drwxr-xr-x. 3 root root 25 Apr 14 23:53 ordererOrganizations drwxr-xr-x. 4 root root 54 Apr 14 23:53 peerOrganizations
首先将orderer以及peer分离,内部的路径基本差不多
[root@chow crypto-config]# cd peerOrganizations/ [root@chow peerOrganizations]# ll total 0 drwxr-xr-x. 7 root root 66 Apr 14 23:53 org1.example.com drwxr-xr-x. 7 root root 66 Apr 14 23:53 org2.example.com
peer里按照主体进行分割
[root@chow peerOrganizations]# cd org1.example.com/ [root@chow org1.example.com]# ll total 0 drwxr-xr-x. 2 root root 117 Apr 14 23:53 ca # 存储的是根ca的证书和私钥 drwxr-xr-x. 5 root root 57 Apr 14 23:53 msp # 存储的是根管理员证书和中间证书 drwxr-xr-x. 4 root root 66 Apr 14 23:53 peers # 存储的是每一个peer相关的证书 drwxr-xr-x. 2 root root 120 Apr 14 23:53 tlsca drwxr-xr-x. 4 root root 66 Apr 14 23:53 users # 针对这个组织的每一个用户也有相关的证书,一般情况下包含一个管理员和一个普通用户(之前的配置文件写的是一个用户,这里有俩,包含一个admin用户)
6.channel-artifacts生成哪些配置文件
[root@chow first-network]# cd channel-artifacts/ [root@chow channel-artifacts]# ls -al total 28 drwxr-xr-x. 2 root root 111 Apr 14 23:53 . drwxr-xr-x. 6 root root 4096 Apr 14 23:53 .. -rw-r--r--. 1 root root 394 Apr 14 23:53 channel.tx # 创建的通道初始配置 -rw-r--r--. 1 root root 9089 Apr 14 23:53 genesis.block # 整个网络中的创世区块 -rw-r--r--. 1 root root 0 Apr 14 20:20 .gitkeep -rw-r--r--. 1 root root 284 Apr 14 23:53 Org1MSPanchors.tx # 主体一的锚节点配置 -rw-r--r--. 1 root root 284 Apr 14 23:53 Org2MSPanchors.tx # 主体二的锚节点配置
7.configtx.yaml配置项
分成三块:Profile、Organizations、Application
# 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: Orderer: <<: *OrdererDefaults Organizations: - *OrdererOrg Consortiums: SampleConsortium: Organizations: - *Org1 - *Org2 TwoOrgsChannel: Consortium: SampleConsortium Application: <<: *ApplicationDefaults Organizations: - *Org1 - *Org2 ################################################################################ # # 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 # orderer组织 # 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 # 组织ID # MSPDir is the filesystem path which contains the MSP configuration MSPDir: crypto-config/ordererOrganizations/example.com/msp # 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.example.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.example.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.example.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.example.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一个单独的配置 # Orderer Type: The orderer implementation to start # Available types are "solo" and "kafka" OrdererType: solo # orderer类型,整个网络中有两种类型:solo和kafka Addresses: - orderer.example.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: