在Ubuntu18.04中搭建Hyperledger Explorer

一、环境准备(本节是紧接着上一篇博客编写:https://note.youdao.com/share/?id=08cba7c7957649ad31049483b42f3067&type=note#/ )

参考官方文档:https://github.com/hyperledger/blockchain-explorer

1、nodejs安装(nodejs 8.11.x (Note that v9.x is not yet supported)),由于上一篇博客已经安装了nvm,所以只需用nvm安装指定版本即可(此处安装8.11.4版本)

root@ubuntu:~# nvm install 8.11.4
Downloading and installing node v8.11.4...
Downloading https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz...
################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.11.4 (npm v5.6.0)
root@ubuntu:~# node -v
v8.11.4
root@ubuntu:~# 

2、postgresql安装(PostgreSQL 9.5 or greater),这里以docker的形式进行安装

docker pull postgres:9.5
docker run --name postgres1 -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres:9.5 #postgres镜像默认的用户名为postgres,设置一个密码postgres

3、安装jq

sudo apt-get install jq

二、具体步骤

1、下载blockchain-explorer源码

cd /root/demo
git clone https://github.com/hyperledger/blockchain-explorer.git
cd blockchain-explorer

2、数据库初始化

root@iZq0c7mivu8vrvZ:~# docker exec -it b88997d1aad3 /bin/bash    #进入postgresql容器:b88997d1aad3
root@b88997d1aad3:/# su postgres   #切换用户
postgres@b88997d1aad3:/$ psql   #进入命令行客户端
psql (9.5.15)
Type "help" for help.

postgres=# CREATE DATABASE fabricexplorer owner postgres; #创建fabricexplorer数据库并指定用户fabricexplorer

数据库创建好后执行初始化sql语句(也可以用postgresql相关桌面客户端连接数据库),以下数据库脚本参考官方https://github.com/hyperledger/blockchain-explorer/blob/master/app/persistence/fabric/postgreSQL/db/explorerpg.sql:
-- ----------------------------
--  Table structure for `blocks`
-- ----------------------------
DROP TABLE IF EXISTS blocks;

CREATE TABLE blocks (
  id SERIAL PRIMARY KEY,
  blocknum integer DEFAULT NULL,
  datahash character varying(256) DEFAULT NULL,
  prehash character varying(256) DEFAULT NULL,
  txcount integer DEFAULT NULL,
  createdt Timestamp DEFAULT NULL,
  prev_blockhash character varying(256) DEFAULT NULL,
  blockhash character varying(256) DEFAULT NULL,
  channel_genesis_hash character varying(256) DEFAULT NULL
);

ALTER table blocks owner to postgres;

-- ----------------------------
--  Table structure for `chaincodes`
-- ----------------------------
DROP TABLE IF EXISTS chaincodes;

CREATE TABLE chaincodes (
  id SERIAL PRIMARY KEY,
  name character varying(255) DEFAULT NULL,
  version character varying(255) DEFAULT NULL,
  path character varying(255) DEFAULT NULL,
  channel_genesis_hash character varying(256) DEFAULT NULL,
  txcount integer DEFAULT 0,
  createdt Timestamp DEFAULT NULL
);

ALTER table chaincodes owner to postgres;
Alter sequence chaincodes_id_seq restart with 10;

-- ---------------------------
--  Table structure for `peer_ref_chaincode`
-- ----------------------------
DROP TABLE IF EXISTS peer_ref_chaincode;

CREATE TABLE peer_ref_chaincode (
  id SERIAL PRIMARY KEY,
  peerid varchar(64) DEFAULT NULL,
  chaincodeid varchar(64) DEFAULT NULL,
  cc_version varchar(64) DEFAULT NULL,
  channelid character varying(256) DEFAULT NULL,
  createdt Timestamp DEFAULT NULL
);
ALTER table peer_ref_chaincode owner to postgres;

-- ----------------------------
--  Table structure for `channel`
-- ----------------------------
DROP TABLE IF EXISTS channel;

--   state character(1) NOT NULL DEFAULT A CHECK (state in (A, D, S))

CREATE TABLE channel (
  id SERIAL PRIMARY KEY,
  name varchar(64) DEFAULT NULL,
  blocks integer DEFAULT NULL,
  trans integer DEFAULT NULL,
  createdt Timestamp DEFAULT NULL,
  channel_genesis_hash character varying(256) DEFAULT NULL,
  channel_hash character varying(256) DEFAULT NULL,
  channel_config  bytea default NULL,
  channel_block  bytea DEFAULT NULL,
  channel_tx  bytea DEFAULT NULL,
  channel_version character varying(128) DEFAULT NULL
);

ALTER table channel owner to postgres;
Alter sequence channel_id_seq restart with 3;
-- ----------------------------
--  Table structure for `peer`
-- ----------------------------
DROP TABLE IF EXISTS peer;

--   state character(1) NOT NULL DEFAULT A CHECK (state in (A, D, S))

CREATE TABLE peer (
  id SERIAL PRIMARY KEY,
  org integer DEFAULT NULL,
  channel_genesis_hash character varying(256) DEFAULT NULL,
  mspid varchar(64) DEFAULT NULL,
  requests varchar(64) DEFAULT NULL,
  events varchar(64) DEFAULT NULL,
  server_hostname varchar(64) DEFAULT NULL,
  createdt timestamp DEFAULT NULL,
  peer_type character varying(64) DEFAULT NULL
);
ALTER table peer owner to postgres;
-- ---------------------------
--  Table structure for `peer_ref_channel`
-- ----------------------------
DROP TABLE IF EXISTS peer_ref_channel;

CREATE TABLE peer_ref_channel (
  id SERIAL PRIMARY KEY,
  createdt Timestamp DEFAULT NULL,
  peerid varchar(64),
  channelid character varying(256),
  peer_type character varying(64) DEFAULT NULL
);
ALTER table peer_ref_channel owner to postgres;

-- ====================Orderer BE-303=====================================
-- ----------------------------
--  Table structure for `orderer`
-- ----------------------------
DROP TABLE IF EXISTS orderer;

--   state character(1) NOT NULL DEFAULT A CHECK (state in (A, D, S))

CREATE TABLE orderer (
  id SERIAL PRIMARY KEY,
  requests varchar(64) DEFAULT NULL,
  server_hostname varchar(64) DEFAULT NULL,
  createdt timestamp DEFAULT NULL
);
ALTER table orderer owner to postgres;

--// ====================Orderer BE-303=====================================
-- ----------------------------
--  Table structure for `transactions`
-- ----------------------------
DROP TABLE IF EXISTS transactions;
CREATE TABLE transactions (
  id SERIAL PRIMARY KEY,
  blockid integer DEFAULT NULL,
  txhash character varying(256) DEFAULT NULL,
  createdt timestamp DEFAULT NULL,
  chaincodename character varying(255) DEFAULT NULL,
  status  integer DEFAULT NULL,
  creator_msp_id character varying(128) DEFAULT NULL,
  endorser_msp_id character varying(800) DEFAULT NULL,
  chaincode_id character varying(256) DEFAULT NULL,
  type character varying(128) DEFAULT NULL,
  read_set  json default NULL,
  write_set  json default NULL,
  channel_genesis_hash character varying(256) DEFAULT NULL,
  validation_code character varying(50) DEFAULT NULL,
  envelope_signature character varying DEFAULT NULL,
  payload_extension character varying DEFAULT NULL,
  creator_id_bytes character varying DEFAULT NULL,
  creator_nonce character varying DEFAULT NULL,
  chaincode_proposal_input character varying DEFAULT NULL,
  tx_response character varying DEFAULT NULL,
  payload_proposal_hash character varying DEFAULT NULL,
  endorser_id_bytes character varying DEFAULT NULL,
  endorser_signature character varying DEFAULT NULL
  );

ALTER table transactions owner to postgres;
Alter sequence transactions_id_seq restart with 6;

DROP TABLE IF EXISTS write_lock;
CREATE TABLE write_lock (
  write_lock SERIAl PRIMARY KEY
);

ALTER table write_lock owner to postgres;
Alter sequence write_lock_write_lock_seq restart with 2;

DROP INDEX IF EXISTS blocks_blocknum_idx;
CREATE INDEX ON Blocks (blocknum);

DROP INDEX IF EXISTS blocks_channel_genesis_hash_idx;
CREATE INDEX ON Blocks (channel_genesis_hash);

DROP INDEX IF EXISTS blocks_createdt_idx;
CREATE INDEX ON Blocks (createdt);

DROP INDEX IF EXISTS transaction_txhash_idx;
CREATE INDEX ON Transactions (txhash);

DROP INDEX IF EXISTS transaction_channel_genesis_hash_idx;
CREATE INDEX ON Transactions (channel_genesis_hash);

DROP INDEX IF EXISTS transaction_createdt_idx;
CREATE INDEX ON Transactions (createdt);

DROP INDEX IF EXISTS transaction_blockid_idx;
CREATE INDEX ON Transactions (blockid);

DROP INDEX IF EXISTS transaction_chaincode_proposal_input_idx;
CREATE INDEX ON Transactions ((md5(chaincode_proposal_input)));

DROP INDEX IF EXISTS channel_channel_genesis_hash_idx;
CREATE INDEX ON channel (channel_genesis_hash);

DROP INDEX IF EXISTS channel_channel_hash_idx;
CREATE INDEX ON channel (channel_hash);

GRANT SELECT, INSERT, UPDATE,DELETE ON ALL TABLES IN SCHEMA PUBLIC to postgres;

3、blockchain-explorer中数据库设置

cd blockchain-explorer/app
vi explorerconfig.json
    配置数据库
    "postgreSQL": {
        "host": "127.0.0.1",
        "port": "5432",
        "database": "fabricexplorer",
        "username": "postgres",
        "passwd": "postgres"
    }

4、启动fabric-samples中first-networkc网络(参考:https://note.youdao.com/share/?id=08cba7c7957649ad31049483b42f3067&type=note#/)

5、修改blockchain-explorer/app/platform/fabric/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": true,
          "adminPrivateKey": {
            "path": "/root/demo/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore"
          },
          "signedCert": {
            "path": "/root/demo/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts"
          }
        },
        "Org2MSP": {
          "mspid": "Org2MSP",
          "adminPrivateKey": {
            "path": "/root/demo/fabric-samples/first-network/crypto-config/peerOrganizations/org2.example.com/users/[email protected]/msp/keystore"
          },
          "signedCert": {
            "path": "/root/demo/fabric-samples/first-network/crypto-config/peerOrganizations/org2.example.com/users/[email protected]/msp/signcerts"
          }
        },
        "OrdererMSP": {
          "mspid": "OrdererMSP",
          "adminPrivateKey": {
            "path": "/root/demo/fabric-samples/first-network/crypto-config/ordererOrganizations/example.com/users/[email protected]/msp/keystore"
          }
        }
      },
      "peers": {
        "peer0.org1.example.com": {
          "tlsCACerts": {
            "path": "/root/demo/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": "/root/demo/fabric-samples/bin",
  "license": "Apache-2.0"
}

6、包引入并测试(包引入过程可能很慢,一定要注意nodejs的版本)

cd blockchain-explorer
npm install
cd blockchain-explorer/app/test
npm install
npm run test
cd client/
npm install
npm test -- -u --coverage
npm run build

7、打开防火墙端口并启动服务

# 打开端口
root@ubuntu:~# sudo ufw enable   #激活ufw
root@ubuntu:~# sudo ufw allow 8080/tcp   #开启8080/tcp端口

# 启动服务
root@ubuntu:~# cd blockchain-explorer
root@ubuntu:~/demo/blockchain-explorer# ./start.sh    #启动
************************************************************************************
**************************** Hyperledger Explorer **********************************
************************************************************************************
***** Please check the log [logs/console/console-2018-12-26.log] for any error *****
************************************************************************************
root@ubuntu:~/demo/blockchain-explorer# cat logs/console/console-2018-12-26.log   #查看启动日志,若没有报错且提示:Please open web browser to access :http://localhost:8080/

8、访问。输入:http://localhost:8080
在Ubuntu18.04中搭建Hyperledger Explorer_第1张图片

三、总结
1、搭建大体过程参照官网或者相关博客
2、nodejs和postgresql的版本得符合要求,特别是nodejs
3、配置文件严格按照要求,不然的话服务启动不起来
4、本次没有对fabric的版本和explorer的版本进行操作,都是用的默认的最新版本
5、再次启动网络的步骤(1、产生fabric证书文件;2、启动fabric网络;3、启动explorer,注意配置文件,多试几次)
附本人笔记:https://note.youdao.com/share/?id=4ec81cc743f386d3000865cb9390e2f8&type=note#/

你可能感兴趣的:(区块链)