目錄
安裝環境
0. 前提條件:
1. 下載Fabric(版本2.3.3)
2. 安裝示例、二進制和 Docker 鏡像(直接看方法三)
(1)方法一(未成功):按官網直接執行
(2)方法二(未成功):直接執行bootstrap.sh腳本 ,與方法一相同的錯誤
(3)方法三: 直接下載bootstrap.sh中的檔案
3. 啟動測驗網路
4. 創建通道(channel)
5. 安裝go依賴
6. 在通道啟動一個鏈碼
7. 與網路互動
參考檔案
安裝環境
虛擬機:VMware Workstation 15 Pro
虛擬機安裝的作業系統:Centos7
0. 前提條件:
(1)安裝Git 參考 Centos7 安裝 git_ling1998的博客-CSDN博客
(2)安裝Go 參考 Centos7 安裝 Go_ling1998的博客-CSDN博客
(3)安裝Docker-ce 參考 Centos7 安裝 Docker_ling1998的博客-CSDN博客
(4)安裝Docker Compose 參考 Centos7 安裝 Docker Compose_ling1998的博客-CSDN博客
1. 下載Fabric(版本2.3.3)
[root@localhost ~]# mkdir -p $GOPATH/src/github.com/hyperledger # 創建下載目錄
[root@localhost ~]# cd $GOPATH/src/github.com/hyperledger #進入目錄
[root@localhost hyperledger]# git clone https://github.com/hyperledger/fabric.git
下載太慢了,要等等等(可以使用科學上網工具)
下載時可能出現下面錯誤,多嘗試幾次

2. 安裝示例、二進制和 Docker 鏡像(直接看方法三)
(1)方法一(未成功):按官網直接執行
curl -sSL https://bit.ly/2ysbOFE | bash -s #官網

(2)方法二(未成功):直接執行bootstrap.sh腳本 ,與方法一相同的錯誤
#進入fabric/scripts目錄
cd fabric/scripts
#執行bootstrap.sh
./bootstrap.sh

(3)方法三: 直接下載bootstrap.sh中的檔案
參見bootstrap.sh腳本中相關內容,得到3個下載地址
VERSION=2.3.3 #fabric版本
......
CA_VERSION=1.5.2 #fabric-ca版本
ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
......
cloneSamplesRepo() {
......
#下載fabirc-samples
git clone -b main https://github.com/hyperledger/fabric-samples.git && cd fabric-samples
......
}
......
pullBinaries() {
echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
#下載fabric二制進檔案
download "${BINARY_FILE}" "https://github.com/hyperledger/fabric/releases/download/v${VERSION}/${BINARY_FILE}"
......
#下載fabric-ca二進制檔案
download "${CA_BINARY_FILE}" "https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"
......
}
......
BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz
首先,下載fabric-sample,嘗試使用git與wget,多次執行報錯后(如下圖),直接在windows中下載,然后傳到centos7對應的目錄下(如下圖目錄圖),下載地址:GitHub - hyperledger/fabric-samples

上傳到fabric/scripts目錄中,與bootstrap.sh腳本同層級

其次,下載fabric二進制檔案(版本2.3.3)使用wget報錯,直接在windows下載,然后上傳到centos7中解壓,下載地址為:https://github.com/hyperledger/fabric/releases/download/v2.3.3/hyperledger-fabric-linux-amd64-2.3.3.tar.gz
最后,下載fabric-ca二進制檔案(版本1.5.2),需關閉防火墻,同時科學上網,如果wget報錯,建議還是在windows瀏覽器中下載,然后上傳到centos7中解壓,下載地址為:?????https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
[root@localhost scripts]# systemctl stop firewalld.service #關閉防火墻
[root@localhost scripts]# firewall-cmd --state #查看防火墻狀態
not running
[root@localhost scripts]# wget https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz #下載
以上2個壓縮檔案都下載到了fabric/scripts目錄中

解壓檔案至fabric-samples中
[root@localhost scripts]# tar -xvf hyperledger-fabric-linux-amd64-2.3.3.tar.gz -C fabric-samples
[root@localhost scripts]# tar -xvf hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz.tar.gz -C fabric-samples

執行./bootstrap.sh腳本
[root@localhost scripts]# ./bootstrap.sh -b #僅拉取fabric的鏡像檔案
設定全域都可以使用二進制檔案
[root@localhost scripts]# cd fabric-samples/bin #進入bin目錄
[root@localhost bin]# ls
configtxgen cryptogen fabric-ca-client idemixgen osnadmin
configtxlator discover fabric-ca-server orderer peer
[root@localhost bin]# cp * /usr/local/bin #將二進制檔案copy至/usr/local/bin下
3. 啟動測驗網路
[root@localhost bin]# cd .. #回到fabric-samples目錄
[root@localhost fabric-samples]# cd test-network #進入test-network目錄
[root@localhost test-network]# ./network.sh #啟動測驗網路腳本
-bash: ./network.sh: Permission denied #提示執行network.sh檔案時沒有權限
[root@localhost test-network]# chmod u+x network.sh #授權
[root@localhost test-network]# ./network.sh #再次執行測驗網路腳本
[root@localhost test-network]# ./network.sh up #啟動測驗網路

4. 創建通道(channel)
[root@localhost test-network]# ./network.sh createChannel

5. 安裝go依賴
[root@localhost fabric-samples]# cd asset-transfer-basic/chaincode-go/ #進入目錄
[root@localhost chaincode-go]# ls #查看目錄內容
assetTransfer.go chaincode go.mod go.sum #此時沒有vendor目錄
[root@localhost chaincode-go]# go env #查看go環境配置
......
GOPROXY="https://proxy.golang.org,direct" #查看代理設定
......
[root@localhost chaincode-go]# go env -w GOPROXY=https://goproxy.cn #更改代理
[root@localhost chaincode-go]# go mod vendor #安裝依賴
[root@localhost chaincode-go]# ls #查看目錄內容
assetTransfer.go chaincode go.mod go.sum vendor #此時出現vendor目錄
6. 在通道啟動一個鏈碼
[root@localhost chaincode-go]# cd ..
[root@localhost asset-transfer-basic]# cd ..
[root@localhost fabric-samples]# cd test-network #進入到test-network目錄
[root@localhost test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go #在通道中啟動一個鏈碼(官網)
若啟動鏈碼時出現錯誤:./network.sh: line 270: scripts/deployCC.sh: Permission denied
則去授權,最好將目錄下的所有.sh檔案都授權,避免之后執行時再出現授權錯誤

[root@localhost test-network]# cd scripts
[root@localhost scripts]# ls -l
total 36
-rw-r--r--. 1 root root 2284 Oct 25 02:33 configUpdate.sh
-rwxr--r--. 1 root root 2562 Oct 25 02:33 createChannel.sh
-rw-r--r--. 1 root root 11675 Oct 25 02:33 deployCC.sh
-rw-r--r--. 1 root root 3476 Oct 25 02:33 envVar.sh
drwxr-xr-x. 2 root root 58 Oct 25 02:33 org3-scripts
-rw-r--r--. 1 root root 1941 Oct 25 02:33 setAnchorPeer.sh
-rw-r--r--. 1 root root 7269 Oct 25 02:33 utils.sh
[root@localhost scripts]# chmod -R +x ../scripts #目錄下所有檔案都授權
[root@localhost scripts]# ls -l
total 36
-rwxr-xr-x. 1 root root 2284 Oct 25 02:33 configUpdate.sh
-rwxr-xr-x. 1 root root 2562 Oct 25 02:33 createChannel.sh
-rwxr-xr-x. 1 root root 11675 Oct 25 02:33 deployCC.sh
-rwxr-xr-x. 1 root root 3476 Oct 25 02:33 envVar.sh
drwxr-xr-x. 2 root root 58 Oct 25 02:33 org3-scripts
-rwxr-xr-x. 1 root root 1941 Oct 25 02:33 setAnchorPeer.sh
-rwxr-xr-x. 1 root root 7269 Oct 25 02:33 utils.sh
[root@localhost scripts]# cd ..
[root@localhost test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
7. 與網路互動
(按官網上步驟執行即可,參見官網 使用Fabric的測驗網路 — hyperledger-fabricdocs master 檔案)
[root@localhost test-network]# export PATH=${PWD}/../bin:$PATH
[root@localhost test-network]# export FABRIC_CFG_PATH=$PWD/../config/
[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
[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-10-25 17:53:30.230 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'[{"AppraisedValue":300,"Color":"blue","ID":"asset1","Owner":"Tomoko","Size":5},{"AppraisedValue":400,"Color":"red","ID":"asset2","Owner":"Brad","Size":5},{"AppraisedValue":500,"Color":"green","ID":"asset3","Owner":"Jin Soo","Size":10},{"AppraisedValue":600,"Color":"yellow","ID":"asset4","Owner":"Max","Size":10},{"AppraisedValue":700,"Color":"black","ID":"asset5","Owner":"Adriana","Size":15},{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Michel","Size":15}]
[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-10-25 17:55:15.507 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
[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
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Christopher","Size":15}
參考檔案:
使用Fabric的測驗網路 — hyperledger-fabricdocs master 檔案
??????關于fabric中執行./bootstrap.sh之后出錯的解決方案_lakersssss24的博客-CSDN博客
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/341958.html
標籤:區塊鏈
上一篇:區塊鏈入門
