1. 程式人生 > >區塊鏈 建立Hyperledger Fabric區塊鏈網路

區塊鏈 建立Hyperledger Fabric區塊鏈網路

fabric-samples-1.4.0 開發搭建

一、環境安裝前準備:

(1)首先安裝基本工具

yum install git
yum install curl
yum -y install epel-release
yum install python-pip
pip install --upgrade pip

(2)安裝Docker,安裝好後可以確認Docker和Docker Compose的版本:

docker --version
docker-compose --version

(2)安裝Go語言

# go 環境變數
export PATH=
$PATH:/usr/local/go/bin export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$HOME/go/bin # go 版本 go version

(3)安裝Node.js

yum install gcc gcc-c++
gcc -v

4.8.5版本過低,若已為最新版請跳過至步驟,升級gcc,前往http://ftp.gnu.org/gnu/gcc/下載你想要的版本

wget http://ftp.gnu.org/gnu/gcc/gcc-8.2.0/gcc-8.2.0.tar.gz
tar -C /usr/local -xzf gcc-
8.2.0.tar.gz cd /usr/local/gcc-8.2.0 ./contrib/download_prerequisites mkdir build cd build ../configure -enable-checking=release -enable-languages=c,c++ disable-multilib yum groupinstall "Development Tools" make #需要很長時間 make install reboot #重啟

(4)繼續安裝nodejs

node -v

(5)安裝 npm

npm install npm -g
npm -v

二、 下載fabric-samples原始碼

(1)git clone https://github.com/hyperledger/fabric-samples.git
(2)目前官網預設是 1.3 版本需要修改配置檔案

注意:需要安裝二進位制可執行檔案 和配置檔案,可以提前下載好二進位制檔案上傳到
/root/go/src/github.com/hyperledger/fabric-samples 下
下載地址:https://download.csdn.net/download/liruizi/10849641

#進入 scripts 目錄
/root/go/src/github.com/hyperledger/fabric-samples/scripts
[[email protected] scripts]# vi bootstrap.sh 

在這裡插入圖片描述

三、想要現在執行嗎

fabric提供一個完全註釋的指令碼byfn.sh,利用這些Docker映象可以快速引導一個由4個代表2個不同組織的peer節點以及一個排序服務節點的Hyperledger fabric網路。它還將啟動一個容器來執行一個將peer節點加入channel、部署例項化鏈碼服務以及驅動已經部署的鏈碼執行交易的指令碼。

以下是該byfn.sh指令碼的幫助文件:
./byfn.sh -h
Usage:
  byfn.sh -m up|down|restart|generate [-c <channel name>] [-t <timeout>]
  byfn.sh -h|--help (print this message)
    -m <mode> - one of 'up', 'down', 'restart' or 'generate'
      - 'up' - bring up the network with docker-compose up
      - 'down' - bring up the network with docker-compose up
      - 'restart' - bring up the network with docker-compose up
      - 'generate' - generate required certificates and genesis block
    -c <channel name> - config name to use (defaults to "mychannel")
    -t <timeout> - CLI timeout duration in microseconds (defaults to 10000)

Typically, one would first generate the required certificates and
genesis block, then bring up the network. e.g.:

  byfn.sh -m generate -c <channelname>
  byfn.sh -m up -c <channelname>

如果你選擇不提供channel名稱,則指令碼將使用預設名稱mychannel。CLI超時引數(用-t標誌指定)是一個可選值;如果你選擇不設定它,那麼CLI容器將會在指令碼執行完之後退出。

生成網路神器

準備好了嗎?好吧!執行以下命令。你將會看到會發生什麼伴隨yes/no命令列提示的簡要說明。輸入y來執行描述的動作。

./byfn.sh -m generate
Generating certs and genesis block for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
/root/go/src/github.com/hyperledger/fabric-samples/bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x

/root/go/src/github.com/hyperledger/fabric-samples/bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2018-12-14 09:14:11.119 CST [common.tools.configtxgen] main -> WARN 001 Omitting the channel ID for configtxgen for output operations is deprecated.  Explicitly passing the channel ID will be required in the future, defaulting to 'testchainid'.
2018-12-14 09:14:11.119 CST [common.tools.configtxgen] main -> INFO 002 Loading configuration
2018-12-14 09:14:11.131 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2018-12-14 09:14:11.131 CST [common.tools.configtxgen.localconfig] Load -> INFO 004 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.145 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 005 orderer type: solo
2018-12-14 09:14:11.145 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 006 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.149 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Generating genesis block
2018-12-14 09:14:11.151 CST [common.tools.configtxgen] doOutputBlock -> INFO 008 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2018-12-14 09:14:11.196 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2018-12-14 09:14:11.215 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.230 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2018-12-14 09:14:11.230 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.231 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2018-12-14 09:14:11.232 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2018-12-14 09:14:11.277 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2018-12-14 09:14:11.293 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.309 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2018-12-14 09:14:11.309 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.309 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2018-12-14 09:14:11.309 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2018-12-14 09:14:11.355 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2018-12-14 09:14:11.371 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.387 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2018-12-14 09:14:11.387 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2018-12-14 09:14:11.387 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2018-12-14 09:14:11.388 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

第一步生成我們各種網路實體的所有證書和金鑰,genesis block用於引導排序服務,以及配置Channel所需要的一組交易配置集合。

啟動網路

接下來,你可以使用以下命令來啟動整個網路。再試提示你是否繼續。回答y:

./byfn.sh -m up
Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
LOCAL_VERSION=1.4.0
DOCKER_IMAGE_VERSION=1.4.0-rc1
=================== WARNING ===================
  Local fabric binaries and docker images are  
  out of  sync. This may cause problems.       
===============================================
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
Pulling orderer.example.com (hyperledger/fabric-orderer:latest)...
latest: Pulling from hyperledger/fabric-orderer
3b37166ec614: Already exists
504facff238f: Already exists
ebbcacd28e10: Already exists
c7fb3351ecad: Already exists
2e3debadcbf7: Already exists
fc435e46e32e: Already exists
a4922bafdce8: Already exists
2cb4429027c1: Already exists
8c2b02bf9e47: Pull complete
7e18daca12c4: Pull complete
Creating peer0.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating orderer.example.com    ... done
Creating peer1.org1.example.com ... done
Creating peer1.org2.example.com ... done
Creating cli                    ... done

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

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

Channel name : mychannel
Creating channel...
+ peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
+ res=0
+ set +x
2018-12-14 01:14:52.493 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2018-12-14 01:14:52.513 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2018-12-14 01:14:52.516 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-12-14 01:14:52.547 UTC [cli.common] readBlock -> INFO 004 Received block: 0
===================== Channel 'mychannel' created ===================== 

Having all peers join the channel...
+ peer channel join -b mychannel.block
+ res=0
+ set +x
2018-12-14 01:14:52.602 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2018-12-14 01:14:52.616 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2018-12-14 01:14:52.619 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-12-14 01:14:52.638 UTC [channelCmd] executeJoin -> INFO 004 Successfully submitted proposal to join channel
===================== peer0.org1 joined channel 'mychannel' ===================== 

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2018-12-14 01:14:55.755 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2018-12-14 01:14:55.768 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no