以下内容记录了基于Fabric 架构,利用docker容器创建一个简单的区块链案例(helloworld),并实现第一个智能合约(链码)。详细配置可以参考fabric-sample/first-network
1.环境准备
安装以下环境,详细安装过程上网搜索相关资料:
- git
- nodejs
- npm
- golang
- docker
- docker-compose
配置gopath环境变量,以mac系统为例,打开~/.bash_profile 添加下面内容:
#go的安装路径
export GOROOT=/usr/local/Cellar/go/1.13.4 #go安装包安装的路径
#hyperledger相关可运行文件,所在的目录,自己可以随意设置
export GOPATH=$HOME/go
#一些其他与运行hyperledger fabric运行有关的可执行文件所在的目录
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
2.下载fabric资源
- 下载fabric
# 进入文件hyperledger文件路径
# /Users/zgq/go为GPATH
cd /Users/zgq/go/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git
cd fabric
git tag #查看所有版本,点击q退出
git checkout v1.14.2 #切换到tag中你想要切换到的版本
- 下载fabric-samples
cd cd /Users/zgq/go/src
# 从git上克隆fabric-samples ,里面有fabric例子
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples
git tag #查看所有版本,点击q退出
git checkout v1.14.2 #切换到fabric相同的版本
- 下载运行hyperledger fabric所需要的二进制文件
# 将$GOPATH/src/github.com/hyperledger/fabric/scripts/bootstrap.sh拷贝到 fabric-samples中
# 终端进入到 fabric-samples文件夹中
# 修改bootstrap.sh权限
chmod +x bootstrap.sh
#运行bootstrap.sh
./bootstrap.sh
# 系统会下载一堆docker镜像、和二进制文件。
二进制文件下载有时候会因为网络问题无法下载,可以通过这个网站手动下载。
https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric
https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca
压缩包内有 bin 和 config 两个文件夹,将 bin 和 config 文件夹复制到 fabric-samples 文件夹内。
3. 体验Fabric区块链网络
你可以通过first-network来体验fabric网络。
进入fabric-samples/first-network,执行命令:
#生成网络
./byfn.sh generate
#启动
./byfn.sh up
#停止
./byfn.sh down
具体可参考 https://hyperledger-fabric.readthedocs.io/en/latest/build_network.html
4.创建Helloworld区块链网络
4.1 架构设计
本案例部署一个排序(orderer)服务,两个组织(org1,org2)和四个节点(peer),每个组织包括两个节点, 另外每个组织一个ca(可选)。
需要五台计算机(也可以在一个主机上模拟,这里在一台mac环境搭建演示)架构配置如下:
- orderer.example.com
- peer0.org1.example.com
- peer1.org1.example.com
- ca.org1.example.com
- peer0.org2.example.com
- peer1.org2.example.com
- ca.org2.example.com
4.2 准备
#命令行进入到fabric-samples目录下
cd ~/go/src/fabric-samples
#新建helloworld目录
mkdir helloworld
#拷贝脚本到helloworld下方便操作
cp -r ../bin /bin
4.3 生成配置文件
配置文件主要包括证书和通道
生成后分别放在两个文件夹中:crypto-config、channel-artifacts
4.3.1 创建crypto-config.yaml
使用fabric提供的cryptogen工具生成文件模板
./bin/cryptogen showtemplate > crypto-config.yaml
修改crypto-config.yaml
,添加组织,和orderer节点。
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:
# ---------------------------------------------------------------------------
# Orderer
# ---------------------------------------------------------------------------
- Name: Orderer
Domain: example.com
EnableNodeOUs: true
# ---------------------------------------------------------------------------
# "Specs" - See PeerOrgs below for complete description
# ---------------------------------------------------------------------------
Specs:
- Hostname: orderer
- Hostname: orderer2
- Hostname: orderer3
- Hostname: orderer4
- Hostname: orderer5
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
# ---------------------------------------------------------------------------
# Org1
# ---------------------------------------------------------------------------
- Name: Org1
Domain: org1.example.com
EnableNodeOUs: true
Specs:
- Hostname: peer0
- Hostname: peer1
- Hostname: peer2
- Hostname: peer3
- Hostname: peer4
# ---------------------------------------------------------------------------
# "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.example.com
EnableNodeOUs: true
Specs:
- Hostname: peer0
- Hostname: peer1
- Hostname: peer2
- Hostname: peer3
- Hostname: peer4
Template:
Count: 2
Users:
Count: 1
4.3.2 生成证书文件
cryptogen generate --config=./crypto-config.yaml
4.3.3 生成创世区块
首先要确保channel-artifacts文件夹存在,如果不存在需要手动创建,不然会报错。
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
4.3.4 生成通道配置文件
其中通道名mychannel可以修改为自己的名称
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel
4.3.5 生成锚节点配置文件
Org1
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
Org2
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
所有