区块链学习(6) Hyperledger Fabric环境搭建(mac版)

时间:2022-06-05 12:15:22

Fabric简介

Fabric项目的目标是实现一个通用的权限区块链的底层基础框架。为了适用于不同的场合,采用模块化架构,提供可切换和可扩展的组建,包括共识算法,加密安全,数字资产,记录仓库,智能合约和身份鉴权等服务。Fabric克服了比特币等公有链项目的权限,如吞吐量低,无隐私性,无最终确定性以及共识算法低效等,使得用户能够方便地开发商业应用。

在超级账本联盟成立之前,IBM公司就已经开源了一个叫做”开放区块链”(OpenBlockchain,OBC)项目。在联盟成立之后,IBM把OBC项目约44000行代码贡献给了Linux基金会,这部分代码成为了Fabric的代码的主要组成部分。在2016年3约的一次黑客松编程活动中,Blockstream和数字资产两个成员公司把格子的区块链功能代码融合到OBC中,最终建立了Fabric的雏形,也就是Fabric项目进入孵化阶段的基础代码。

现在如京东,阿里,腾讯这些大公司都在钻研非数字货币的区块链技术,都是用的这种权限区块链的思想,也就是联盟链。所以, 联盟链的商业价值,并不比公有链的价值低。

安装Docker

首先安装Docker,安装好后可以确认Docker和Docker Compose的版本:

docker --version
docker-compose --version

安装Go语言

Hyperledger Fabric主要是基于go语言的,所以得安装go语言以及配置它的环境,具体流程请参考我的上一篇文章

克隆Hyperledger Fabric Samples

luoxiaohui:fabric luoxiaohui$ ls
luoxiaohui:fabric luoxiaohui$ git clone https://github.com/hyperledger/fabric-samples.git
Cloning into 'fabric-samples'...
remote: Counting objects: 1373, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 1373 (delta 6), reused 34 (delta 4), pack-reused 1337
Receiving objects: 100% (1373/1373), 512.34 KiB | 204.00 KiB/s, done.
Resolving deltas: 100% (608/608), done.

下载Platform-specific Binaries

由于国内网络原因,不能直接从https://goo.gl上下载,需要先下载一个shell文件,此shell文件地址在这里,将此文件命名为boostrap.sh,拷贝此文件到fabric-sample目录下。将bootstrap.sh文件的第9行版本由1.0.4改为1.1.0接下来在命令行中执行如下:

luoxiaohui:fabric-samples luoxiaohui$ git tag
v1.0.2
v1.0.6
v1.1.0
v1.1.0-alpha
v1.1.0-preview
v1.1.0-rc1
luoxiaohui:fabric-samples luoxiaohui$ git checkout v1.1.0
Previous HEAD position was ba0a098... FAB-5995 Update samples to work with v1.0.2
HEAD is now at 1252c7a... [FAB-8920] Pin fabric-samples to node.js "~1.1.0" luoxiaohui:fabric-samples luoxiaohui$ sh bootstrap.sh 

然后就是漫长的等待下载啦~
下载fabric工具成功后,在命令行里看log,应该是没有error的。此时再看fabric-samples目录下,多了一个bin目录,将此bin目录绝对路径设置到环境变量里。

创建网络

打开fabric-sample下的示例first-network,其中byfn.sh为启动这个网络的启动脚本,启动脚本中除建立一个包含4个节点和1个Order service的网络外,还会启动一个容器用来执行脚本在channel中加入节点,部署和初始化chaincode,以及在部署的chaincode上执行交易。
启动脚本
第一步,生成必要文件,执行命令:

luoxiaohui:fabric-samples luoxiaohui$ cd first-network/
luoxiaohui:first-network luoxiaohui$ ./byfn.sh -m generate

默认channel名称为mychannel,脚本程序会给网络实例生成数字证书和密钥;生成genesis block用来启动ordering service;一些用来配置channel的配置交易。
第二步,启动网络,执行命令:

luoxiaohui:first-network luoxiaohui$ ./byfn.sh -m up

当你看到下面的文字的时候,说明启动成功:

Starting with channel 'mychannel' and CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
2018-03-21 09:41:41.994 UTC [main] main -> INFO 001 Exiting.....
LOCAL_VERSION=1.1.0
DOCKER_IMAGE_VERSION=1.1.0
Creating network "net_byfn" with the default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_orderer.example.com" with default driver
Creating peer1.org2.example.com ... 
Creating peer0.org2.example.com ... 
Creating peer0.org1.example.com ... 
Creating orderer.example.com ... 
Creating peer1.org1.example.com ... 
Creating peer1.org2.example.com
Creating peer0.org2.example.com
Creating peer1.org1.example.com
Creating orderer.example.com
Creating peer0.org1.example.com ... done
Creating cli ... 
Creating cli ... done

 ____    _____      _      ____    _____ 
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |  
 ___) |   | |    / ___ \  |  _ <    | |  
|____/    |_|   /_/   \_\ |_| \_\   |_|  

Build your first network (BYFN) end-to-end test

关闭网络:

./byfn.sh -m down

上面通过脚本./byfn.sh生成了一个fabric网络,接下来我们将详细说明脚本中所执行的命令信息。

参考文章:
http://hyperledger-fabric.readthedocs.io/en/latest/build_network.html
https://www.jianshu.com/p/8beb3a355f99