1. 搭建单群组FISCO BCOS联盟链
本节以搭建单群组FISCO BCOS链为例操作。使用开发部署工具 build_chain.sh脚本在本地搭建一条4 节点的FISCO BCOS链,以Ubuntu 16.04 64bit系统为例操作。
*注解
- 若需在已有区块链上进行升级,请转至版本及兼容章节。
- 搭建多群组的链操作类似。
- 本节使用预编译的静态`fisco-bcos`二进制文件,在CentOS 7和Ubuntu 16.04 64bit上经过测试。
- 使用docker模式搭建 ,供有丰富docker经验和容器化部署需求的用户参考。
第一步. 安装依赖
# 最新homebrew默认下载的为openssl@3,需要指定版本openssl@1.1下载
brew install openssl@1.1 curl
openssl version
OpenSSL 1.1.1n 15 Mar 2022
sudo apt install -y openssl curl
sudo yum install -y openssl openssl-devel
## 创建操作目录
cd ~ && mkdir -p fisco && cd fisco
## 下载脚本
curl -#LO && chmod u+x build_chain.sh
*注解
- 如果因为网络问题导致长时间无法下载build_chain.sh脚本,请尝试:curl -#LO && chmod u+x build_chain.sh
第三步. 搭建单群组4节点联盟链
-
国密版本请执行: bash build_chain.sh -l 127.0.0.1:4 -p 30300,20200,8545 -g -G - 其中-g表示生成国密配置,-G表示使用国密SSL连接
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文件中的错误信息。
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] Execute the download_console.sh script in directory named by IP to get FISCO-BCOS console.e.g. bash /home/ubuntu/fisco/nodes/127.0.0.1/download_console.sh==============================================================[INFO] FISCO-BCOS Path : bin/fisco-bcos[INFO] Start Port : 30300 20200 8545[INFO] Server IP : 127.0.0.1:4[INFO] Output Dir : /home/ubuntu/fisco/nodes[INFO] CA Key Path : /home/ubuntu/fisco/nodes/cert/ca.key==============================================================[INFO] All completed. Files in /home/ubuntu/fisco/nodes
-
启动所有节点
bash nodes/127.0.0.1/start_all.sh
try to start node0try to start node1try to start node2try to start node3 node1 start successfully node2 start successfully node0 start successfully node3 start successfully
-
检查进程是否启动
ps -ef | grep -v grep | grep fisco-bcos
fisco 5453 1 1 17:11 pts/0 00:00:02 /home/ubuntu/fisco/nodes/127.0.0.1/node0/../fisco-bcos -c config.inifisco 5459 1 1 17:11 pts/0 00:00:02 /home/ubuntu/fisco/nodes/127.0.0.1/node1/../fisco-bcos -c config.inifisco 5464 1 1 17:11 pts/0 00:00:02 /home/ubuntu/fisco/nodes/127.0.0.1/node2/../fisco-bcos -c config.inifisco 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
info|2019-01-21 17:30:58.316769| [P2P][Service] heartBeat,connected count=3info|2019-01-21 17:31:08.316922| [P2P][Service] heartBeat,connected count=3info|2019-01-21 17:31:18.317105| [P2P][Service] heartBeat,connected count=3
-
执行下面指令,检查是否在共识
tail -f nodes/127.0.0.1/node0/log/log* | grep +++
info|2020-12-22 17:24:43.729402|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=1,hash=2e133146...info|2020-12-22 17:24:47.740603|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=1,hash=eb199760...
2. 配置及使用控制台
第一步. 准备依赖
-
安装java (推荐使用java 14).
# ubuntu系统安装java
sudo apt install -y default-jdk
#centos系统安装java
sudo yum install -y java java-devel
-
获取控制台并回到fisco目录
cd ~/fisco && curl -LO
-
如果因为网络问题导致长时间无法下载,请尝试: cd ~/fisco && curl -#LO && bash download_console.sh -
拷贝控制台配置文件
# 最新版本控制台使用如下命令拷贝配置文件cp -n console/conf/config-example.toml console/conf/config.toml
-
配置控制台证书
使用1.x版本控制台时:
- 搭建国密版时,如果使用国密SSL请执行:cp nodes/127.0.0.1/sdk/gm/* console/conf/
- 搭建国密版时,请修改applicationContext.xml中encryptType修改为1
cp -r nodes/127.0.0.1/sdk/* console/conf/
第二步. 启动并使用控制台
-
启动
cd ~/fisco/console && bash start.sh
=============================================================================================
Welcome to FISCO BCOS console( 2.6. 0)!
Type 'help' or 'h' for help. Type 'quit' or 'q' to quit console.
_______ _ _____ _ _____ _ _____ _ _____ _ ______ _ _____ _ _____ _ _____ _
| | / / / | / / /
| $$$$$$$$ $$$$$$ | $$$$$$| $$$$$$ | $$$$$$ | $$$$$$$ | $$$$$$| $$$$$$ | $$$$$$
| $$_ _ | $$ | $$__ _$$ | $$ $$| $$ | $$ | $$_ _/ $$ | $$ $$| $$ | $$| $$__ _$$
| $$ | $$ $$ | $$ | $$ | $$ | $$ $$ | $$ | $$ | $$ $$
| $$$$$ | $$ _$$$$$$| $$ _ _ | $$ | $$ | $$$$$$$| $$ _ _ | $$ | $$ _$$$$$$
| $$ _| $$ _ | __| $$ | $$__/ | $$_ _/ $$ | $$__/ $$| $$_ _/ | $$__/ $$| _ _ | $$
| $$ | $$ $$ $$ $$ $$ $$ $$ | $$ $$ $$ $$ $$ $$ $$ $$
$$ $$$$$$ $$$$$$ $$$$$$ $$$$$$ $$$$$$$ $$$$$$ $$$$$$ $$$$$$
=============================================================================================
-
用控制台获取信息
# 获取客户端版本
[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'
}
]
[group:1]>
3. 部署及调用HelloWorld合约
第一步. 编写HelloWorld合约
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;
}
}
# 在控制台输入以下指令 部署成功则返回合约地址[group:1]> deploy HelloWorldtransaction hash: 0xd0305411e36d2ca9c1a4df93e761c820f0a464367b8feb9e3fa40b0f68eb23facontract address:0xb3c223fc0bf6646959f254ac4e4a7e355b50a344
第三步. 调用HelloWorld合约
# 查看当前块高
[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"
]