Bitcoin-Core-Development

如何開始調試比特幣核心功能測試?

  • April 18, 2021

我想開始調試比特幣核心程式碼。我聽說調試比特幣核心功能測試是一個很好的起點,因為它們是在 Python 而不是 C++ 中,而且它們更容易理解。有什麼建議或好的資源可以幫助我做到這一點嗎?

要了解功能測試,我認為首先要了解如何bitcoind工作以及如何使用 RPC 介面是很重要的。

在終端視窗中以 regtest 模式啟動 Bitcoin Core(預設情況下不連接任何對等點的本地測試網路):

$ bitcoind -regtest

密切關注輸出!這是您的調試日誌,重要的消息會列印在那裡——許多測試甚至會檢查以確保列印了特定的消息。

在第二個視窗中,獲取命令列表:

$ bitcoin-cli -regtest help

您可以獲得任何特定命令的幫助:

$  bitcoin-cli -regtest help getnewaddress
getnewaddress ( "label" "address_type" )

Returns a new Bitcoin address for receiving payments.
If 'label' is specified, it is added to the address book 
so payments received with the address will be associated with 'label'.

Arguments:
1. label           (string, optional, default="") The label name for the address to be linked to. It can also be set to the empty string "" to represent the default label. The label does not need to exist, it will be created if there is no label by the given name.
2. address_type    (string, optional, default=set by -addresstype) The address type to use. Options are "legacy", "p2sh-segwit", and "bech32".

Result:
"str"    (string) The new bitcoin address

Examples:
> bitcoin-cli getnewaddress 
> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getnewaddress", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/

試一試!

$ bitcoin-cli -regtest getnewaddress
bcrt1qua6ku8kkx4m57tg6q44wh00h58vpk7lm8h82sy

現在去看看一個簡單的測試,比如wallet_resendwallettransactions.py並註意語法node.getnewaddress()——此時你可能確切地知道它做了什麼以及返回值是什麼。

要更深入地了解測試框架如何啟動和配置各個節點,請閱讀test_framework.py. 您會注意到許多功能測試將配置參數傳遞給start_nodes()函式。

要獲取這些命令行參數的字典,請執行:

$ bitcoind -help

這些是我認為的關鍵工具,程序中的幫助資訊是非常寶貴的資源。通過功能測試並查找 rpc 命令和啟動參數,您最終會本能地了解更複雜的測試是如何工作的。

我會指出一些不同的資源。

  • 有關 Bitcoin Core 中功能測試的更多資訊(以及編寫它們的指導),我會查看功能測試README
  • 對於調試功能測試的案例研究,Sjors Provoost 在 2017 年寫了一篇博文。(其中一些現在可能已經過時,但總體構想非常好。)
  • Fabian Jahr 在 Bitcoin Edge Dev++ 上做了幾個關於功能測試框架調試的演講。
  • Fabian在 Advancing Bitcoin 上舉辦了關於調試比特幣核心的研討會
  • Fabian 還一直在起草一份調試文件,其中包含有關調試單元測試和功能測試的指導。它目前面向 MacOS 使用者,因此如果您使用的是 Linux 或 Windows,則可能沒有那麼有用。
  • 最後,Gloria Zhao 一直在起草一份文件,其中包含有關功能測試框架的更多資訊。

這應該足以讓你開始!如有任何問題,歡迎在 IRC 的#bitcoin-core-pr-reviews 頻道上發布。

引用自:https://bitcoin.stackexchange.com/questions/98992