比特币全节点部署

选型

比特币节点有很多实现

  • bitcoin core:官方节点,c/c++实现,正式网络大多数都是用这个来运行全节点
  • btcd:golang的实现版本,如果用golang来写一些功能,可以参考这个代码
  • libbitcoin:c/c++实现,特点是它有较好的工具链
  • parity-bitcoin:rust实现,是以太坊创始人之一Gavin Wood的新公司Parity Technologies下的一个产品,它同样也实现了以太坊rust版本

还有很多其他实现,如java版本和js版本。如果用作商业用途,那bitcoin core必然是最稳妥的选择。所以这里选择了bitcoin core。

服务器配置

  • 硬盘:500G。比特币从08年开始运行,已经积累了大量数据,区块链内容大小达210G。
  • 内存:16G。一般来说4G就够,但如果要查历史记录,需要加载完整的交易索引表-tindex,这导致需要8G+的内存
  • cpu:4核。
  • 带宽:一般就行。阿里云的一般2-3天就能同步完。
  • 系统选择:centos 7版本

节点设置

  • 下载bitcoin coreLinux版本bitcoin-0.16.1-x86_64-linux-gnu.tar.gz
  • 解压到系统目录
1
tar -xzvf bitcoin-0.16.1-x86_64-linux-gnu.tar.gz -C /usr/local
  • bitcoin的默认配置目录为 ~/.bitcoin/bitcoin.conf,填写以下配置,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf

# [core]
# Specify a non-default location to store blockchain and other data.
datadir=/data/btc_data
# Set database cache size in megabytes; machines sync faster with a larger cache. Recommend setting as high as possible based upon machine's available RAM.
dbcache=10240
# Maintain a full transaction index, used by the getrawtransaction rpc call.
txindex=1

# [rpc]
# Enable Add Witness Address RPC

deprecatedrpc=addwitnessaddress

# Accept command line and JSON-RPC commands.
server=1
# Accept public REST requests.
rest=1
# Bind to given address to listen for JSON-RPC connections. This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -rpcport. Use [host]:port notation for IPv6. This option can be specified multiple times. (default: 127.0.0.1 and ::1 i.e., localhost, or if -rpcallowip has been specified, 0.0.0.0 and :: i.e., all addresses)
rpcbind=127.0.0.1:8332

rpcuser=btcuser
rpcpassword=btcpassword

# rpcallowip=0.0.0.0
  • 启动bitcoin
1
bitcoind -daemon
  • 关闭bitcoin,一定要用以下命令,否则会有可能导致数据异常,需要重新同步数据
1
bitcoin-cli stop
  • 一些其他命令
1
2
3
4
5
6
7
8
# 查看网络状态:
bitcoin-cli getnetworkinfo
# 查看网络节点:
bitcoin-cli getpeerinfo
# 查看区块链信息:如同步进度、
bitcoin-cli getblockchaininfo
# 查看所有命令
bitcoin-cli help
  • 其他,确保ntp服务是开启的,大多数区块链都要求开启。

其他工具