一、前言
本教程在ubuntu20.04版本下运行,请在执行fabric2.2环境部署前先部署ubuntu20.04环境,所有部署都按照官方文档[hyperledger fabric]进行,在运行过程中遇到的问题及解决方案,我也会逐个解答,如果你对区块链并不太了解,请查阅区块链相关概念。文中若有理解不到位、表达不清晰的地方,欢迎批评指正。
二、hyperledger fabric 介绍
Hyperledger Fabric 是 Hyperledger (超级账本)中的区块链项目之一,也是经典的联盟链之一。它有一个账本,使用智能合约,由参与者管理交易的系统,但它又不完全去中心化,即想要加入联盟的成员们需要从可信赖的成员服务提供者(MSP)注册,以下是一些相关概念的介绍。
三、测试网络示例
3.1搭建开发环境
1.安装git
sudo apt-get install git
2.安装curl
sudo apt-get install curl
3.安装docker
sudo apt-get -y install docker-compose
docker --version
docker-compose --version
# 提示,以后相关docker的错误,执行步骤2,步骤3,步骤1
#1. 重启docker
sudo systemctl start docker
# 设置系统自启动docker,可选
sudo systemctl enable docker
#2. 将用户添加到docker组,确保在用户命令下可以执行
sudo gpasswd -a $USER docker
#3. 更新用户组
newgrp docker
# docker 信息
docker info
4. 安装go
安装并解压:
# 下载
wget https://studygolang.com/dl/golang/go1.20.9.linux-amd64.tar.gz
# 解压
sudo tar -C /usr/local -xzf go1.20.9.linux-amd64.tar.gz
# Go环境配置
mkdir $HOME/go
#用vi打开~./bashrc,配置环境变量
vi ~/.bashrc
# 在最下方插入
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
#使配置的环境变量生效
source ~/.bashrc
#检查是否配置正确
go version
# 配置goproxy环境变量加速国内下载
go env -w GOPROXY=https://goproxy.io
3.2 安装示例、二进制和Docker镜像
下面说明中的 cURL 命令将设置您的环境,以便您可以运行 Fabric 测试网络。具体来说,它会执行以下步骤:
- 克隆 hyperledger/fabric-samples仓库
- 下载最新的Hyperledger-Fabric Docker镜像,并将其标记为 latest
- 将Hyperledger Fabric CLI工具二进制文件和配置文件下载到fabric-samples/bin 和/config目录中这些二进制文件将有助于后续和测试网络交互。
1. 获取安装脚本
curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh
运行脚本-h查看以下操作
./install-fabric.sh -h
Usage: ./install-fabric.sh [-f|--fabric-version <arg>] [-c|--ca-version <arg>] <comp-1> [<comp-2>] ... [<comp-n>] ...
<comp>: Component to install one or more of d[ocker]|b[inary]|s[amples]. If none specified, all will be installed
-f, --fabric-version: FabricVersion (default: '2.5.4')
-c, --ca-version: Fabric CA Version (default: '1.5.7')
2. 选择安装指定组件
./install-fabric.sh --fabric-version 2.2.0 binary
3.注意事项
自动下载脚本通常因为网络问题,无法直接下载完相关内容
以下为手动下载方案
- 手动克隆hyperledger/fabric/仓库,获取自动下载脚本
- 修改自动下载部分内容,改为手动获取指定镜像、fabric-samples仓库(注:脚本中标明相关组件版本号要求)
克隆hyperledger/fabric-samples仓库
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
# 获取fabric-samples源码
git clone https://github.com/hyperledger/fabric-samples.git
选择适当的版本标签,进入目录切换分支
cd fabric-samples
# 可自行选择版本
git checkout release-2.2
#查看版本
git branch
克隆hyperledger/fabric仓库
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
# 获取fabric-samples源码
git clone https://github.com/hyperledger/fabric.git
选择适当的版本标签,进入目录切换分支
cd fabric
# 可自行选择版本
git checkout release-2.2
#查看版本
git branch
查看部署脚本需求各组件版本
# if version not passed in, default to latest released version
VERSION=2.2.13
# if ca version not passed in, default to latest released version
CA_VERSION=1.5.6
修改bootstrap.sh
# This will download the .tar.gz
download() {
#local BINARY_FILE=$1
#local URL=$2
#echo "===> Downloading: " "${URL}"
#curl -L --retry 5 --retry-delay 3 "${URL}" | tar xz || rc=$?
#if [ -n "$rc" ]; then
# echo "==> There was an error downloading the binary file."
# return 22
#else
echo "==> Done."
#fi
}
3.3 启动测试网络
你可以再fabric-samples代码仓库的test-network目录中找到启动网络的脚本。使用一下命令导航至测试网络目录:
cd fabric-samples/test-network
在此目录中,你可以找到带注释的脚本network.sh,该脚本在本地计算机上使用docker镜像建立fabric网络。你可以运行./network.sh -h以打印帮助文本到终端:
Usage:
network.sh <Mode> [Flags]
Modes:
up - bring up fabric orderer and peer nodes. No channel is created
up createChannel - bring up fabric network with one channel
createChannel - create and join a channel after the network is created
deployCC - deploy the asset transfer basic chaincode on the channel or specify
down - clear the network with docker-compose down
restart - restart the network
Flags:
-ca <use CAs> - create Certificate Authorities to generate the crypto material
-c <channel name> - channel name to use (defaults to "mychannel")
-s <dbtype> - the database backend to use: goleveldb (default) or couchdb
-r <max retry> - CLI times out after certain number of attempts (defaults to 5)
-d <delay> - delay duration in seconds (defaults to 3)
-ccn <name> - the short name of the chaincode to deploy: basic (default),ledger, private, secured
-ccl <language> - the programming language of the chaincode to deploy: go (default), java, javascript, typescript
-ccv <version> - chaincode version. 1.0 (default)
-ccs <sequence> - chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc
-ccp <path> - Optional, chaincode path. Path to the chaincode. When provided the -ccn will be used as the deployed name and not the short name of the known chaincodes.
-cci <fcn name> - Optional, chaincode init required function to invoke. When provided this function will be invoked after deployment of the chaincode and will define the chaincode as initialization required.
-i <imagetag> - the tag to be used to launch the network (defaults to "latest")
-cai <ca_imagetag> - the image tag to be used for CA (defaults to "latest")
-verbose - verbose mode
-h - print this message
Possible Mode and flag combinations
up -ca -c -r -d -s -i -verbose
up createChannel -ca -c -r -d -s -i -verbose
createChannel -c -r -d -verbose
deployCC -ccn -ccl -ccv -ccs -ccp -cci -r -d -verbose
Taking all defaults:
network.sh up
Examples:
network.sh up createChannel -ca -c mychannel -s couchdb -i 2.0.0
network.sh createChannel -c channelName
network.sh deployCC -ccn basic -ccl javascript
在test-network目录中,运行以下命令删除先前运行的所有容器或工程:
./network.sh down
然后,你可以通过执行以下命令来启动网络。
./network.sh up
此命令创建一个由两个对等节点和一个订购节点组成的Fabric网络。运行./network.sh up 时没有创建任何channel, 我们将在后续的步骤实现。如果命令执行成功,你将看到已创建的节点日志。
Creating network "net_test" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating orderer.example.com ... done
Creating peer0.org2.example.com ... done
Creating peer0.org1.example.com ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d0c74b9d6af hyperledger/fabric-orderer:latest "orderer" 4 seconds ago Up Less than a second 0.0.0.0:7050->7050/tcp orderer.example.com
ea1cf82b5b99 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up Less than a second 0.0.0.0:7051->7051/tcp peer0.org1.example.com
cd8d9b23cb56 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up 1 second 7051/tcp, 0.0.0.0:9051->9051/tcp peer0.org2.example.com