比特币源码研读(0)bitcoin本地编译与使用

论坛 期权论坛 区块链     
chaors   2018-11-20 23:38   6661   0
            前言入坑区块链,对于第一个把区块链技术推向巅峰的比特币不能不了解。而了解一项技术应用的最佳途径莫过于亲自编译和阅读比特币的源码,尽管笔者可能力有不逮,但愿意一试。我想,过程可能很艰难,终点会有美丽的风景等着我。
推荐精通比特币第二版,讲解比特币原理非常不错的一本书。
bitcoin github项目介绍打开bitcoin的github地址,我们发现共有四个目录:
  • bitcoin 比特币核心源代码 C++
  • bips 有关比特币改进建议的相关文档  我们经常听到的“”
  • libbase58  比特币base58编解码库  C
  • libblkmaker  比特币区块模板库 C

bitcoin项目目录打开bitcoin,我们看一下核心代码的架构:

bitcoin项目结构
Mac下bitcoin编译文档撸起袖子就是干1.从github拉取对应版本的bitcoin源码
  1. //拉取bitcoin源代码git clone https://github.com/bitcoin/bitcoin.git//切换版本到最新git checkout 0.16
复制代码
2.按官方build文档依次执行看官方build文档竟意外地发现,可以用Qt来构建和调试bitcoin。有了IDE之后,在看源码的时候,相关函数就可以直接跳转到定义的地方。不知方便了多少。。。
按着步骤来,一步也不要拉下,不然后面可能会有意向不到的障碍。约摸10min左右,整个build过程结束。
3.几个主要程序
  • Bitcoin Core.app  Bitcoin客户端图形界面版
  • bitcoind   /src/bitcoind  Bitcoin简洁命令行版,也是下一步源代码分析的重点(不能与Bitcoin Core同事运行,如果不小心尝试同时运行另外一个客户端,它会提示已经有一个客户端在运行并且自动退出)
  • bitcoin-cli  /src/bitcoin-cli Bitcoind的一个功能完备的RPC客户端,可以通过它在命令行查询某个区块信息,交易信息等
  • bitcoin-tx   /src/bitcoind   比特币交易处理模块,可以进行交易的查询和创建
[h1]bitcoind[/h1]1.启动与结束bitcoind
  1. cd .../bitcoin/src//1.启动bitcoind,会联网进行block的同步,180多G耗时耗内存./bitcoind//2.启动并指定同步数./bitcoind -maxconnections=0//3.或者在1启动后通过bitcoin-cli无效化当前区块./bitcoin-cli invalidateblock `bitcoin-cli getbestblockhash`//4.结束./bitcoin-cli stop
复制代码
:如果突然发现你的Mac内存不够用了,老铁,一定是bitcoin干的!Mac下默认的block同步路径可是让我好找,贴出来省得大家到时候懵逼不知道哪里找:
  1. /Users/[User]/Library/Application Support/Bitcoin/blocks
复制代码
2. bitcoind其他命令./bitcoind -help可以查看bitcoind支持的各种命令和带参格式:

bitcoind -help
  1. //bitcoind 命令通用格式  bitcoind [选项]  bitcoind [选项]  [参数]  将命令发送到 -server 或 bitcoind  bitcoind [选项] help           列出命令  bitcoind [选项] help     获取该命令的帮助 //bitcoind常见命令  -conf=     指定配置文件(默认:bitcoin.conf)  -pid=      指定 pid (进程 ID)文件(默认:bitcoind.pid)  -gen               生成比特币  -gen=0             不生成比特币  -min               启动时最小化  -splash            启动时显示启动屏幕(默认:1)  -datadir=  指定数据目录  -dbcache=       设置数据库缓存大小,单位为兆字节(MB)(默认:25)  -dblogsize=     设置数据库磁盘日志大小,单位为兆字节(MB)(默认:100)  -timeout=       设置连接超时,单位为毫秒  -proxy=   通过 Socks4 代理链接  -dns               addnode 允许查询 DNS 并连接  -port=       监听  上的连接(默认:8333,测试网络 testnet:18333)  -maxconnections=  最多维护 个节点连接(默认:125)  -addnode=      添加一个节点以供连接,并尝试保持与该节点的连接  -connect=      仅连接到这里指定的节点  -irc               使用 IRC(因特网中继聊天)查找节点(默认:0)  -listen            接受来自外部的连接(默认:1)  -dnsseed           使用 DNS 查找节点(默认:1)  -banscore=      与行为异常节点断开连接的临界值(默认:100)  -bantime=       重新允许行为异常节点连接所间隔的秒数(默认:86400)  -maxreceivebuffer=  最大每连接接收缓存,*1000 字节(默认:10000)  -maxsendbuffer=  最大每连接发送缓存,*1000 字节(默认:10000)  -upnp              使用全局即插即用(UPNP)映射监听端口(默认:0)  -detachdb          分离货币块和地址数据库。会增加客户端关闭时间(默认:0)  -paytxfee=    您发送的交易每 KB 字节的手续费  -testnet           使用测试网络  -debug             输出额外的调试信息  -logtimestamps     调试信息前添加[时间戳](http://8btc.com/article-165-1.html)  -printtoconsole    发送跟踪/调试信息到控制台而不是 debug.log 文件  -printtodebugger   发送跟踪/调试信息到调试器  -rpcuser=  JSON-RPC 连接使用的用户名  -rpcpassword=  JSON-RPC 连接使用的密码  -rpcport=    JSON-RPC 连接所监听的 (默认:8332)  -rpcallowip=   允许来自指定 地址的 JSON-RPC 连接  -rpcconnect=   发送命令到运行在 地址的节点(默认:127.0.0.1)  -blocknotify= 当最好的货币块改变时执行命令(命令中的 %s 会被替换为货币块哈希值)  -upgradewallet     将钱包升级到最新的格式  -keypool=       将密匙池的尺寸设置为 (默认:100)  -rescan            重新扫描货币块链以查找钱包丢失的交易  -checkblocks=   启动时检查多少货币块(默认:2500,0 表示全部)  -checklevel=    货币块验证的级别(0-6,默认:1)**SSL 选项:**  -rpcssl                                  使用 OpenSSL(https)JSON-RPC 连接  -rpcsslcertificatechainfile=  服务器证书文件(默认:server.cert)  -rpcsslprivatekeyfile=         服务器私匙文件(默认:server.pem)  -rpcsslciphers=                    可接受的密码(默认:TLSv1+HIGH:!SSLv2:!aNULL:!eNULL:!AH:!3DES:@STRENGTH)
复制代码
[h1]bitcoin-cli[/h1]Bitcoin Core包含的一个功能完备的RPC客户端,可以查询区块,钱包,交易等各项信息。比特币使用的是Json-RPC接口。
1.命令示例
  1. //获取height为0的区块哈希./src/bitcoin-cli getblockhash 0000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f//获取已知某哈希的区块具体信息./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.其他命令
  1. A、一般性的命令//帮助指令help ( "command" )stop         //停止bitcoin服务getinfo      //获取当前节点信息pinggetnettotalsgetnetworkinfogetpeerinfogetconnectioncountverifychain ( checklevel numblocks )getaddednodeinfo dns ( "node" )addnode "node" "add|remove|onetry"B、钱包、账户、地址、转帐、发消息getwalletinfo       //获取钱包信息walletpassphrase "passphrase" timeout     //解锁钱包  密码-多久钱包自动锁定walletlock          //锁定钱包walletpassphrasechange "oldpassphrase" "newpassphrase"     //更改钱包密码backupwallet "destination"      //钱包备份importwallet "filename"            //钱包备份文件导入dumpwallet "filename"             //钱包恢复listaccounts ( minconf )        //列出所有用户getaddressesbyaccount "account"   //列出用户所有的钱包地址getaccountaddress "account"getaccount "bitcoinaddress"validateaddress "bitcoinaddress"dumpprivkey "bitcoinaddress"setaccount "bitcoinaddress" "account"getnewaddress ( "account" )      //获取新的钱包地址,由钱包地址池生成keypoolrefill ( newsize )importprivkey "bitcoinprivkey" ( "label" rescan )      //导入私钥createmultisig nrequired ["key",...]addmultisigaddress nrequired ["key",...] ( "account" )getbalance ( "account" minconf )    //显示所有经过至少minconf个确认的交易加和后的余额getunconfirmedbalance//获取account或bitcoinaddress对应收到的比特币数量getreceivedbyaccount "account" ( minconf )getreceivedbyaddress "bitcoinaddress" ( minconf )listreceivedbyaccount ( minconf includeempty )listreceivedbyaddress ( minconf includeempty )move "fromaccount" "toaccount" amount ( minconf "comment" )listunspent ( minconf maxconf  ["address",...] )listlockunspentlockunspent unlock [{"txid":"txid","vout":n},...]getrawchangeaddresslistaddressgroupingssettxfee amountsendtoaddress "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、Mingcreaterawtransaction [{"txid":"id","vout":n},...] {"address":amount,...}signrawtransaction "hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex","redeemScript":"hex"},...] ["privatekey1",...] sighashtype )sendrawtransaction "hexstring" ( allowhighfees )gettransaction "txid"   //获取简化版交易信息//获取完整交易信息getrawtransaction "txid" ( verbose )  //得到一个描述交易的16进制字符串decoderawtransaction "hexstring"    //解码上面的字符串listtransactions ( "account" count from )   //获取钱包对应的交易listsinceblock ( "blockhash" target-confirmations )getrawmempool ( verbose )gettxoutsetinfogettxout "txid" n ( includemempool )decodescript "hex"getblockchaininfogetblockcountgetbestblockhashgetblockhash indexgetblock "hash" ( verbose )getmininginfogetdifficultygetnetworkhashps ( blocks height )gethashespersec                                                                                                                                                       getgeneratesetgenerate generate ( genproclimit )getwork ( "data" )getblocktemplate ( "jsonrequestobject" )submitblock "hexdata" ( "jsonparametersobject" )
复制代码
Mac下bitcoin源码的编译就完成了,同时我们也了解了bitcoind和bitcoin-cli基本命令。万里长征终于迈出了第一步,今天就到这里了。
下一篇认识bitcoin源码整体框架和代码模块的功能。
[h2]互联网颠覆世界,区块链颠覆互联网![/h2]
--------------------------------------------------20180417 23:08
         
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:70
帖子:14
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP