223588b1bbAdd a --descriptors option to various tests (Andrew Chow)869f7ab30atests: Add RPCOverloadWrapper which overloads some disabled RPCs (Andrew Chow)cf06062859Correctly check for default wallet (Andrew Chow)886e0d75f5Implement CWallet::IsSpentKey for non-LegacySPKMans (Andrew Chow)3c19fdd2a2Return error when no ScriptPubKeyMan is available for specified type (Andrew Chow)388ba94231Change wallet_encryption.py to use signmessage instead of dumpprivkey (Andrew Chow)1346e14831Functional tests for descriptor wallets (Andrew Chow)f193ea889dadd importdescriptors RPC and tests for native descriptor wallets (Hugo Nguyen)ce24a94494Add IsLegacy to CWallet so that the GUI knows whether to show watchonly (Andrew Chow)1cb42b22b1Generate new descriptors when encrypting (Andrew Chow)82ae02b165Be able to create new wallets with DescriptorScriptPubKeyMans as backing (Andrew Chow)b713baa75aImplement GetMetadata in DescriptorScriptPubKeyMan (Andrew Chow)8b9603bd0bChange GetMetadata to use unique_ptr<CKeyMetadata> (Andrew Chow)72a9540df9Implement FillPSBT in DescriptorScriptPubKeyMan (Andrew Chow)84b4978c02Implement SignMessage for descriptor wallets (Andrew Chow)bde7c9fa38Implement SignTransaction in DescriptorScriptPubKeyMan (Andrew Chow)d50c8ddd41Implement GetSolvingProvider for DescriptorScriptPubKeyMan (Andrew Chow)f1ca5feb4aImplement GetKeypoolOldestTime and only display it if greater than 0 (Andrew Chow)586b57a9a6Implement ReturnDestination in DescriptorScriptPubKeyMan (Andrew Chow)f866957979Implement GetReservedDestination in DescriptorScriptPubKeyMan (Andrew Chow)a775f7c7fdImplement Unlock and Encrypt in DescriptorScriptPubKeyMan (Andrew Chow)bfdd073486Implement GetNewDestination for DescriptorScriptPubKeyMan (Andrew Chow)58c7651821Implement TopUp in DescriptorScriptPubKeyMan (Andrew Chow)e014886a34Implement SetupGeneration for DescriptorScriptPubKeyMan (Andrew Chow)46dfb99768Implement writing descriptorkeys, descriptorckeys, and descriptors to wallet file (Andrew Chow)4cb9b69be0Implement several simple functions in DescriptorScriptPubKeyMan (Andrew Chow)d1ec3e4f19Add IsSingleType to Descriptors (Andrew Chow)953feb3d27Implement loading of keys for DescriptorScriptPubKeyMan (Andrew Chow)2363e9fcaaLoad the descriptor cache from the wallet file (Andrew Chow)46c46aebb7Implement GetID for DescriptorScriptPubKeyMan (Andrew Chow)ec2f9e1178Implement IsHDEnabled in DescriptorScriptPubKeyMan (Andrew Chow)741122d4c1Implement MarkUnusedAddresses in DescriptorScriptPubKeyMan (Andrew Chow)2db7ca765cImplement IsMine for DescriptorScriptPubKeyMan (Andrew Chow)db7177af8cAdd LoadDescriptorScriptPubKeyMan and SetActiveScriptPubKeyMan to CWallet (Andrew Chow)78f8a92910Implement SetType in DescriptorScriptPubKeyMan (Andrew Chow)834de0300cStore WalletDescriptor in DescriptorScriptPubKeyMan (Andrew Chow)d8132669e1Add a lock cs_desc_man for DescriptorScriptPubKeyMan (Andrew Chow)3194a7f88aIntroduce WalletDescriptor class (Andrew Chow)6b13cd3fa8Create LegacyScriptPubKeyMan when not a descriptor wallet (Andrew Chow)aeac157c9dReturn nullptr from GetLegacyScriptPubKeyMan if descriptor wallet (Andrew Chow)96accc73f0Add WALLET_FLAG_DESCRIPTORS (Andrew Chow)6b8119af53Introduce DescriptorScriptPubKeyMan as a dummy class (Andrew Chow)06620302c7Introduce SetType function to tell ScriptPubKeyMans the type and internal-ness of it (Andrew Chow) Pull request description: Introducing the wallet of the glorious future (again): native descriptor wallets. With native descriptor wallets, addresses are generated from descriptors. Instead of generating keys and deriving addresses from keys, addresses come from the scriptPubKeys produced by a descriptor. Native descriptor wallets will be optional for now and can only be created by using `createwallet`. Descriptor wallets will store descriptors, master keys from the descriptor, and descriptor cache entries. Keys are derived from descriptors on the fly. In order to allow choosing different address types, 6 descriptors are needed for normal use. There is a pair of primary and change descriptors for each of the 3 address types. With the default keypool size of 1000, each descriptor has 1000 scriptPubKeys and descriptor cache entries pregenerated. This has a side effect of making wallets large since 6000 pubkeys are written to the wallet by default, instead of the current 2000. scriptPubKeys are kept only in memory and are generated every time a descriptor is loaded. By default, we use the standard BIP 44, 49, 84 derivation paths with an external and internal derivation chain for each. Descriptors can also be imported with a new `importdescriptors` RPC. Native descriptor wallets use the `ScriptPubKeyMan` interface introduced in #16341 to add a `DescriptorScriptPubKeyMan`. This defines a different IsMine which uses the simpler model of "does this scriptPubKey exist in this wallet". Furthermore, `DescriptorScriptPubKeyMan` does not have watchonly, so with native descriptor wallets, it is not possible to have a wallet with both watchonly and non-watchonly things. Rather a wallet with `disable_private_keys` needs to be used for watchonly things. A `--descriptor` option was added to some tests (`wallet_basic.py`, `wallet_encryption.py`, `wallet_keypool.py`, `wallet_keypool_topup.py`, and `wallet_labels.py`) to allow for these tests to use descriptor wallets. Additionally, several RPCs are disabled for descriptor wallets (`importprivkey`, `importpubkey`, `importaddress`, `importmulti`, `addmultisigaddress`, `dumpprivkey`, `dumpwallet`, `importwallet`, and `sethdseed`). ACKs for top commit: Sjors: utACK223588b1bb(rebased, nits addressed) jonatack: Code review re-ACK223588b1bb. fjahr: re-ACK223588b1bbinstagibbs: light re-ACK223588bmeshcollider: Code review ACK223588b1bbTree-SHA512: 59bc52aeddbb769ed5f420d5d240d8137847ac821b588eb616b34461253510c1717d6a70bab8765631738747336ae06f45ba39603ccd17f483843e5ed9a90986
Functional tests
Writing Functional Tests
Example test
The file test/functional/example_test.py is a heavily commented example of a test case that uses both the RPC and P2P interfaces. If you are writing your first test, copy that file and modify to fit your needs.
Coverage
Running test/functional/test_runner.py with the --coverage argument tracks which RPCs are
called by the tests and prints a report of uncovered RPCs in the summary. This
can be used (along with the --extended argument) to find out which RPCs we
don't have test cases for.
Style guidelines
- Where possible, try to adhere to PEP-8 guidelines
- Use a python linter like flake8 before submitting PRs to catch common style nits (eg trailing whitespace, unused imports, etc)
- The oldest supported Python version is specified in doc/dependencies.md. Consider using pyenv, which checks .python-version, to prevent accidentally introducing modern syntax from an unsupported Python version. The Travis linter also checks this, but possibly not in all cases.
- See the python lint script that checks for violations that could lead to bugs and issues in the test code.
- Avoid wildcard imports
- Use a module-level docstring to describe what the test is testing, and how it is testing it.
- When subclassing the BitcoinTestFramwork, place overrides for the
set_test_params(),add_options()andsetup_xxxx()methods at the top of the subclass, then locally-defined helper methods, then therun_test()method. - Use
'{}'.format(x)for string formatting, not'%s' % x.
Naming guidelines
- Name the test
<area>_test.py, where area can be one of the following:featurefor tests for full features that aren't wallet/mining/mempool, egfeature_rbf.pyinterfacefor tests for other interfaces (REST, ZMQ, etc), eginterface_rest.pymempoolfor tests for mempool behaviour, egmempool_reorg.pyminingfor tests for mining features, egmining_prioritisetransaction.pyp2pfor tests that explicitly test the p2p interface, egp2p_disconnect_ban.pyrpcfor tests for individual RPC methods or features, egrpc_listtransactions.pytoolfor tests for tools, egtool_wallet.pywalletfor tests for wallet features, egwallet_keypool.py
- use an underscore to separate words
- exception: for tests for specific RPCs or command line options which don't include underscores, name the test after the exact RPC or argument name, eg
rpc_decodescript.py, notrpc_decode_script.py
- exception: for tests for specific RPCs or command line options which don't include underscores, name the test after the exact RPC or argument name, eg
- Don't use the redundant word
testin the name, eginterface_zmq.py, notinterface_zmq_test.py
General test-writing advice
- Instead of inline comments or no test documentation at all, log the comments to the test log, e.g.
self.log.info('Create enough transactions to fill a block'). Logs make the test code easier to read and the test logic easier to debug. - Set
self.num_nodesto the minimum number of nodes necessary for the test. Having additional unrequired nodes adds to the execution time of the test as well as memory/CPU/disk requirements (which is important when running tests in parallel). - Avoid stop-starting the nodes multiple times during the test if possible. A stop-start takes several seconds, so doing it several times blows up the runtime of the test.
- Set the
self.setup_clean_chainvariable inset_test_params()to control whether or not to use the cached data directories. The cached data directories contain a 200-block pre-mined blockchain and wallets for four nodes. Each node has 25 mature blocks (25x50=1250 BTC) in its wallet. - When calling RPCs with lots of arguments, consider using named keyword arguments instead of positional arguments to make the intent of the call clear to readers.
- Many of the core test framework classes such as
CBlockandCTransactiondon't allow new attributes to be added to their objects at runtime like typical Python objects allow. This helps prevent unpredictable side effects from typographical errors or usage of the objects outside of their intended purpose.
RPC and P2P definitions
Test writers may find it helpful to refer to the definitions for the RPC and P2P messages. These can be found in the following source files:
/src/rpc/*for RPCs/src/wallet/rpc*for wallet RPCsProcessMessage()in/src/net_processing.cppfor parsing P2P messages
Using the P2P interface
-
messages.py contains all the definitions for objects that pass over the network (
CBlock,CTransaction, etc, along with the network-level wrappers for them,msg_block,msg_tx, etc). -
P2P tests have two threads. One thread handles all network communication with the bitcoind(s) being tested in a callback-based event loop; the other implements the test logic.
-
P2PConnectionis the class used to connect to a bitcoind.P2PInterfacecontains the higher level logic for processing P2P payloads and connecting to the Bitcoin Core node application logic. For custom behaviour, subclass the P2PInterface object and override the callback methods. -
Can be used to write tests where specific P2P protocol behavior is tested. Examples tests are p2p_unrequested_blocks.py, p2p_compactblocks.py.
Prototyping tests
The TestShell class exposes the BitcoinTestFramework
functionality to interactive Python3 environments and can be used to prototype
tests. This may be especially useful in a REPL environment with session logging
utilities, such as
IPython.
The logs of such interactive sessions can later be adapted into permanent test
cases.
Test framework modules
The following are useful modules for test developers. They are located in test/functional/test_framework/.
authproxy.py
Taken from the python-bitcoinrpc repository.
test_framework.py
Base class for functional tests.
util.py
Generally useful functions.
mininode.py
Basic code to support P2P connectivity to a bitcoind.
script.py
Utilities for manipulating transaction scripts (originally from python-bitcoinlib)
key.py
Test-only secp256k1 elliptic curve implementation
blocktools.py
Helper functions for creating blocks and transactions.
Benchmarking with perf
An easy way to profile node performance during functional tests is provided
for Linux platforms using perf.
Perf will sample the running node and will generate profile data in the node's
datadir. The profile data can then be presented using perf report or a graphical
tool like hotspot.
There are two ways of invoking perf: one is to use the --perf flag when
running tests, which will profile each node during the entire test run: perf
begins to profile when the node starts and ends when it shuts down. The other
way is the use the profile_with_perf context manager, e.g.
with node.profile_with_perf("send-big-msgs"):
# Perform activity on the node you're interested in profiling, e.g.:
for _ in range(10000):
node.p2p.send_message(some_large_message)
To see useful textual output, run
perf report -i /path/to/datadir/send-big-msgs.perf.data.xxxx --stdio | c++filt | less
See also:
- Installing perf
- Perf examples
- Hotspot: a GUI for perf output analysis