1. 準備工作
- 安裝nodejs
sudo apt-get install nodejs sudo apt install nodejs-legacy # 驗證版本 node -v # 更新 npm 包鏡像源 sudo npm config set registry https://registry.npm.taobao.org
- 安裝npm
sudo apt-get install npm # 驗證版本 npm -v
- 安裝jq
sudo apt-get install jq # 驗證版本 jq -V
- 安裝postgreSQL,過程中需設置密碼
# 更新 apt 包索引 sudo apt-get update # 安裝數據庫,安裝後會自動添加 postgres 的操作系統用戶,密碼是隨機的 sudo apt-get install postgresql # 打開客戶端工具 psql sudo -u postgres psql # 修改 postgres 數據庫用戶的密碼爲123456 postgres=# ALTER USER postgres WITH PASSWORD '123456'; # 退出 psql postgres=# \q # 修改 ubuntu 操作系統的 postgres 用戶的密碼 sudo passwd -d postgres sudo -u postgres passwd # 修改配置實現遠程訪問 vi /etc/postgresql/9.5/main/postgresql.conf # 修改連接權限 #listen_addresses = 'localhost' 改爲 listen_addresses = '*' # 啓用密碼驗證 #password_encryption = on 改爲 password_encryption = on # 設置所有用戶可連接 vi /etc/postgresql/9.4/main/pg_hba.conf # 在最後一行插入以下內容 host all all 0.0.0.0/0 md5 # 重啓服務 /etc/init.d/postgresql restart
- git在上篇已經安裝過了,不再贅述
2. 拉取項目
- 進入文件目錄
cd ~ cd go/hyperledger/
- clone blockchain-explorer
git clone https://github.com/hyperledger/blockchain-explorer.git
- 修改文件夾權限
sudo chmod -R 777 blockchain-explorer
3. 創建數據庫
- 修改配置文件
cd ~ # 進入配置文件目錄 cd go/hyperledger/blockchain-explorer/app/ # 修改配置文件 username passwd vi explorerconfig.json
- 進入數據庫文件目錄
cd ~ cd go/hyperledger/blockchain-explorer/app/persistence/fabric/postgreSQL/db/
- 創建數據庫
sudo ./createdb.sh
- 查看是否創建成功
# 連接 postgresSQL sudo -u postgres psql # 查看已有數據庫 \l # 查看已創建表 \d # 退出 \q
4. 啓動first-network
- 進入文件目錄
cd ~ cd go/hyperledger/fabric/fabric-samples/first-network/
- 啓動網絡
./byfn.sh up
5. 修改Explorer Fabric配置
- 進入文件目錄
cd ~ cd go/hyperledger/blockchain-explorer/app/platform/fabric/connection-profile/
- 修改配置文件
vi first-network.json
- 修改 fabric_path 爲實際目錄
6. 構建Hyperledger Explorer
- 進入文件目錄
cd ~ cd go/hyperledger/blockchain-explorer/
- install, run tests, build project
# 如果此步驟運行報錯,請參照最下方異常處理,更換 node 版本 # 更換成功後需先執行 ./main.sh clean 然後再重複此步驟 ./main.sh install
- test REST API, UI components
./main.sh test
7. 運行
cd ~
cd go/hyperledger/blockchain-explorer/
./start.sh
8. chaincode測試
-
進入cli容器
docker exec -it cli bash
-
查詢
# -C,--channelID: 當前命令運行的通道 # -n,--name: chaincode 的名字 # -c, --ctor: JSON 格式的構造參數 # 查詢 A 賬戶餘額,返回 90 peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' # 查詢 B 賬戶餘額,返回 210 peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
-
交易
# -o, --orderer: orderer 節點的地址 # -C,--channelID: 當前命令運行的通道 # -n,--name: chaincode 的名字 # -c, --ctor: JSON 格式的構造參數 # --tls: 通信時是否使用 tls 加密 # --cafile: 當前 orderer 節點 pem 格式的 tls 證書文件,要使用絕對路徑 # --peerAddresses: 指定要連接的 peer 節點的地址 # --tlsRootCertFiles: 連接的 peer 節點的 TLS 根證書 # A 賬戶向 B 賬戶轉賬 10,成功返回 Chaincode invoke successful. result: status:200 peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddress peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddress peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
-
交易執行後可再執行查詢測試,區塊瀏覽器數據即時更新。
每執行一次交易,區塊瀏覽器 BLOCKS & TRANSACTIONS 加一
異常處理
- 第6步
./main.sh install
報錯,是因爲Hyperledger Explorer需要Nodejs 8.11.x,需升級node版本cd ~ # 進入文件目錄 cd /usr/local/ # 下載 node-v8.11.4 sudo wget https://npm.taobao.org/mirrors/node/latest-v8.x/node-v8.11.4-linux-x64.tar.xz # 解壓 sudo tar xvf node-v8.11.1-linux-x64.tar.xz # 返回根目錄 cd ~ # 修改環境變量 vi ~/.bashrc # 在最後一行加入以下內容 export PATH=/usr/local/node-v8.11.4-linux-x64/bin:$PATH # 環境變量立即生效 source ~/.bashrc # 查看 node 版本 node -v