FISCO BCOS安装

前言

当下区块链技术已经不算是新技术了,很多基于区块链的应用也在各个行业落地应用,本人作为一个喜欢技术的程序猿当然也要了解下这方面的信息。
目前比较流行的区块链主要分为三大类。

  • 公有链
  • 联盟链
  • 私有链

三种不同的类型都自己的特征与应用场景。其中联盟链是区块链落地时间的热点。
特别推荐付晓岩老师对当下主流联盟链的分析,给大家整理成一个表格,方便大家对比。


5大代表联盟链对比_付晓岩.png

基于付晓岩老师的分析,选择先学习一下FISCO BCOS。
今天根据官方文档,一步一步安装FISCO BCOS的运行环境。

官方文档

https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/introduction.html

安装

官方配置

硬件

配置 最低配置 推荐配置
CPU 1.5GHz 2.4GHz
内存 1GB 8GB
核心 1核 4核
带宽 1Mb 10Mb

支持的平台

  • CentOS 7.2+
  • Ubuntu 16.04
  • macOS 10.14+

测试配置

硬件

配置 测试配置
CPU 1.8GHz
内存 4GB
核心 4核
带宽 1000Mb

软件

  • VMware® Workstation 14 Pro 14.1.1 build-7528167
  • CentOS Linux release 7.6.1810 (Core)

单群组FISCO BCOS联盟链的搭建

准备环境

安装依赖
build_chain.sh脚本依赖于openssl, curl,使用下面的指令安装

yum install -y openssl curl

创建操作目录

cd ~ && mkdir -p fisco && cd fisco

下载build_chain.sh脚本

wget https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/v2.2.0/build_chain.sh && chmod u+x build_chain.sh

搭建单群组4节点联盟链

bash build_chain.sh -l "127.0.0.1:4" -p 30300,20200,8545

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

[INFO] Downloading fisco-bcos binary from https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/v2.2.0/fisco-bcos.tar.gz ...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   608  100   608    0     0    561      0  0:00:01  0:00:01 --:--:--   561
  1 8239k    1  135k    0     0   4846      0  0:29:00  0:00:28  0:28:32  7531
curl: (28) Operation too slow. Less than 102400 bytes/sec transferred the last 20 seconds
[INFO] Download speed is too low, try https://www.fisco.com.cn/cdn/fisco-bcos/releases/download/v2.2.0/fisco-bcos.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 8239k  100 8239k    0     0  2684k      0  0:00:03  0:00:03 --:--:-- 2685k
==============================================================
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] Start Port        : 30300 20200 8545
[INFO] Server IP         : 127.0.0.1:4
[INFO] RPC listen IP     : 127.0.0.1
[INFO] Output Dir        : /root/fisco/nodes
[INFO] CA Key Path       : /root/fisco/nodes/cert/ca.key
==============================================================
[WARN] RPC listens 127.0.0.1 will cause nodes' JSON-RPC and Channel service to be inaccessible form other machines
[INFO] Execute the download_console.sh script to get FISCO-BCOS console, download_console.sh is in directory named by IP.
 bash download_console.sh
==============================================================
[INFO] All completed. Files in /root/fisco/nodes

启动FISCO BCOS链

启动所有节点

bash nodes/127.0.0.1/start_all.sh

启动成功会输出类似下面内容的响应。

try to start node0
try to start node1
try to start node2
try to start node3
 node3 start successfully
 node1 start successfully
 node0 start successfully
 node2 start successfully

检查进程

检查进程是否启动

ps -ef | grep -v grep | grep fisco-bcos

正常情况会有类似下面的输出;如果进程数不为4,则进程没有启动(一般是端口被占用导致的)

root      73442      1  3 18:31 pts/0    00:00:03 /root/fisco/nodes/127.0.0.1/node1/../fisco-bcos -c config.ini
root      73443      1  3 18:31 pts/0    00:00:03 /root/fisco/nodes/127.0.0.1/node0/../fisco-bcos -c config.ini
root      73444      1  3 18:31 pts/0    00:00:03 /root/fisco/nodes/127.0.0.1/node3/../fisco-bcos -c config.ini
root      73445      1  3 18:31 pts/0    00:00:03 /root/fisco/nodes/127.0.0.1/node2/../fisco-bcos -c config.ini

检查日志输出

如下,查看节点node0链接的节点数

tail -f nodes/127.0.0.1/node0/log/log*  | grep connected

正常情况会不停地输出链接信息,从输出可以看出node0与另外3个节点有链接。

[root@cos7 fisco]# tail -f nodes/127.0.0.1/node0/log/log*  | grep connected
info|2020-02-27 12:20:19.780271|[P2P][Service] heartBeat,connected count=3
info|2020-02-27 12:20:29.781452|[P2P][Service] heartBeat,connected count=3
info|2020-02-27 12:20:39.782412|[P2P][Service] heartBeat,connected count=3
info|2020-02-27 12:20:49.783186|[P2P][Service] heartBeat,connected count=3

执行下面指令,检查是否在共识

tail -f nodes/127.0.0.1/node0/log/log*  | grep +++

正常情况会不停输出==Generating seal #F44336==,表示共识正常。

info|2020-02-27 12:21:57.596481|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=0,hash=7283e312...
info|2020-02-27 12:22:01.611037|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=0,hash=e254af10...
info|2020-02-27 12:22:05.625375|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=0,hash=788f1922...
info|2020-02-27 12:22:09.637097|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=0,hash=f5b737d3...

配置及使用控制台

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

准备依赖

安装openjdk
详细过程参考本人之前写的文章:https://www.jianshu.com/p/2a46b1e7b67f
获取控制台并回到fisco目录

cd ~/fisco && bash nodes/127.0.0.1/download_console.sh

如果出现如下错误信息,是因为网络原因,没有下载成功

Downloading console 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     9  100     9    0     0      4      0  0:00:02  0:00:02 --:--:--     4

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

正确结果如下:

Downloading console 1.0.8
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   605  100   605    0     0    264      0  0:00:02  0:00:02 --:--:--   264
  0     0    0     0    0     0      0      0 --:--:--  0:01:33 --:--:--     0
curl: (28) Connection timed out after 90712 milliseconds
 Download speed is too low, try https://www.fisco.com.cn/cdn/console/releases/download/v1.0.8/console.tar.gz 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 37.7M  100 37.7M    0     0  5049k      0  0:00:07  0:00:07 --:--:-- 7606k

拷贝控制台配置文件
若节点未采用默认端口,请将文件中的20200替换成节点对应的channle端口。

cp -n console/conf/applicationContext-sample.xml console/conf/applicationContext.xml

配置控制台证书

cp nodes/127.0.0.1/sdk/* console/conf/

启动控制台

启动

cd ~/fisco/console && bash start.sh

输出下述信息表明启动成功 否则请检查conf/applicationContext.xml中节点端口配置是否正确

=============================================================================================
Welcome to FISCO BCOS console(1.0.8)!
Type 'help' or 'h' for help. Type 'quit' or 'q' to quit console.
 ________ ______  ______   ______   ______       _______   ______   ______   ______  
|        |      \/      \ /      \ /      \     |       \ /      \ /      \ /      \ 
| $$$$$$$$\$$$$$|  $$$$$$|  $$$$$$|  $$$$$$\    | $$$$$$$|  $$$$$$|  $$$$$$|  $$$$$$\
| $$__     | $$ | $$___\$| $$   \$| $$  | $$    | $$__/ $| $$   \$| $$  | $| $$___\$$
| $$  \    | $$  \$$    \| $$     | $$  | $$    | $$    $| $$     | $$  | $$\$$    \ 
| $$$$$    | $$  _\$$$$$$| $$   __| $$  | $$    | $$$$$$$| $$   __| $$  | $$_\$$$$$$\
| $$      _| $$_|  \__| $| $$__/  | $$__/ $$    | $$__/ $| $$__/  | $$__/ $|  \__| $$
| $$     |   $$ \\$$    $$\$$    $$\$$    $$    | $$    $$\$$    $$\$$    $$\$$    $$
 \$$      \$$$$$$ \$$$$$$  \$$$$$$  \$$$$$$      \$$$$$$$  \$$$$$$  \$$$$$$  \$$$$$$

=============================================================================================
[group:1]> 

使用控制台获取信息

[group:1]> getNodeVersion
{
    "Build Time":"20191227 07:38:36",
    "Build Type":"Linux/clang/Release",
    "Chain Id":"1",
    "FISCO-BCOS Version":"2.2.0",
    "Git Branch":"HEAD",
    "Git Commit Hash":"21ab507e85322a0ea7ca4688f247a964cd4f2c35",
    "Supported Version":"2.2.0"
}

[group:1]> getPeers
[
    {
        "Agency":"agency",
        "IPAndPort":"127.0.0.1:51198",
        "Node":"node1",
        "NodeID":"e163bdc497260ecdbd5bc20bcb4e2484dcd8883f4a9816732e0ab3e11dc0f48d9ddc8abb8632e25b6d231ea976f7e89f722102b7538eb30e8e176ff4125fab25",
        "Topic":[
            
        ]
    },
    {
        "Agency":"agency",
        "IPAndPort":"127.0.0.1:30302",
        "Node":"node2",
        "NodeID":"98942d77d4ff2b8f088caad0bbe85677b125d8d2b77a30b02d482501bcdc0583ef7af029381c7f90135a890df144f0ce1bedbfadbb0939a2a48d331298739d00",
        "Topic":[
            
        ]
    },
    {
        "Agency":"agency",
        "IPAndPort":"127.0.0.1:51190",
        "Node":"node3",
        "NodeID":"e525788110bed6e5ce4cb0127e72188913c278102f5bd812a1122cac086ed0d352f140ad14735a4ca6f052a9ce8ba5ba3854bf358dfcb14cc7502938bf7d8b06",
        "Topic":[
            
        ]
    }
]

[group:1]> 

部署及调用HelloWorld合约

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


pragma solidity ^0.4.24;

contract HelloWorld{
    string name;

    constructor() public{
       name = "Hello, World!";
    }

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

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

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

[group:1]> deploy HelloWorld
contract address: 0x25f3702a7b2c8214270c752ba9d593df20ccdf4c

调用HelloWorld合约

[group:1]> call HelloWorld 0x25f3702a7b2c8214270c752ba9d593df20ccdf4c set "Hello, FISCO BCOS"
transaction hash: 0x7de71362da4a520cdf8c7f4b7bc4f1650e83b2f75551367b6c70976c3b2405c1

[group:1]> getBlockNumber
2

[group:1]> call HelloWorld 0x25f3702a7b2c8214270c752ba9d593df20ccdf4c get
Hello, FISCO BCOS

你可能感兴趣的:(FISCO BCOS安装)