探索比特币源码2-配置Bitcoin Core节点
探索比特幣源碼2-配置Bitcoin Core節點
回顧上文探索比特幣源碼1-運行比特幣核心
我們已經運行上了Bitcoin Core,開始同步區塊鏈數據
這個過程會持續一兩天,我也不能干等著,便開始進行一些小練習,比如區塊信息的查詢,交易信息的查詢等等。
一個小插曲
一切本來很順利,但是似乎是由于我運行如下命令后與Bitcoind產生了沖突
$ curl https://blockchain.info/unspent?active=1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK這個命令查詢指定賬戶所有的可用未消費輸出utxo
在這之后,我再運行命令行幫助程序bitcoin-cli訪問比特幣核心客戶端的RPC接口時,如bitcoin-cli getblockchaininfo等命令,無一例外的報了如下的錯誤,甚至是bitcoin-cli stop。
$ bitcoin-cli getblockchaininfo error: Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (/home/username/.bitcoin/bitcoin.conf) $ bitcoin-cli stop error: Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (/home/username/.bitcoin/bitcoin.conf)這個錯誤看似是由于我們之前沒有配置bitcoin.conf導致的,我嘗試配置了bitcoin.conf,但并沒有效果
經過一番的探索,我發現了報錯的真正原因是Bitcoin Core客戶端意外的關閉了,很可能是剛剛執行的命令,即HTTP命令行客戶端curl進行通信的過程中與比特幣節點存在某些沖突(這僅僅只是我個人的猜測,還望了解的大佬告知)
但無論如何,問題被找到了,解決方案簡單到爆炸,重新運行客戶端即可…
因此,如果你也遇到了這個問題,首先檢查客戶端是否正在運行是個明智的舉動。
配置bitcoind并重建索引
配置bitcoind
雖然剛剛的小插曲被證實與Bitcoin Core的配置無關,但為了避免后面還會遇到什么問題,這里還是打算將上一文忽略的配置問題解決。
這里手動創建并編輯配置文件bitcoin.conf,該文件通常要放在~/.bitcoin/bitcoin.conf路徑下
無論進行何種配置,配置文件bitcoin.conf必須包含rpcuser和rpcpassword兩個條目。
如下是一個完整索引節點的配置示例:
txindex=1 rpcuser=bitcoinrpc rpcpassward=CHANGE_THIS默認情況下,Bitcoin Core構建一個僅包含與用戶錢包有關的交易的數據庫。如果想要使用諸如getrawtransaction之類的命令訪問任何交易的信息(后面會慢慢介紹),則需要配置Bitcoin Core以構建完整的交易索引,這可以通過在配置文件中設置txindex = 1來實現。
這里是一個當設備資源有限時的配置示例:
maxconnections=15 prune=5000 minrelaytxfee=0.0001 maxmempool=200 maxreceivebuffer=2500 maxsendbuffer=500 rpcuser=bitcoinrpc rpcpassword=CHANGE_THIS更多的配置選項及相關解釋可以通過bitcoind --help命令查看
顯然,為了擁有完整的功能我們需要配置一個完整索引節點。
重建索引
編輯好配置文件后,重新運行bitcoind客戶端,并將輸出打印到屏幕上
$ bitcoind -printtoconsole......2018-07-20 08:01:43 LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=1005, size=90286441, heights=182434...184177, time=2012-06-01...2012-06-12)2018-07-20 08:01:43 Checking all blk files are present...2018-07-20 08:01:43 LoadBlockIndexDB: transaction index disabled2018-07-20 08:01:43 : You need to rebuild the database using -reindex to change -txindex.Please restart with -reindex or -reindex-chainstate to recover.: You need to rebuild the database using -reindex to change -txindex.Please restart with -reindex or -reindex-chainstate to recover.2018-07-20 08:01:43 Aborted block database rebuild. Exiting.2018-07-20 08:01:43 Shutdown: In progress...2018-07-20 08:01:43 scheduler thread interrupt2018-07-20 08:01:43 Shutdown: done可以看到,按照提示,我們需要使用-reindex選項重新啟動bitcoind,并等待它重建索引
$ bitcoind -printtoconsole -reindex......2018-07-20 08:06:10 Reindexing block file blk00006.dat...2018-07-20 08:06:15 Loaded 7472 blocks from external file in 4869ms2018-07-20 08:06:15 Reindexing block file blk00007.dat...2018-07-20 08:06:19 Loaded 6097 blocks from external file in 4374ms2018-07-20 08:06:19 Reindexing block file blk00008.dat...2018-07-20 08:06:26 Loaded 6127 blocks from external file in 6473ms2018-07-20 08:06:26 Reindexing block file blk00009.dat.........現在我們等待重建索引的完成就可以了。
注:當我們的bitcoind客戶端出現過問題并嘗試解決之后,一個好的習慣是首先使用bitcoind -printtoconsole將運行過程打印到終端上,這樣可以直觀的了解錯誤是否解決,確認無誤后再重新運行,轉為后臺守護進程的模式。
總結
以上是生活随笔為你收集整理的探索比特币源码2-配置Bitcoin Core节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android系统架构图
- 下一篇: Modbus协议栈开发笔记之八:Modb