Hyperledger caliper 运行环境搭建

版权声明:转载请注明出处 https://blog.csdn.net/cyLee_/article/details/86518392

前言

Github里caliper的版本应该是最新的,但该版本与caliper官网所描述的运行方法
在这里插入图片描述
并不适配,simple文件夹下并没有config.json和 fabric.json文件:在这里插入图片描述
因此,最终使用了旧版caliper才跑成功(或许没有找到正确方法,在新版上的尝试一直没有成功)。

Pre-requisites

按照官网要求,需要安装的基础环境如下:

  • make,g++
  • NodeJS 8.X
  • node-gyp
  • Docker
  • Docker-compose

注意:NodeJS版本必须是8.X,否则不会跑通。安装node-gyp的过程中可能会出现 SyntaxError: Block-scoped declarations (let, const, function, class) 这个错误,需要将node-js安装到最新版本。安装完成后再使用nvm把版本切换回来就可以:

//切换到指定版本
$ nvm use 8.15.0   
//将该版本设为默认
$ nvm alias default 8.15.0 
//查看当前node版本
$ node -v

安装过程

1.安装 make,g++ 编译工具

sudo apt-get install make g++

2.安装node.js

这里我使用的nvm安装node.js:

//检查可用的node版本
$ nvm ls-remote
//选择一个版本进行安装
$ nvm install 8.15.0
//将其设置为默认版本
$ nvm alias default 8.15.0 

安装完成后查看 node 与 npm 的版本:

$ node -v
v8.15.0
$ npm -v
v11.6.0

3.安装 node-gyp

npm 全局安装 node-gyp:

sudo npm install -g node-gyp

4.安装Docker

  • 由于 apt 源使用HTTPS以确保软件下载过程中不被篡改。因此,我们首先需要添加使用HTTPS传输的软件包以及CA证书。
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
  • 为了确认所下载软件包的合法性,需要添加软件源的 GPG 秘钥
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  • 然后,我们需要向 sources.list 中添加 Docker 软件源
$ sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

以上命令会添加稳定版本的Docker CE apt 镜像源。

  • 更新 apt 软件包缓存,并安装 docker-ce:
$ sudo apt-get update
$ sudo apt-get install docker-ce
  • 查看 Docker 版本:
$ docker -v
  • 启动 Docker CE:
$ sudo systemctl enable docker
$ sudo systemctl start docker
  • 建立 docker 用户组
    默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
$ sudo groupadd docker

其实一般按照上面的方法安装 Docker 后就已经创建好 docker 用户组了,可以使用 $ cat /etc/group | grep docker 命令来验证,所以就不需要再建立 docker 用户组了,再建立也会报错提示用户组已存在的。

  • 将当前用户加入 docker 用户组:
$ sudo usermod -aG docker $USER

下次登录时即可方便的使用 docker 命令。

  • 测试 Docker 是否安装正确:
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete 
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
  • 配置镜像加速器
    国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。 Docker 官方和国内很多云服务商都提供了国内加速器服务,例如:
    Docker 官方提供的中国 registry mirror https://registry.docker-cn.com
    七牛云加速器 https://reg-mirror.qiniu.com/

当配置某一个加速器地址之后,若发现拉取不到镜像,请切换到另一个加速器地址。
国内各大云服务商均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务。

我们以 Docker 官方加速器 https://registry.docker-cn.com 为例进行介绍。
/etc/docker/daemon.json 中写入如下内容(如果文件不存在则创建)

{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ]
}

之后重新启动服务

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

注意:这里摘录的是原作者的安装方式。由于之前已经安装过Docker,所以并没有影响到后面的使用。但这里打开/etc/docker/daemon.json是一个只读文件,并未研究如何解决。

5.安装 Docker-compose

  • 通过二进制包来安装,从 官方 GitHub Release 处直接下载编译好的二进制文件即可。
$ sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

$ sudo chmod +x /usr/local/bin/docker-compose
  • 查看 Docker compose 版本
$ docker-compose --version
docker-compose version 1.22.0, build f46880fe

下载caliper repository

从 GitHub 克隆 caliper 代码仓库:

git clone https://github.com/hyperledger/caliper.git

这里下载的是最新版的caliper

在caliper目录进行安装:

//进入caliper目录
$ cd caliper
//安装依赖包
$ npm install

安装Fabric SDKs

在 caliper 目录下本地安装 fabric SDKs:

npm install grpc@1.10.1 fabric-ca-client fabric-client

以上命令默认安装 fabric 最新版本的 SDKs,但是由于 caliper 验证过的最新版本是 v1.1.0,所以我们最好安装 v1.1.0 版本:

npm install fabric-ca-client@1.1.0 fabric-client@1.1.0

到这里,所有环境已经安装完毕,必须保证所有环境依赖(尤其是版本搭配和node-gyp 包)安装正确才能跑通。

查看 npm 包版本信息及 docker 镜像信息:

$ npm ls fabric-client
caliper@0.1.0 /home/user1/go/src/github.com/hyperledger/caliper
└── fabric-client@1.1.0

$ npm ls fabric-ca-client
caliper@0.1.0 /home/user1/go/src/github.com/hyperledger/caliper
└── fabric-ca-client@1.1.0

$ npm ls grpc
caliper@0.1.0 /home/user1/go/src/github.com/hyperledger/caliper
├─┬ fabric-client@1.1.0
│ └── grpc@1.10.1  deduped
└── grpc@1.10.1

caliper 在创建 fabric 网络时默认是使用的 fabric v1.1.0 版本,docker 下载的 peer、Orderer、ca 镜像也是 x86_64-v1.1.0 版本,所以 fabric SDKs 的 版本必须一致。

$ docker image ls
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
hyperledger/fabric-ca        x86_64-1.1.0        72617b4fa9b4        6 months ago        299MB
hyperledger/fabric-orderer   x86_64-1.1.0        ce0c810df36a        6 months ago        180MB
hyperledger/fabric-peer      x86_64-1.1.0        b023f9be0771        6 months ago        187MB

运行 benchmark

所有预定义的benchmark都可以在benchmark文件夹下找到。进行测试时在根目录下cd caliper 运行:

node benchmark/simple/main.js -c yourconfig.json -n yournetwork.json
  • c 用于指定区块链的配置文件,不指定的话默认为config.json;
  • n 用于指定区块链网络配置文件,不指定的话由-c指定的配置文件定义。

测试结果

除了在控制台显示测试结果之外,该工具还会生成一个html的测试报告:
在这里插入图片描述

Bugs

如果直接拿来跑不对time out进行修改,会遇到 REQUEST_TIMEOUT 的问题:

因为机器性能问题,开启的peer 较多,两分钟不足以在本机上实例化chaincode,因此可能出现timeout问题:

# Instantiate chaincode......
error: [Peer.js]: sendProposal - timed out after:120000
error: [Peer.js]: sendProposal - timed out after:120000
error: [Peer.js]: sendProposal - timed out after:120000
error: [Peer.js]: sendProposal - timed out after:120000
error: [client-utils.js]: sendPeersProposal - Promise is rejected: Error: REQUEST_TIMEOUT
    at Timeout._onTimeout (/home/user1/go/src/github.com/hyperledger/caliper/node_modules/fabric-client/lib/Peer.js:124:19)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)

对两个地方进行修改后,fail交易减少了很多:

1.修改~/caliper/src/fabric/e2eUtils.js#211中的request_time 时间,可以修改为480000或者更高。
2.修改~/caliper/node_modules/fabric-client/config/default.json,我将45000修改到了90000.

感谢

本文主要参考了@曹灰灰同学的博文Hyperledger caliper 安装记录,对我的疑惑进行了认真的的解答与指导,十分感谢作者提供的帮助。

猜你喜欢

转载自blog.csdn.net/cyLee_/article/details/86518392