虚拟机搭建FISCO BCOS基本环境
环境
- Linux镜像 CentOS-7-x86_64-DVD-2003
- 虚拟机环境:2核,1.5G运存,20G存储
参考教程
https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/installation.html
安装
-
环境安装
-
sudo yum install -y openssl curl
-
如果在虚拟机运行1. 报错,那么参考:https://blog.csdn.net/Wonz5130/article/details/84864233
-
cd ~ && mkdir -p fisco && cd fisco
-
curl -#LO https://gitee.com/FISCO-BCOS/FISCO-BCOS/raw/master/tools/build_chain.sh && chmod u+x build_chain.sh
-
搭建单群组4节点联盟链
1.确保机器的30300~30303,20200~20203,8545~8548
端口没有被占用。
-
查看机器所有的开启的端口
-
netstat -ntlp
-
-
查看某一指定端口:例如80端口
1. ``` lsof -i tcp:80 ```
-
使用脚本搭建节点
-
bash build_chain.sh -l "127.0.0.1:4" -p 30300,20200,8545
-
注释 其中-p选项指定起始端口,分别是p2p_port,channel_port,jsonrpc_port 出于安全性和易用性考虑,v2.3.0版本最新配置将listen_ip拆分成jsonrpc_listen_ip和channel_listen_ip,但仍保留对listen_ip的解析功能,详细请参考 这里 为便于开发和体验,channel_listen_ip参考配置是 0.0.0.0 ,出于安全考虑,请根据实际业务网络情况,修改为安全的监听地址,如:内网IP或特定的外网IP
-
如果成功->会输出
All completed
, 报错参考nodes/build.log
-
启动链
-
启动所有节点
-
bash nodes/127.0.0.1/start_all.sh
-
如果 1. 成功会输出类似下面内容的响应。否则请使用
netstat -an | grep tcp
检查机器的30300~30303,20200~20203,8545~8548
端口是否被占用-
try to start node0 try to start node1 try to start node2 try to start node3 node1 start successfully node2 start successfully node0 start successfully node3 start successfully
-
-
检查进程
-
ps -ef | grep -v grep | grep fisco-bcos
-
如果 1. 正常,会输出
fisco 5453 1 1 17:11 pts/0 00:00:02 /home/ubuntu/fisco/nodes/127.0.0.1/node0/../fisco-bcos -c config.ini fisco 5459 1 1 17:11 pts/0 00:00:02 /home/ubuntu/fisco/nodes/127.0.0.1/node1/../fisco-bcos -c config.ini fisco 5464 1 1 17:11 pts/0 00:00:02 /home/ubuntu/fisco/nodes/127.0.0.1/node2/../fisco-bcos -c config.ini fisco 5476 1 1 17:11 pts/0 00:00:02 /home/ubuntu/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=3 info|2019-01-21 17:31:08.316922| [P2P][Service] heartBeat,connected count=3 info|2019-01-21 17:31:18.317105| [P2P][Service] heartBeat,connected count=3
-
执行下面指令,检查是否在共识
tail -f nodes/127.0.0.1/node0/log/log* | grep +++
-
正常情况会不停输出
++++Generating seal
,表示共识正常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...
-
配置及使用控制台
配置
-
安装JDK8及以上(使用orcale:https://www.oracle.com/technetwork/java/javase/downloads/index.html)
-
配置环境变量,并用
java -version
测试 -
下载控制台文件
cd ~/fisco && curl -#LO https://github.com/FISCO-BCOS/console/releases/download/v1.1.0/download_console.sh && bash download_console.sh
- 拷贝控制台配置文件
1. cp -n console/conf/applicationContext-sample.xml console/conf/applicationContext.xml 2. 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.3)! Type 'help' or 'h' for help. Type 'quit' or 'q' to quit console. ________ ______ ______ ______ ______ _______ ______ ______ ______ | \| \ / \ / \ / \ | \ / \ / \ / \ | $$$$$$$$ \$$$$$$| $$$$$$\| $$$$$$\| $$$$$$\ | $$$$$$$\| $$$$$$\| $$$$$$\| $$$$$$\ | $$__ | $$ | $$___\$$| $$ \$$| $$ | $$ | $$__/ $$| $$ \$$| $$ | $$| $$___\$$ | $$ \ | $$ \$$ \ | $$ | $$ | $$ | $$ $$| $$ | $$ | $$ \$$ \ | $$$$$ | $$ _\$$$$$$\| $$ __ | $$ | $$ | $$$$$$$\| $$ __ | $$ | $$ _\$$$$$$\ | $$ _| $$_ | \__| $$| $$__/ \| $$__/ $$ | $$__/ $$| $$__/ \| $$__/ $$| \__| $$ | $$ | $$ \ \$$ $$ \$$ $$ \$$ $$ | $$ $$ \$$ $$ \$$ $$ \$$ $$ \$$ \$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$ =============================================================================================
使用控制台获取信息
-
进入控制台的命令行模式
-
获取客户端版本
input: getNodeVersion = = = output: { "Build Time":"20200814 08:45:06", "Build Type":"Linux/clang/Release", "Chain Id":"1", "FISCO-BCOS Version":"2.6.0", "Git Branch":"HEAD", "Git Commit Hash":"e4a5ef2ef64d1943fccc4ebc61467a91779fb1c0", "Supported Version":"2.6.0" }
-
获取节点链接信息
input: getPeers = = = output: { "Build Time":"20200814 08:45:06", "Build Type":"Linux/clang/Release", "Chain Id":"1", "FISCO-BCOS Version":"2.6.0", "Git Branch":"HEAD", "Git Commit Hash":"e4a5ef2ef64d1943fccc4ebc61467a91779fb1c0", "Supported Version":"2.6.0" } [group:1]> getPeers [ { "Agency":"agency", "IPAndPort":"127.0.0.1:30301", "Node":"node1", "NodeID":"3901e91bede67484109be5a6ca84aa56401750ecd77f83bf30a4f859e177849dcf42ba9ae5344528898732975244540f20cb4e8764d2d6795a0a1965a1014c0a", "Topic":[ ] }, { "Agency":"agency", "IPAndPort":"127.0.0.1:30303", "Node":"node3", "NodeID":"2883806120b73d89aae77f23c837ae8c4cf3d42c6bc06c33ff6cf4f3ed431441b974723f59049226255416d725ca5307092f53be828e517fc88226af2e43b79f", "Topic":[ ] }, { "Agency":"agency", "IPAndPort":"127.0.0.1:30302", "Node":"node2", "NodeID":"b83ae31201748c802a91743a4d31af2996ca7f1ffd53204652290a1c949bef26e8955f5a3a863e222b5eb75949b56da44f844a14bd7fd3215043db3cb02e3ccb", "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合约已经内置于控制台中,位于控制台目录下contracts/solidity/HelloWorld.sol
,参考下面命令部署即可。
# 在控制台输入以下指令 部署成功则返回合约地址
[group:1]> deploy HelloWorld
contract address:0xb3c223fc0bf6646959f254ac4e4a7e355b50a344
调用HelloWorld合约
# 查看当前块高
input: getBlockNumber
= = =
output: 1
# 调用get接口获取name变量 此处的合约地址是deploy指令返回的地址
input: call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 get
= = =
output: Hello, World!
# 调用set设置name
input: call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 set "Hello, FISCO BCOS"
0x21dca087cb3e44f44f9b882071ec6ecfcb500361cad36a52d39900ea359d0895
= = =