0x00 写在前面
1.写这篇文章的初衷
- 在区块链技术走遍大街小巷的当下,我想有那么一部分人想去了解区块链的真实面目,而比特币作为区块链的首个应用,也是区块链的推广者,是大家学习区块链不可能回避的。在这个前提下,不管你怎么去参透《精通比特币》、还是去无限次的阅读白皮书,总是无法去真正体会到比特币设计的精妙。可以让你真正理解比特币的一个必要条件就是源码的阅读与调试,我这里是用了必要条件,因为还需要其他几个必要条件才可以共同组成充分条件,我举两个例子,白皮书、《精通比特币》。将代码、白皮书、《精通比特币》三者相辅相成的去理解比特币,是我推崇的一个过程,但是顺序,我希望是先白皮书,然后《精通比特币》,最后研究源码。
- 我建议,对比特币只停留在诸如一下的理解:比特币很贵、比特币是分布式、比特币要人挖矿,先不要往下去涉及本文的内容,只会事倍功半。我希望,对比特币的理解已经达到诸如以下水平:比特币底层采取区块链技术、pow共识机制、挖矿与钱包可分离、双重支付攻击、自私挖矿等,请在评论区留下您的见解与修改意见,我们共同进步。
- 在网络上有太多关于如何编译比特币源码的文章,但我觉得大多存在一个共同的问题,就是一次编译成功案例,并没有给出一个通用的编译办法,只是提供一次自己成功的经历。而这不方便更多的人去学习。
2.本文的硬件及文档
-
win7 64位系统,vm14上搭建ubuntu-16.04.5-desktop-amd64虚拟机,其中虚拟机配置为,2g内存,270g硬盘(截止2018.08.31,比特币全节点需要240g硬盘空间),关于虚拟机的其他配置,网络上有很多,我只给出一个截图。
-
本文代码以github上的比特币最新版本为基础,当前release版本为(0.16.2)
-
其他不需要任何硬件以及文档
0x01 本文目录
1.安装虚拟机
-
VMware Pro 14,本文采用的虚拟机版本,下载之后采取傻瓜式安装
-
ubuntu-16.04.5-desktop-amd64,本文的linux系统版本,文件为iso格式
-
关于如何安装虚拟机,本文不做详细讲解
2.下载比特币源码
-
此处为在linux系统下下载比特币源码
-
先打开终端,详见下图
-
安装git
-
下载bitcoin源码
-
git clone https://github.com/bitcoin/bitcoin.git
执行完之后代码会在主目录下,如下图:
3.安装比特币编译所需依赖
//主要依赖,一条指令
sudo apt-get install build-essential libtool autotools-dev automake pkg-config libssl-dev libevent-dev bsdmainutils python3 libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-test-dev libboost-thread-dev
//安装db4.8,即BerkeleyDB,共四条指令
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:bitcoin/bitcoin
sudo apt-get update
sudo apt-get install libdb4.8-dev libdb4.8++-dev
//防火墙相关,一条指令
sudo apt-get install libminiupnpc-dev
//ZMQ,类似于 Socket 的一系列接口,一条指令
sudo apt-get install libzmq3-dev
//QT5,一条指令
sudo apt-get install libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools libprotobuf-dev protobuf-compiler
//QR,即二维码,一条指令
sudo apt-get install libqrencode-dev
4.编译比特币
-
首先需要跳转目录,即进入到bitcoin文件夹,操作如下
-
然后按序执行以下指令,四条指令
./autogen.sh //这个脚本会完成所有的automake以及autoconf的操作
./configure //检测配置环境,包括依赖库等
sudo make //编译
sudo make install //安装,与上一条的区别,可以百度
-
运行比特币客户端
//运行钱包,会下载全节点所有数据 bitcoin-qt
0x10 本文参考
-
本文的过程绝大部分参考 build-unix.md,此文档在比特币源码中的doc文件夹中,介绍了编译的notes,一下附上原文,共读者参考比对
UNIX BUILD NOTES ==================== Some notes on how to build Bitcoin Core in Unix. (For BSD specific instructions, see `build-*bsd.md` in this directory.) Note --------------------- Always use absolute paths to configure and compile bitcoin and the dependencies, for example, when specifying the path of the dependency: ../dist/configure --enable-cxx --disable-shared --with-pic --prefix=$BDB_PREFIX Here BDB_PREFIX must be an absolute path - it is defined using $(pwd) which ensures the usage of the absolute path. To Build --------------------- ```bash ./autogen.sh ./configure make make install # optional ``` This will build bitcoin-qt as well if the dependencies are met. Dependencies --------------------- These dependencies are required: Library | Purpose | Description ------------|------------------|---------------------- libssl | Crypto | Random Number Generation, Elliptic Curve Cryptography libboost | Utility | Library for threading, data structures, etc libevent | Networking | OS independent asynchronous networking Optional dependencies: Library | Purpose | Description ------------|------------------|---------------------- miniupnpc | UPnP Support | Firewall-jumping support libdb4.8 | Berkeley DB | Wallet storage (only needed when wallet enabled) qt | GUI | GUI toolkit (only needed when GUI enabled) protobuf | Payments in GUI | Data interchange format used for payment protocol (only needed when GUI enabled) libqrencode | QR codes in GUI | Optional for generating QR codes (only needed when GUI enabled) univalue | Utility | JSON parsing and encoding (bundled version will be used unless --with-system-univalue passed to configure) libzmq3 | ZMQ notification | Optional, allows generating ZMQ notifications (requires ZMQ version >= 4.x) For the versions used, see [dependencies.md](dependencies.md) Memory Requirements -------------------- C++ compilers are memory-hungry. It is recommended to have at least 1.5 GB of memory available when compiling Bitcoin Core. On systems with less, gcc can be tuned to conserve memory with additional CXXFLAGS: ./configure CXXFLAGS="--param ggc-min-expand=1 --param ggc-min-heapsize=32768" ## Linux Distribution Specific Instructions ### Ubuntu & Debian #### Dependency Build Instructions Build requirements: sudo apt-get install build-essential libtool autotools-dev automake pkg-config libssl-dev libevent-dev bsdmainutils python3 libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-test-dev libboost-thread-dev BerkeleyDB is required for the wallet. **For Ubuntu only:** db4.8 packages are available [here](https://launchpad.net/~bitcoin/+archive/bitcoin). You can add the repository and install using the following commands: sudo apt-get install software-properties-common sudo add-apt-repository ppa:bitcoin/bitcoin sudo apt-get update sudo apt-get install libdb4.8-dev libdb4.8++-dev Ubuntu and Debian have their own libdb-dev and libdb++-dev packages, but these will install BerkeleyDB 5.1 or later, which break binary wallet compatibility with the distributed executables which are based on BerkeleyDB 4.8. If you do not care about wallet compatibility, pass `--with-incompatible-bdb` to configure. See the section "Disable-wallet mode" to build Bitcoin Core without wallet. Optional (see --with-miniupnpc and --enable-upnp-default): sudo apt-get install libminiupnpc-dev ZMQ dependencies (provides ZMQ API 4.x): sudo apt-get install libzmq3-dev #### Dependencies for the GUI If you want to build Bitcoin-Qt, make sure that the required packages for Qt development are installed. Qt 5 is necessary to build the GUI. To build without GUI pass `--without-gui`. To build with Qt 5 you need the following: sudo apt-get install libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools libprotobuf-dev protobuf-compiler libqrencode (optional) can be installed with: sudo apt-get install libqrencode-dev Once these are installed, they will be found by configure and a bitcoin-qt executable will be built by default. ### Fedora #### Dependency Build Instructions Build requirements: sudo dnf install gcc-c++ libtool make autoconf automake openssl-devel libevent-devel boost-devel libdb4-devel libdb4-cxx-devel python3 Optional: sudo dnf install miniupnpc-devel To build with Qt 5 you need the following: sudo dnf install qt5-qttools-devel qt5-qtbase-devel protobuf-devel libqrencode (optional) can be installed with: sudo dnf install qrencode-devel Notes ----- The release is built with GCC and then "strip bitcoind" to strip the debug symbols, which reduces the executable size by about 90%. miniupnpc --------- [miniupnpc](http://miniupnp.free.fr/) may be used for UPnP port mapping. It can be downloaded from [here]( http://miniupnp.tuxfamily.org/files/). UPnP support is compiled in and turned off by default. See the configure options for upnp behavior desired: --without-miniupnpc No UPnP support miniupnp not required --disable-upnp-default (the default) UPnP support turned off by default at runtime --enable-upnp-default UPnP support turned on by default at runtime Berkeley DB ----------- It is recommended to use Berkeley DB 4.8. If you have to build it yourself, you can use [the installation script included in contrib/](/contrib/install_db4.sh) like so ```shell ./contrib/install_db4.sh `pwd` ``` from the root of the repository. **Note**: You only need Berkeley DB if the wallet is enabled (see the section *Disable-Wallet mode* below). Boost ----- If you need to build Boost yourself: sudo su ./bootstrap.sh ./bjam install Security -------- To help make your bitcoin installation more secure by making certain attacks impossible to exploit even if a vulnerability is found, binaries are hardened by default. This can be disabled with: Hardening Flags: ./configure --enable-hardening ./configure --disable-hardening Hardening enables the following features: * Position Independent Executable Build position independent code to take advantage of Address Space Layout Randomization offered by some kernels. Attackers who can cause execution of code at an arbitrary memory location are thwarted if they don't know where anything useful is located. The stack and heap are randomly located by default but this allows the code section to be randomly located as well. On an AMD64 processor where a library was not compiled with -fPIC, this will cause an error such as: "relocation R_X86_64_32 against `......' can not be used when making a shared object;" To test that you have built PIE executable, install scanelf, part of paxutils, and use: scanelf -e ./bitcoin The output should contain: TYPE ET_DYN * Non-executable Stack If the stack is executable then trivial stack based buffer overflow exploits are possible if vulnerable buffers are found. By default, bitcoin should be built with a non-executable stack but if one of the libraries it uses asks for an executable stack or someone makes a mistake and uses a compiler extension which requires an executable stack, it will silently build an executable without the non-executable stack protection. To verify that the stack is non-executable after compiling use: `scanelf -e ./bitcoin` the output should contain: STK/REL/PTL RW- R-- RW- The STK RW- means that the stack is readable and writeable but not executable. Disable-wallet mode -------------------- When the intention is to run only a P2P node without a wallet, bitcoin may be compiled in disable-wallet mode with: ./configure --disable-wallet In this case there is no dependency on Berkeley DB 4.8. Mining is also possible in disable-wallet mode, but only using the `getblocktemplate` RPC call not `getwork`. Additional Configure Flags -------------------------- A list of additional configure flags can be displayed with: ./configure --help Setup and Build Example: Arch Linux ----------------------------------- This example lists the steps necessary to setup and build a command line only, non-wallet distribution of the latest changes on Arch Linux: pacman -S git base-devel boost libevent python git clone https://github.com/bitcoin/bitcoin.git cd bitcoin/ ./autogen.sh ./configure --disable-wallet --without-gui --without-miniupnpc make check Note: Enabling wallet support requires either compiling against a Berkeley DB newer than 4.8 (package `db`) using `--with-incompatible-bdb`, or building and depending on a local version of Berkeley DB 4.8. The readily available Arch Linux packages are currently built using `--with-incompatible-bdb` according to the [PKGBUILD](https://projects.archlinux.org/svntogit/community.git/tree/bitcoin/trunk/PKGBUILD). As mentioned above, when maintaining portability of the wallet between the standard Bitcoin Core distributions and independently built node software is desired, Berkeley DB 4.8 must be used. ARM Cross-compilation ------------------- These steps can be performed on, for example, an Ubuntu VM. The depends system will also work on other Linux distributions, however the commands for installing the toolchain will be different. Make sure you install the build requirements mentioned above. Then, install the toolchain and curl: sudo apt-get install g++-arm-linux-gnueabihf curl To build executables for ARM: cd depends make HOST=arm-linux-gnueabihf NO_QT=1 cd .. ./configure --prefix=$PWD/depends/arm-linux-gnueabihf --enable-glibc-back-compat --enable-reduce-exports LDFLAGS=-static-libstdc++ make For further documentation on the depends system see [README.md](../depends/README.md) in the depends directory.
0x11 写在最后
-
后续,我会尽可能陆续更新代码的研读顺序
-
希望更多志同道合的人士,可以提出建议与批评,共同进步