安装
本节以搭建单群组FISCO BCOS链为例操作。使用脚本在本地搭建一条4节点的FISCO BCOS链,以Ubuntu 16.04
系统为例操作。
注解
- 搭建多群组的链操作类似,感兴趣可以 。
- 本节使用预编译的静态
fisco-bcos
二进制文件,在CentOS 7和Ubuntu 16.04上经过测试。
- 安装依赖
build_chain.sh
脚本依赖于openssl, curl
,使用下面的指令安装。CentOS将下面命令中的apt
替换为yum
执行即可。macOS执行即可。
- 创建操作目录
- cd ~ && mkdir -p fisco && cd fisco
- 下载
build_chain.sh
脚本
- curl -LO https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/`curl -s https://api.github.com/repos/FISCO-BCOS/FISCO-BCOS/releases | grep "\"v2\." | sort -u | tail -n 1 | cut -d \" -f 4`/build_chain.sh && chmod u+x build_chain.sh
搭建单群组4节点联盟链
在fisco目录下执行下面的指令,生成一条单群组4节点的FISCO链。需要保证机器的30300~30303,20200~20203,8545~8548
端口没有被占用。
- bash build_chain.sh -l "127.0.0.1:4" -p 30300,20200,8545
注解
- 其中-p选项指定起始端口,分别是p2p_port,channel_port,jsonrpc_port,出于安全考虑jsonrpc/channel默认监听127.0.0.1,需要外网访问请添加-i参数。
- 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链
- 执行下面指令,启动所有节点
启动成功会输出类似下面内容的相应。否则请使用netstat -an | grep tcp
检查机器的30300~30303,20200~20203,8545~8548
端口是否被占用。
- 执行下面指令,检查进程是否启动
- ps -ef | grep -v grep | grep fisco-bcos
正常情况会的到类似下面的输出,如果进程数不为4,那么进程没启动的原因一般是端口被占用。
- 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
检查日志输出
- 执行下面指令,查看节点node0链接的节点数
- tail -f nodes/127.0.0.1/node0/log/log* | grep connected
正常情况会不停地输出链接信息,从输出可以看出node0与另外3个节点有链接。
- 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
- 执行下面指令,检查是否在共识
- tail -f nodes/127.0.0.1/node0/log/log* | grep +++
正常情况会不停输出表示共识正常。
准备依赖
- # 回到fisco目录
- $ cd ~/fisco
- # 安装openjdk macOS执行 brew cask install java 安装java
- $ sudo apt install -y default-jdk
- # 获取控制台
- $ bash <(curl -s https://raw.githubusercontent.com/FISCO-BCOS/console/master/tools/download_console.sh)
- # 拷贝控制台配置文件,若节点未采用默认端口,请将文件中的20200替换成其他端口
- $ cp -n 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 configuration.
- # 回到控制台目录
- $ cd ~/fisco/console
- # 启动控制台
- $ ./start.sh
- # 输出下述信息表明启动成功 否则请检查conf/applicationContext.xml中节点端口配置是否正确
- =============================================================================================
- Welcome to FISCO BCOS console(1.0.3)!
- Type 'help' or 'h' for help. Type 'quit' or 'q' to quit console.
- ________ ______ ______ ______ ______ _______ ______ ______ ______
- | \| \ / \ / \ / \ | \ / \ / \ / \
- | $$$$$$$$ \$$$$$$| $$$$$$\| $$$$$$\| $$$$$$\ | $$$$$$$\| $$$$$$\| $$$$$$\| $$$$$$\
- | $$__ | $$ | $$___\$$| $$ \$$| $$ | $$ | $$__/ $$| $$ \$$| $$ | $$| $$___\$$
- | $$ \ | $$ \$$ \ | $$ | $$ | $$ | $$ $$| $$ | $$ | $$ \$$ \
- | $$$$$ | $$ _\$$$$$$\| $$ __ | $$ | $$ | $$$$$$$\| $$ __ | $$ | $$ _\$$$$$$\
- | $$ _| $$_ | \__| $$| $$__/ \| $$__/ $$ | $$__/ $$| $$__/ \| $$__/ $$| \__| $$
- | $$ | $$ \ \$$ $$ \$$ $$ \$$ $$ | $$ $$ \$$ $$ \$$ $$ \$$ $$
- \$$ \$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$
- =============================================================================================
使用控制台获取信息
- # 获取客户端版本
- {
- "Build Time":"20190121 06:21:05",
- "Build Type":"Linux/clang/Debug",
- "FISCO-BCOS Version":"2.0.0",
- "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合约提供两个接口,分别是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合约已经内置于控制台中,位于控制台目录下solidity/contracts/HelloWorld.sol
,参考下面命令部署即可。
- # 在控制台输入以下指令 部署成功则返回合约地址
- contract address:0xb3c223fc0bf6646959f254ac4e4a7e355b50a344