基於Debian搭建Hyperledger Fabric 2.4開發環境及執行簡單案例

丿風色幻想發表於2022-04-06

相關實驗原始碼已上傳:https://github.com/wefantasy/FabricLearn

前言

基於truffle框架實現以太坊公開拍賣智慧合約中我們已經實現了以太坊智慧合約的編寫及部署,但其工作方式註定其只能應用於有限的業務場景中。相比之下,基於超級賬本的Fabric具有高可擴充套件性和高可定製性,能夠應用在更為複雜的商業場景中,但Fabric技術涉及很多新的概念,原始碼跟新速度快且各版本間相容性差,對初學者很不友好。為了使能夠快速掌握Fabric,本文基於其目前最新的2.4版本搭建了一套區塊鏈執行環境,並在此之上部署了官方示例chaincode並對其進行互動除錯,最終整個環境及示例程式碼能夠正常執行且得出預期結果。

環境搭建

網上幾乎所有的Fabric教程都是基於Ubuntu環境而不是Windows,其原因主要是Fabric的執行需要的Docker環境在Windows下表現不佳,此外Fabric許多官方文件也是基於Ubuntu纂寫,在windows下執行可能會遇到難以預估的bug。原本為了方便後期部署至公網伺服器想在CentOS上搭建環境,但由於CentOS8停止維護,且CentOS Stream使用體驗頗差,於是最終選擇了Debian系統。
本環境各系統、軟體版本如下:

系統、軟體 版本
VMware Pro 16.0.0
Debian debian-11.2.0-amd64-DVD-1.iso
git 2.30.2
curl 7.74.0
docker 20.10
golang go1.17.8
jq jq-1.6
fabric 2.4.0
fabric-ca 1.5.2
fabric-samples v2.3.0

本環境各Docker映象版本如下:

映象 版本
hyperledger/fabric-tools 2.4
hyperledger/fabric-peer 2.4
hyperledger/fabric-orderer 2.4
hyperledger/fabric-ccenv 2.4
hyperledger/fabric-baseos 2.4
hyperledger/fabric-ca 1.5

警告:建議Fabric所有實驗過程皆在root許可權下進行,否則在sudo許可權切換的過程中會出現很多環境變數的問題。

雜項安裝

  1. 安裝最新版本Git
    apt install git
    
  2. 安裝最新版本cURL
    apt install curl
    
  3. 安裝Golang
  4. 安裝jq
    apt install jq
    

安裝Fabric

官方指令碼安裝

為了幫助開發者快速搭建Fabric環境,官方建立了一個Fabric環境搭建的批處理工具bootstrap.sh,可以通過該工具直接安裝環境:

wget https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh
chmod +x bootstrap.sh
./bootstrap.sh

不出意外的話會看見指令碼順利的環境安裝過程:
./bootstrap.sh

手動安裝

當然,直接使用官方指令碼不出意外的話肯定會出意外(網路原因),在此我們可以通過手動安裝需要的各項環境。

  1. 安裝fabric-samples
    fabric-samplesFabric的官方Demo集合,其內部包含多個示例,每個示例有GolangJavaScripttypescriptJava的鏈碼實現,並且這些鏈碼可以直接部署到對應的Fabric上,對初學者很有幫助。fabric-samples安裝非常簡單,使用git clone git@github.com:hyperledger/fabric-samples.git將專案原始碼克隆到本地即可,若一直失敗也可以直接在release中下載對應版本的壓縮包。
  2. 安裝Fabric
    Fabric是聯盟鏈的核心開發工具,包含了我們開發、編譯、部署過程中的所有命令。
  3. 下載fabric 2.4.0並解壓
wget https://github.com/hyperledger/fabric/releases/download/v2.4.0/hyperledger-fabric-linux-amd64-2.4.0.tar.gz
mkdir /usr/local/fabric
tar -xzvf hyperledger-fabric-linux-amd64-2.3.2.tar.gz -C /usr/local/fabric
  1. 下載fabric-ca 1.5.2並解壓
wget https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
tar -xzvf hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
mv bin/* /usr/local/fabric/bin
  1. 設定環境變數,在/etc/profile末尾新增
#Fabric
export FABRIC=/usr/local/fabric
export PATH=$PATH:$FABRIC/bin
  1. 更新環境變數source /etc/profile

安裝Docker

  1. 如果存在則移除舊的版本
apt remove docker docker-engine docker.io containerd runc
  1. 更新apt索引包並允許其使用HTTPS安裝
apt update
apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
  1. 新增Docker官方GPG金鑰
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  1. 新增Docker倉庫
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. 安裝Docker引擎
apt update
apt install docker-ce docker-ce-cli containerd.io
  1. 安裝docker-compose
apt install docker-compose

安裝Docker映象依賴

Fabric相關映象均可以在DockerHub官方映象網站進行下載,搜尋需要的映象則可獲取安裝方法,本試驗用到的所有映象為:

docker pull hyperledger/fabric-tools:2.4
docker pull hyperledger/fabric-peer:2.4
docker pull hyperledger/fabric-orderer:2.4
docker pull hyperledger/fabric-ccenv:2.4
docker pull hyperledger/fabric-baseos:2.4
docker pull hyperledger/fabric-ca:1.5

使用docker images命令檢視安裝完成後映象:

hyperledger/fabric-tools     2.4       625237d887db   4 weeks ago    473MB
hyperledger/fabric-peer      2.4       ee643d889779   4 weeks ago    62.3MB
hyperledger/fabric-orderer   2.4       df64446ac2df   4 weeks ago    37.3MB
hyperledger/fabric-ccenv     2.4       da4f00cb576a   4 weeks ago    517MB
hyperledger/fabric-baseos    2.4       0287ebf8aaf3   4 weeks ago    6.94MB
hyperledger/fabric-ca        1.5       4ea287b75c63   6 months ago   69.8MB

示例程式碼中使用的映象標籤都為latest,但如果在pull時直接選擇latest可能會報錯,因此我們在上面映象拉取完成後手動使用以下命令為映象打上latest標籤:

# docker tag IMAGEID(映象id) REPOSITORY:TAG(倉庫:標籤)
docker tag 625237d887db hyperledger/fabric-tools:latest
docker tag ee643d889779 hyperledger/fabric-peer:latest
docker tag df64446ac2df hyperledger/fabric-orderer:latest
docker tag da4f00cb576a hyperledger/fabric-ccenv:latest
docker tag 0287ebf8aaf3 hyperledger/fabric-baseos:latest
docker tag 4ea287b75c63 hyperledger/fabric-ca:latest

最終的映象為:
最終的映象

執行測試

啟動fabric網路

  1. 進入fabric-sample的test-network目錄
cd fabric-samples/test-network
  1. 執行./network.sh up 啟動網路
Creating network "fabric_test" with the default driver
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
Creating peer0.org1.example.com ... done
Creating orderer.example.com    ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done
CONTAINER ID   IMAGE                               COMMAND             CREATED                  STATUS
PORTS
            NAMES
7738c1e84751   hyperledger/fabric-tools:latest     "/bin/bash"         Less than a second ago   Up Less than a second                   cli
1f24de2c6cd5   hyperledger/fabric-peer:latest      "peer node start"   2 seconds ago            Up Less than a second   0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp                peer0.org2.example.com
bfc48b20360c   hyperledger/fabric-orderer:latest   "orderer"           2 seconds ago            Up Less than a second   0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp   orderer.example.com
b9a61fdaf47a   hyperledger/fabric-peer:latest      "peer node start"   2 seconds ago            Up Less than a second   0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp                peer0.org1.example.com

最終出現以上輸出日誌則表示網路啟動成功,每個加入Fabric網路的Node和User都需要隸屬於某個組織,以上網路中包含了兩個平行組織————peer0.org1.example.compeer0.org2.example.com,它還包括一個作為ordering service維護網路的orderer.example.com

建立channel

上節已經在機器上執行了peer節點和orderer節點,現在可以使用network.sh為Org1和Org2之間建立channel。channel是特定網路成員之間的私有通道,只能被屬於該通道的組織使用,並且對網路的其他成員是不可見的。每個channel都有一個單獨的區塊鏈賬本,屬於該通道的組織可以讓其下peer加入該通道,以讓peer能夠儲存channel上的帳本並驗證賬本上的交易。
使用以下命令建立自定義通道testchannel:

./network.sh createChannel -c testchannel

./network.sh createChannel -c testchannel

部署chaincode

建議部署操作全部在root賬戶下進行,否則可能發生未知錯誤,以下流程為筆者在非root使用者下所遇問題,最終重建虛擬機器全部指令在root賬戶下才完成部署。

建立通道後,您可以開始使用智慧合約與通道賬本互動。智慧合約包含管理區塊鏈賬本上資產的業務邏輯,由成員執行的應用程式網路可以在賬本上呼叫智慧合約建立,更改和轉讓這些資產。可以通過./network.sh deployCC命令部署智慧合約,但本過程可能會出現很多問題。
使用以下命令部署chaincode:

./network.sh deployCC -c testchannel -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

此命令執行後可能會出現錯誤:scripts/deployCC.sh: line 114: log.txt: Permission denied,很明顯這是許可權不足所致,加上sudo試試:

./network.sh deployCC -c testchannel -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

加上sudo後出現新的錯誤:deployCC.sh: line 59: go: command not found。檢查本使用者go命令可用,檢查root使用者go命令可用,單單sudo後不能用。查閱資料後發現這是因為linux系統為了安全,限制在使用sudo時會清空自定義的環境變數,最簡單的解決方法是在/etc/sudoers檔案中直接將該限制註釋[1]
vim /etc/sudoers
加上註釋後重新執行上條命令,又出現了新的錯誤:

go: github.com/golang/protobuf@v1.3.2: Get "https://proxy.golang.org/github.com/golang/protobuf/@v/v1.3.2.mod": dial tcp 172.217.160.81:443: i/o timeout

很明顯這是因為本地網路無法訪問proxy.golang.org所致,在命令列輸入go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn,direct命令配置國內代理[2]後再次執行。令人意外的是錯誤不變,設定的代理沒有生效?手動使用go get github.com/golang/protobuf手動下載安裝後再次執行錯誤還是不變,此時檢查本地GOPATH目錄下已有github.com/golang/protobuf包,為什麼沒有識別到?此時靈機一動,使用go env檢視GOPATH環境變數,發現與本地使用者不一致,原來sudo命令會使用rootgo環境變數,而之前設定的代理、下載的包都只能在本地使用者下生效,因此這個問題最終的解決方案是直接切換到root使用者下重新配置go代理並執行。成功執行後可看見如下結果:

2021-08-15 00:45:54.064 PDT [chaincodeCmd] ClientWait -> INFO 001 txid [ebeb8df6904f45b81fb30714f7eecb30b4bbfd32f4acc809f34f7c660e396eb8] committed with status (VALID) at localhost:7051
2021-08-15 00:45:54.144 PDT [chaincodeCmd] ClientWait -> INFO 002 txid [ebeb8df6904f45b81fb30714f7eecb30b4bbfd32f4acc809f34f7c660e396eb8] committed with status (VALID) at localhost:9051
Chaincode definition committed on channel 'testchannel'
Using organization 1
Querying chaincode definition on peer0.org1 on channel 'testchannel'...
Attempting to Query committed status on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID testchannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'testchannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org1 on channel 'testchannel'
Using organization 2
Querying chaincode definition on peer0.org2 on channel 'testchannel'...
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID testchannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'testchannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org2 on channel 'testchannel'
Chaincode initialization is not required

合約互動

安裝fabric中我們已經設定了fabric可執行檔案的環境變數,需保證可以成功在test-network目錄下使用peer命令。

  1. 設定FABRIC_CFG_PATH變數,其下需包含core.yaml檔案
export FABRIC_CFG_PATH=$PWD/../config/
# export FABRIC_CFG_PATH=/usr/local/fabric/config/
  1. 設定其它Org1組織的變數依賴
# Environment variables for Org1
# CORE_PEER_TLS_ROOTCERT_FILE和CORE_PEER_MSPCONFIGPATH環境變數指向Org1的organizations資料夾中的身份證照。
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
  1. 初始化chaincode
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'

初始化chaincode
4. 查詢賬本資產列表

peer chaincode query -C testchannel -n basic -c '{"Args":["GetAllAssets"]}'

查詢賬本資產列表
5. 修改賬本資產

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'

修改賬本資產
6. 關閉網路

./network.sh down

該命令將停止並刪除節點和鏈碼容器、組織加密材料、刪除之前執行的通道專案和docker卷,並從Docker Registry移除鏈碼映象。

因為asset-transfer (basic)鏈碼的背書策略需要交易同時被Org1Org2簽名,所以鏈碼呼叫指令需要使用--peerAddresses標籤來指向peer0.org1.example.compeer0.org2.example.com;因為網路的TLS被開啟,指令也需要用--tlsRootCertFiles標籤指向每個peer節點的TLS證照。

參考


  1. qq_JWang_03215367. 解決command not found 報錯. 慕課. [2018-07-31] ↩︎

  2. 沐沐子楓. failed to normalize chaincode path: 'go list' failed with: go. 部落格園. [2020-11-27] ↩︎

相關文章