Hyperledger Explorer 部署
Hyperledger Explorer 是一款簡單,功能強大,易於使用且易於維護的開源瀏覽器,目的是爲了能夠監控鏈碼、查看區塊、調用鏈碼等,這個項目目前還在發展中,所能支持的功能有限,目前版本(release-3.6)只支持查詢功能,能夠查看網絡組成、區塊、交易內容、鏈碼、通道信息等。
fabric-samples & hyperledger-explorer
通過結合first-network網絡來部署 hyperledger-explorer
先參考 Hyperledger Fabric 超級賬本 搭建網絡 fabric-samples 教程啓動一個 fabric-first-network 建議版本爲v1.2.0、v1.2.1、v.1.4~v.1.44
安裝基礎軟件
本次實驗基於CentOS7,Ubuntu系統類似
nodejs 8.11.x (對於版本9.x還不支持)
PostgreSQL 9.5 以上
Jq (yum install jq -y)
docker 17.06.2-ce [https://www.docker.com/community-edition]
docker-compose 1.14.0 [https://docs.docker.com/compose/]
1.0 CentOS 安裝nodejs
@ 安裝nodejs
1. CentOS 安裝nodejs
wget https://nodejs.org/dist/v8.11.0/node-v8.11.0-linux-x64.tar.gz
tar zxvf node-v8.11.0-linux-x64.tar.gz && cd node-v8.11.0-linux-x64/bin/ && ls -a
cp node npm /usr/bin
./npm install [email protected] -g
查看nodejs版本
node -v
v8.11.0
查看npm版本
npm -v
6.4.1
如果需要npm更新版本
npm i -g npm 或者 npm i -g [email protected]
如果需要更新nodejs版本
npm i n -g
n stable 或者 n v8.12.0
2.0 CentOS 安裝PostgreSQL (可選一)
進入官網:https://www.postgresql.org 、選擇 Download -> CentOS -> Select Version 、這邊演示版本 9.6
Install the repository RPM:yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
Install the client packages:
yum install postgresql96
Optionally install the server packages:
yum install postgresql96-server postgresql96-contrib
postgresql96-server - 數據庫核心服務端
postgresql96-contrib - 附加第三方擴展
postgresql96-devel - C語言開發Header頭文件和庫Optionally initialize the database and enable automatic start:
/usr/pgsql-9.6/bin/postgresql96-setup initdb
systemctl enable postgresql-9.6
systemctl start postgresql-9.6
開放防火牆
firewall-cmd –add-service=postgresql –permanent
firewall-cmd –reload
設置postgresql密碼
sudo -u postgres psql -c “ALTER USER postgres PASSWORD ‘postgres’;”
2.1 使用docker-compose安裝PostgreSQL(可選二,建議)
在任意處創建目錄pg
mkdir -p pg && cd pg
在這個目錄下創建並編輯文件postgresql.yaml
vim postgresql.yaml
內容(拉取docker鏡像)
version: '3.1'
services:
db:
image: postgres
restart: always
container_name: fabric.postgres.db
environment:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: always
ports:
- 9090:8080
由於瀏覽器默認端口也是8080,我們可自定義端口號。
保存之後啓動容器
docker-compose -f postgresql.yaml up -d
Creating network “pg_default” with the default driver
Creating fabric.postgres.db … done
Creating pg_adminer_1 …
這個時候postgresql服務暴露在端口5432,同時在端口9090有一個數據庫可視化管理工具adminer
docker ps
CONTAINER ID IMAGE COMMAND CREATED PORTS NAMES
2b86d6244e8c postgres "docker-entrypoint.s…" 2 hours ago 0.0.0.0:5432->5432/tcp fabric.postgres.db
6ab723977140 adminer "entrypoint.sh docke…" 2 hours ago 0.0.0.0:9090->8080/tcp pg_adminer_1_920204f95ca8
3.0 下載hyperledger-explorer代碼
下載hyperledger-explorer代碼,選擇對應的版本。
本文使用版本 Fabric Version Supported v1.4.0 to v1.4.4
git clone https://github.com/hyperledger/blockchain-explorer.git && cd blockchain-explorer
自行選擇、切換分支版本。(或者官網直接下載本地)
git branch -a && git fetch origin && git checkout release-3.6 && git branch
master
* release-3.6
remotes/origin/HEAD -> origin/master
remotes/origin/code-formatting
remotes/origin/development
remotes/origin/discovery
remotes/origin/fab-1.2-integration
remotes/origin/master
remotes/origin/react
remotes/origin/reactbranch
remotes/origin/rearchitects-react
remotes/origin/reducks
remotes/origin/release-3
remotes/origin/release-3.1
remotes/origin/release-3.2
remotes/origin/release-3.3
remotes/origin/release-3.4
remotes/origin/release-3.5
remotes/origin/release-3.6
remotes/origin/release-3.7
remotes/origin/release-3.8
M app/persistence/fabric/postgreSQL/db/createdb.sh
M app/persistence/fabric/postgreSQL/db/explorerpg.sql
M app/persistence/fabric/postgreSQL/db/processenv.js
M app/persistence/fabric/postgreSQL/db/updatepg.sql
M app/platform/fabric/config.json
M client/package-lock.json
Already on 'release-3.6'
master
* release-3.6
創建數據庫表
進入到創建數據庫表的目錄
cd blockchain-explorer/app/persistence/fabric/postgreSQL/db
如果在安裝PostgreSQL是採用2.2方式,那麼需要修改createdb.sh
mv createdb.sh createdb.sh.bak && vim createdb
內容改爲(替換blockchain-explorer路徑)
#!/bin/bash
echo "Copying ENV variables into temp file..."
node processenv.js
if [ $( jq .DATABASE_USERNAME /tmp/process.env.json) == null ]; then
export USER=$( jq .postgreSQL.username ../../../../explorerconfig.json )
else
export USER=$( jq .DATABASE_USERNAME /tmp/process.env.json)
fi
if [ $(jq .DATABASE_DATABASE /tmp/process.env.json) == null ]; then
export DATABASE=$(jq .postgreSQL.database ../../../../explorerconfig.json )
else
export DATABASE=$(jq .DATABASE_DATABASE /tmp/process.env.json)
fi
if [ $(jq .DATABASE_PASSWORD /tmp/process.env.json) == null ]; then
export PASSWD=$(jq .postgreSQL.passwd ../../../../explorerconfig.json | sed "y/\"/'/")
else
export PASSWD=$(jq .DATABASE_PASSWORD /tmp/process.env.json | sed "y/\"/'/")
fi
echo "USER=${USER}"
echo "DATABASE=${DATABASE}"
echo "PASSWD=${PASSWD}"
if [ -f /tmp/process.env.json ] ; then
rm /tmp/process.env.json
fi
echo "Executing SQL scripts..."
case $OSTYPE in
darwin*) psql postgres -v dbname=$DATABASE -v user=$USER -v passwd=$PASSWD -f ./explorerpg.sql ;
psql postgres -v dbname=$DATABASE -v user=$USER -v passwd=$PASSWD -f ./updatepg.sql ;;
linux*) docker run -it --network pg_default -v /data/pg/blockchain-explorer/app/persistence/fabric/postgreSQL/db:/db --rm --link fabric.postgres.db:postgres postgres psql -h postgres -U postgres -v dbname=$DATABASE -v user=$USER -v passwd=$PASSWD -f /db/explorerpg.sql ;
docker run -it --network pg_default -v /data/pg/blockchain-explorer/app/persistence/fabric/postgreSQL/db:/db --rm --link fabric.postgres.db:postgres postgres psql -h postgres -U postgres -v dbname=$DATABASE -v user=$USER -v passwd=$PASSWD -f /db/updatepg.sql ;;
esac
設置權限
mv createdb createdb.sh && chmod +x createdb.sh
執行腳本創建數據庫
./createdb.sh
期間會有兩次密碼輸入,密碼都是postgres
配置fabric
進入到fabric配置目錄
cd blockchain-explorer/app/platform/fabric
默認目錄下的config.json文件中所有的密鑰、證書路徑都是以fabric-path替代的,因此需要替換成對應fabric-samples的路徑
fabirc-path\替換你自己fabric-samples路徑
檢查 adminPrivateKey、signedCert、tlsCACerts等對應的文件是否相同、請務必一致!!!
vim config.json
{
"network-configs": {
"network-1": {
"version": "1.0",
"clients": {
"client-1": {
"tlsEnable": true,
"organization": "Org1MSP",
"channel": "mychannel",
"credentialStore": {
"path": "./tmp/credentialStore_Org1/credential",
"cryptoStore": {
"path": "./tmp/credentialStore_Org1/crypto"
}
}
}
},
"channels": {
"mychannel": {
"peers": {
"peer0.org1.example.com": {}
},
"connection": {
"timeout": {
"peer": {
"endorser": "6000",
"eventHub": "6000",
"eventReg": "6000"
}
}
}
}
},
"organizations": {
"Org1MSP": {
"mspid": "Org1MSP",
"fullpath": false,
"adminPrivateKey": {
"path":
"/opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore"
},
"signedCert": {
"path":
"/opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts"
}
},
"Org2MSP": {
"mspid": "Org2MSP",
"adminPrivateKey": {
"path":
"/opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org2.example.com/users/[email protected]/msp/keystore"
}
},
"OrdererMSP": {
"mspid": "OrdererMSP",
"adminPrivateKey": {
"path":
"/opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/ordererOrganizations/example.com/users/[email protected]/msp/keystore"
}
}
},
"peers": {
"peer0.org1.example.com": {
"tlsCACerts": {
"path":
"/opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt"
},
"url": "grpcs://localhost:7051",
"eventUrl": "grpcs://localhost:7053",
"grpcOptions": {
"ssl-target-name-override": "peer0.org1.example.com"
}
},
"peer1.org1.example.com": {
"url": "grpcs://localhost:8051"
},
"peer0.org2.example.com": {
"url": "grpcs://localhost:9051"
},
"peer1.org2.example.com": {
"url": "grpcs://localhost:10051"
}
},
"orderers": {
"orderer.example.com": {
"url": "grpcs://localhost:7050"
}
}
},
"network-2": {}
},
"configtxgenToolPath": "/opt/gopath/src/github.com/hyperledger/fabric-samples/bin",
"license": "Apache-2.0"
}
然後保存
Build 瀏覽器
進入到blockchain-explorer目錄,依次執行如下命令
cd blockchain-explorer
npm install
cd app/test
npm install
npm run test
cd ../../client/
npm install
npm test – -u –coverage
npm run build
在執行過程中,進入到client目錄之後執行npm test – -u –coverage的時候有可能會出現一個錯誤,可以忽略,如果一切正常會得到如下執行結果
Test Suites: 26 passed, 26 total
Tests: 171 passed, 171 total
Snapshots: 0 total
Time: 10.789s
Ran all test suites.
...
這個build過程中可能會遇到一些問題,可以嘗試刪除node_modules、package-lock.json來重新操作
啓動 hyperledger-explorer
如果之前的步驟都順利完成了,那麼接下來便可以啓動瀏覽器了。
由於官方提供了兩種啓動模式,一種是host,一種是local,這個可以通過編輯app目錄下的explorerconfig.json來配置,對於sync模式,一般是說多個同步進程在不同的節點位置上,也就是在分佈式環境下部署瀏覽器,則需要採用sync模式,如果是本地docker環境,則採用local模式即可
在sync模式下,確保每一個節點上的配置信息一樣
**sync模式**
"sync": {
"type": "host"
}
**Local模式**
"sync": {
"type": "local"
}
啓動瀏覽器
cd blockchain-explorer/
./start.sh (it will have the backend up).
接下來訪問 http://localhost:8080 即可
停止瀏覽器服務
./stop.sh (it will stop the node server).
如果是sync模式
cd blockchain-explorer/
./syncstart.sh
停止服務
./syncstop.sh (it will stop the sync node).
啓動之後界面
反饋與建議
-
vx:@xiaozhang