Skip to content

Commit 5f19155

Browse files
author
MarcoFalke
committed
Merge #18724: test: add coverage for -rpcwallet cli option
2495110 test: add coverage for -rpcwallet cli option (Jon Atack) Pull request description: The bitcoin-cli `-rpcwallet=` option is an essential RPC/CLI option when more than one wallet is loaded (see `bitcoin-cli -help | grep -A5 rpcwallet` or `src/bitcoin-cli.cpp::L61`) and it currently has no test coverage. It is not only used by users, but also by the test framework and ~10 test files via `get_wallet_rpc()`. This PR adds coverage, while simultaneously improving the `-getinfo` coverage when multiple wallets are loaded. This is similar to the test coverage that would be added in #18594. ACKs for top commit: robot-visions: ACK 2495110 Tree-SHA512: caaa8b99fb8fa481ab2c6b2a287ed29720bb4553c3f66657462c44fa2990acaaf36cabeaaf81408678e5fdce4e105d729dd94b5ed8588dd1a6f2cb03fc25acf3
2 parents a215c61 + 2495110 commit 5f19155

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

test/functional/interface_bitcoin_cli.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test bitcoin-cli"""
6+
from decimal import Decimal
67
from test_framework.test_framework import BitcoinTestFramework
78
from test_framework.util import assert_equal, assert_raises_process_error, get_auth_cookie
89

@@ -72,8 +73,48 @@ def run_test(self):
7273
assert_equal(cli_get_info['paytxfee'], wallet_info['paytxfee'])
7374
assert_equal(cli_get_info['relayfee'], network_info['relayfee'])
7475
assert_equal(self.nodes[0].cli.getwalletinfo(), wallet_info)
76+
77+
# Setup to test -getinfo and -rpcwallet= with multiple wallets.
78+
wallets = ['', 'Encrypted', 'secret']
79+
amounts = [Decimal('59.999928'), Decimal(9), Decimal(31)]
80+
self.nodes[0].createwallet(wallet_name=wallets[1])
81+
self.nodes[0].createwallet(wallet_name=wallets[2])
82+
w1 = self.nodes[0].get_wallet_rpc(wallets[0])
83+
w2 = self.nodes[0].get_wallet_rpc(wallets[1])
84+
w3 = self.nodes[0].get_wallet_rpc(wallets[2])
85+
w1.walletpassphrase(password, self.rpc_timeout)
86+
w1.sendtoaddress(w2.getnewaddress(), amounts[1])
87+
w1.sendtoaddress(w3.getnewaddress(), amounts[2])
88+
89+
# Mine a block to confirm; adds a block reward (50 BTC) to the default wallet.
90+
self.nodes[0].generate(1)
91+
92+
self.log.info("Test -getinfo with multiple wallets loaded returns no balance")
93+
assert_equal(set(self.nodes[0].listwallets()), set(wallets))
94+
assert 'balance' not in self.nodes[0].cli('-getinfo').send_cli().keys()
95+
96+
self.log.info("Test -getinfo with multiple wallets and -rpcwallet returns specified wallet balance")
97+
for i in range(len(wallets)):
98+
cli_get_info = self.nodes[0].cli('-getinfo').send_cli('-rpcwallet={}'.format(wallets[i]))
99+
assert_equal(cli_get_info['balance'], amounts[i])
100+
101+
self.log.info("Test -getinfo with multiple wallets and -rpcwallet=non-existing-wallet returns no balance")
102+
assert 'balance' not in self.nodes[0].cli('-getinfo').send_cli('-rpcwallet=does-not-exist').keys()
103+
104+
self.log.info("Test -getinfo after unloading all wallets except a non-default one returns its balance")
105+
self.nodes[0].unloadwallet(wallets[0])
106+
self.nodes[0].unloadwallet(wallets[2])
107+
assert_equal(self.nodes[0].listwallets(), [wallets[1]])
108+
assert_equal(self.nodes[0].cli('-getinfo').send_cli()['balance'], amounts[1])
109+
110+
self.log.info("Test -getinfo -rpcwallet=remaining-non-default-wallet returns its balance")
111+
assert_equal(self.nodes[0].cli('-getinfo').send_cli('-rpcwallet={}'.format(wallets[1]))['balance'], amounts[1])
112+
113+
self.log.info("Test -getinfo with -rpcwallet=unloaded wallet returns no balance")
114+
assert 'balance' not in self.nodes[0].cli('-getinfo').send_cli('-rpcwallet={}'.format(wallets[2])).keys()
75115
else:
76116
self.log.info("*** Wallet not compiled; cli getwalletinfo and -getinfo wallet tests skipped")
117+
self.nodes[0].generate(1) # maintain block parity with the wallet_compiled conditional branch
77118

78119
self.log.info("Test -version with node stopped")
79120
self.stop_node(0)
@@ -85,7 +126,7 @@ def run_test(self):
85126
self.nodes[0].wait_for_cookie_credentials() # ensure cookie file is available to avoid race condition
86127
blocks = self.nodes[0].cli('-rpcwait').send_cli('getblockcount')
87128
self.nodes[0].wait_for_rpc_connection()
88-
assert_equal(blocks, BLOCKS)
129+
assert_equal(blocks, BLOCKS + 1)
89130

90131

91132
if __name__ == '__main__':

0 commit comments

Comments
 (0)