PlatON在CentOS上编译部署

931 阅读6分钟
本文作者为万向区块链CTO罗荣阁。


目录

1. CentOS 环境准备
1.1. 使用rpm 安装devtoolset-7
1.2. 使用rpm 安装dos2unix
1.3. 准备PlatON代码
1.4. 确保build脚本正常
1.5. 准备mpclib
1.6. 编译PlatON
1.7. 重新编译前的清理工作
2. 初始化和运行PlatON
2.1. PlatON节点初始化
2.1.1. 生成 account 和 key pair
2.1.2. 配置初始化文件
2.1.3. 初始化PlatON
2.2. 启动 PlatON 节点
2.3. PlatON help 信息
2.4. 重新初始化PlatON节点
3. CentOS升级gcc原始步骤
3.1. 安装CentOS-release-scl
3.2. 使用yum 安装devtoolset-7
3.3. 编译获得静态库libstdc++.so.6


PlatON在CentOS上编译部署

PlatON官网上主要支持在 Ubuntu 和 Windows 环境中编译部署,但是也会有在CentOS上编译部署的需要。本文主要介绍了在CentOS上的环境准备、编译生成可执行文件和运行节点程序的过程。PlatON 官方代码库更新速度较快,有可能官方代码做出了一些设置变化,如果有步骤不能正确执行,可以参考 PlatON 官网具体信息。


1. CentOS 环境准备

Ubuntu在社区中很流行,但CentOS在企业级市场中占有较大优势。目前,CentOS在AWS、阿里云、UCloud 云服务商中能够使用的最高官方版本是CentOS 7.5。

本文就在CentOS 7.5下编译部署PlatON。


1.1. 使用rpm 安装devtoolset-7

因为PlatON的WASM库和MPC库依赖,开发和编译环境需要支持C++14。GCC 6.1版本以上是默认支持C++14。 https://www.gnu.org/software/gcc/projects/cxx-status.html#cxx14

CentOS 7.5的默认GCC版本为4.8.5,开发和编译PlatON需要升级GCC。Redhat官方的升级方法是通过devtoolset。

• 2018年1月,devtoolset 6 发布。

• 2018年5月,CentOS 7.5 发布。

• 2018年9月,devtoolset 7 发布。 

https://access.redhat.com/solutions/19458 http://mirror.centos.org/centos/7/sclo/x86_64/rh/devtoolset-7/ https://www.softwarecollections.org/en/scls/rhscl/devtoolset-7/

考虑以上的发布时间顺序、相互具体版本要求,选择在CentOS 7.5中安装devtoolset 7。

将以下33个rpm拷贝到一个目录中,再执行:

$ sudo rpm -ivh boost-date-time-1.53.0-27.el7.x86_64.rpm efivar-libs-31-4.el7.x86_64.rpm kernel-debug-devel-3.10.0-862.el7.x86_64.rpm libgfortran4-7.2.1-1.1.1.el7.x86_64.rpm libquadmath-4.8.5-28.el7.x86_64.rpm mokutil-12-1.el7.centos.x86_64.rpm scl-utils-20130529-18.el7_4.x86_64.rpm devtoolset-7-7.1-4.el7.x86_64.rpm devtoolset-7-binutils-2.28-11.el7.x86_64.rpm devtoolset-7-dwz-0.12-1.1.el7.x86_64.rpm devtoolset-7-dyninst-9.3.2-3.el7.x86_64.rpm devtoolset-7-elfutils-0.170-5.el7.x86_64.rpm devtoolset-7-elfutils-libelf-0.170-5.el7.x86_64.rpm devtoolset-7-elfutils-libs-0.170-5.el7.x86_64.rpm devtoolset-7-gcc-7.3.1-5.13.el7.x86_64.rpm devtoolset-7-gcc-c++-7.3.1-5.13.el7.x86_64.rpm devtoolset-7-gcc-gfortran-7.3.1-5.13.el7.x86_64.rpm devtoolset-7-gdb-8.0.1-36.el7.x86_64.rpm devtoolset-7-libquadmath-devel-7.3.1-5.13.el7.x86_64.rpm devtoolset-7-libstdc++-devel-7.3.1-5.13.el7.x86_64.rpm devtoolset-7-ltrace-0.7.91-2.el7.x86_64.rpm devtoolset-7-make-4.2.1-3.el7.x86_64.rpm devtoolset-7-memstomp-0.1.5-5.1.el7.x86_64.rpm devtoolset-7-oprofile-1.2.0-2.el7.1.x86_64.rpm devtoolset-7-perftools-7.1-4.el7.x86_64.rpm devtoolset-7-runtime-7.1-4.el7.x86_64.rpm devtoolset-7-strace-4.17-7.el7.x86_64.rpm devtoolset-7-systemtap-3.1-4s.el7.x86_64.rpm devtoolset-7-systemtap-client-3.1-4s.el7.x86_64.rpm devtoolset-7-systemtap-devel-3.1-4s.el7.x86_64.rpm devtoolset-7-systemtap-runtime-3.1-4s.el7.x86_64.rpm devtoolset-7-toolchain-7.1-4.el7.x86_64.rpm devtoolset-7-valgrind-3.13.0-11.el7.x86_64.rpm


1.2. 使用rpm 安装dos2unix

$ sudo rpm -ivh dos2unix-6.0.3-7.el7.x86_64.rpm


1.3. 准备PlatON代码

PlatON git仓库代码

$ go get github.com/PlatONnetwork/PlatON-Go

更新代码

$ go get -u github.com/PlatONnetwork/PlatON-Go


1.4. 确保build脚本正常

$ cd build

$ dos2unix *.sh

$ chmod 755 *.sh


1.5. 准备mpclib

下载地址 https://download.platon.network/mpclib.tar.gz

$ tar xf mpclib.tar.gz

$ mv mpclib $GOPATH/src/github.com/PlatONnetwork/PlatON-Go/


1.6. 编译PlatON

每次进bash需要手动切换GCC版本到 7.3.1

$ scl enable devtoolset-7 bash

或者直接设置在环境配置中,这样每次打开bash默认是GCC 7.3.1

$ vi ~/.bashrc

source scl_source enable devtoolset-7

添加mpclib中的依赖包

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GOPATH/src/github.com/PlatONnetwork/PlatON-Go/mpclib

$ echo $LD_LIBRARY_PATH

/opt/rh/devtoolset-7/root/usr/lib64:/opt/rh/devtoolset-7/root/usr/lib:/opt/rh/devtoolset-7/root/usr/lib64/dyninst:/opt/rh/devtoolset-7/root/usr/lib/dyninst:/opt/rh/devtoolset-7/root/usr/lib64:/opt/rh/devtoolset-7/root/usr/lib:/home/wxuser/work/golang/src/github.com/PlatONnetwork/PlatON-Go/mpclib$ cd PlatON-Go/

• 带mpc编译

$ make platon-with-mpc > ~/temp/make_platon.txt

$ make all-with-mpc > ~/temp/make_all.txt

编译后生成的可执行程序在 build/bin/ 中。

将生成的可执行程序拷贝到节点启动目录

$ cd build
$ cp -R bin/ ~/platon-node


1.7. 重新编译前的清理工作

$ make clean

$ rm -rf build/_workspace/*


2. 初始化和运行PlatON

2.1. PlatON节点初始化

2.1.1. 生成 account 和 key pair

$ ./platon --datadir ./data account new

INFO [01-09|17:25:14.269] Maximum peer count ETH=50 LES=0 total=50
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 
Repeat passphrase: 
Address: {60208c048e7eb8e38b0fac40406b819ce95aa7af}

本次操作密码设成了“0”。

$ ll data/keystore/

-rw------- 1 wxuser wxuser 491 Jan 9 17:25 UTC--2019-01-09T09-25-28.487164507Z--60208c048e7eb8e38b0fac40406b819ce95aa7af$ ./ethkey genkeypair

Address : 0xC71433b47f1b0053f935AEf64758153B24cE7445
PrivateKey: b428720a89d003a1b393c642e6e32713dd6a6f82fe4098b9e3a90eb38e23b6bb
PublicKey : 68bb049008c7226de3188b6376127354507e1b1e553a2a8b988bb99b33c4d995e426596fc70ce12f7744100bc69c5f0bce748bc298bf8f0d0de1f5929850b5f4

将节点私钥存储在 ./data/platon/nodekey中,私钥来自上一步PrivateKey。

$ mkdir -p ./data/platon

$ echo "b428720a89d003a1b393c642e6e32713dd6a6f82fe4098b9e3a90eb38e23b6bb" > ./data/platon/nodekey

$ cat ./data/platon/nodekey

b428720a89d003a1b393c642e6e32713dd6a6f82fe4098b9e3a90eb38e23b6bb


2.1.2. 配置初始化文件

将initialNodes改为节点公钥,alloc为账户地址分配金额。

$ vi genesis.json

{
"config": {
"chainId": 300,
"homesteadBlock": 1,
"eip150Block": 2,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 3,
"eip158Block": 3,
"byzantiumBlock": 4,
"cbft": {
"initialNodes": ["enode://68bb049008c7226de3188b6376127354507e1b1e553a2a8b988bb99b33c4d995e426596fc70ce12f7744100bc69c5f0bce748bc298bf8f0d0de1f5929850b5f4@127.0.0.1:16789"]
}
},
"nonce": "0x0",
"timestamp": "0x5c074288",
"extraData": "0x00000000000000000000000000000000000000000000000000000000000000007a9ff113afc63a33d11de571a679f914983a085d1e08972dcb449a02319c1661b931b1962bce02dfc6583885512702952b57bba0e307d4ad66668c5fc48a45dfeed85a7e41f0bdee047063066eae02910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b77760",
"difficulty": "0x40000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x60208c048e7eb8e38b0fac40406b819ce95aa7af",
"alloc": {
"0x60208c048e7eb8e38b0fac40406b819ce95aa7af": {
"balance": "999000000000000000000"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}


2.1.3. 初始化PlatON

$ ./platon --datadir ./data init genesis.json

INFO [01-09|17:31:58.832] Maximum peer count ETH=50 LES=0 total=50
INFO [01-09|17:31:58.833] Allocated cache and file handles database=/home/wxuser/manual-Platon/build/bin/data/platon/chaindata cache=16 handles=16
INFO [01-09|17:31:58.839] Writing custom genesis block 
INFO [01-09|17:31:58.840] Persisted trie from memory database nodes=1 size=150.00B time=34.546µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-09|17:31:58.840] Successfully wrote genesis state database=chaindata hash=4fe06b…382a26
INFO [01-09|17:31:58.840] Allocated cache and file handles database=/home/wxuser/manual-Platon/build/bin/data/platon/lightchaindata cache=16 handles=16
INFO [01-09|17:31:58.848] Writing custom genesis block 
INFO [01-09|17:31:58.848] Persisted trie from memory database nodes=1 size=150.00B time=238.177µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-09|17:31:58.848] Successfully wrote genesis state database=lightchaindata hash=4fe06b…382a26


2.2. 启动 PlatON 节点

$ ./platon --identity "node1" --rpc --rpcaddr 0.0.0.0 --datadir ./data --rpcapi "db,eth,net,web3,admin,personal,miner" --networkid 300 --mine --verbosity 3 --miner.etherbase 0x60208c048e7eb8e38b0fac40406b819ce95aa7af

INFO [01-09|17:42:01.165] Maximum peer count ETH=50 LES=0 total=50
INFO [01-09|17:42:01.166] Starting peer-to-peer node instance=Geth/node1/v1.8.16-stable-7ee6fe39/linux-amd64/go1.11.4
INFO [01-09|17:42:01.166] Allocated cache and file handles database=/home/wxuser/manual-Platon/build/bin/data/platon/chaindata cache=768 handles=512
INFO [01-09|17:42:01.183] Initialised chain configuration config="{ChainID: 300 Homestead: 1 DAO: <nil> DAOSupport: false EIP150: 2 EIP155: 3 EIP158: 3 Byzantium: 4 Constantinople: <nil> Engine: &{0 0 0 0 0 [{127.0.0.1 16789 16789 68bb049008c7226de3188b6376127354507e1b1e553a2a8b988bb99b33c4d995e426596fc70ce12f7744100bc69c5f0bce748bc298bf8f0d0de1f5929850b5f4 [149 178 250 27 246 47 49 86 100 108 50 3 199 20 51 180 127 27 0 83 249 53 174 246 71 88 21 59 36 206 116 69] {0 0 <nil>}}] 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 <nil>}}"
INFO [01-09|17:42:01.183] Initialising Ethereum protocol versions="[63 62]" network=300
INFO [01-09|17:42:01.184] Loaded most recent local header number=0 hash=4fe06b…382a26 age=1mo5d6h
INFO [01-09|17:42:01.184] Loaded most recent local full block number=0 hash=4fe06b…382a26 age=1mo5d6h
INFO [01-09|17:42:01.184] Loaded most recent local fast block number=0 hash=4fe06b…382a26 age=1mo5d6h
INFO [01-09|17:42:01.184] Read the StateDB instance from the cache map sealHash=bbbae7…30dbfb
INFO [01-09|17:42:01.184] Loaded local transaction journal transactions=0 dropped=0
INFO [01-09|17:42:01.185] Regenerated local transaction journal transactions=0 accounts=0
INFO [01-09|17:42:01.185] Loaded local mpc transaction journal mpc transactions=0 dropped=0
INFO [01-09|17:42:01.185] Init mpc processor success osType=linux icepath= httpEndpoint=http://127.0.0.1:6789
INFO [01-09|17:42:01.185] commitDuration commitDuration=950.000
INFO [01-09|17:42:01.185] Set the block time at the end of the last round of consensus startTimeOfEpoch=1543979656
INFO [01-09|17:42:01.185] Starting P2P networking 
INFO [01-09|17:42:03.298] UDP listener up self=enode://aa18a88c1463c1f1026c6cb0b781027d898d19ed9c11b10ad7a3a9ee2d0c09ab607d9b24bc4580bd816c0194215461cd88bf65955e0d87cf69e0157d464c582b@[::]:16789
INFO [01-09|17:42:03.299] Transaction pool price threshold updated price=1000000000
INFO [01-09|17:42:03.300] IPC endpoint opened url=/home/wxuser/manual-Platon/build/bin/data/platon.ipc
INFO [01-09|17:42:03.300] RLPx listener up self=enode://aa18a88c1463c1f1026c6cb0b781027d898d19ed9c11b10ad7a3a9ee2d0c09ab607d9b24bc4580bd816c0194215461cd88bf65955e0d87cf69e0157d464c582b@[::]:16789
INFO [01-09|17:42:03.300] HTTP endpoint opened url=http://0.0.0.0:6789 cors= vhosts=localhost
INFO [01-09|17:42:03.300] Transaction pool price threshold updated price=1000000000


2.3. PlatON help 信息

$ ./platon --help


2.4. 重新初始化PlatON节点

确保PlatON进程已经被杀死,再删除data目录。

$ cd build/bin

$ rm -rf data/platon

然后可以再重新初始化。


3. CentOS升级gcc原始步骤

CentOS 7.5升级GCC的原始步骤。


3.1. 安装CentOS-release-scl

$ sudo yum install centos-release-scl

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
centos-release-scl noarch 2-2.el7.centos extras 12 k
Installing for dependencies:
centos-release-scl-rh noarch 2-2.el7.centos extras 12 k

Transaction Summary
================================================================================
Install 1 Package (+1 Dependent package)

Total download size: 24 k
Installed size: 39 k


3.2. 使用yum 安装devtoolset-7

$ sudo yum install devtoolset-7

==============================================================================
Package Arch Version Repository Size
==============================================================================
Installing:
devtoolset-7 x86_64 7.1-4.el7 centos-sclo-rh 6.2 k
Installing for dependencies:
boost-date-time x86_64 1.53.0-27.el7 CentOS-7_Local 52 k
devtoolset-7-binutils x86_64 2.28-11.el7 centos-sclo-rh 5.3 M
devtoolset-7-dwz x86_64 0.12-1.1.el7 centos-sclo-rh 99 k
devtoolset-7-dyninst x86_64 9.3.2-3.el7 centos-sclo-rh 3.6 M
devtoolset-7-elfutils x86_64 0.170-5.el7 centos-sclo-rh 390 k
devtoolset-7-elfutils-libelf x86_64 0.170-5.el7 centos-sclo-rh 194 k
devtoolset-7-elfutils-libs x86_64 0.170-5.el7 centos-sclo-rh 280 k
devtoolset-7-gcc x86_64 7.3.1-5.13.el7 centos-sclo-rh 29 M
devtoolset-7-gcc-c++ x86_64 7.3.1-5.13.el7 centos-sclo-rh 11 M
devtoolset-7-gcc-gfortran x86_64 7.3.1-5.13.el7 centos-sclo-rh 11 M
devtoolset-7-gdb x86_64 8.0.1-36.el7 centos-sclo-rh 3.2 M
devtoolset-7-libquadmath-devel x86_64 7.3.1-5.13.el7 centos-sclo-rh 154 k
devtoolset-7-libstdc++-devel x86_64 7.3.1-5.13.el7 centos-sclo-rh 2.6 M
devtoolset-7-ltrace x86_64 0.7.91-2.el7 centos-sclo-rh 148 k
devtoolset-7-make x86_64 1:4.2.1-3.el7 centos-sclo-rh 485 k
devtoolset-7-memstomp x86_64 0.1.5-5.1.el7 centos-sclo-rh 442 k
devtoolset-7-oprofile x86_64 1.2.0-2.el7.1 centos-sclo-rh 1.8 M
devtoolset-7-perftools x86_64 7.1-4.el7 centos-sclo-rh 3.6 k
devtoolset-7-runtime x86_64 7.1-4.el7 centos-sclo-rh 20 k
devtoolset-7-strace x86_64 4.17-7.el7 centos-sclo-rh 558 k
devtoolset-7-systemtap x86_64 3.1-4s.el7 centos-sclo-rh 141 k
devtoolset-7-systemtap-client x86_64 3.1-4s.el7 centos-sclo-rh 2.1 M
devtoolset-7-systemtap-devel x86_64 3.1-4s.el7 centos-sclo-rh 2.1 M
devtoolset-7-systemtap-runtime x86_64 3.1-4s.el7 centos-sclo-rh 405 k
devtoolset-7-toolchain x86_64 7.1-4.el7 centos-sclo-rh 3.8 k
devtoolset-7-valgrind x86_64 1:3.13.0-11.el7 centos-sclo-rh 7.6 M
efivar-libs x86_64 31-4.el7 CentOS-7_Local 68 k
kernel-debug-devel x86_64 3.10.0-862.el7 CentOS-7_Local 16 M
libgfortran4 x86_64 7.2.1-1.1.1.el7 CentOS-7_Local 676 k
libquadmath x86_64 4.8.5-28.el7 CentOS-7_Local 188 k
mokutil x86_64 12-1.el7.centos CentOS-7_Local 41 k
scl-utils x86_64 20130529-18.el7_4 CentOS-7_Local 24 k

Transaction Summary
==============================================================================
Install 1 Package (+32 Dependent packages)

Total download size: 100 M
Installed size: 289 M


3.3. 编译获得静态库libstdc++.so.6

通过devtoolset 7升级GCC,并不会得到静态库: libstdc++.so.6.0.24。可以在“另一个”CentOS 7.5环境中,编译gcc 7.3.0得到这个文件。

http://www.netgull.com/gcc/releases/gcc-7.3.0/gcc-7.3.0.tar.gz

ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2 ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2 ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2

$ tar xvf gcc-7.3.0.tar.gz

$ cd gcc-7.3.0

$ ./contrib/download_prerequisites

说明:如果脚本无法成功通过ftp下载文件,就手动下载以上4个压缩包,放到 gcc-7.3.0/中,再执行这个脚本,解压缩并建立link。

$ mkdir build

$ cd build

$ ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib

说明: --enable-checking=release //增加一些检查,也可以–disable-checking生成的编译器在编译过程中不做检查; --enable-languages=c,c++ //让gcc支持的编程语言 ; --disable-multilib //取消多目标库编译(取消32位库编译)。

$ make

libstdc++.so.6.0.24 在 build/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.24 中。如果需要,可以把它拷贝到另外一个Linux的 /opt/rh/devtoolset-7/root/lib64/ 中。(目前不需要)

$ sudo mv libstdc++.so.6.0.24 /opt/rh/devtoolset-7/root/lib64/ 

$ cd /opt/rh/devtoolset-7/root/lib64/ 

$ sudo chown root.root libstdc++.so.6.0.24 

$ sudo chmod 755 libstdc++.so.6.0.24 

$ sudo ln -s libstdc++.so.6.0.24 libstdc++.so.6


作者简介:

上海万向区块链股份公司CTO,产品和研发团队负责人,ISO TC307 区块链国际标准本体论项目负责人。拥有15 年的IT 技术规划及管理方面的从业经历,曾在中国证监会直属的证券期货交易所负责技术系统规划工作,长期与纳斯达克交易所、纽约证券交易所、泛欧交易所等国际领先交易所集团共同开展技术项目,与欧美技术架构师进行过深入合作交流,具有深厚的金融技术背景和全球化视野,撰写的论文还曾获评证监会优秀论文。在区块链前沿技术及其在金融行业应用方面具有很强的研究能力。