• Posts tagged "btc"

Blog Archives

bitcoin比特币节点安装

比特币吸金之道系列文章,由计算机黑客发明的网络货币,无国界,无政府,无中心。没有政府滥发货币,没有通货膨胀。在全球计算机网络中,自由的实现货币兑换和流通。

本系列文章只讲程序和策略,不谈挖矿…

关于作者:

  • 张丹(Conan), 程序员/Quant: Java,R,Nodejs
  • blog: http://blog.fens.me
  • email: bsspirit@gmail.com

转载请注明出处:
http://blog.fens.me/bitcoin-install

前言

量化交易的第一步就是获取数据,那么获得比特币的数据,最直接的方法就是搭建一个比特币的客户端,直接从比特币的公链上面同步账本获得数据。比特币网络的构建基础,是基于无中心节点和透明化,这样每个节点都会获得比特币网络中的全量数据。

那么,先我们从搭建bitcoin客户端节点开始吧。

目录

  1. bitcoin安装过程
  2. 启动bitcoin的节点

1. bitcoin安装过程

从源代码安装bitcoin客户端的操作过程,分为以下7步:

  1. 从github下载代码库
  2. 切换最新的版本
  3. 安装系统依赖库
  4. 运行autogen.sh脚本
  5. 运行configure脚本
  6. 运行make和make install
  7. 验证bitcoin安装成功

接下来,我们就一步一步的进行操作。本文使用的Linux系统环境为:Linux Ubuntu 16.04 LTS 64bit。

1.1 先从bitcoin的官方github下载代码库。

安装git的过程,就不再多说了,只需要一条命令apt install git就行了。


> git clone https://github.com/bitcoin/bitcoin.git
> cd bitcoin

1.2 找到最新的tag版本为v0.16.0。


> git tag
noversion
v0.1.5
...
v0.16.0
v0.16.0rc1
v0.16.0rc2
v0.16.0rc3
v0.16.0rc4
v0.2.0
...
v0.9.3
v0.9.3rc1
v0.9.3rc2
v0.9.4
v0.9.5
v0.9.5rc1
v0.9.5rc2

切换到最新版本的分支。


> git checkout v0.16.0

1.3 安装需要依赖的系统工具,这里会用到Libtool、pkg-config、libboost、libdb工具包,需要提前安装。


> apt install libtool
> apt install pkg-config
> apt install libboost-all-dev
> apt install libdb5.3++-dev 
> apt install libevent-dev

1.4 用./autogen.sh脚本进行编译,


> ./autogen.sh 

1.5 运行configure脚本

执行完成后,发现当前目标生了configure文件,然后运行configure文件。运行时,会出现一个BerkerlyDB的错误,它指定要求装4.8版本的BerkerlyDB。解决方法有2种,第1种,去下载对应版本的BerkerlyDB;第二种,跳过这一步的检查。我们先用第2种方法,跳过BerkeleyDB的兼容性检查。


> ./configure
configure: error: Found Berkeley DB other than 4.8, required for portable wallets (--with-incompatible-bdb to ignore or --disable-wallet to disable wallet functionality)

# 跳过BerkeleyDB的兼容性检查
> ./configure --with-incompatible-bdb

1.6 执行make和make install。


> make 
> make install

1.7 验证bitcoin安装成功

验证bitcoin安装成功,有2个命令 bitcoin-cli和bitcoind。我们需要检查一下,是否这个启动命令,安装到在系统环境中。

  • bitcoind,用于启动bitcoin的节点,同步账本数据。
  • bitcoin-cli,用于与bitcoind节点通信。

查看启动命令的系统安装位置。


> which bitcoin-cli
/usr/local/bin/bitcoin-cli

> which bitcoind
/usr/local/bin/bitcoind

分别查看bitcoind 和 bitcoin-cli 命令的帮助信息。


> bitcoind -help
> bitcoin-cli -help

这样就完成了,bitcoin的客户端的安装。

2. 启动bitcoin节点

通过bitcoind命令启动bitcoin的核心客户端程序。运行前,我先要定义一个bitcoin.conf的配置文件,用于设置RPC-JSON的访问用户。

新建配置文件 .bitcoin/bitcoin.conf。设置rpc用户的账号和密码,请大家按照复杂密码设定要求设置。官方推荐的bitcoin.conf文件的配置项,打开文件


> vi .bitcoin/bitcoin.conf

rpcuser=bsspirit
rpcpassword=98jfidayelqlvjieJDIjda

启动bitcoind服务端,并把数据写入到/data/btc目录,在控制台打印输出。


> bitcoind -datadir=/data/btc -conf=/root/.bitcoin/bitcoin.conf -printtoconsole

2018-05-30 01:51:06 Bitcoin Core version v0.16.0 (release build)
2018-05-30 01:51:06 InitParameterInteraction: parameter interaction: -whitelistforcerelay=1 -> setting -whitelistrelay=1
2018-05-30 01:51:06 Assuming ancestors of block 0000000000000000005214481d2d96f898e3d5416e43359c145944a909d242e0 have valid signatures.
2018-05-30 01:51:06 Setting nMinimumChainWork=000000000000000000000000000000000000000000f91c579d57cad4bc5278cc
2018-05-30 01:51:06 Using the 'sse4' SHA256 implementation
2018-05-30 01:51:06 Using RdRand as an additional entropy source
2018-05-30 01:51:06 Default data directory /root/.bitcoin
2018-05-30 01:51:06 Using data directory /data/btc
2018-05-30 01:51:06 Using config file /root/.bitcoin/bitcoin.conf
2018-05-30 01:51:06 Using at most 125 automatic connections (1024 file descriptors available)
2018-05-30 01:51:06 Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2018-05-30 01:51:06 Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2018-05-30 01:51:06 Using 4 threads for script verification
2018-05-30 01:51:06 scheduler thread start
2018-05-30 01:51:06 HTTP: creating work queue of depth 16
2018-05-30 01:51:06 Config options rpcuser and rpcpassword will soon be deprecated. Locally-run instances may remove rpcuser to use cookie-based auth, or may be replaced with rpcauth. Please see share/rpcuser for rpcauth auth generation.
2018-05-30 01:51:06 HTTP: starting 4 worker threads
2018-05-30 01:51:06 Using wallet directory /data/btc
2018-05-30 01:51:06 init message: Verifying wallet(s)...
2018-05-30 01:51:06 Using BerkeleyDB version Berkeley DB 5.3.28: (September  9, 2013)
2018-05-30 01:51:06 Using wallet wallet.dat
2018-05-30 01:51:06 CDBEnv::Open: LogDir=/data/btc/database ErrorFile=/data/btc/db.log
2018-05-30 01:51:06 Cache configuration:
2018-05-30 01:51:06 * Using 2.0MiB for block index database
2018-05-30 01:51:06 * Using 8.0MiB for chain state database
2018-05-30 01:51:06 * Using 440.0MiB for in-memory UTXO set (plus up to 286.1MiB of unused mempool space)
2018-05-30 01:51:06 init message: Loading block index...
2018-05-30 01:51:06 Opening LevelDB in /data/btc/blocks/index
2018-05-30 01:51:07 Opened LevelDB successfully
2018-05-30 01:51:07 Using obfuscation key for /data/btc/blocks/index: 0000000000000000
2018-05-30 01:51:08 LoadBlockIndexDB: last block file = 0
2018-05-30 01:51:08 LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=1, size=293, heights=0...0, time=2009-01-03...2009-01-03)
2018-05-30 01:51:08 Checking all blk files are present...
2018-05-30 01:51:08 LoadBlockIndexDB: transaction index disabled
2018-05-30 01:51:08 Opening LevelDB in /data/btc/chainstate
2018-05-30 01:51:08 Opened LevelDB successfully
2018-05-30 01:51:08 Using obfuscation key for /data/btc/chainstate: 6adad0af82f188f1
2018-05-30 01:51:08 Loaded best chain: hashBestChain=000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f height=0 date=2009-01-03 18:15:05 progress=0.000000
2018-05-30 01:51:08 init message: Rewinding blocks...
2018-05-30 01:51:09 init message: Verifying blocks...
2018-05-30 01:51:09  block index            2369ms
2018-05-30 01:51:09 init message: Loading wallet...
2018-05-30 01:51:09 nFileVersion = 160000
2018-05-30 01:51:09 Keys: 2001 plaintext, 0 encrypted, 2001 w/ metadata, 2001 total
2018-05-30 01:51:09  wallet                   67ms
2018-05-30 01:51:09 setKeyPool.size() = 2000
2018-05-30 01:51:09 mapWallet.size() = 0
2018-05-30 01:51:09 mapAddressBook.size() = 0
2018-05-30 01:51:09 mapBlockIndex.size() = 279999
2018-05-30 01:51:09 nBestHeight = 0
2018-05-30 01:51:09 Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there
2018-05-30 01:51:09 AddLocal(103.211.167.71:8333,1)
2018-05-30 01:51:09 Discover: IPv4 eth0: 103.211.167.71
2018-05-30 01:51:09 Bound to [::]:8333
2018-05-30 01:51:09 Bound to 0.0.0.0:8333
2018-05-30 01:51:09 init message: Loading P2P addresses...
2018-05-30 01:51:09 torcontrol thread start
2018-05-30 01:51:09 Loaded 2445 addresses from peers.dat  9ms
2018-05-30 01:51:09 init message: Loading banlist...
2018-05-30 01:51:09 init message: Starting network threads...
2018-05-30 01:51:09 dnsseed thread start
2018-05-30 01:51:09 net thread start
2018-05-30 01:51:09 addcon thread start
2018-05-30 01:51:09 init message: Done loading
2018-05-30 01:51:09 opencon thread start
2018-05-30 01:51:09 msghand thread start
2018-05-30 01:51:09 New outbound peer connected: version: 70015, blocks=525031, peer=0
2018-05-30 01:51:11 New outbound peer connected: version: 70015, blocks=525031, peer=1

....

启动参数:

  • -datadir,用于指定目录
  • -conf,用于指定配置文件
  • -printtoconsole,用于控制台输出

如果打算后台执行,可以增加-daemon参数。同时,去掉-printtoconsole参数,在后面的日志文件中,进行输出。


> bitcoind -datadir=/data/btc -conf=/root/.bitcoin/bitcoin.conf -daemon
Bitcoin server starting

# 查看日志,在数据输出的目录,跟踪debug.log文件。
> tail -f /data/btc/debug.log 
11-04-11 22:01:50' progress=0.001211 cache=23.8MiB(165438txo)
2018-05-30 04:21:12 UpdateTip: new best=00000000000044640cb1d3e034f8df6604d1ac1346e6162bfca8dc60c4bbd887 height=117873 version=0x00000001 log2_work=61.645341 tx=403428 date='2011-04-11 22:02:29' progress=0.001212 cache=23.8MiB(165441txo)
2018-05-30 04:21:12 UpdateTip: new best=0000000000006488914c16dc226f1d73d5dcd6c258c3bc926fa0f804909dbd1b height=117874 version=0x00000001 log2_work=61.645483 tx=403445 date='2011-04-11 22:19:06' progress=0.001212 cache=23.8MiB(165459txo)
2018-05-30 04:21:12 UpdateTip: new best=000000000000988f65c0d5b018241f0dd13ea6b73c6228efccbbb577b5791f06 height=117875 version=0x00000001 log2_work=61.645624 tx=403453 date='2011-04-11 22:21:40' progress=0.001212 cache=23.8MiB(165460txo)

...

运行bitcoin-cli getblockchaininfo 命令,显示关于比特币网络节点、钱包、区块链数据库状态的基础信息。


> bitcoin-cli getblockchaininfo
{
  "chain": "main",
  "blocks": 2867,
  "headers": 525050,
  "bestblockhash": "000000006d6af482c12555a44bed3a0d4bbadf0fa27274225a1ed808b8a7d405",
  "difficulty": 1,
  "mediantime": 1233666084,
  "verificationprogress": 8.75401245716694e-06,
  "initialblockdownload": true,
  "chainwork": "00000000000000000000000000000000000000000000000000000b340b340b34",
  "size_on_disk": 852350,
  "pruned": false,
  "softforks": [
    {
      "id": "bip34",
      "version": 2,
      "reject": {
        "status": false
      }
    },
    {
      "id": "bip66",
      "version": 3,
      "reject": {
        "status": false
      }
    },
    {
      "id": "bip65",
      "version": 4,
      "reject": {
        "status": false
      }
    }
  ],
  "bip9_softforks": {
    "csv": {
      "status": "defined",
      "startTime": 1462060800,
      "timeout": 1493596800,
      "since": 0
    },
    "segwit": {
      "status": "defined",
      "startTime": 1479168000,
      "timeout": 1510704000,
      "since": 0
    }
  },
  "warnings": ""
}

看到这些信息,代表你的bitcoin程序已经启动了。有几十G的数据,需要同步!

安装bitcoin相比用apt安装要复杂一些,不过按照官方文档的介绍基本能走通的,安装过程对于技术人来不是什么大问题。不同的公链,有不同的节点软件,安装方法也不同,下一篇文章将介绍以太坊的客户端Geth的安装

转载请注明出处:
http://blog.fens.me/bitcoin-install

打赏作者