Hyperledger Fabric 1.4 搭建区块链浏览器
Hyperledger Fabric 1.4 搭建區塊鏈瀏覽器
原創iCyberpunk 最后發布于2020-01-09 12:11:01 閱讀數 233 ?收藏
展開
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
1
2
3
4
5
6
7
8
安裝npm
sudo apt-get install npm
# 驗證版本
npm -v
1
2
3
4
安裝jq
sudo apt-get install jq
# 驗證版本
jq -V
1
2
3
4
安裝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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
git在上篇已經安裝過了,不再贅述
2. 拉取項目
進入文件目錄
cd ~
cd go/hyperledger/
1
2
clone blockchain-explorer
git clone https://github.com/hyperledger/blockchain-explorer.git
1
修改文件夾權限
sudo chmod -R 777 blockchain-explorer
1
3. 創建數據庫
修改配置文件
cd ~
# 進入配置文件目錄
cd go/hyperledger/blockchain-explorer/app/
# 修改配置文件 username passwd
vi explorerconfig.json
1
2
3
4
5
6
7
進入數據庫文件目錄
cd ~
cd go/hyperledger/blockchain-explorer/app/persistence/fabric/postgreSQL/db/
1
2
創建數據庫
sudo ./createdb.sh
1
查看是否創建成功
# 連接 postgresSQL
sudo -u postgres psql
# 查看已有數據庫
\l
# 查看已創建表
\d
# 退出
\q
1
2
3
4
5
6
7
8
9
10
11
4. 啟動first-network
進入文件目錄
cd ~
cd go/hyperledger/fabric/fabric-samples/first-network/
1
2
啟動網絡
./byfn.sh up
1
5. 修改Explorer Fabric配置
進入文件目錄
cd ~
cd go/hyperledger/blockchain-explorer/app/platform/fabric/connection-profile/
1
2
修改配置文件
vi first-network.json
1
修改 fabric_path 為實際目錄
6. 構建Hyperledger Explorer
官方示例:https://github.com/hyperledger/blockchain-explorer
進入文件目錄
cd ~
cd go/hyperledger/blockchain-explorer/
1
2
install, run tests, build project
# 如果此步驟運行報錯,請參照最下方異常處理,更換 node 版本
# 更換成功后需先執行 ./main.sh clean 然后再重復此步驟
./main.sh install
1
2
3
test REST API, UI components
./main.sh test
1
7. 運行
cd ~
cd go/hyperledger/blockchain-explorer/
?? ?
./start.sh
1
2
3
4
5
8. chaincode測試
進入cli容器
docker exec -it cli bash
1
查詢
# -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"]}'
1
2
3
4
5
6
7
8
9
交易
# -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"]}'
1
2
3
4
5
6
7
8
9
10
11
交易執行后可再執行查詢測試,區塊瀏覽器數據即時更新。
每執行一次交易,區塊瀏覽器 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
————————————————
版權聲明:本文為CSDN博主「iCyberpunk」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/oafzzl/article/details/103878692
總結
以上是生活随笔為你收集整理的Hyperledger Fabric 1.4 搭建区块链浏览器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单记录一下fabric版本1.4的环境
- 下一篇: Fabric 链码Chaincode 的