最近在學習Hyperledger Fabric,它是由 Linux 基金會發起建立的開源區塊鏈分佈式帳本。javascript
Hyperledger Fabric是一個開源區塊鏈實現,開發環境創建在 VirtualBox 虛擬機上,部署環境能夠自建網絡,也能夠直接部署在 BlueMix 上,部署方式可傳統可 Docker 化,共識達成算法插件化,支持用 Go 和 JavaScript 開發智能合約,尤以企業級的安全機制和 membership 機制爲特點。
java
今天來聊聊怎麼搭建一個基於Ubuntu Server的Hyperledger Fabric吧。node
01 基本環境配置
建議用虛擬機裝一個Ubuntu Server版本便可,不要裝圖形界面,那個太卡了。Ubuntu Server版本安裝的時候注意,選擇英文版本的,中文簡體安裝會出錯。git

安裝好後記得換源成阿里雲的,這裏換的時候因爲虛擬機上不支持複製黏貼,只能手動輸入一下啦:github
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
換源後把下面的軟件裝上:golang
-
ssh -
git -
go -
node.js -
docker -
docker-compose
注意大部分只須要sudo apt-install便可安裝,go、docker這種百度一下就知道怎麼裝了,就不展開講了。nodejs記得給升級到最新版本。web
02 fabric環境搭建
如今網上關於fabric的教程大部分是基於1.4版本的,今天來演示下搭建2.1版本的Fabric環境進行學習。算法
2.1 下載fabric源碼
進入GO的目錄建立hyperledger文件夾:docker
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger/
而後下載fabric的源碼,注意這裏用碼雲Gitee的連接,GitHub的太慢了得下到明年去:typescript
git clone https://gitee.com/mirrors/fabric.git

不知道爲啥我這gitee也好慢啊。。。
將 fabric 切換至 2.1 版本:
cd fabric/
git branch -a
git checkout release-2.1


記得加個sudo,沒有管理員權限啥都幹不了。
2.2 下載fabric-samples源碼、fabric鏡像
cd scripts/
sudo ./bootstrap.sh

而後就進入了漫長的等待了。fabric-samples是從GitHub上下載的,可能會比較慢。下載完成後會列出全部下載的docker鏡像。

2.3 網絡測試
上面的工做完成後,當前目錄多了一個fabric-samples文件夾,咱們進去該目錄下的test-network目錄測試下搭建的環境是否成功:
cd fabric-samples/
cd test-network/
而後啓動咱們的測試網絡:
sudo ./network.sh up
記得加sudo保平安哦。開始了一堆代碼以後出現:

說明建立成功了。
03 Fabcar測試
fabcar是一個小demo,咱們這裏運行它測試下看看。咱們回到fabric-samples目錄下,而後進入/fabcar目錄中,先把網絡給清理一下,而後啓動:
sudo ./networkDown.sh
sudo ./startFabric.sh

可能存在的問題
問題1
固然了你可能會遇到問題,好比找不到go命令等,這是由於sudo命令會重置當前的環境變量,致使設置go找不到。
Error: failed to normalize chaincode path: failed to determine module root: exec: "go": executable file not found in $PATH
!!!!!!!!!!!!!!! Chaincode packaging on peer0.org1 has failed !!!!!!!!!!!!!!!!
根據網上解決sudo環境變量問題的方法,在本身的shell配置文件中以下設置
vim ~/.bashrc 添加以下
alias sudo='sudo env PATH=$PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH'

而後刷新下讓配置生效:
source ~/.bashrc
問題2
安裝golang fabric api依賴包的時候,長時間無響應,最後報以下錯誤:
go: github.com/hyperledger/fabric-contract-api-go@v1.0.0: Get https://proxy.golang.org/github.com/hyperledger/fabric-contract-api-go/@v/v1.0.0.mod: dial tcp 172.217.27.145:443: i/o timeout ~/fabric-samples/test-network Finished vendoring Go dependencies ++ peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1 ++ res=1 ++ set +x Error: failed to normalize chaincode path: 'go list' failed with: go: github.com/hyperledger/fabric-contract-api-go@v1.0.0: Get https://proxy.golang.org/github.com/hyperledger/fabric-contract-api-go/@v/v1.0.0.mod: dial tcp 172.217.27.145:443: i/o timeout: exit status 1 !!!!!!!!!!!!!!! Chaincode packaging on peer0.org1 has failed !!!!!!!!!!!!!!!!
ERROR !!! Deploying chaincode failed
解決辦法:
golang1.13.x 能夠直接執行:
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
finally,終於成功了:

04 SDK交互
在上一步執行成功後,會輸出各個語言環境下的SDK交互實例,好比JavaScript是這樣的:
JavaScript:
Start by changing into the "javascript" directory:
cd javascript
Next, install all required packages:
npm install
Then run the following applications to enroll the admin user, and register a new user
called appUser which will be used by the other applications to interact with the deployed
FabCar contract:
node enrollAdmin
node registerUser
You can run the invoke application as follows. By default, the invoke application will
create a new car, but you can update the application to submit other transactions:
node invoke
You can run the query application as follows. By default, the query application will
return all cars, but you can update the application to evaluate other transactions:
node query
能夠選擇javascript,typescript,javago語言與網絡交互,這裏用javascript試試。進入fabcar中的javascript目錄中,這裏得切換到root用戶,我也不知道sudo爲何還存在權限問題,執行:
cd javascript/
su
npm install

安裝相關的依賴。完成後咱們按照合約流程依次啓動:
註冊管理員帳號:
sudo node enrollAdmin.js

註冊用戶:
sudo node registerUser.js

咱們接下來,執行一筆交易
sudo node invoke.js

查詢交易後的狀態:
sudo node query.js

完成啦!!!至此,Fabric的環境已經搭建完成。你們能夠在上面愉快的學習了。
參考資料
最新超詳細的 Hyperledger Fabric2.2 環境搭建部署
https://blog.csdn.net/shengsikandan/article/details/107656060
fabric2.1.0 打包chaincode 報錯
https://blog.csdn.net/qq_32247229/article/details/108860823
go 提示failed to normalize chaincode path
https://www.jason-z.com/post/165
ubuntu16.04下搭建fabric 1.4.3環境
https://blog.csdn.net/Sun_Hui_/article/details/100928155
推薦閱讀:
乾貨 | 學習算法,你須要掌握這些編程基礎(包含JAVA和C++)


本文分享自微信公衆號 - 程序猿聲(ProgramDream)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。