• 沒有找到結果。

Fabric 區塊鏈安裝流程-以 basic-network 單台主機環境為例 33

第三章 系統架構

第二節 Fabric 區塊鏈網絡

二、 Fabric 區塊鏈安裝流程-以 basic-network 單台主機環境為例 33

於不同台電腦的 Docker Container 可以互相溝通,讓組織身分設定可以設定 到多台環境上。

圖 18 Fabric 區塊鏈網絡

二、

Fabric 區塊鏈安裝流程-以 basic-network 單台主機環境為例

Fabric 的安裝細節相當複雜,因此本研究歸納一份 Fabric 在 Ubuntu 的 basic-network 單台主機環境安裝流程以利於 Fabric 的上手。整體流程分為安 裝前置環境作業及 Fabric 網絡安裝。

(一) 安裝前置環境作業:

Fabric 網絡的前置環境包括 docker、Python、golang、nodejs、npm、

Hyperledger images、curl,對於沒有指令基礎的初學者來說較為複雜,因此

Docker Swarm

Host Host

Docker 為 Fabric 網絡的運行環境,Fabric 的 Peer、Orderer、CA 等都是 利用 docker image 安裝,運行在 docker container 上;而 Python 及 golang 為 我們本機上運行 Fabric 網絡所需要的運作環境;nodejs 為運作 Fabric node sdk 及 Fabric node Chaincode 所需的環境,而 npm 為管理 nodejs 套件的工具;curl 為有助於我們安裝的工具,可以使用網路上的指令檔。

$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get install curl

安裝 docker: $ curl -fsSL https://get.docker.com/ | sh

讓 user 可以有 docker 指令權限: $ sudo usermod -aG docker yourusername 重啟 docker 環境: $ sudo service docker restart

安裝 docker-compose: $ sudo curl -L

https://github.com/docker/compose/releases/download/1.20.1/docker-compose-`u name -s`-`uname -m` -o /usr/local/bin/docker-compose

增加執行權限:$ sudo chmod +x /usr/local/bin/docker-compose 檢查版本(是否安裝成功): $ docker-compose -version

安裝 Python: $ sudo apt-get install python-pip

安裝 golang 環境:

$ sudo apt-get install -y cpp gccgo "build-essential" libsnappy-dev zlib1g-dev libbz2-dev

$ wget https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz

$ sudo mv go /usr/lib/go-1.9.1/

安裝 nodejs 和 npm,使用 nvm 安裝利於切換版本,(原本版本需要 V6 LTS 版本,但 Nodejs Chaincode api 撰寫需使用 V8.10):

$ sudo apt-get install build-essential libssl-dev

$ curl https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash

$ source ~/.profile

$ nvm install v6.9.5

$ nvm install v8.10

$ npm install npm@latest -g

$ nvm use v8.10

$ node –v

下載 hyperledger images:

Fabric 當中有 peer、ca、orderer、tools、couchdb、kafka、zookeeper、javaenv、

ccenv 等 image,本研究撰寫期間,hpyerledger docker hub 上選用 1.1.0 版本 安裝。

可以使用以下指令安裝:

$ docker pull hyperledger/fabric-peer:x86_64-1.1.0

$ docker pull hyperledger/fabric-ca:x86_64-1.1.0

$ docker pull hyperledger/fabric-orderer:x86_64-1.1.0

$ docker pull hyperledger/fabric-tools:x86_64-1.1.0

$ docker pull hyperledger/fabric-couchdb:latest

$ docker pull hyperledger/fabric-kafka:latest

$ docker pull hyperledger/fabric-zookeeper:latest

$ docker pull hyperledger/fabric-javaenv:x86_64-1.1.0

$ docker pull hyperledger/fabric-ccenv:x86_64-1.1.0

$ docker pull hyperledger/fabric-baseimage:x86_64-0.4.7

$ docker pull hyperledger/fabric-baseos:x86_64-0.4.7 並將 image 標籤為 latest 版本:

$ docker tag hyperledger/fabric-ca:x86_64-1.1.0 hyperledger/fabric-ca:latest

$ docker tag hyperledger/fabric-tools:x86_64-1.1.0 hyperledger/fabric-tools:latest

$ docker tag hyperledger/fabric-couchdb:x86_64-1.1.0 hyperledger/fabric-couchdb:latest

$ docker tag hyperledger/fabric-kafka:x86_64-1.1.0 hyperledger/fabric-kafka:latest

$ docker tag hyperledger/fabric-zookeeper:x86_64-1.1.0 hyperledger/fabric-zookeeper: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 指令寫進 script 檔中,只要執行 script 檔,便會全部執行 完畢。

利用 curl 建立 fabric 的 binary 工具(也可利用 curl 下載 docker image,我選擇 用上述的方式自行操作 version 以及 Tag):

$ cd /home/your user name/fabric_binary

$ curl -sSL https://goo.gl/6wtTN5 | bash -s 1.1.0

$ ls bin

$ vi ~/.bashrc (最下面加入 5 行(最下面應會看到有 nvm path 的設定,要在 那之下),每次登入進來方便 go 及 nodejs 環境設定)

export PATH=/usr/lib/go-1.9.1/bin:$PATH export GOPATH=/opt/gopath/

export GOROOT=/usr/lib/go-1. 9.1/

export PATH=/home/your user name/fabric_binary/bin:$PATH nvm use v8.10

$ source ~/.bashrc

(二) Fabric 網絡的安裝步驟:

接下來進入 Fabric 網絡的安裝步驟,可以使用官方的 basic-network 或 是本研究使用的包含 Nodejs Chaincode api 的 basic-nerwotk。整體安裝步驟 包括第 0 步,配置 crypto-config.yaml、生成公私鑰和證書、配置

configtx.yaml、生成創世區塊和 Channel 配置,第 1 步,配置

docker-compose.yml 以啟動 Fabric 網絡、create channel、join channel、install chaincode 及 instantiate chaincode。

其中 crypto-config.yaml 及 configtx.yaml 會在本節第三點中進行詳細說 明。

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

圖 19 Fabric 網絡安裝步驟圖

首先下載範例檔:git clone

https://github.com/wearetheledger/fabric-network-boilerplate.git,這邊採用 wearetheledger 的 github 檔,是為了方便以後使用 wearetheledger 提供的 Nodejs Chaincode api 撰寫 Nodejs Chaincode,但 basic-network 的範例是相同 的。

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

圖 20 Fabric 網絡安裝步驟圖 – 第 0 步

在官方的 basic-network 中已經幫我們產生好第 0 步,如需要更改內容,可 以更改第 0 步的配置檔,然後使用generate.sh重新部署環境。

0.1:生成公私鑰和證書(Peer 及 Orderer 的設定)

0.1.1:配置crypto-config.yaml:裡面需要設定組織的名字和域名,還有需要幾 套公私鑰和證書。

0.1 跟 0.1.1 都使用 0.1.2 來執行。

0.1.2:$ cryptogen generate –config=./crypto-config.yaml

0.2:生成創世區塊和 Channel 配置

0.2.1:配置configtx.yaml:Orderer 可以設定共識的演算法、共識區塊大小、切 割時間等,而 Peer 的配置包含了 MSP 的配置、Anchor Peer 的配置等,如需

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

要設置更多 Organization 及 Channel 需要對此作修改。

0.2 跟 0.2.1 的指令用 0.2.2、0.2.3 跟 0.2.4 指令執行。

0.2.2:生成創世區塊(產生 Orderer 需要):$ configtxgen –profile OneOrgOrdererGenesis –outputBlock ./config/genesis.block 0.2.3:生成 Channel 配置(application channel):$

configtxgen -profile OneOrgChannel

-outputCreateChannelTx ./config/channel.tx -channelID $CHANNEL_NAME 0.2.4:配置 Anchor Peer:$ configtxgen -profile OneOrgChannel

-outputAnchorPeersUpdate ./config/Org1MSPanchors.tx -channelID

$CHANNEL_NAME -asOrg Org1MSP

圖 21 Fabric 網絡安裝步驟圖 – 第 1 步

1:雖然接下來只要啟動範例中寫好的startFabric.sh的 script 檔即完成安

1.1:startFabric.sh 會呼叫 basic-network 目錄中的start.sh檔。

1.2:start.sh 會啟用docker-compose.yml檔,也就是 Fabric network 的生成檔,

裡面會設定網絡中的 ca、orderer、peer、couchdb 及 cli 的 image file、環境、

掛載目錄等等,掛載也包括會把之前產生的 msp、genesis.block 等檔案掛載 到 docker 的目錄裡。

執行指令啟動 yml 檔:$ docker-compose -f docker-compose.yml up -d ca.example.com orderer.example.com peer0.org1.example.com couchdb 1.3:以組織中的成員身分進入並執行 peer channel create 指令:$

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.e xample.com/msp" peer0.org1.example.com peer channel create -o

orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx 以組織中的成員身分進入並執行 peer channel join 指令:$

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.e xample.com/msp" peer0.org1.example.com peer channel join -b

mychannel.block

1.4:使用 docker-compose.yml 生成 cli docker container:$

docker-compose -f ./docker-compose.yml up -d cli 1.4.1:install chaincode fabcar 到 peer 上: docker exec -e

"CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fa bric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.exam ple.com/msp" cli peer chaincode install -n fabcar -v 1.0 -p "$CC_SRC_PATH" -l

"$LANGUAGE"

1.4.2:在 mychannel 這個 channel 中初始 chaincode 並生成對應 Channel 的 docker container: docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fa bric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.exam ple.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n fabcar -l "$LANGUAGE" -v 1.0 -c

'{"function":"init","Args":["v0"]}

' -P "OR ('Org1MSP.member','Org2MSP.member')"

1.4.3:測試是否安裝成功:$ docker exec -e

"CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fa bric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.exam ple.com/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n fabcar -c '{"function":"initLedger","Args":[""]}'

三、

Fabric 區塊鏈身分、組織及通道管理配置

行證書生成和配置操作。Fabric 提供了 cryptogen (crypto Generator)工具,可以根 據配置自動批量生成所需要的證書和私鑰文件。cryptogen 工具支持從配置文件 (通常命名為 crypto-config.yaml)中讀入 YAML 格式的配置模板訊息。

crypto-config.yaml 配置文件範例如下:[15]

# "OrdererOrgs" - Definition of organizations managing orderer nodes OrdererOrgs:

- Name: Orderer

Domain: example.com

#一個節點包括 Hostname、CommonName、SANS 等域,可以用 Specs 指定 一組節點,或著用 Template 指定自動生成節點的個數。默認通用名為主機 名.組織域,例如域 org1.example.com 中 Peer 節點的名稱可能為

peer0.org1.example.com、peer1.org1.example.com 等。

Specs:

# "PeerOrgs" – Definition of organizations managing peer nodes PeerOrgs:

兩個 Peer 組織則採用 Template 自動生成了 Count 個數的主機。Users 的 Count 值則會生成指定個數的普通用戶。

我們配置好 crypto-config.yaml 文件後,就可以用 cryptogen 去讀取該文件,

並生成對應的公私鑰和證書了:

cryptogen generate –config=./crypto-config.yaml

生成的文件都保存到 crypto-config 資料夾,可以查看資料夾中生成了哪些文件:

tree crypto-config

(三) 生成通道配置

配合 cryptogen 生成的組織結構身分文件使用,離線生成跟通道有關的配置信 息。configtxgen 工具支持從配置文件(通常命名為 configtx.yaml)中讀入 YAML 格 式的配置模板訊息。

主要功能有:

1. 生成創世區塊

2. 生成 Channel 配置區塊

3. Anchor(錨) Peer 的配置

configtx.yaml 配置文件範例如下: [16]

# profile

# - Different configuration profiles may be encoded here to be specified as parameters to the configtxgen tool

Profiles:

TwoOrgsOrdererGenesis: # Orderer system channel configuration Capabilities:

# Section: Organizations

# - This section defines the different organizationl identities which will be referenced later in the configuration including orderer and peer organizations.

Organizations:

# SECTION: Orderer

# - Configuration of Orderer. 包括共識類型、Address、Batch、Channel 數量等,

被 Profiles 部分引用。

Orderer: &OrdererDefaults

# Orderer Type: The orderer implementation to start

# SECTION: Application

# - Configuration of Application Channels Application: &ApplicationDefaults

Organizations: #加入到通道中的組織的訊息

# SECTION: Capabilities

# - This section defines the capabilities of fabric network. This is a new

# concept as of v1.1.0 and should not be utilized in mixed networks with

# v1.0.x peers and orderers.

# For instance, if a new MSP type is added, newer binaries

# binaries without this support would be unable to validate those

# transactions. This could lead to different versions of the fabric binaries

# having different world states.

Application: &ApplicationCapabilities V1_1: true

配置修改好後,利用 configtxgen 工具:

生成創世區塊

configtxgen –profile TwoOrgsOrdererGenesis –outputBlock genesis.block

生成 Channel 配置區塊

configtxgen –profile TwoOrgsChannel –outputCreateChannelTx channel.tx –channelID mychannel

Anchor Peer 設定

configtxgen –profile TwoOrgsChannel –outputAnchorPeersUpdate

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

Org1MSPanchors.tx –channelID mychannel –asOrg Org1MSP

configtxgen –profile TwoOrgsChannel –outputAnchorPeersUpdate Org2MsPanchors.tx –channelID mychannel –asOrg Org2MSP

完成後可以看到 4 個文件: channel.tx、genesis.block、Org1MSPanchors.tx 和 Org2MSPanchors.tx。

相關文件