区块链Fabric-从入门到实战 Fabric环境搭建

Posted Jornici

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区块链Fabric-从入门到实战 Fabric环境搭建相关的知识,希望对你有一定的参考价值。

Fabric ubuntu16虚拟机环境搭建

Ubuntu16镜像

ubuntu-16.04.3-server-amd64.iso (如有需要,私信我)

激活root用户

su -s

passwd root

修改软件源

cd /etc/apt/

sudo cp sources.list sources.list.bak

sudo gedit sources.list

deb-src http://mirror.neu.edu.cn/ubuntu/ xenial main restricted #Added by software-properties

deb http://mirror.neu.edu.cn/ubuntu/ xenial main restricted

deb-src http://mirror.neu.edu.cn/ubuntu/ xenial restricted multiverse universe #Added by software-properties

deb http://mirror.neu.edu.cn/ubuntu/ xenial-updates main restricted

deb-src http://mirror.neu.edu.cn/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties

deb http://mirror.neu.edu.cn/ubuntu/ xenial universe

deb http://mirror.neu.edu.cn/ubuntu/ xenial-updates universe

deb http://mirror.neu.edu.cn/ubuntu/ xenial multiverse

deb http://mirror.neu.edu.cn/ubuntu/ xenial-updates multiverse

deb http://mirror.neu.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse

deb-src http://mirror.neu.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties

deb http://archive.canonical.com/ubuntu xenial partner

deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://mirror.neu.edu.cn/ubuntu/ xenial-security main restricted

deb-src http://mirror.neu.edu.cn/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties

deb http://mirror.neu.edu.cn/ubuntu/ xenial-security universe

deb http://mirror.neu.edu.cn/ubuntu/ xenial-security multiverse

sudo apt-get update

sudo apt-get upgrade

sudo apt install net-tools -y

设置固定ip

虚拟机环境下需要看dhcp的范围,再设置静态ip

sudo vi /etc/network/interfaces

##########################################

auto lo

iface lo inet loopback

auto ens33 //注意自己的网卡名称

iface ens33 inet static

address 192.168.154.106

netmask 255.255.255.0

gateway 192.168.1.2 #虚拟机环境下网关1 则连不上网,需要配置2

#########################################

#永久修改

sudo vi /etc/resolvconf/resolv.conf.d/base

#插入内容

nameserver 114.114.114.114

nameserver 8.8.8.8

sudo resolvconf -u

sudo /etc/init.d/networking restart

sudoip addr flush ens32 # 刷新IP

sudo systemctl restart networking.service

如果出现虚拟机连不上网,请将vmnet8网卡删除,再次添加,后执行

sudoip addr flush ens32 # 刷新IP

sudo systemctl restart networking.service

并如实际情况修改 sudo vi /etc/network/interfaces

即可。

开启SSH 链接

sudo apt-get install openssh-server

sudo ps -e |grep ssh

sudo service ssh start

Ubuntu 安装Docker

docker version 18.09

sudo apt install -y docker.io

systemctl enable docker.service

安装docker-compose

docker-compose version 1.22

sudo curl -Lhttps://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 --version

Go Ubuntu安装

上传go1.15.11.linux-amd64.tar.gz

cp/home/jornici/go1.15.11.linux-amd64.tar.gz /usr/local/share/

cd/usr/local/share/

tar-C /usr/local/ -zxf go1.15.11.linux-amd64.tar.gz

mkdir-p /root/go/bin

mkdir-p /root/go/src

mkdir-p /root/go/pkg

cat/etc/profile

vim/etc/profile

export GOROOT=/usr/local/go

export GOPATH=/root/go

export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

source/etc/profile

git 安装

sudo apt install -y git

Fabric安装以及E2E执行

#当前路径为/root

mkdir -p go/src/github.com/hyperledger/

cd go/src/github.com/hyperledger/

git clone -b v1.4.4 https://github.com/hyperledger/fabric

git clone -b v1.4.4 https://github.com/hyperledger/fabric-samples

cd fabric

apt-get install -y make

apt-get install -y gcc

make cryptogen

make configtxgen

make configtxlator

cd.build/

cd bin/

ls

cp * /usr/bin/

cd /root/go/src/github.com/hyperledger/fabric

ls

make peer

make orderer

make peer-docker

make orderer-docker

make tools-docker

cd ../fabric-samples/first-network/

docker rm -f $(docker ps -aq) && docker rmi -f $(docker images | grep dev |awk 'print $3') && docker volume prune

./byfn.sh generate

./byfn.sh up

./byfn.sh down

实战:区块链hyperledger fabric 初体验 - 4:钻石交易展示平台

本文Step by Step 搭建一个由IBM提供的基于Fabric的钻石交易展示平台。

本文环境是

ubuntu 16.04 4.4.0-104-generic

Docker version 17.03.0-ce, build 60ccb22

docker-compose version 1.14.0, build c7bdf9e

0.设置本地环境

0.1 安装git

省略


0.2 安装Go

下载Go for linux

https://studygolang.com/dl/golang/go1.10.linux-amd64.tar.gz

$ sudo tar -C /usr/local -xzf /your-download-path/go1.10.linux-amd64.tar.gz

修改.bashrc 添加
export PATH=$PATH:/usr/local/go/bin
执行~/.bashrc

$ source ~/.bashrc

验证安装结果

$ go version


0.3 安装Node.js

Download and install Node.js v6.2.0

https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.gz

安装

$ sudo tar -C /usr/local -xzf /your-download-pathnode-v6.9.5-linux-x64.tar.gz

修改.bashrc 添加
export PATH=$PATH:/usr/local/node-v6.9.5-linux-x64/bin
执行~/.bashrc

$ source ~/.bashrc

验证安装结果

$ node -v
$ npm -v


1、下载钻石交易应用


$ git clone https://github.com/IBM-Blockchain/marbles.git --depth 1
$ cd marbles/
$ git checkout master

2、创建区块链网络

2.1 下载Fabric Sample

$ git clone https://github.com/hyperledger/fabric-samples.git
$ cd fabric-samples

$ git checkout v1.1.0

$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o setup_script.sh

2.2 修改setup_script.sh

export VERSION=${1:-1.1.0}

export THIRDPARTY_IMAGE_VERSION=1.0.6

2.3 运行setup_script.sh下载平台相关的二进制文件和images

sudo bash setup_script.sh

2.4 为下载的image建立latest tag

  docker tag  hyperledger/fabric-tools:x86_64-1.1.0 hyperledger/fabric-tools:latest
  docker tag hyperledger/fabric-orderer:x86_64-1.1.0 hyperledger/fabric-orderer:latest
  docker tag hyperledger/fabric-peer:x86_64-1.1.0 hyperledger/fabric-peer:latest
  docker tag hyperledger/fabric-javaenv:x86_64-1.1.0 hyperledger/fabric-javaenv:latest
  docker tag hyperledger/fabric-ccenv:x86_64-1.1.0 hyperledger/fabric-ccenv:latest
  docker tag hyperledger/fabric-couchdb:x86_64-1.0.6 hyperledger/fabric-couchdb:latest
  docker tag hyperledger/fabric-kafka:x86_64-1.0.6 hyperledger/fabric-kafka:latest
  docker tag hyperledger/fabric-zookeeper:x86_64-1.0.6 hyperledger/fabric-zookeeper:latest

2.5 编辑.bashrc把下载的二进制文件加入PATH环境变量

export PATH=$PWD/bin:$PATH

2.6 执行.bashrc

$ source ~/.bashrc

2.7 启动网络

$ cd ./fabcar
$ ./startFabric.sh

2.8 检查网络,执行docker ps 应该有红框内的5个containers.

技术分享图片

2.9 安装测试所需的软件

$ npm install

2.10 用Fabcar测试网络

    node enrollAdmin.js
    node registerUser.js
    node query.js

技术分享图片

3.安装并实例化链代码

3.1  安装

$ cd /YOUR_PATH/marbles/
$ npm install

3.2 再次执行下面脚本去创建证书

$ cd /YOUR_FABRIC_SAMPLE_PATH/fabcar

$   node enrollAdmin.js

$  node registerUser.js
$   node query.js

3.3 修改marbles/config/connection_profile_local.json

                "credentialStore": {
                        "path": "/YOUR_FABRIC_SAMPLE_PATH/fabcar/hfc-key-store"
                }
                        "x-adminCert": {
                                "path": "/YOUR_FABRIC_SAMPLE_PATH/basic-network/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/admincerts/[email protected]"
                        },
                        "x-adminKeyStore": {
                                "path": "/YOUR_FABRIC_SAMPLE_PATH/basic-network/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/"
                        }

3.4 安装链码

$ ./scripts

$ node install_chaincode.js

技术分享图片

3.5 实例化链码

$ node instantiate_chaincode.js

技术分享图片

4. 托管 Marbles

$ cd ..
$ sudo npm install gulp -g

BTW: 解决“sudo: npm:找不到命令”问题

$ sudo ln -s /usr/local/node-v6.9.5-linux-x64/bin/node /usr/bin/node
$ sudo ln -s /usr/local/node-v6.9.5-linux-x64/lib/node /usr/lib/node
$ sudo ln -s /usr/local/node-v6.9.5-linux-x64/lib/npm /usr/lib/npm

$ npm install

技术分享图片

4.5 运行钻石交易应用

$ gulp marbles_local

BTW:if you run into the issue like

error: [Client.js]: Failed to load user "admin" from local key value store. Error: TypeError: privateKey.isPublic is not a function
    at _cryptoSuite.importKey.then.then (/home/yan/workspace/marbles/node_modules/fabric-client/lib/User.js:255:45)
error: [Client.js]: Failed to load an instance of requested user "admin" from the state store on this Client instance. Error: TypeError: privateKey.isPublic is not a function
    at _cryptoSuite.importKey.then.then (/home/yan/workspace/marbles/node_modules/fabric-client/lib/User.js:255:45)
error: [fcw] Failed to get enrollment marblesDockerComposeNetworkmychannelOrg1MSPfabricpeerorg1 TypeError: privateKey.isPublic is not a function
    at _cryptoSuite.importKey.then.then (/home/yan/workspace/marbles/node_modules/fabric-client/lib/User.js:255:45)
error: [fcw] could not format error
error: Exhausted all CAs. There are no more CAs to try.
error: could not enroll...
warn: Error enrolling admin

you may need copy hfc-key-store directory to your home directory $HOME/.hfc-key-store, and re-config the client.credentialStore.path of connection_profile_local.json

5 使用 Marbles

5.1 访问http://localhost:3001/login

技术分享图片

5.2 点击Login 设置应用

技术分享图片

点击Guided

技术分享图片

点击next step

技术分享图片

点击next step

技术分享图片

点击next step

技术分享图片

点击 create

技术分享图片

点击next step

技术分享图片

点击Enter

5.3 创建钻石

技术分享图片

技术分享图片

点击Create,查看控制台

技术分享图片

技术分享图片

技术分享图片

技术分享图片

5.4 交易钻石,拖拽Amy的蓝钻给Alice

技术分享图片

技术分享图片

技术分享图片

技术分享图片

5.5 删除钻石

技术分享图片

技术分享图片

技术分享图片

技术分享图片

5.5 故事模式

点击Settings

技术分享图片

技术分享图片

点击Enable然后交易钻石

技术分享图片

技术分享图片

未完待续......





以上是关于区块链Fabric-从入门到实战 Fabric环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

学习Hyperledger Fabric 实战联盟链

实战:区块链hyperledger fabric 初体验 - 4:钻石交易展示平台

实战:区块链hyperledger fabric 初体验 - 3: 链码实例安装实例化调用及代码

万字解析——区块链hyperledger fabric2.2部署实战教程

万字解析——区块链hyperledger fabric2.2部署实战教程

学习Hyperledger Fabric 实战联盟链 实战高清无密