系統環境:
[root@localhost ~]# cat /proc/version
Linux version 3.10.0-1160.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Mon Oct 19 16:18:59 UTC 2020
官網手冊:
https://www.hyperledger.org/use/fabric
環境搭建
一、基礎軟體安裝
# 安裝Git(Git是一個免費的、開源的分布式版本控制系統)
# https://git-scm.com/downloads
[root@localhost ~]# yum install git
# 驗證
[root@localhost ~]# git version
git version 1.8.3.1
安裝cURL
cURL是一個利用URL語法在命令列下作業的檔案傳輸工具,它支持檔案上傳和下載,所以是綜合傳輸工具
# https://curl.haxx.se/download.html
[root@localhost ~]# yum install curl
# 驗證
[root@localhost ~]# curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.53.1 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets
安裝Docker(Docker Engine)
Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的鏡像中,然后發布到任何流行的 Linux或Windows 機器上,也可以實作虛擬化
# https://docs.docker.com/get-docker/
Docker引擎(Docker Engine)安裝請參考該教程:https://blog.csdn.net/Bjxhub/article/details/119352882
# 關鍵命令
[root@localhost ~]# sudo yum install docker-ce docker-ce-cli containerd.io
# 確認安裝后的docker版本
[root@localhost ~]# docker --version
Docker version 20.10.7, build f0df350
# Docker Compose 依賴 Docker Engine 進行任何有意義的作業,因此請確保根據您的設定,在本地或遠程安裝了 Docker Engine,
# 使用 Docker Compose 可以輕松、高效的管理容器,它是一個用于定義和運行多容器 Docker 的應用程式工具
Docker Compose安裝官方教程:https://docs.docker.com/compose/install/
# 下載docker-compose,建議關閉防火墻,否則報錯:curl: (35) Encountered end of file
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]#
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 賦權
[root@localhost ~]# sudo chmod +x /usr/local/bin/docker-compose
# 驗證
[root@localhost ~]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c
#Docker引擎(Docker Engine)與Docker Compose的版本(此處均取最新)對應關系:
https://docs.docker.com/compose/compose-file/compose-versioning/
# 啟動
[root@localhost ~]# sudo systemctl start docker
# 開機啟動
[root@localhost ~]# sudo systemctl enable docker
安裝Go
Google 的 Robert Griesemer,Rob Pike 及 Ken Thompson 開發的一種靜態強型別、編譯型語言
##!!!!----若Go與Jq無法安裝,則需添加安裝源:[[root@localhost ~]# yum install epel-release]----!!!!!
# https://golang.org/doc/install(國內無法訪問)
[root@localhost ~]# yum install go
# 驗證
[root@localhost ~]# go version
go version go1.15.5 linux/amd64
安裝JQ
jq是一個輕量級且靈活的命令列JSON處理器
# https://stedolan.github.io/jq/download/
[root@localhost ~]# yum install jq
# 驗證
[root@localhost ~]# jq --version
jq-1.6
二、 安裝 Fabric 和 Fabric 示例
2.1 自動搭建(不推薦,國內基本不成功,推薦直接進入2.2 手動搭建)
# 創建一個作業目錄并進入
[root@localhost ~]# mkdir -p mkdir -p $HOME/go/src/github.com/fabric
[root@localhost ~]# cd $HOME/go/src/github.com/fabric
# 查看當前全路徑
[root@localhost fabric]# pwd
/root/go/src/github.com/fabric
# 方式一: 執行自動搭建腳本(不推薦)
# !!!!自動下載最新版本的 Fabric 示例、docker 鏡像和二進制檔案,!!!!
[root@localhost fabric]# curl -sSL https://bit.ly/2ysbOFE | bash -s
# 方式二: 更換腳本地址再執行(不推薦)
若上述命令報錯:curl: (35) Encountered end of file
原因:命令中的https://bit.ly/2ysbOFE在國內無法訪問,
官方給出了可在國內可訪問的新地址:https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/bootstrap.sh
解決方案:執行如下自動搭建命令(需要一個漫長的等待~)
[root@localhost fabric]# curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/bootstrap.sh| bash -s
Clone hyperledger/fabric-samples repo
===> Cloning hyperledger/fabric-samples repo
正克隆到 'fabric-samples'...
error: RPC failed; result=35, HTTP code = 0
fatal: The remote end hung up unexpectedly
fabric-samples v2.3.2 does not exist, defaulting main
fatal: Not a git repository (or any of the parent directories): .git
Pull Hyperledger Fabric binaries
===> Downloading version 2.3.2 platform specific fabric binaries
===> Downloading: https://github.com/hyperledger/fabric/releases/download/v2.3.2/hyperledger-fabric-linux-amd64-2.3.2.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 73.5M 100 73.5M 0 0 75919 0 0:16:56 0:16:56 --:--:-- 98853
==> Done.
===> Downloading version 1.5.0 platform specific fabric-ca-client binary
===> Downloading: https://github.com/hyperledger/fabric-ca/releases/download/v1.5.0/hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
curl: (35) Encountered end of file
gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
==> There was an error downloading the binary file.
------> 1.5.0 fabric-ca-client binary is not available to download (Available from 1.1.0-rc1) <----
<—!!!—>
若想繼續自動搭建,參考教程:https://blog.csdn.net/mutou___/article/details/109849720
<—!!!—>
2.2 手動搭建搭建(推薦)
Step1、下載fabric原始碼
# 進入之前創建的作業目錄
[root@localhost ~]# cd $HOME/go/src/github.com/fabric
# !!不推薦該獲取方式
[root@localhost fabric]# git clone https://github.com/hyperledger/fabric.git
# 注意:這一步下載會很慢,可以使用科學上網方法
# 也可以使用Chrome瀏覽器中GitHub加速插件,下面使用加速插件連接替換
# !!!!推薦以下獲取方式:
# 以下代碼執行其中一行即可,
git clone https://hub.fastgit.org/hyperledger/fabric.git
# or
git clone https://github.com.cnpmjs.org/hyperledger/fabric.git
# or
git clone https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric.git
Step2、下載Docker鏡像
[root@localhost fabric]# cd fabric/scripts/
[root@localhost scripts]# sudo ./bootstrap.sh -b -s
......
===> List out hyperledger docker images
hyperledger/fabric-tools 2.3 a206a1593b4c 3 months ago 448MB
hyperledger/fabric-tools 2.3.2 a206a1593b4c 3 months ago 448MB
hyperledger/fabric-tools latest a206a1593b4c 3 months ago 448MB
hyperledger/fabric-peer 2.3 85c825d4769f 3 months ago 54.2MB
hyperledger/fabric-peer 2.3.2 85c825d4769f 3 months ago 54.2MB
hyperledger/fabric-peer latest 85c825d4769f 3 months ago 54.2MB
hyperledger/fabric-orderer 2.3 7cad713cbfea 3 months ago 37.8MB
hyperledger/fabric-orderer 2.3.2 7cad713cbfea 3 months ago 37.8MB
hyperledger/fabric-orderer latest 7cad713cbfea 3 months ago 37.8MB
hyperledger/fabric-ccenv 2.3 627c556b15ca 3 months ago 514MB
hyperledger/fabric-ccenv 2.3.2 627c556b15ca 3 months ago 514MB
hyperledger/fabric-ccenv latest 627c556b15ca 3 months ago 514MB
hyperledger/fabric-baseos 2.3 e50ea411d694 3 months ago 6.86MB
hyperledger/fabric-baseos 2.3.2 e50ea411d694 3 months ago 6.86MB
hyperledger/fabric-baseos latest e50ea411d694 3 months ago 6.86MB
hyperledger/fabric-ca 1.5 24a7c19a9fd8 4 months ago 70.8MB
hyperledger/fabric-ca 1.5.0 24a7c19a9fd8 4 months ago 70.8MB
hyperledger/fabric-ca latest 24a7c19a9fd8 4 months ago 70.8MB
Step3、下載fabric-samples
[root@localhost scripts]# git clone https://hub.fastgit.org/hyperledger/fabric-samples.git
正克隆到 'fabric-samples'...
remote: Enumerating objects: 7662, done.
remote: Counting objects: 100% (129/129), done.
remote: Compressing objects: 100% (99/99), done.
remote: Total 7662 (delta 48), reused 74 (delta 27), pack-reused 7533
接收物件中: 100% (7662/7662), 4.37 MiB | 1.08 MiB/s, done.
處理 delta 中: 100% (3974/3974), done.
Step4、下載二進制檔案
[root@localhost scripts]# cat bootstrap.sh
# 查看以下版本資訊
# if version not passed in, default to latest released version
VERSION=2.3.2
# if ca version not passed in, default to latest released version
CA_VERSION=1.5.0
#
[root@localhost scripts]# cd /opt
# 因需要用到wget命令,未安裝的需要安裝,也可以采用手動下載再上傳的方式,
[root@localhost opt]# yum install wget
# 下載hyperledger-fabric-linux-amd64-2.3.0.tar.gz
[root@localhost opt]# wget https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric/releases/download/v2.3.2/hyperledger-fabric-linux-amd64-2.3.2.tar.gz
# 下載hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz
[root@localhost opt]# wget https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric-ca/releases/download/v1.5.0/hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz
# 查看已下載壓縮包
[root@localhost opt]# ls
containerd hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz hyperledger-fabric-linux-amd64-2.3.2.tar.gz
# 解壓(兩個壓縮檔案解壓出 bin/ config/ 兩個目錄,兩個目錄應位于fabric-samples之下~)
[root@localhost opt]# tar -zxvf hyperledger-fabric-linux-amd64-2.3.2.tar.gz -C $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/
[root@localhost opt]# tar -zxvf hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz -C $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/
#
其他參考鏈接:
Ubuntu 系統環境手動安裝:https://blog.csdn.net/mutou___/article/details/109986259
三、 啟動測驗網路
# 進入test-network目錄
[root@localhost test-network]# cd $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/test-network
# 洗掉先前可能運行的網路
[root@localhost test-network]# ./network.sh down
Stopping network
Stopping cli ... done
Stopping orderer.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Removing cli ... done
Removing orderer.example.com ... done
Removing peer0.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing network fabric_test
Removing volume docker_orderer.example.com
Removing volume docker_peer0.org1.example.com
Removing volume docker_peer0.org2.example.com
WARNING: The DOCKER_SOCK variable is not set. Defaulting to a blank string.
Removing network fabric_test
WARNING: Network fabric_test not found.
Removing volume docker_peer0.org3.example.com
WARNING: Volume docker_peer0.org3.example.com not found.
Removing remaining containers
Removing generated chaincode docker images
# 執行./network.sh up 前,建議重啟下Docker容器,
否則可能會報錯:
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-10eb04b81c62 -j RETURN: iptables: No chain/target/match by that name.
[root@localhost test-network]# systemctl restart docker
# 如仍未解決,請嘗試關閉防火墻:
[root@localhost test-network]# systemctl stop firewalld
# 啟動測驗網路
[root@localhost test-network]# ./network.sh up
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' with crypto from 'cryptogen'
LOCAL_VERSION=2.3.2
DOCKER_IMAGE_VERSION=2.3.2
/root/go/src/github.com/fabric/fabric/scripts/fabric-samples/bin/cryptogen
Generating certificates using cryptogen tool
Creating Org1 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output=organizations
org1.example.com
+ res=0
Creating Org2 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output=organizations
org2.example.com
+ res=0
Creating Orderer Org Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output=organizations
+ res=0
Generating CCP files for Org1 and Org2
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 orderer.example.com ... done
Creating peer0.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c231a061efeb hyperledger/fabric-tools:latest "/bin/bash" Less than a second ago Up Less than a second cli
d8105d031285 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
13ebc165136a 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
e77cc6055337 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, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp peer0.org2.example.com
# 網路啟動驗證,可查看啟動了以下幾個模擬網路的容器:
# 測驗網路包括兩個對等組織,Org1 和 Org2,它還包括一個維護網路訂購服務的單一訂購者組織,
[root@localhost test-network]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c231a061efeb hyperledger/fabric-tools:latest "/bin/bash" 49 seconds ago Up 48 seconds cli
d8105d031285 hyperledger/fabric-orderer:latest "orderer" 51 seconds ago Up 49 seconds 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
13ebc165136a hyperledger/fabric-peer:latest "peer node start" 51 seconds ago Up 49 seconds 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp peer0.org1.example.com
e77cc6055337 hyperledger/fabric-peer:latest "peer node start" 51 seconds ago Up 49 seconds 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp peer0.org2.example.com
四、 創建頻道(channel)
# 默認 為mychannel,可以自定義channel名:./network.sh createChannel -c <自定義Channel名>
[root@localhost test-network]# ./network.sh createChannel
Creating channel 'mychannel'.
If network is not up, starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb
Generating channel genesis block 'mychannel.block'
/root/go/src/github.com/fabric/fabric/scripts/fabric-samples/bin/configtxgen
.......
2021-08-05 05:31:24.428 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-08-05 05:31:24.442 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
Anchor peer set for org 'Org2MSP' on channel 'mychannel'
Channel 'mychannel' joined
五、 啟用鏈碼(chaincode)
# 在啟用碼鏈前,需要更改go代理,確保啟用成功,
否則,會報如下錯誤:
......
Error: failed to normalize chaincode path: 'go list' failed with: : signal: killed
Chaincode packaging has failed
Deploying chaincode failed
[root@localhost test-network]# go env -w GOPROXY=https://goproxy.io,direct
[root@localhost test-network]# go env -w GO111MODULE=on
# deployCC子命令將在peer0.org1.example.com和peer0.org2.example.com上安裝資產轉移(基本)鏈碼,然后在使用通道標志指定的通道上部署鏈碼(如果未指定通道,則在mychannel上部署),
[root@localhost test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
deploying chaincode on channel 'mychannel'
executing with the following
......
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 'mychannel'
Chaincode initialization is not required
六、 與網路互動(peer)
# <!--1、進入作業目錄--!>
# 確保您從test-network目錄中操作
# 進入test-network目錄
[root@localhost ~]# cd $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/test-network
# <!--2、添加環境變數--!>
# 添加二進制檔案的環境變數
[root@localhost test-network]# export PATH=${PWD}/../bin:$PATH
# FABRIC_CFG_PATH
[root@localhost test-network]# export FABRIC_CFG_PATH=$PWD/../config/
# <!--3、Org1環境變數設定--!>
# 設定允許peer以Org1身份操作CLI的環境變數:
[root@localhost test-network]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network]# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
[root@localhost test-network]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
[root@localhost test-network]# export CORE_PEER_ADDRESS=localhost:7051
# <!--4、Org1初始化分類賬--!>
# 使用資產初始化分類賬( initialize the ledger with assets)
[root@localhost test-network]# 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 mychannel -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":[]}'
2021-08-05 15:53:20.773 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
# <!--4、Org1獲取分類賬的資產串列--!>
# 獲取添加到渠道分類賬的資產串列(get the list of assets that were added to your channel ledger)
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300},{"ID":"asset2","color":"red","size":5,"owner":"Brad","appraisedValue":400},{"ID":"asset3","color":"green","size":10,"owner":"Jin Soo","appraisedValue":500},{"ID":"asset4","color":"yellow","size":10,"owner":"Max","appraisedValue":600},{"ID":"asset5","color":"black","size":15,"owner":"Adriana","appraisedValue":700},{"ID":"asset6","color":"white","size":15,"owner":"Michel","appraisedValue":800}]
# <!--5、Org1修改賬本上資產的所有者--!>
# 通過呼叫資產轉移(基本)鏈代碼來更改賬本上資產的所有者(change the owner of an asset on the ledger by invoking the asset-transfer (basic) chaincode)
[root@localhost test-network]# 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 mychannel -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"]}'
2021-08-05 15:58:26.650 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
# <!--6、Org2環境變數設定--!>
# 設定允許peer以Org2身份操作CLI的環境變數:
[root@localhost test-network]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network]# export CORE_PEER_LOCALMSPID="Org2MSP"
[root@localhost test-network]# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
[root@localhost test-network]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
[root@localhost test-network]# export CORE_PEER_ADDRESS=localhost:9051
# <!--7、Org2查詢分類賬的資產(asset6)--!>
# 查詢在peer0.org2.example.com上運行的資產轉移(基本)鏈代碼
# query the asset-transfer (basic) chaincode running on peer0.org2.example.com
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}
#關閉網路
[root@localhost test-network]# ./network.sh down
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/292110.html
標籤:區塊鏈
上一篇:配置Bitcoin節點
