跳转到正文
Lishude's Web Note
返回

搭建比特币测试环境

这是我正在写的一个电子书《比特币应用开发指南》,这是第一章的搭建测试环境的一小节。


搭建比特币节点

本书使用 BitcoinCore 0.16.3 和 Ubuntu 16.04 作为示例。

下载

运行下列命令下载 BitcoinCore 0.16.3 压缩包

$ wget https://bitcoincore.org/bin/bitcoin-core-0.16.3/bitcoin-0.16.3-x86_64-linux-gnu.tar.gz
--2019-02-22 22:12:13-- https://bitcoincore.org/bin/bitcoin-core-0.16.3/bitcoin-0.16.3-x86_64-linux-gnu.tar.gz
Resolving bitcoincore.org (bitcoincore.org)... 107.191.99.5, 198.251.83.116
Connecting to bitcoincore.org (bitcoincore.org)|107.191.99.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26154766 (25M) [application/octet-stream]
Saving to: ‘bitcoin-0.16.3-x86_64-linux-gnu.tar.gz’

bitcoin-0.16.3-x86_64-linux-gnu.tar 100%[==============================================>] 24.94M 75.7KB/s in 10m 0s

--2019-02-22 22:22:13 (46.5 KB/s) - ‘bitcoin-0.16.3-x86_64-linux-gnu.tar.gz’ saved [26154766/26154766]

检验下载

下载校验和文件

$ wget https://bitcoincore.org/bin/bitcoin-core-0.16.3/SHA256SUMS.asc
--2019-02-22 22:13:13--  https://bitcoincore.org/bin/bitcoin-core-0.16.3/SHA256SUMS.asc
Resolving bitcoincore.org (bitcoincore.org)... 198.251.83.116, 107.191.99.5
Connecting to bitcoincore.org (bitcoincore.org)|198.251.83.116|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1957 (1.9K) [application/octet-stream]
Saving to: ‘SHA256SUMS.asc’

SHA256SUMS.asc                   100%[=================================================>]   1.91K  --.-KB/s    in 0s

2019-02-22 22:13:16 (81.0 MB/s) - ‘SHA256SUMS.asc’ saved [1957/1957]

下载完成后使用以下命令验证 SHA256SUMS.asc 文件中是否列出了发布文件的校验和:

$ sha256sum --ignore-missing --check SHA256SUMS.asc
bitcoin-0.16.3-x86_64-linux-gnu.tar.gz: OK
sha256sum: WARNING: 20 lines are improperly formatted

在上述命令生成的输出中,必须确保输出内容中包含“OK”。

通过运行以下命令拉取发布签名密钥的副本:

$ gpg --recv-keys 01EA5486DE18A882D4C2684590C8019E36C2E964
gpg: requesting key 36C2E964 from hkp server keys.gnupg.net
gpg: /home/lishude/.gnupg/trustdb.gpg: trustdb created
gpg: key 36C2E964: public key "Wladimir J. van der Laan (Bitcoin Core binary release signing key) <laanwj@gmail.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

laanwj 是当前 BitcoinCore 的维护者和版本发布者,上面命令就是导入他的公钥。

然后就可以验证校验和文件是否签名正确:

$ gpg --verify SHA256SUMS.asc
gpg: Signature made Wed 19 Sep 2018 04:23:24 AM CST using RSA key ID 36C2E964
gpg: Good signature from "Wladimir J. van der Laan (Bitcoin Core binary release signing key) <laanwj@gmail.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 01EA 5486 DE18 A882 D4C2  6845 90C8 019E 36C2 E964

检查输出中必须包含 gpg: Good signature 以及完整的一行 Primary key fingerprint: 01EA 5486 DE18 A882 D4C2 6845 90C8 019E 36C2 E964

全局安装

移动文件到 /usr/local 目录,这样就可以全局运行 bitcoin 客户端,这里你可能需要管理员权限。

$ sudo tar zxvf bitcoin-0.16.3-x86_64-linux-gnu.tar.gz --strip-components=1 -C /usr/local
bitcoin-0.16.3/bin/
bitcoin-0.16.3/bin/bitcoin-cli
bitcoin-0.16.3/bin/bitcoind
bitcoin-0.16.3/bin/bitcoin-qt
bitcoin-0.16.3/bin/bitcoin-tx
bitcoin-0.16.3/bin/test_bitcoin
bitcoin-0.16.3/include/
bitcoin-0.16.3/include/bitcoinconsensus.h
bitcoin-0.16.3/lib/
bitcoin-0.16.3/lib/libbitcoinconsensus.so
bitcoin-0.16.3/lib/libbitcoinconsensus.so.0
bitcoin-0.16.3/lib/libbitcoinconsensus.so.0.0.0
bitcoin-0.16.3/share/
bitcoin-0.16.3/share/man/
bitcoin-0.16.3/share/man/man1/
bitcoin-0.16.3/share/man/man1/bitcoin-cli.1
bitcoin-0.16.3/share/man/man1/bitcoind.1
bitcoin-0.16.3/share/man/man1/bitcoin-qt.1
bitcoin-0.16.3/share/man/man1/bitcoin-tx.1

可以看到可执行文件有下面 4 个,其中前两个是本书介绍和讲解的内容。

通过打印版本号来查看是否正确安装:

$ bitcoin-cli --version
Bitcoin Core RPC client version v0.16.3
$ bitcoind --version
Bitcoin Core Daemon version v0.16.3
Copyright (C) 2009-2018 The Bitcoin Core developers

Please contribute if you find Bitcoin Core useful. Visit
<https://bitcoincore.org> for further information about the software.
The source code is available from <https://github.com/bitcoin/bitcoin>.

This is experimental software.
Distributed under the MIT software license, see the accompanying file COPYING
or <https://opensource.org/licenses/MIT>

This product includes software developed by the OpenSSL Project for use in the
OpenSSL Toolkit <https://www.openssl.org> and cryptographic software written by
Eric Young and UPnP software written by Thomas Bernard.

如果有以上类似的输出,则证明全局安装完成。

配置

主网节点

现在可以直接运行 bitcoind 直接启动守护进程,当然默认不是以守护态启动的,需要加上 bitcoind -daemon.

$ bitcoind -daemon
Bitcoind server starting

这样我们就以主网节点的方式启动,先看一下检查节点运行状态:

$ bitcoin-cli getblockchaininfo
{
    "chain": "main",
    "blocks": 0,
    "headers": 311998,
    "bestblockhash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
    "difficulty": 1,
    "mediantime": 1231006505,
    "verificationprogress": 2.411095771117041e-09,
    "initialblockdownload": true,
    "chainwork": "0000000000000000000000000000000000000000000000000000000100010001",
    "size_on_disk": 293,
    "pruned": false,
    "softforks": []
}

如果出现类型输出则说明运行正常,这个时候我们检查家目录会自动生成一个文件夹 .bitcoin。这个文件是配置文件、本地钱包、区块数据的所在目录。

$ ls -al .bitcoin
total 800
drwxrwxr-x 5 lishude lishude   4096 Feb 22 22:45 .
drwxr-xr-x 6 lishude lishude   4096 Feb 22 22:43 ..
-rw------- 1 lishude lishude     37 Feb 22 22:43 banlist.dat
drwx------ 3 lishude lishude   4096 Feb 22 22:44 blocks
drwx------ 2 lishude lishude   4096 Feb 22 22:45 chainstate
-rw------- 1 lishude lishude  13806 Feb 22 22:45 debug.log
-rw------- 1 lishude lishude 247985 Feb 22 22:45 fee_estimates.dat
-rw------- 1 lishude lishude      0 Feb 22 22:43 .lock
-rw------- 1 lishude lishude     17 Feb 22 22:45 mempool.dat
-rw------- 1 lishude lishude 522270 Feb 22 22:45 peers.dat
drwxrwxr-x 2 lishude lishude   4096 Feb 22 22:45 wallets

如果不想每次启动时候加 -daemon 参数,那么可以创建一个配置文件,配置文件默认路径为 ~/.bitcoin/bitcoin.conf。

daemon=1

server=1
rpcuser=test
rpcpassword=test

除了 -daemon 配置了守护态启动之外,我们还加入了其它配置:

这样子我们就还启动了 HTTP-JOSNRPC 功能,可以使用 HTTP Request 的形式和比特币节点进行交互。默认监听端口号是 8332,这个可以通过 rpcport 来修改。

现在就使用了配置文件的内容,不用在跟 -daemon 等命令行参数了,直接就可以运行 bitcoind。运行成功,现在以上面获取区块状态为例,我们使用 cURL 测试下 HTTP-JSONRPC 功能。

curl
    --request POST \
    --user test:test \
    --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockchaininfo", "params": [] }' \
    -H 'content-type: applicaiton/json;' \
    http://127.0.0.1:8332/

内容除了没有格式化外,和使用 bitcoin-cli 一致。另外 JSONRPC 和其它命令的内容会在后续章节继续说明。

测试节点

我们通过配置成功运行了主网节点,为了开发方便一般我们还会配置“回归测试节点”,也就是 regtest 网络模式。

修改配置文件添加下列配置:

空行后为新增配置,完整配置如下:

daemon=1
server=1
rpcuser=test
rpcpassword=test

regtest=1
txindex=1
rpcallowip=0.0.0.0/0
rpcport=8332
discover=0

继续运行 bitcoind,这个时候会发现 .bitcoin 文件夹比之前多了一个 regtest 的文件夹,这个文件是本地测试时所有的数据,内容基本和上一层主网下的一样。另外删除这个文件夹之后就相当于还原测试环境,这个相当友好。



上一篇
Nodejs 多线程以及线程池教程
下一篇
禁用 Mac 内置键盘和触控板