dash/test/functional/wallet_listdescriptors.py

128 lines
5.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Copyright (c) 2014-2021 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the listdescriptors RPC."""
from test_framework.descriptors import (
descsum_create
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)
class ListDescriptorsTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
self.skip_if_no_sqlite()
# do not create any wallet by default
def init_wallet(self, i):
return
def run_test(self):
node = self.nodes[0]
assert_raises_rpc_error(-18, 'No wallet is loaded.', node.listdescriptors)
Merge bitcoin/bitcoin#22446: test: Fix wallet_listdescriptors.py if bdb is not compiled 0c845e3f8995eb8dc543a63899e5633a46091b4e test: Fix wallet_listdescriptors.py if bdb is not compiled (Hennadii Stepanov) Pull request description: If build system is configured `--without-bdb`, the `wallet_listdescriptors.py` fails: ``` $ test/functional/wallet_listdescriptors.py --descriptors 2021-07-14T13:20:52.931000Z TestFramework (INFO): Initializing test directory /tmp/bitcoin_func_test_02p7o1c9 2021-07-14T13:21:23.377000Z TestFramework (INFO): Test that the command is not available for legacy wallets. 2021-07-14T13:21:23.381000Z TestFramework (ERROR): JSONRPC error Traceback (most recent call last): File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/test_framework.py", line 128, in main self.run_test() File "test/functional/wallet_listdescriptors.py", line 34, in run_test node.createwallet(wallet_name='w1', descriptors=False) File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/test_node.py", line 685, in createwallet return self.__getattr__('createwallet')(wallet_name, disable_private_keys, blank, passphrase, avoid_reuse, descriptors, load_on_startup, external_signer) File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/coverage.py", line 47, in __call__ return_val = self.auth_service_proxy_instance.__call__(*args, **kwargs) File "/home/hebasto/GitHub/bitcoin/test/functional/test_framework/authproxy.py", line 146, in __call__ raise JSONRPCException(response['error'], status) test_framework.authproxy.JSONRPCException: Compiled without bdb support (required for legacy wallets) (-4) 2021-07-14T13:21:23.436000Z TestFramework (INFO): Stopping nodes 2021-07-14T13:21:24.092000Z TestFramework (WARNING): Not cleaning up dir /tmp/bitcoin_func_test_02p7o1c9 2021-07-14T13:21:24.092000Z TestFramework (ERROR): Test failed. Test logging available at /tmp/bitcoin_func_test_02p7o1c9/test_framework.log 2021-07-14T13:21:24.092000Z TestFramework (ERROR): 2021-07-14T13:21:24.092000Z TestFramework (ERROR): Hint: Call /home/hebasto/GitHub/bitcoin/test/functional/combine_logs.py '/tmp/bitcoin_func_test_02p7o1c9' to consolidate all logs 2021-07-14T13:21:24.092000Z TestFramework (ERROR): 2021-07-14T13:21:24.092000Z TestFramework (ERROR): If this failure happened unexpectedly or intermittently, please file a bug and provide a link or upload of the combined log. 2021-07-14T13:21:24.092000Z TestFramework (ERROR): https://github.com/bitcoin/bitcoin/issues 2021-07-14T13:21:24.092000Z TestFramework (ERROR): ``` This PR fixes this issue. Also see #20267. ACKs for top commit: achow101: ACK 0c845e3f8995eb8dc543a63899e5633a46091b4e Tree-SHA512: d7116a9ae30c7b7e3f55f55d2eea66f9e293c38d6757ed66d0477e4256ff5fedca5ddedafa0ef0c09f4dc1f7f973163e5a46090da26b067fdddbd9ea2ee76633
2021-07-15 07:36:40 +02:00
if self.is_bdb_compiled():
self.log.info('Test that the command is not available for legacy wallets.')
node.createwallet(wallet_name='w1', descriptors=False)
assert_raises_rpc_error(-4, 'listdescriptors is not available for non-descriptor wallets', node.listdescriptors)
self.log.info('Test the command for empty descriptors wallet.')
node.createwallet(wallet_name='w2', blank=True, descriptors=True)
Merge #21063: wallet, rpc: update listdescriptors response format 2e5f7def22e1b212fbd69e9147145d9d1f408aaf wallet, rpc: update listdescriptors response format (Ivan Metlushko) Pull request description: Update `listdescriptors` response format according to [RPC interface guidelines](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#rpc-interface-guidelines). This is a follow up for #20226 **Before:** ``` Result: [ (json array) Response is an array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] ``` **After:** ``` Result: { (json object) "wallet_name" : "str", (string) Name of wallet this operation was performed on "descriptors" : [ (json array) Array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] } ``` ACKs for top commit: achow101: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf meshcollider: utACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf jonatack: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf Tree-SHA512: 49bf73e46e2a61003ce594a4bfc506eb9592ccb799c2909c43a1a527490a4b4009f78dc09f3d47b4e945d3d7bb3cd2632cf48c5ace5feed5066158cc010dddc1
2021-04-02 06:31:17 +02:00
assert_equal(0, len(node.get_wallet_rpc('w2').listdescriptors()['descriptors']))
self.log.info('Test the command for a default descriptors wallet.')
node.createwallet(wallet_name='w3', descriptors=True)
result = node.get_wallet_rpc('w3').listdescriptors()
Merge #21063: wallet, rpc: update listdescriptors response format 2e5f7def22e1b212fbd69e9147145d9d1f408aaf wallet, rpc: update listdescriptors response format (Ivan Metlushko) Pull request description: Update `listdescriptors` response format according to [RPC interface guidelines](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#rpc-interface-guidelines). This is a follow up for #20226 **Before:** ``` Result: [ (json array) Response is an array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] ``` **After:** ``` Result: { (json object) "wallet_name" : "str", (string) Name of wallet this operation was performed on "descriptors" : [ (json array) Array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] } ``` ACKs for top commit: achow101: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf meshcollider: utACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf jonatack: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf Tree-SHA512: 49bf73e46e2a61003ce594a4bfc506eb9592ccb799c2909c43a1a527490a4b4009f78dc09f3d47b4e945d3d7bb3cd2632cf48c5ace5feed5066158cc010dddc1
2021-04-02 06:31:17 +02:00
assert_equal("w3", result['wallet_name'])
assert_equal(2, len(result['descriptors']))
assert_equal(2, len([d for d in result['descriptors'] if d['active']]))
assert_equal(1, len([d for d in result['descriptors'] if d['internal']]))
for item in result['descriptors']:
assert item['desc'] != ''
assert item['next'] == 0
assert item['range'] == [0, 0]
assert item['timestamp'] is not None
self.log.info('Test descriptors with hardened derivations are listed in importable form.')
xprv = 'tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg'
xpub_acc = 'tpubDCMVLhErorrAGfApiJSJzEKwqeaf2z3NrkVMxgYQjZLzMjXMBeRw2muGNYbvaekAE8rUFLftyEar4LdrG2wXyyTJQZ26zptmeTEjPTaATts'
hardened_path = '/84\'/1\'/0\''
wallet = node.get_wallet_rpc('w2')
wallet.importdescriptors([{
'desc': descsum_create('pkh(' + xprv + hardened_path + '/0/*)'),
'timestamp': 1296688602,
}])
Merge #21063: wallet, rpc: update listdescriptors response format 2e5f7def22e1b212fbd69e9147145d9d1f408aaf wallet, rpc: update listdescriptors response format (Ivan Metlushko) Pull request description: Update `listdescriptors` response format according to [RPC interface guidelines](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#rpc-interface-guidelines). This is a follow up for #20226 **Before:** ``` Result: [ (json array) Response is an array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] ``` **After:** ``` Result: { (json object) "wallet_name" : "str", (string) Name of wallet this operation was performed on "descriptors" : [ (json array) Array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] } ``` ACKs for top commit: achow101: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf meshcollider: utACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf jonatack: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf Tree-SHA512: 49bf73e46e2a61003ce594a4bfc506eb9592ccb799c2909c43a1a527490a4b4009f78dc09f3d47b4e945d3d7bb3cd2632cf48c5ace5feed5066158cc010dddc1
2021-04-02 06:31:17 +02:00
expected = {
'wallet_name': 'w2',
'descriptors': [
{'desc': descsum_create('pkh([80002067' + hardened_path + ']' + xpub_acc + '/0/*)'),
'timestamp': 1296688602,
'active': False,
'range': [0, 0],
'next': 0},
],
}
assert_equal(expected, wallet.listdescriptors())
assert_equal(expected, wallet.listdescriptors(False))
self.log.info('Test list private descriptors')
expected_private = {
'wallet_name': 'w2',
'descriptors': [
{'desc': descsum_create('pkh(' + xprv + hardened_path + '/0/*)'),
'timestamp': 1296688602,
'active': False,
'range': [0, 0],
'next': 0},
],
}
assert_equal(expected_private, wallet.listdescriptors(True))
Merge bitcoin/bitcoin#21329: descriptor wallet: Cache last hardened xpub and use in normalized descriptors e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 wallet, rpc: listdescriptors does not need unlocked (Andrew Chow) 3280704886b60644d103a5eb310691c003a39328 Pass in DescriptorCache to ToNormalizedString (Andrew Chow) 7a26ff10c2f2e139fbc63e2f37fb33ea4efae088 Change DescriptorImpl::ToStringHelper to use an enum (Andrew Chow) 75530c93a83f3e94bcb78b6aa463c5570c1e737e Remove priv option for ToNormalizedString (Andrew Chow) 74fede3b8ba69e2cc82c617cdf406ab79df58825 wallet: Upgrade existing descriptor caches (Andrew Chow) 432ba9e5434da90d2cf680f23e8c7b7164c9f945 wallet: Store last hardened xpub cache (Andrew Chow) d87b544b834077f102724415e0fada6ee8b2def2 descriptors: Cache last hardened xpub (Andrew Chow) cacc3910989c4f3d7afa530dbab042461426abce Move DescriptorCache writing to WalletBatch (Andrew Chow) 0b4c8ef75cd03c8f0a8cfadb47e0fbcabe3c5e59 Refactor Cache merging and writing (Andrew Chow) 976b53b085d681645fd3a008fe382de85647e29f Revert "Cache parent xpub inside of BIP32PubkeyProvider" (Andrew Chow) Pull request description: Currently fetching a normalized descriptor requires the wallet to be unlocked as it needs the private keys to derive the last hardened xpub. This is not very user friendly as normalized descriptors shouldn't require and don't involve the private keys except for derivation. We solve this problem by caching the last hardened xpub (which has to be derived at some point when generating the address pool). However the last hardened xpub was not already being cached. We only cached the immediate parent xpub and derived child keys. For example, with a descriptor derivation path of `/84'/0'/0'/0/*`, the parent xpub that is cached is `m/84'/0'/0'/0`, and the child keys of `m/84'/0'/0'/0/i` (note that child keys would not be cached in this case). This parent xpub is not suitable for the normalized descriptor form as we want the key at `m/84'/0'/0'`. So this PR adds another field to `DescriptorCache` to cache the last hardened xpub so that we can use them for normalized descriptors. Since `DescriptorCache` is changing, existing descriptor wallets need to be upgraded to use this new cache. The upgrade will occur in the background either at loading time (if the wallet is not encrypted) or at unlocking time in the same manner that `UpgradeKeyMetadata` operates. It will use a new wallet flag `WALLET_FLAG_LAST_HARDENED_XPUB_CACHED` to indicate whether the descriptor wallet has the last hardened xpub cache. Lastly `listdescriptors` will not require the wallet to be locked and `getaddressinfo`'s `parent_desc` will always be output (assuming the upgrade has occurred). ACKs for top commit: fjahr: tACK e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 S3RK: reACK e6cf0ed jonatack: Semi ACK e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 reviewed, debug-built and ran unit tests and some of the descriptor functional tests at each commit. I'm not very familiar with this code and it could be clearer to the uninitiated IMHO, so I'm not confident enough to give a full ACK. Various minor suggestions follow, most of them for readability, feel free to pick and choose. meshcollider: Code review + functional test run ACK e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 Tree-SHA512: ac27aade8644525cd65bfcaf27ff32afb974085b1451faf4ff68c6671a690bd6a41d4f39a33cbf461ae0fbe85995c0a4c08dbd36171da1c1d2a1d00053ad298d
2021-06-30 22:39:03 +02:00
self.log.info("Test listdescriptors with encrypted wallet")
wallet.encryptwallet("pass")
assert_equal(expected, wallet.listdescriptors())
self.log.info('Test list private descriptors with encrypted wallet')
assert_raises_rpc_error(-13, 'Please enter the wallet passphrase with walletpassphrase first.', wallet.listdescriptors, True)
wallet.walletpassphrase(passphrase="pass", timeout=1000000)
assert_equal(expected_private, wallet.listdescriptors(True))
self.log.info('Test list private descriptors with watch-only wallet')
node.createwallet(wallet_name='watch-only', descriptors=True, disable_private_keys=True)
watch_only_wallet = node.get_wallet_rpc('watch-only')
watch_only_wallet.importdescriptors([{
'desc': descsum_create('pkh(' + xpub_acc + ')'),
'timestamp': 1296688602,
}])
assert_raises_rpc_error(-4, 'Can\'t get descriptor string', watch_only_wallet.listdescriptors, True)
self.log.info('Test non-active non-range combo descriptor')
node.createwallet(wallet_name='w4', blank=True, descriptors=True)
wallet = node.get_wallet_rpc('w4')
wallet.importdescriptors([{
'desc': descsum_create('combo(' + node.get_deterministic_priv_key().key + ')'),
'timestamp': 1296688602,
}])
Merge #21063: wallet, rpc: update listdescriptors response format 2e5f7def22e1b212fbd69e9147145d9d1f408aaf wallet, rpc: update listdescriptors response format (Ivan Metlushko) Pull request description: Update `listdescriptors` response format according to [RPC interface guidelines](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#rpc-interface-guidelines). This is a follow up for #20226 **Before:** ``` Result: [ (json array) Response is an array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] ``` **After:** ``` Result: { (json object) "wallet_name" : "str", (string) Name of wallet this operation was performed on "descriptors" : [ (json array) Array of descriptor objects { (json object) "desc" : "str", (string) Descriptor string representation "timestamp" : n, (numeric) The creation time of the descriptor "active" : true|false, (boolean) Activeness flag "internal" : true|false, (boolean, optional) Whether this is internal or external descriptor; defined only for active descriptors "range" : [ (json array, optional) Defined only for ranged descriptors n, (numeric) Range start inclusive n (numeric) Range end inclusive ], "next" : n (numeric, optional) The next index to generate addresses from; defined only for ranged descriptors }, ... ] } ``` ACKs for top commit: achow101: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf meshcollider: utACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf jonatack: re-ACK 2e5f7def22e1b212fbd69e9147145d9d1f408aaf Tree-SHA512: 49bf73e46e2a61003ce594a4bfc506eb9592ccb799c2909c43a1a527490a4b4009f78dc09f3d47b4e945d3d7bb3cd2632cf48c5ace5feed5066158cc010dddc1
2021-04-02 06:31:17 +02:00
expected = {
'wallet_name': 'w4',
'descriptors': [
{'active': False,
'desc': 'combo(038af19f35924e37ad7c3c8045d1e19b9b90b7310e08b892e620c253a102fe49f0)#2j2j0825',
'timestamp': 1296688602},
]
}
assert_equal(expected, wallet.listdescriptors())
if __name__ == '__main__':
ListDescriptorsTest().main()