你可以从bitcoin.org下载标准客户端,即比特币核心,也叫“中本聪客户端”(satoshi client)。它实现了比特币系统的所有方面,包括钱包、对整个交易账簿(区块链)完整拷贝的交易确认引擎,和点对点比特币网络中的一个完整网络节点。

在Bitcoin网站的选择钱包页面,下载参考客户端比特币核心。根据操作系统不同,你将下载不同的可执行安装工具。对于Windows,它是一个ZIP归档文件或.exe格式的可执行文件。对于Mac OS,它是一个.dmg格式的磁盘映像。Linux版本包括用于Ubuntu系统的PPA包,或是 tar.gz格式的压缩包。图3-1所示的Bitcoin.org页面上列出了一些推荐的比特币客户端。

比特币技术学习(从源码编译比特币核心比特币核心)(1)

第一次运行比特币核心

如果你下载了一个安装包,比如.exe、.dmg、或PPA,你可以和安装其它任何应用程序一样,在你的操作系统上安装它。对于Windows,运行.exe文件,按照提示一步步操作。对于Mac OS,启动.dmg文件,然后将Bitcoin-qt 图标拖拽到你的应用程序目录就可以了。对于Ubuntu,在文件资源管理器中双击PPA文件,会打开程序包管理器来安装它。一旦完成了安装,在你的应用程序列表中会有一个新的应用叫Bitcoin-QT。双击这个图标就可以启动比特币客户端了。

第一次运行比特币核心时,它会开始下载整个区块链,这个过程可能需要数天(见下图)。让它在后台运行,直到显示“已同步”,并且余额旁边不再显示“数据同步中”。

从源码编译比特币核心比特币核心

对于开发者,可以选择下载包含完整源代码的ZIP包,也可以从GitHub上克隆权威的源码仓库。在GitHub上的比特币页面,在侧边栏选择下载ZIP。或者使用git命令行(git command line)在自己系统上创建源码的本地拷贝。在下面的例子中,我们将通过unix风格的命令行,在Linux或是Mac OS 上克隆源代码:

$ git clone https://github.com/bitcoin/bitcoin.git Cloning into 'bitcoin'... remote: Counting objects: 31864, done. remote: Compressing objects: 100% (12007/12007), done. remote: Total 31864 (delta 24480), reused 26530 (delta 19621) Receiving objects: 100% (31864/31864), 18.47 MiB | 119 KiB/s, done. Resolving deltas: 100% (24480/24480), done. $

在git clone操作完成后,在你本地的bitcoin目录就会有一份完整的源码拷贝。通过在命令提示行输入cd bitcoin切换到这个目录下:

$ cd bitcoin

默认情况下,本地拷贝将与最新的代码同步,这可能是bitcoin的一个不稳定或是 beta 版本。在编译这些代码之前,签出发布标签(realease tag)以选择某一特定版本(a specific version)。 这将通过一个关键的标签标记,让本地拷贝与代码仓库的特定快照同步。开发者用标签来标记代码的特定发行版本号(version numBTCer)。首先,要找到可用的标签,可以通过git tag命令:

$ git tag v0.1.5 v0.1.6test1 v0.2.0 v0.2.10 v0.2.11 v0.2.12 [... many more tags ...] v0.8.4rc2 v0.8.5 v0.8.6 v0.8.6rc1 v0.9.0rc1

列出的标签是bitcoin的所有发行版本。按照约定,带有rc后缀的是预发行版本,可以用来测试。没有后缀的稳定版本可以直接在产品环境上运行。从上面的列表中,选择最新的发行版本,目前是v0.9.0rc1。为了让本地代码跟这个版本一致,我们需要用git checkout 命令:

$ git checkout v0.9.0rc1 Note: checking out 'v0.9.0rc1'. HEAD is now at 15ec451... Merge pull request #3605 $

源代码包含文档,可以在多个文件夹中找到。在命令提示行输入more README.md可以在bitcoin目录下的README.md中查看主文档,用空格键可以翻页。在这一章,我们将构建命令行的比特币客户端,在linux上称作bitcoind。在您的平台上,通过输入more doc/build-unix.md,可以阅读编译bitcoind命令行客户端的说明。Mac OSX和Windows平台的说明可以在doc目录下找到,分别是build-osx.md或是build-msw.md。

仔细阅读build文档第一部分中build的必备条件。这些是在你编译之前你的系统上必须具备的库文件。如果缺少这些必备条件,构建过程将会出现错误并导致失败。如果因为缺少一个必备条件而发生这种情况,你可以先安装它,然后在你停下的地方重新构建。假设这些必备条件已经具备,你就可以开始构建过程,通过authgen.sh脚本,生成一组构建脚本。

$ ./autogen.sh configure.ac:12: installing `src/build-aux/config.guess' configure.ac:12: installing `src/build-aux/config.sub' configure.ac:37: installing `src/build-aux/install-sh' configure.ac:37: installing `src/build-aux/missing' src/Makefile.am: installing `src/build-aux/depcomp' $

autogen.sh脚本创建了一系列的自动配置脚本,会询问你的系统以发现正确的设置,确保你已安装必要的库来编译源码。这里面最重要的是configure脚本,它会提供许多不同的选项来定制构建过程。输入./configure --help 可以查看各种不同的选项:

$ ./configure --help `configure' configures Bitcoin Core 0.9.0 to adapt to many kinds of systems. Usage: ./configure [OPTION]... [VAR=VALUE]... To assign environment variables (e.g., CC, CFLAGS...), specify them as VAR=VALUE. See below for descriptions of some of the useful variables. Defaults for the options are specified in brackets. Configuration: -h, --help display this help and exit --help=short display options specific to this package --help=recursive display the short help of all the included packages -V, --version display version information and exit [... many more options and variables are displayed below ...] Optional Features: --disable-option-checking ignore unrecognized --enable/--with options --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no) --enable-FEATURE[=ARG] include FEATURE [ARG=yes] [... more options ...] Use these variables to override the choices made by `configure' or to help it to find libraries and programs with nonstandard names/locations. Report bugs to <info@bitcoin.org>. $

通过使用--enable-FEATURE和--disable-FEATURE选项,configure脚本允许你启用或是禁用某些功能,FEATURE需要被替换成功能名称,在上面的帮助输出中可以找到。在这一章,我们将用默认的功能来构建bitcoind客户端。这里不会使用配置选项,但是你应该检查一遍,明白哪些可选的功能可以作为客户端的一部分。下一次,运行configure脚本就可以自动发现所有必要的库,然后为我们的系统创建一个定制的构建脚本。

$ ./configure checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... no checking for mawk... mawk checking whether make sets $(MAKE)... yes [... many more system features are tested ...] configure: creating ./config.status config.status: creating Makefile config.status: creating src/Makefile config.status: creating src/test/Makefile config.status: creating src/qt/Makefile config.status: creating src/qt/test/Makefile config.status: creating share/setup.nsi config.status: creating share/qt/Info.plist config.status: creating qa/pull-tester/run-bitcoind-for-test.sh config.status: creating qa/pull-tester/build-tests.sh config.status: creating src/bitcoin-config.h config.status: executing depfiles commands $

如果一切顺利,configure命令将会以创建可定制的构建脚本结束。这些构建脚本允许我们编译bitcoind。如果有缺失的库或是错误,configur命令将会以错误信息终止。如果出现了错误,可能是因为缺少库或是有不兼容的库。重新检查构建文档,确认你已经安装缺失的必备条件。然后运行configure,看看错误是否消失了。下一步,你将编译源代码,这个过程可能需要1个小时完成。在编译的过程中,你应该过几秒或是几分钟看一下输出结果。如果出现了问题,你会看到错误。如果中断了,编译的过程可以在任何时候恢复。输入make命令就可以开始编译了:

$ make Making all in src make[1]: Entering directory `/home/ubuntu/bitcoin/src' make all-recursive make[2]: Entering directory `/home/ubuntu/bitcoin/src' Making all in . make[3]: Entering directory `/home/ubuntu/bitcoin/src' CXX addrman.o CXX alert.o CXX rpcserver.o CXX bloom.o CXX chainparams.o [... many more compilation messages follow ...] CXX test_bitcoin-wallet_tests.o CXX test_bitcoin-rpc_wallet_tests.o CXXLD test_bitcoin make[4]: Leaving directory `/home/ubuntu/bitcoin/src/test' make[3]: Leaving directory `/home/ubuntu/bitcoin/src/test' make[2]: Leaving directory `/home/ubuntu/bitcoin/src' make[1]: Leaving directory `/home/ubuntu/bitcoin/src' make[1]: Entering directory `/home/ubuntu/bitcoin' make[1]: Nothing to be done for `all-am'. make[1]: Leaving directory `/home/ubuntu/bitcoin' $

如果一切顺利,bitcoind现在已经编译完成。最后一步就是通过make命令,安装 bitcoind 可执行文件到你的系统路径下:

$ sudo make install Making install in src Making install in . /bin/mkdir -p '/usr/local/bin' /usr/bin/install -c bitcoind bitcoin-cli '/usr/local/bin' Making install in test make install-am /bin/mkdir -p '/usr/local/bin' /usr/bin/install -c test_bitcoin '/usr/local/bin' $

你可以通过询问系统下面2个可执行文件的路径,来确认bitcoin是否安装成功。

$ which bitcoind /usr/local/bin/bitcoind $ which bitcoin-cli /usr/local/bin/bitcoin-cli

bitcoind 默认的安装位置是/usr/local/bin。当你第一次运行bitcoind时,它会提醒你用一个安全密码给JSON-RPC接口创建一个配置文件。通过在终端输入bitcoind就可以运行bitcoind了:

$ bitcoind Error: To use the "-server" option, you must set a rpcpassword in the configuration file: /home/ubuntu/.bitcoin/bitcoin.conf It is recommended you use the following random password: rpcuser=bitcoinrpc rpcpassword=2XA4DuKNCbtZXsBQRRNDEwEY2nM6M4H9Tx5dFjoAVVbK (you do not need to remember this password) The username and password MUST NOT be the same. If the file does not exist, create it with owner-readable-only file permissions. It is also recommended to set alertnotify so you are notified of problems; for example: alertnotify=echo %s | mail -s "Bitcoin Alert" admin@foo.com

在你喜欢的编辑器中编辑配置文件并设置参数,将其中的密码替换成bitcoind推荐的强密码。不要使用出现在这里的密码。在.bitcoin目录下创建一个名为.bitcoin/bitcoin.conf 的文件,然后输入用户名和密码:

rpcuser=bitcoinrpc rpcpassword=2XA4DuKNCbtZXsBQRRNDEwEY2nM6M4H9Tx5dFjoAVVbK

当你正在编辑配置文件的时候,你可能想要设置一些其他选项,例如txindex(见“交易数据库索引及txindex选项”)。通过输入bitcoind --help,可以查看所有可用的选项列表。

现在可以运行比特币核心客户端。当你第一次运行的时候,它会下载所有的区块,重新构建比特币区块链。这是一个好几个GB的文件,可能需要大约2天的时间全部下载完。你可以通过SourceForge上的BitTorrent客户端下载区块链的部分拷贝来缩短区块链的初始化时间。

选项 -daemon 可以以后台模式运行 bitcoind。

$ bitcoind -daemon Bitcoin version v0.9.0rc1-beta (2014-01-31 09:30:15 0100) Using OpenSSL version OpenSSL 1.0.1c 10 May 2012 Default data directory /home/bitcoin/.bitcoin Using data directory /bitcoin/ Using at most 4 connections (1024 file descriptors available) init message: Verifying wallet... dbenv.open LogDir=/bitcoin/database ErrorFile=/bitcoin/db.log Bound to [::]:8333 Bound to 0.0.0.0:8333 init message: Loading block index... Opening LevelDB in /bitcoin/blocks/index Opened LevelDB successfully Opening LevelDB in /bitcoin/chainstate Opened LevelDB successfully [... more startup messages ...]

,