前言
入坑區(qū)塊鏈,對(duì)于第一個(gè)把區(qū)塊鏈技術(shù)推向巔峰的比特幣不能不了解摧冀。而了解一項(xiàng)技術(shù)應(yīng)用的最佳途徑莫過(guò)于親自編譯和閱讀比特幣的源碼菜拓,盡管筆者可能力有不逮瓣窄,但愿意一試。我想纳鼎,過(guò)程可能很艱難俺夕,終點(diǎn)會(huì)有美麗的風(fēng)景等著我。
推薦精通比特幣第二版贱鄙,講解比特幣原理非常不錯(cuò)的一本書(shū)啥么。
bitcoin github項(xiàng)目介紹
打開(kāi)bitcoin的github地址,我們發(fā)現(xiàn)共有四個(gè)目錄:
bitcoin 比特幣核心源代碼 C++
bips 有關(guān)比特幣改進(jìn)建議的相關(guān)文檔 我們經(jīng)常聽(tīng)到的“”
libbase58 比特幣base58編解碼庫(kù) C
libblkmaker 比特幣區(qū)塊模板庫(kù) C
打開(kāi)bitcoin,我們看一下核心代碼的架構(gòu):
擼起袖子就是干
1.從github拉取對(duì)應(yīng)版本的bitcoin源碼
//拉取bitcoin源代碼
git clone https://github.com/bitcoin/bitcoin.git
//切換版本到最新
git checkout 0.16
2.按官方build文檔依次執(zhí)行
看官方build文檔竟意外地發(fā)現(xiàn)贰逾,可以用Qt來(lái)構(gòu)建和調(diào)試bitcoin。有了IDE之后菠秒,在看源碼的時(shí)候疙剑,相關(guān)函數(shù)就可以直接跳轉(zhuǎn)到定義的地方。不知方便了多少践叠。言缤。。
按著步驟來(lái)禁灼,一步也不要拉下管挟,不然后面可能會(huì)有意向不到的障礙。約摸10min左右弄捕,整個(gè)build過(guò)程結(jié)束僻孝。
3.幾個(gè)主要程序
Bitcoin Core.app Bitcoin客戶端圖形界面版
bitcoind /src/bitcoind Bitcoin簡(jiǎn)潔命令行版,也是下一步源代碼分析的重點(diǎn)(不能與Bitcoin Core同事運(yùn)行,如果不小心嘗試同時(shí)運(yùn)行另外一個(gè)客戶端守谓,它會(huì)提示已經(jīng)有一個(gè)客戶端在運(yùn)行并且自動(dòng)退出)
bitcoin-cli /src/bitcoin-cli Bitcoind的一個(gè)功能完備的RPC客戶端穿铆,可以通過(guò)它在命令行查詢某個(gè)區(qū)塊信息,交易信息等
bitcoin-tx /src/bitcoind 比特幣交易處理模塊斋荞,可以進(jìn)行交易的查詢和創(chuàng)建
bitcoind
1.啟動(dòng)與結(jié)束bitcoind
cd .../bitcoin/src
//1.啟動(dòng)bitcoind,會(huì)聯(lián)網(wǎng)進(jìn)行block的同步荞雏,180多G耗時(shí)耗內(nèi)存
./bitcoind
//2.啟動(dòng)并指定同步數(shù)
./bitcoind -maxconnections=0
//3.或者在1啟動(dòng)后通過(guò)bitcoin-cli無(wú)效化當(dāng)前區(qū)塊
./bitcoin-cli invalidateblock `bitcoin-cli getbestblockhash`
//4.結(jié)束
./bitcoin-cli stop
注:如果突然發(fā)現(xiàn)你的Mac內(nèi)存不夠用了,老鐵平酿,一定是bitcoin干的凤优!Mac下默認(rèn)的block同步路徑可是讓我好找,貼出來(lái)省得大家到時(shí)候懵逼不知道哪里找:
/Users/[User]/Library/Application Support/Bitcoin/blocks
2. bitcoind其他命令
./bitcoind -help可以查看bitcoind支持的各種命令和帶參格式:
//bitcoind 命令通用格式
bitcoind [選項(xiàng)]
bitcoind [選項(xiàng)] <命令> [參數(shù)] 將命令發(fā)送到 -server 或 bitcoind
bitcoind [選項(xiàng)] help 列出命令
bitcoind [選項(xiàng)] help <命令> 獲取該命令的幫助
//bitcoind常見(jiàn)命令
-conf=<文件名> 指定配置文件(默認(rèn):bitcoin.conf)
-pid=<文件名> 指定 pid (進(jìn)程 ID)文件(默認(rèn):bitcoind.pid)
-gen 生成比特幣
-gen=0 不生成比特幣
-min 啟動(dòng)時(shí)最小化
-splash 啟動(dòng)時(shí)顯示啟動(dòng)屏幕(默認(rèn):1)
-datadir=<目錄名> 指定數(shù)據(jù)目錄
-dbcache=<n style="word-wrap: break-word;"> 設(shè)置數(shù)據(jù)庫(kù)緩存大小蜈彼,單位為兆字節(jié)(MB)(默認(rèn):25)</n>
-dblogsize=<n style="word-wrap: break-word;"> 設(shè)置數(shù)據(jù)庫(kù)磁盤日志大小筑辨,單位為兆字節(jié)(MB)(默認(rèn):100)</n>
-timeout=<n style="word-wrap: break-word;"> 設(shè)置連接超時(shí),單位為毫秒</n>
-proxy=<ip:端口 style="word-wrap: break-word;"> 通過(guò) Socks4 代理鏈接</ip:端口>
-dns addnode 允許查詢 DNS 并連接
-port=<端口> 監(jiān)聽(tīng) <端口> 上的連接(默認(rèn):8333柳刮,測(cè)試網(wǎng)絡(luò) testnet:18333)
-maxconnections=<n style="word-wrap: break-word;"> 最多維護(hù) <n style="word-wrap: break-word;">個(gè)節(jié)點(diǎn)連接(默認(rèn):125)</n></n>
-addnode=<ip style="word-wrap: break-word;"> 添加一個(gè)節(jié)點(diǎn)以供連接挖垛,并嘗試保持與該節(jié)點(diǎn)的連接</ip>
-connect=<ip style="word-wrap: break-word;"> 僅連接到這里指定的節(jié)點(diǎn)</ip>
-irc 使用 IRC(因特網(wǎng)中繼聊天)查找節(jié)點(diǎn)(默認(rèn):0)
-listen 接受來(lái)自外部的連接(默認(rèn):1)
-dnsseed 使用 DNS 查找節(jié)點(diǎn)(默認(rèn):1)
-banscore=<n style="word-wrap: break-word;"> 與行為異常節(jié)點(diǎn)斷開(kāi)連接的臨界值(默認(rèn):100)</n>
-bantime=<n style="word-wrap: break-word;"> 重新允許行為異常節(jié)點(diǎn)連接所間隔的秒數(shù)(默認(rèn):86400)</n>
-maxreceivebuffer=<n style="word-wrap: break-word;"> 最大每連接接收緩存痒钝,<n style="word-wrap: break-word;">*1000 字節(jié)(默認(rèn):10000)</n></n>
-maxsendbuffer=<n style="word-wrap: break-word;"> 最大每連接發(fā)送緩存,<n style="word-wrap: break-word;">*1000 字節(jié)(默認(rèn):10000)</n></n>
-upnp 使用全局即插即用(UPNP)映射監(jiān)聽(tīng)端口(默認(rèn):0)
-detachdb 分離貨幣塊和地址數(shù)據(jù)庫(kù)痢毒。會(huì)增加客戶端關(guān)閉時(shí)間(默認(rèn):0)
-paytxfee=<amt style="word-wrap: break-word;"> 您發(fā)送的交易每 KB 字節(jié)的手續(xù)費(fèi)</amt>
-testnet 使用測(cè)試網(wǎng)絡(luò)
-debug 輸出額外的調(diào)試信息
-logtimestamps 調(diào)試信息前添加[時(shí)間戳](http://8btc.com/article-165-1.html)
-printtoconsole 發(fā)送跟蹤/調(diào)試信息到控制臺(tái)而不是 debug.log 文件
-printtodebugger 發(fā)送跟蹤/調(diào)試信息到調(diào)試器
-rpcuser=<用戶名> JSON-RPC 連接使用的用戶名
-rpcpassword=<密碼> JSON-RPC 連接使用的密碼
-rpcport=<port style="word-wrap: break-word;"> JSON-RPC 連接所監(jiān)聽(tīng)的 <端口>(默認(rèn):8332)</port>
-rpcallowip=<ip style="word-wrap: break-word;"> 允許來(lái)自指定 <ip style="word-wrap: break-word;">地址的 JSON-RPC 連接</ip></ip>
-rpcconnect=<ip style="word-wrap: break-word;"> 發(fā)送命令到運(yùn)行在 <ip style="word-wrap: break-word;">地址的節(jié)點(diǎn)(默認(rèn):127.0.0.1)</ip></ip>
-blocknotify=<命令> 當(dāng)最好的貨幣塊改變時(shí)執(zhí)行命令(命令中的 %s 會(huì)被替換為貨幣塊哈希值)
-upgradewallet 將錢包升級(jí)到最新的格式
-keypool=<n style="word-wrap: break-word;"> 將密匙池的尺寸設(shè)置為 <n style="word-wrap: break-word;">(默認(rèn):100)</n></n>
-rescan 重新掃描貨幣塊鏈以查找錢包丟失的交易
-checkblocks=<n style="word-wrap: break-word;"> 啟動(dòng)時(shí)檢查多少貨幣塊(默認(rèn):2500送矩,0 表示全部)</n>
-checklevel=<n style="word-wrap: break-word;"> 貨幣塊驗(yàn)證的級(jí)別(0-6,默認(rèn):1)</n>
**SSL 選項(xiàng):**
-rpcssl 使用 OpenSSL(https)JSON-RPC 連接
-rpcsslcertificatechainfile=<文件.cert> 服務(wù)器證書(shū)文件(默認(rèn):server.cert)
-rpcsslprivatekeyfile=<文件.pem> 服務(wù)器私匙文件(默認(rèn):server.pem)
-rpcsslciphers=<密碼> 可接受的密碼(默認(rèn):TLSv1+HIGH:!SSLv2:!aNULL:!eNULL:!AH:!3DES:@STRENGTH)
bitcoin-cli
Bitcoin Core包含的一個(gè)功能完備的RPC客戶端,可以查詢區(qū)塊哪替,錢包栋荸,交易等各項(xiàng)信息。比特幣使用的是Json-RPC接口凭舶。
1.命令示例
//獲取height為0的區(qū)塊哈希
./src/bitcoin-cli getblockhash 0
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
//獲取已知某哈希的區(qū)塊具體信息
./src/bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
{
"hash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
"confirmations": 187783,
"strippedsize": 285,
"size": 285,
"weight": 1140,
"height": 0,
"version": 1,
"versionHex": "00000001",
"merkleroot": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"tx": [
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
],
"time": 1231006505,
"mediantime": 1231006505,
"nonce": 2083236893,
"bits": "1d00ffff",
"difficulty": 1,
"chainwork": "0000000000000000000000000000000000000000000000000000000100010001",
"nextblockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
}
2.其他命令
A晌块、一般性的命令
//幫助指令
help ( "command" )
stop //停止bitcoin服務(wù)
getinfo //獲取當(dāng)前節(jié)點(diǎn)信息
ping
getnettotals
getnetworkinfo
getpeerinfo
getconnectioncount
verifychain ( checklevel numblocks )
getaddednodeinfo dns ( "node" )
addnode "node" "add|remove|onetry"
B、錢包帅霜、賬戶匆背、地址、轉(zhuǎn)帳身冀、發(fā)消息
getwalletinfo //獲取錢包信息
walletpassphrase "passphrase" timeout //解鎖錢包 密碼-多久錢包自動(dòng)鎖定
walletlock //鎖定錢包
walletpassphrasechange "oldpassphrase" "newpassphrase" //更改錢包密碼
backupwallet "destination" //錢包備份
importwallet "filename" //錢包備份文件導(dǎo)入
dumpwallet "filename" //錢包恢復(fù)
listaccounts ( minconf ) //列出所有用戶
getaddressesbyaccount "account" //列出用戶所有的錢包地址
getaccountaddress "account"
getaccount "bitcoinaddress"
validateaddress "bitcoinaddress"
dumpprivkey "bitcoinaddress"
setaccount "bitcoinaddress" "account"
getnewaddress ( "account" ) //獲取新的錢包地址钝尸,由錢包地址池生成
keypoolrefill ( newsize )
importprivkey "bitcoinprivkey" ( "label" rescan ) //導(dǎo)入私鑰
createmultisig nrequired ["key",...]
addmultisigaddress nrequired ["key",...] ( "account" )
getbalance ( "account" minconf ) //顯示所有經(jīng)過(guò)至少minconf個(gè)確認(rèn)的交易加和后的余額
getunconfirmedbalance
//獲取account或bitcoinaddress對(duì)應(yīng)收到的比特幣數(shù)量
getreceivedbyaccount "account" ( minconf )
getreceivedbyaddress "bitcoinaddress" ( minconf )
listreceivedbyaccount ( minconf includeempty )
listreceivedbyaddress ( minconf includeempty )
move "fromaccount" "toaccount" amount ( minconf "comment" )
listunspent ( minconf maxconf ["address",...] )
listlockunspent
lockunspent unlock [{"txid":"txid","vout":n},...]
getrawchangeaddress
listaddressgroupings
settxfee amount
sendtoaddress "bitcoinaddress" amount ( "comment" "comment-to" )
sendfrom "fromaccount" "tobitcoinaddress" amount ( minconf "comment" "comment-to" )
sendmany "fromaccount" {"address":amount,...} ( minconf "comment" )
signmessage "bitcoinaddress" "message"
verifymessage "bitcoinaddress" "signature" "message"
C、Tx搂根、Block珍促、Ming
createrawtransaction [{"txid":"id","vout":n},...] {"address":amount,...}
signrawtransaction "hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex","redeemScript":"hex"},...] ["privatekey1",...] sighashtype )
sendrawtransaction "hexstring" ( allowhighfees )
gettransaction "txid" //獲取簡(jiǎn)化版交易信息
//獲取完整交易信息
getrawtransaction "txid" ( verbose ) //得到一個(gè)描述交易的16進(jìn)制字符串
decoderawtransaction "hexstring" //解碼上面的字符串
listtransactions ( "account" count from ) //獲取錢包對(duì)應(yīng)的交易
listsinceblock ( "blockhash" target-confirmations )
getrawmempool ( verbose )
gettxoutsetinfo
gettxout "txid" n ( includemempool )
decodescript "hex"
getblockchaininfo
getblockcount
getbestblockhash
getblockhash index
getblock "hash" ( verbose )
getmininginfo
getdifficulty
getnetworkhashps ( blocks height )
gethashespersec
getgenerate
setgenerate generate ( genproclimit )
getwork ( "data" )
getblocktemplate ( "jsonrequestobject" )
submitblock "hexdata" ( "jsonparametersobject" )
Mac下bitcoin源碼的編譯就完成了,同時(shí)我們也了解了bitcoind和bitcoin-cli基本命令剩愧。萬(wàn)里長(zhǎng)征終于邁出了第一步猪叙,今天就到這里了。
下一篇認(rèn)識(shí)bitcoin源碼整體框架和代碼模塊的功能仁卷。
互聯(lián)網(wǎng)顛覆世界穴翩,區(qū)塊鏈顛覆互聯(lián)網(wǎng)!
--------------------------------------------------20180417 23:08