安装

本章将介绍使用FISCO BCOS所需的必要安装和配置。本章通过在单机上部署一条4节点的FISCO BCOS联盟链,帮助用户掌握FISCO BCOS部署流程。

单群组FISCO BCOS联盟链的搭建

本节以搭建单群组FISCO BCOS链为例操作。使用build_chain脚本在本地搭建一条4节点的FISCO BCOS链,以Ubuntu 16.04系统为例操作。本节使用预编译的静态fisco-bcos二进制文件,在CentOS 7和Ubuntu 16.04上经过测试。

注解

准备环境

build_chain脚本依赖于openssl,推荐根据自己操作系统安装openssl 1.0.2以上版本。

# Ubuntu 16.04 安装依赖 CentOS将下面命令中的apt替换为yum执行即可
$ sudo apt install -y openssl curl
# 准备环境
$ cd ~ && mkdir fisco && cd fisco
# 下载build_chain.sh脚本
$ curl -LO https://raw.githubusercontent.com/FISCO-BCOS/FISCO-BCOS/master/tools/build_chain.sh && chmod u+x build_chain.sh

搭建单群组4节点联盟链

# 生成一条4节点的FISCO链 4个节点都属于同一群组 下面指令在fisco目录下执行
# -p指定起始端口,分别是p2p_port,channel_port,jsonrpc_port
# 为安全jsonrpc/channel默认监听127.0.0.1,需要外网访问请添加 -i 参数
# 根据下面的指令,需要保证机器的30300~30303,20200~20203,8545~8548端口没有被占用
# 默认从GitHub下载最新稳定版本可执行程序
$ ./build_chain.sh -l "127.0.0.1:4" -p 30300,20200,8545

如果命令执行成功会输出All completed。如果执行出错,请检查nodes/build.log文件中的错误信息。

Checking fisco-bcos binary...
Binary check passed.
==============================================================
Generating CA key...
==============================================================
Generating keys ...
Processing IP:127.0.0.1 Total:4 Agency:agency Groups:1
==============================================================
Generating configurations...
Processing IP:127.0.0.1 Total:4 Agency:agency Groups:1
==============================================================
[INFO] FISCO-BCOS Path   : bin/fisco-bcos
[INFO] Start Port        : 30300 20200 8545
[INFO] Server IP         : 127.0.0.1:4
[INFO] State Type        : storage
[INFO] RPC listen IP     : 127.0.0.1
[INFO] Output Dir        : /home/ubuntu16/fisco/nodes
[INFO] CA Key Path       : /home/ubuntu16/fisco/nodes/cert/ca.key
==============================================================
[INFO] All completed. Files in /home/ubuntu16/fisco/nodes

启动FISCO BCOS链

# 进入节点目录 当前目录fisco
$ cd nodes/127.0.0.1
# 启动所有节点
$ ./start_all.sh

检查进程及端口监听

# 检查进程是否启动 如果进程数不为4,那么进程没启动的原因一般是端口被占用
$ ps -ef | grep -v grep | grep fisco-bcos
fisco       5453     1  1 17:11 pts/0    00:00:02 /home/fisco/fisco/nodes/127.0.0.1/node0/../fisco-bcos -c config.ini
fisco       5459     1  1 17:11 pts/0    00:00:02 /home/fisco/fisco/nodes/127.0.0.1/node1/../fisco-bcos -c config.ini
fisco       5464     1  1 17:11 pts/0    00:00:02 /home/fisco/fisco/nodes/127.0.0.1/node2/../fisco-bcos -c config.ini
fisco       5476     1  1 17:11 pts/0    00:00:02 /home/fisco/fisco/nodes/127.0.0.1/node3/../fisco-bcos -c config.ini

# 检查监听的端口,当前版本每个节点监听3个端口,分别用于p2p,channel,jsonrpc通信
$ netstat -ntlp | grep fisco-bcos
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:30300           0.0.0.0:*               LISTEN      5453/fisco-bcos
tcp        0      0 127.0.0.1:20200         0.0.0.0:*               LISTEN      5453/fisco-bcos
tcp        0      0 127.0.0.1:8545          0.0.0.0:*               LISTEN      5453/fisco-bcos
tcp        0      0 0.0.0.0:30301           0.0.0.0:*               LISTEN      5459/fisco-bcos
tcp        0      0 127.0.0.1:20201         0.0.0.0:*               LISTEN      5459/fisco-bcos
tcp        0      0 127.0.0.1:8546          0.0.0.0:*               LISTEN      5459/fisco-bcos
tcp        0      0 0.0.0.0:30302           0.0.0.0:*               LISTEN      5464/fisco-bcos
tcp        0      0 127.0.0.1:20202         0.0.0.0:*               LISTEN      5464/fisco-bcos
tcp        0      0 127.0.0.1:8547          0.0.0.0:*               LISTEN      5464/fisco-bcos
tcp        0      0 0.0.0.0:30303           0.0.0.0:*               LISTEN      5476/fisco-bcos
tcp        0      0 127.0.0.1:20203         0.0.0.0:*               LISTEN      5476/fisco-bcos
tcp        0      0 127.0.0.1:8548          0.0.0.0:*               LISTEN      5476/fisco-bcos

检查日志输出

# 查看节点node0链接的节点数,从下面的输出可以看出node0与另外3个节点有链接
$ tail -f node0/log/log*  | grep connected
info|2019-01-21 17:30:58.316769| [P2P][Service] heartBeat connected count,size=3
info|2019-01-21 17:31:08.316922| [P2P][Service] heartBeat connected count,size=3
info|2019-01-21 17:31:18.317105| [P2P][Service] heartBeat connected count,size=3

# 检查是否在共识,如果不停输出++++Generating seal表示正常输出
$ tail -f node0/log/log*  | grep +++
info|2019-01-21 17:23:32.576197| [g:1][p:264][CONSENSUS][SEALER]++++++++++++++++Generating seal on,blkNum=1,tx=0,myIdx=2,hash=13dcd2da...
info|2019-01-21 17:23:36.592280| [g:1][p:264][CONSENSUS][SEALER]++++++++++++++++Generating seal on,blkNum=1,tx=0,myIdx=2,hash=31d21ab7...
info|2019-01-21 17:23:40.612241| [g:1][p:264][CONSENSUS][SEALER]++++++++++++++++Generating seal on,blkNum=1,tx=0,myIdx=2,hash=49d0e830...

配置及使用控制台

控制台通过Web3SDK链接FISCO BCOS节点,实现查询区块链状态、部署调用合约等功能,能够快速获取到所需要的信息。控制台指令详细介绍参考这里

准备依赖

# 回到fisco目录
$ cd ~/fisco
# 安装openjdk
$ sudo apt install -y default-jdk
# 获取控制台
$ bash <(curl -s https://raw.githubusercontent.com/FISCO-BCOS/console/master/tools/download_console.sh)
# 重命名控制台配置文件
$ mv console/conf/applicationContext-sample.xml console/conf/applicationContext.xml
# 配置控制台证书
$ cp nodes/127.0.0.1/sdk/* console/conf/

重要

  • 如果控制台配置正确,但是在CentOS系统上启动控制台出现如下错误:

    Failed to connect to the node. Please check the node status and the console configruation.

则是因为使用了CentOS系统自带的JDK版本(会导致控制台与区块链节点认证失败),请从 OpenJDK官网Oracle官网 下载并安装Java 8或以上版本(具体安装步骤 参考附录 ),安装完毕后再启动控制台。

启动控制台

# 回到控制台目录
$ cd ~/fisco/console
# 启动控制台
$ ./start.sh
# 输出下述信息表明启动成功 否则请检查conf/applicationContext.xml中节点端口配置是否正确
=============================================================================================
Welcome to FISCO BCOS console(1.0.2)!
Type 'help' or 'h' for help. Type 'quit' or 'q' to quit console.
 ________  ______   ______    ______    ______         _______    ______    ______    ______
|        \|      \ /      \  /      \  /      \       |       \  /      \  /      \  /      \
| $$$$$$$$ \$$$$$$|  $$$$$$\|  $$$$$$\|  $$$$$$\      | $$$$$$$\|  $$$$$$\|  $$$$$$\|  $$$$$$\
| $$__      | $$  | $$___\$$| $$   \$$| $$  | $$      | $$__/ $$| $$   \$$| $$  | $$| $$___\$$
| $$  \     | $$   \$$    \ | $$      | $$  | $$      | $$    $$| $$      | $$  | $$ \$$    \
| $$$$$     | $$   _\$$$$$$\| $$   __ | $$  | $$      | $$$$$$$\| $$   __ | $$  | $$ _\$$$$$$\
| $$       _| $$_ |  \__| $$| $$__/  \| $$__/ $$      | $$__/ $$| $$__/  \| $$__/ $$|  \__| $$
| $$      |   $$ \ \$$    $$ \$$    $$ \$$    $$      | $$    $$ \$$    $$ \$$    $$ \$$    $$
 \$$       \$$$$$$  \$$$$$$   \$$$$$$   \$$$$$$        \$$$$$$$   \$$$$$$   \$$$$$$   \$$$$$$

=============================================================================================

使用控制台获取信息

# 获取客户端版本
[group:1]> getNodeVersion
{
    "Build Time":"20190121 06:21:05",
    "Build Type":"Linux/clang/Debug",
    "FISCO-BCOS Version":"2.0.0-rc1",
    "Git Branch":"master",
    "Git Commit Hash":"c213e033328631b1b8c2ee936059d7126fd98d1a"
}
# 获取节点链接信息
[group:1]> getPeers
[
    {
        "IPAndPort":"127.0.0.1:49948",
        "NodeID":"b5872eff0569903d71330ab7bc85c5a8be03e80b70746ec33cafe27cc4f6f8a71f8c84fd8af9d7912cb5ba068901fe4131ef69b74cc773cdfb318ab11968e41f",
        "Topic":[]
    },
    {
        "IPAndPort":"127.0.0.1:49940",
        "NodeID":"912126291183b673c537153cf19bf5512d5355d8edea7864496c257630d01103d89ae26d17740daebdd20cbc645c9a96d23c9fd4c31d16bccf1037313f74bb1d",
        "Topic":[]
    },
    {
        "IPAndPort":"127.0.0.1:49932",
        "NodeID":"db75ab16ed7afa966447c403ca2587853237b0d9f942ba6fa551dc67ed6822d88da01a1e4da9b51aedafb8c64e9d208d9d3e271f8421f4813dcbc96a07d6a603",
        "Topic":[]
    }
]

部署及调用HelloWorld合约

HelloWorld合约

HelloWorld合约提供两个接口,分别是get()set(),用于获取/设置合约变量name。合约内容如下:

pragma solidity ^0.4.24;

contract HelloWorld {
    string name;

    function HelloWorld() {
        name = "Hello, World!";
    }

    function get()constant returns(string) {
        return name;
    }

    function set(string n) {
        name = n;
    }
}

部署HelloWorld合约

为了方便用户快速体验,HelloWorld合约已经内置于控制台中,位于控制台目录下solidity/contracts/HelloWorld.sol,所以参考下面命令部署即可。

# 在控制台输入以下指令 部署成功则返回合约地址
[group:1]> deploy HelloWorld
contract address:0xb3c223fc0bf6646959f254ac4e4a7e355b50a344

调用HelloWorld合约

# 查看当前块高
[group:1]> getBlockNumber
1
# 调用get接口获取name变量 此处的合约地址是deploy指令返回的地址
[group:1]> call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 get
Hello, World!
# 查看当前块高,块高不变,因为get接口不更改账本状态
[group:1]> getBlockNumber
1
# 调用set设置name
[group:1]> call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 set "Hello, FISCO BCOS"
0x21dca087cb3e44f44f9b882071ec6ecfcb500361cad36a52d39900ea359d0895
# 再次查看当前块高,块高增加表示已出块,账本状态已更改
[group:1]> getBlockNumber
2
# 调用get接口获取name变量,检查设置是否生效
[group:1]> call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 get
Hello, FISCO BCOS
# 退出控制台
[group:1]> quit