1. 程式人生 > >比特幣BTC原始碼分析(0):環境搭建

比特幣BTC原始碼分析(0):環境搭建

一、Bitcoin編譯執行啟動過程

1、從Github上clone bitcoin原始碼 至本地

~/go/src/github.com/bitcoin$git clone https://github.com/bitcoin/bitcoin.git
Cloning into 'bitcoin'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 126886 (
delta 7), reused 3 (delta 3), pack-reused 126870 Receiving objects: 100% (126886/126886), 113.75 MiB | 6.86 MiB/s, done. Resolving deltas: 100% (88524/88524), done. ~/go/src/github.com/bitcoin

2、根據官方文件指導進行編譯安裝

各系統版本安裝方法:
Mac -osx
Linux
Window

主要命令:

~/go/src/github.com/bitcoin/bitcoin$./autogen.sh
#開啟debug進行configure配置
~/go/src/github.com/bitcoin/bitcoin$./configure -with-gui --enable-debug #成功配置結果如下 Options used to compile and link: with wallet = yes with gui / qt = yes with bip70 = yes with qr = yes with zmq = no with test = yes with bench = yes with upnp = yes use asm = yes
sanitizers = debug enabled = yes gprof enabled = no werror = no target os = darwin build os = darwin CC = gcc CFLAGS = -g -O2 CPPFLAGS = -DDEBUG -DDEBUG_LOCKORDER -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS -I/usr/local/opt/[email protected]/include -DMAC_OSX CXX = g++ -std=c++11 CXXFLAGS = -Og -g3 -ftrapv -Wstack-protector -fstack-protector-all -Wall -Wextra -Wformat -Wvla -Wformat-security -Wthread-safety-analysis -Wrange-loop-analysis -Wredundant-decls -Wno-unused-parameter -Wno-self-assign -Wno-unused-local-typedef -Wno-deprecated-register -Wno-implicit-fallthrough LDFLAGS = -pthread -Wl,-headerpad_max_install_names -Wl,-dead_strip ARFLAGS = cr ~/go/src/github.com/bitcoin/bitcoin$make check

3、編譯結果目錄結構

(1)./src/為整個比特幣核心的程式碼,由C++編寫;

(2)./src/bitcoind為比特幣核心啟動程式;

(3)./src/bitcoin-cli為客戶端控制程式,命令列中會用到;

(4)./qt/為qt專案目錄,qt的客戶端其實就是呼叫了bitcoind和bitcoin-cli的介面。

4、安裝目錄(包含輸出日誌、區塊鏈等等)

預設在其他目錄下:
Linux在~/.bitcoin中;
Mac在/User/YOURNAME/Library/Application Support/Bitcoin中,不太好找,用前往或者終端;
windows在C盤下的Bitcoin。
如果想在程式碼裡改改預設路徑可以去./src/utils.h第455行左右,找GetDefaultDataDir函式。
可用命令列跟蹤日誌檔案,日誌檔案在上面說的bitcoin資料夾裡面(mac上直接點開檔案就行)

5、命令列啟動

(1)命令列啟動客戶端

~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind 
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind -daemon #後臺啟動,一般都要加,然後在debug.log裡看輸出就行
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind -gen=1 #自動挖礦
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind help #其他的自己看去吧

(2)操作客戶端

~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli stop
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli getinfo #檢視當前資訊
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli getpeerinfo #檢視其他節點
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli help #其他的自己看去吧