前言
搭建
需要使用已经封装好的脚本程序build_chain.sh,由于该脚本依赖于openssl, curl
需要先安装相关依赖openssl, curl
下载脚本(可以新建一个文件夹管理)
curl -#LO https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/v2.8.0/build_chain.sh && chmod u+x build_chain.sh
启动脚本,生成一条单群组4节点的FISCO链,注意确保机器端口占用情况
bash build_chain.sh -l 127.0.0.1:4 -p 30300,20200,8545
其中-p选项指定起始端口,分别是p2p_port,channel_port,jsonrpc_port
命令执行成功会输出All completed
启动
启动所有节点
bash nodes/127.0.0.1/start_all.sh
启动成功会输出类似下面内容的响应。否则请使用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
正常情况会有类似下面的输出; 如果进程数不为4,则进程没有启动(一般是端口被占用导致的)
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 ,表示共识正常。
配置及使用控制台
在控制台链接FISCO BCOS节点,实现查询区块链状态、部署调用合约等功能,能够快速获取到所需要的信息。
本质上控制台也是使用命令调用的sdk
cd ~/fisco && curl -LO https://github.com/FISCO-BCOS/console/releases/download/v2.8.0/download_console.sh && bash download_console.sh
cp -n console/conf/config-example.toml console/conf/config.toml
cp -r nodes/127.0.0.1/sdk/* console/conf/
cd ~/fisco/console && bash start.sh
# 获取客户端版本
[group:1]> getNodeVersion
ClientVersion{
version='2.6.0',
supportedVersion='2.6.0',
chainId='1',
buildTime='20200819 15:47:59',
buildType='Darwin/appleclang/RelWithDebInfo',
gitBranch='HEAD',
gitCommitHash='e4a5ef2ef64d1943fccc4ebc61467a91779fb1c0'
}
# 获取节点信息
[group:1]> getPeers
[
PeerInfo{
nodeID='c1bd77e188cd0783256ee06838020f24a697f9af785438403d3620967a4a3612e3abc4bbe986d1e9dddf62d4236bff0b7d19a935a3cd44889f681409d5bf8692',
ipAndPort='127.0.0.1:30302',
agency='agency',
topic=[
],
node='node2'
},
PeerInfo{
nodeID='7f27f5d67f104eacf689790f09313e4343e7887a1a7b79c31cd151be33c7c8dd57c895a66086c3c8e0b54d2fa493407e0d9646b2bd9fc29a94fd3663a5332e6a',
ipAndPort='127.0.0.1:57266',
agency='agency',
topic=[
_block_notify_1
],
node='node1'
},
PeerInfo{
nodeID='862f26d9681ed4c12681bf81a50d0b8c66dd5b6ee7b0b42a4af12bb37b1ad2442f7dcfe8dac4e737ce9fa46aa94d904e8c474659eabf575d6715995553245be5',
ipAndPort='127.0.0.1:30303',
agency='agency',
topic=[
],
node='node3'
}
]
总结
本过程本质上包含两个部分,搭建一个联盟链和使用联盟链的控制台。核心自然是下载了其GitHub上的两个脚本(fisco搭建脚本与fisco控制台脚本)实现区块链搭建,当然在脚本的bash前需要安装脚本依赖环境,控制台在bash前还需要进行一定的配置
补充:部署及调用简单合约
简单合约编写
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合约提供两个接口,分别是get() 和set() ,用于获取/设置合约变量name 。
部署
为了方便用户快速体验,HelloWorld合约已经内置于控制台中,位于控制台目录下contracts/solidity/HelloWorld.sol ,参考下面命令部署即可。
# 在控制台输入以下指令 部署成功则返回合约地址
[group:1]> deploy HelloWorld
transaction hash: 0xd0305411e36d2ca9c1a4df93e761c820f0a464367b8feb9e3fa40b0f68eb23fa
contract address:0xb3c223fc0bf6646959f254ac4e4a7e355b50a344
调用
# 查看当前块高
[group:1]> getBlockNumber
1
# 调用get接口获取name变量 此处的合约地址是deploy指令返回的地址
[group:1]> call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 get
---------------------------------------------------------------------------------------------
Return code: 0
description: transaction executed successfully
Return message: Success
---------------------------------------------------------------------------------------------
Return values:
[
"Hello,World!"
]
---------------------------------------------------------------------------------------------
# 查看当前块高,块高不变,因为get接口不更改账本状态
[group:1]> getBlockNumber
1
# 调用set设置name
[group:1]> call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 set "Hello, FISCO BCOS"
transaction hash: 0x7e742c44091e0d6e4e1df666d957d123116622ab90b718699ce50f54ed791f6e
---------------------------------------------------------------------------------------------
transaction status: 0x0
description: transaction executed successfully
---------------------------------------------------------------------------------------------
Output
Receipt message: Success
Return message: Success
---------------------------------------------------------------------------------------------
Event logs
Event: {}
# 再次查看当前块高,块高增加表示已出块,账本状态已更改
[group:1]> getBlockNumber
2
# 调用get接口获取name变量,检查设置是否生效
[group:1]> call HelloWorld 0xb3c223fc0bf6646959f254ac4e4a7e355b50a344 get
---------------------------------------------------------------------------------------------
Return code: 0
description: transaction executed successfully
Return message: Success
---------------------------------------------------------------------------------------------
Return values:
[
"Hello,FISCO BCOS"
]
---------------------------------------------------------------------------------------------
# 退出控制台
[group:1]> quit
|