interface_bitcoin_cli.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2017-present The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test bitcoin-cli"""
6
7 from decimal import Decimal
8 import re
9 import subprocess
10
11 from test_framework.blocktools import COINBASE_MATURITY
12 from test_framework.netutil import test_ipv6_local
13 from test_framework.test_framework import BitcoinTestFramework
14 from test_framework.util import (
15 assert_equal,
16 assert_greater_than_or_equal,
17 assert_raises_process_error,
18 assert_raises_rpc_error,
19 get_auth_cookie,
20 rpc_port,
21 )
22 import time
23
24 # The block reward of coinbaseoutput.nValue (50) BTC/block matures after
25 # COINBASE_MATURITY (100) blocks. Therefore, after mining 101 blocks we expect
26 # node 0 to have a balance of (BLOCKS - COINBASE_MATURITY) * 50 BTC/block.
27 BLOCKS = COINBASE_MATURITY + 1
28 BALANCE = (BLOCKS - 100) * 50
29
30 JSON_PARSING_ERROR = 'error: Error parsing JSON: foo'
31 BLOCKS_VALUE_OF_ZERO = 'error: the first argument (number of blocks to generate, default: 1) must be an integer value greater than zero'
32 TOO_MANY_ARGS = 'error: too many arguments (maximum 2 for nblocks and maxtries)'
33 WALLET_NOT_LOADED = 'Requested wallet does not exist or is not loaded'
34 WALLET_NOT_SPECIFIED = (
35 "Multiple wallets are loaded. Please select which wallet to use by requesting the RPC "
36 "through the /wallet/<walletname> URI path. Or for the CLI, specify the \"-rpcwallet=<walletname>\" "
37 "option before the command (run \"bitcoin-cli -h\" for help or \"bitcoin-cli listwallets\" to see "
38 "which wallets are currently loaded)."
39 )
40
41
42 def cli_get_info_string_to_dict(cli_get_info_string):
43 """Helper method to convert human-readable -getinfo into a dictionary"""
44 cli_get_info = {}
45 lines = cli_get_info_string.splitlines()
46 line_idx = 0
47 ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
48 while line_idx < len(lines):
49 # Remove ansi colour code
50 line = ansi_escape.sub('', lines[line_idx])
51 if "Balances" in line:
52 # When "Balances" appears in a line, all of the following lines contain "balance: wallet" until an empty line
53 cli_get_info["Balances"] = {}
54 while line_idx < len(lines) and not (lines[line_idx + 1] == ''):
55 line_idx += 1
56 balance, wallet = lines[line_idx].strip().split(" ")
57 # Remove right justification padding
58 wallet = wallet.strip()
59 if wallet == '""':
60 # Set default wallet("") to empty string
61 wallet = ''
62 cli_get_info["Balances"][wallet] = balance.strip()
63 elif ": " in line:
64 key, value = line.split(": ")
65 if key == 'Wallet' and value == '""':
66 # Set default wallet("") to empty string
67 value = ''
68 if key == "Proxies" and value == "n/a":
69 # Set N/A to empty string to represent no proxy
70 value = ''
71 cli_get_info[key.strip()] = value.strip()
72 line_idx += 1
73 return cli_get_info
74
75
76 class TestBitcoinCli(BitcoinTestFramework):
77 def set_test_params(self):
78 self.setup_clean_chain = True
79 self.num_nodes = 1
80 self.uses_wallet = None
81
82 def skip_test_if_missing_module(self):
83 self.skip_if_no_cli()
84
85 def test_netinfo(self):
86 """Test -netinfo output format."""
87 self.log.info("Test -netinfo header and separate local services line")
88 out = self.nodes[0].cli('-netinfo').send_cli().splitlines()
89 assert out[0].startswith(f"{self.config['environment']['CLIENT_NAME']} client ")
90 assert any(re.match(r"^Local services:.+network", line) for line in out)
91
92 self.log.info("Test -netinfo local services are moved to header if details are requested")
93 det = self.nodes[0].cli('-netinfo', '1').send_cli().splitlines()
94 self.log.debug(f"Test -netinfo 1 header output: {det[0]}")
95 assert re.match(rf"^{re.escape(self.config['environment']['CLIENT_NAME'])} client.+services nwl2?$", det[0])
96 assert not any(line.startswith("Local services:") for line in det)
97
98 def test_echojson_positional_equals(self):
99 """Test JSON parameter parsing containing '=' with -named echojson"""
100 self.log.info("Test JSON parameter parsing containing '=' is handled correctly with -named")
101
102 # This should be treated as a positional JSON argument, not as a named
103 result = self.nodes[0].cli("-named", "echojson", '["key=value"]').send_cli()
104 assert_equal(result, [["key=value"]])
105
106 result = self.nodes[0].cli("-named", "echojson", '["key=value", "another=test"]').send_cli()
107 assert_equal(result, [["key=value", "another=test"]])
108
109 result = self.nodes[0].cli("-named", "echojson", '["data=test"]', "42").send_cli()
110 expected = [["data=test"], 42]
111 assert_equal(result, expected)
112
113 # This should be treated as a named parameter, as arg0 and arg1 are valid parameter names
114 result = self.nodes[0].cli("-named", "echojson", 'arg0=["data=test"]', 'arg1=42').send_cli()
115 expected = [["data=test"], 42]
116 assert_equal(result, expected)
117
118 def run_test(self):
119 """Main test logic"""
120 self.test_echojson_positional_equals()
121
122 self.generate(self.nodes[0], BLOCKS)
123
124 self.log.info("Compare responses from getblockchaininfo RPC and `bitcoin-cli getblockchaininfo`")
125 cli_response = self.nodes[0].cli.getblockchaininfo()
126 rpc_response = self.nodes[0].getblockchaininfo()
127 assert_equal(cli_response, rpc_response)
128
129 self.log.info("Test named arguments")
130 assert_equal(self.nodes[0].cli.echo(0, 1, arg3=3, arg5=5), ['0', '1', None, '3', None, '5'])
131 assert_raises_rpc_error(-8, "Parameter arg1 specified twice both as positional and named argument", self.nodes[0].cli.echo, 0, 1, arg1=1)
132 assert_raises_rpc_error(-8, "Parameter arg1 specified twice both as positional and named argument", self.nodes[0].cli.echo, 0, None, 2, arg1=1)
133
134 self.log.info("Test that later cli named arguments values silently overwrite earlier ones")
135 assert_equal(self.nodes[0].cli("-named", "echo", "arg0=0", "arg1=1", "arg2=2", "arg1=3").send_cli(), ['0', '3', '2'])
136 assert_raises_rpc_error(-8, "Parameter args specified multiple times", self.nodes[0].cli("-named", "echo", "args=[0,1,2,3]", "4", "5", "6", ).send_cli)
137
138 user, password = get_auth_cookie(self.nodes[0].datadir_path, self.chain)
139
140 self.log.info("Test -stdinrpcpass option")
141 assert_equal(BLOCKS, self.nodes[0].cli(f'-rpcuser={user}', '-stdinrpcpass', input=password).getblockcount())
142 assert_raises_process_error(1, 'Incorrect rpcuser or rpcpassword were specified', self.nodes[0].cli(f'-rpcuser={user}', '-stdinrpcpass', input='foo').echo)
143
144 self.log.info("Test -stdin and -stdinrpcpass")
145 assert_equal(['foo', 'bar'], self.nodes[0].cli(f'-rpcuser={user}', '-stdin', '-stdinrpcpass', input=f'{password}\nfoo\nbar').echo())
146 assert_raises_process_error(1, 'Incorrect rpcuser or rpcpassword were specified', self.nodes[0].cli(f'-rpcuser={user}', '-stdin', '-stdinrpcpass', input='foo').echo)
147
148 self.log.info("Test connecting to a non-existing server")
149 assert_raises_process_error(1, "Could not connect to the server", self.nodes[0].cli('-rpcport=1').echo)
150
151 self.log.info("Test handling of invalid ports in rpcconnect")
152 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: 127.0.0.1:notaport", self.nodes[0].cli("-rpcconnect=127.0.0.1:notaport").echo)
153 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: 127.0.0.1:-1", self.nodes[0].cli("-rpcconnect=127.0.0.1:-1").echo)
154 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: 127.0.0.1:0", self.nodes[0].cli("-rpcconnect=127.0.0.1:0").echo)
155 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: 127.0.0.1:65536", self.nodes[0].cli("-rpcconnect=127.0.0.1:65536").echo)
156
157 self.log.info("Checking for IPv6")
158 have_ipv6 = test_ipv6_local()
159 if not have_ipv6:
160 self.log.info("Skipping IPv6 tests")
161
162 if have_ipv6:
163 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: [::1]:notaport", self.nodes[0].cli("-rpcconnect=[::1]:notaport").echo)
164 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: [::1]:-1", self.nodes[0].cli("-rpcconnect=[::1]:-1").echo)
165 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: [::1]:0", self.nodes[0].cli("-rpcconnect=[::1]:0").echo)
166 assert_raises_process_error(1, "Invalid port provided in -rpcconnect: [::1]:65536", self.nodes[0].cli("-rpcconnect=[::1]:65536").echo)
167
168 self.log.info("Test handling of invalid ports in rpcport")
169 assert_raises_process_error(1, "Invalid port provided in -rpcport: notaport", self.nodes[0].cli("-rpcport=notaport").echo)
170 assert_raises_process_error(1, "Invalid port provided in -rpcport: -1", self.nodes[0].cli("-rpcport=-1").echo)
171 assert_raises_process_error(1, "Invalid port provided in -rpcport: 0", self.nodes[0].cli("-rpcport=0").echo)
172 assert_raises_process_error(1, "Invalid port provided in -rpcport: 65536", self.nodes[0].cli("-rpcport=65536").echo)
173
174 self.log.info("Test port usage preferences")
175 node_rpc_port = rpc_port(self.nodes[0].index)
176 # Prevent bitcoin-cli from using existing rpcport in conf
177 conf_rpcport = "rpcport=" + str(node_rpc_port)
178 self.nodes[0].replace_in_config([(conf_rpcport, "#" + conf_rpcport)])
179 # prefer rpcport over rpcconnect
180 assert_raises_process_error(1, "Error while attempting to communicate with server 127.0.0.1:1 (Could not connect to the server)", self.nodes[0].cli(f"-rpcconnect=127.0.0.1:{node_rpc_port}", "-rpcport=1").echo)
181 if have_ipv6:
182 assert_raises_process_error(1, "Error while attempting to communicate with server ::1:1 (Could not connect to the server)", self.nodes[0].cli(f"-rpcconnect=[::1]:{node_rpc_port}", "-rpcport=1").echo)
183
184 assert_equal(BLOCKS, self.nodes[0].cli("-rpcconnect=127.0.0.1:18999", f'-rpcport={node_rpc_port}').getblockcount())
185 if have_ipv6:
186 assert_equal(BLOCKS, self.nodes[0].cli("-rpcconnect=[::1]:18999", f'-rpcport={node_rpc_port}').getblockcount())
187
188 # prefer rpcconnect port over default
189 assert_equal(BLOCKS, self.nodes[0].cli(f"-rpcconnect=127.0.0.1:{node_rpc_port}").getblockcount())
190 if have_ipv6:
191 assert_equal(BLOCKS, self.nodes[0].cli(f"-rpcconnect=[::1]:{node_rpc_port}").getblockcount())
192
193 # prefer rpcport over default
194 assert_equal(BLOCKS, self.nodes[0].cli(f'-rpcport={node_rpc_port}').getblockcount())
195 # Re-enable rpcport in conf if present
196 self.nodes[0].replace_in_config([("#" + conf_rpcport, conf_rpcport)])
197
198 self.log.info("Test connecting with non-existing RPC cookie file")
199 assert_raises_process_error(1, "Failed to read cookie file and no rpcpassword was specified.", self.nodes[0].cli('-rpccookiefile=does-not-exist', '-rpcpassword=').echo)
200
201 self.log.info("Test connecting with neither cookie file, nor password")
202 assert_raises_process_error(1, "Cookie file was disabled via -norpccookiefile and no rpcpassword was specified.", self.nodes[0].cli("-norpccookiefile").echo)
203 assert_raises_process_error(1, "Cookie file was disabled via -norpccookiefile and no rpcpassword was specified.", self.nodes[0].cli("-norpccookiefile", "-rpcpassword=").echo)
204
205 self.log.info("Test connecting with invalid cookie file")
206 assert_raises_process_error(1, "Cookie file credentials were invalid and no rpcpassword was specified.", self.nodes[0].cli(f"-rpccookiefile={self.nodes[0].datadir_path / 'bitcoin.conf'}").echo)
207
208 self.log.info("Test connecting without RPC cookie file and with password arg")
209 assert_equal(BLOCKS, self.nodes[0].cli('-norpccookiefile', f'-rpcuser={user}', f'-rpcpassword={password}').getblockcount())
210
211 self.log.info("Test -getinfo with arguments fails")
212 assert_raises_process_error(1, "-getinfo takes no arguments", self.nodes[0].cli('-getinfo').help)
213
214 self.log.info("Test -getinfo with -color=never does not return ANSI escape codes")
215 assert "\u001b[0m" not in self.nodes[0].cli('-getinfo', '-color=never').send_cli()
216
217 self.log.info("Test -getinfo with -color=always returns ANSI escape codes")
218 assert "\u001b[0m" in self.nodes[0].cli('-getinfo', '-color=always').send_cli()
219
220 self.log.info("Test -getinfo with invalid value for -color option")
221 assert_raises_process_error(1, "Invalid value for -color option. Valid values: always, auto, never.", self.nodes[0].cli('-getinfo', '-color=foo').send_cli)
222
223 self.log.info("Test -getinfo returns expected network and blockchain info")
224 if self.is_wallet_compiled():
225 self.import_deterministic_coinbase_privkeys()
226 self.nodes[0].encryptwallet(password)
227 cli_get_info_string = self.nodes[0].cli('-getinfo').send_cli()
228 cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)
229
230 network_info = self.nodes[0].getnetworkinfo()
231 blockchain_info = self.nodes[0].getblockchaininfo()
232 assert_equal(int(cli_get_info['Version']), network_info['version'])
233 assert_equal(cli_get_info['Verification progress'], "%.4f%%" % (blockchain_info['verificationprogress'] * 100))
234 assert_equal(int(cli_get_info['Blocks']), blockchain_info['blocks'])
235 assert_equal(int(cli_get_info['Headers']), blockchain_info['headers'])
236 assert_equal(int(cli_get_info['Time offset (s)']), network_info['timeoffset'])
237 expected_network_info = f"in {network_info['connections_in']}, out {network_info['connections_out']}, total {network_info['connections']}"
238 assert_equal(cli_get_info["Network"], expected_network_info)
239 assert_equal(cli_get_info['Proxies'], network_info['networks'][0]['proxy'])
240 assert_equal(Decimal(cli_get_info['Difficulty']), blockchain_info['difficulty'])
241 assert_equal(cli_get_info['Chain'], blockchain_info['chain'])
242
243 self.log.info("Test -getinfo and bitcoin-cli return all proxies")
244 self.restart_node(0, extra_args=["-proxy=127.0.0.1:9050", "-i2psam=127.0.0.1:7656"])
245 network_info = self.nodes[0].getnetworkinfo()
246 cli_get_info_string = self.nodes[0].cli('-getinfo').send_cli()
247 cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)
248 assert_equal(cli_get_info["Proxies"], "127.0.0.1:9050 (ipv4, ipv6, onion, cjdns), 127.0.0.1:7656 (i2p)")
249
250 if self.is_wallet_compiled():
251 self.log.info("Test -getinfo and bitcoin-cli getwalletinfo return expected wallet info")
252 # Explicitly set the output type in order to have consistent tx vsize / fees
253 # for both legacy and descriptor wallets (disables the change address type detection algorithm)
254 self.restart_node(0, extra_args=["-addresstype=bech32", "-changetype=bech32"])
255 assert_equal(Decimal(cli_get_info['Balance']), BALANCE)
256 assert 'Balances' not in cli_get_info_string
257 wallet_info = self.nodes[0].getwalletinfo()
258 assert_equal(int(cli_get_info['Keypool size']), wallet_info['keypoolsize'])
259 assert_equal(int(cli_get_info['Unlocked until']), wallet_info['unlocked_until'])
260 assert_equal(Decimal(cli_get_info['Min tx relay fee rate (BTC/kvB)']), network_info['relayfee'])
261 assert_equal(self.nodes[0].cli.getwalletinfo(), wallet_info)
262
263 # Setup to test -getinfo, -generate, and -rpcwallet= with multiple wallets.
264 wallets = [self.default_wallet_name, 'Encrypted', 'secret']
265 amounts = [BALANCE + Decimal('9.999928'), Decimal(9), Decimal(31)]
266 self.nodes[0].createwallet(wallet_name=wallets[1])
267 self.nodes[0].createwallet(wallet_name=wallets[2])
268 w1 = self.nodes[0].get_wallet_rpc(wallets[0])
269 w2 = self.nodes[0].get_wallet_rpc(wallets[1])
270 w3 = self.nodes[0].get_wallet_rpc(wallets[2])
271 rpcwallet2 = f'-rpcwallet={wallets[1]}'
272 rpcwallet3 = f'-rpcwallet={wallets[2]}'
273 w1.walletpassphrase(password, self.rpc_timeout)
274 w2.encryptwallet(password)
275 w1.sendtoaddress(w2.getnewaddress(), amounts[1])
276 w1.sendtoaddress(w3.getnewaddress(), amounts[2])
277
278 # Mine a block to confirm; adds a block reward (50 BTC) to the default wallet.
279 self.generate(self.nodes[0], 1)
280
281 self.log.info("Test -getinfo with multiple wallets and -rpcwallet returns specified wallet balance")
282 for i in range(len(wallets)):
283 cli_get_info_string = self.nodes[0].cli('-getinfo', f'-rpcwallet={wallets[i]}').send_cli()
284 cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)
285 assert 'Balances' not in cli_get_info_string
286 assert_equal(cli_get_info["Wallet"], wallets[i])
287 assert_equal(Decimal(cli_get_info['Balance']), amounts[i])
288
289 self.log.info("Test -getinfo with multiple wallets and -rpcwallet=non-existing-wallet returns no balances")
290 cli_get_info_string = self.nodes[0].cli('-getinfo', '-rpcwallet=does-not-exist').send_cli()
291 assert 'Balance' not in cli_get_info_string
292 assert 'Balances' not in cli_get_info_string
293
294 self.log.info("Test -getinfo with multiple wallets returns all loaded wallet names and balances")
295 assert_equal(set(self.nodes[0].listwallets()), set(wallets))
296 cli_get_info_string = self.nodes[0].cli('-getinfo').send_cli()
297 cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)
298 assert 'Balance' not in cli_get_info
299 for k, v in zip(wallets, amounts):
300 assert_equal(Decimal(cli_get_info['Balances'][k]), v)
301
302 # Unload the default wallet and re-verify.
303 self.nodes[0].unloadwallet(wallets[0])
304 assert wallets[0] not in self.nodes[0].listwallets()
305 cli_get_info_string = self.nodes[0].cli('-getinfo').send_cli()
306 cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)
307 assert 'Balance' not in cli_get_info
308 assert 'Balances' in cli_get_info_string
309 for k, v in zip(wallets[1:], amounts[1:]):
310 assert_equal(Decimal(cli_get_info['Balances'][k]), v)
311 assert wallets[0] not in cli_get_info
312
313 self.log.info("Test -getinfo after unloading all wallets except a non-default one returns its balance")
314 self.nodes[0].unloadwallet(wallets[2])
315 assert_equal(self.nodes[0].listwallets(), [wallets[1]])
316 cli_get_info_string = self.nodes[0].cli('-getinfo').send_cli()
317 cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)
318 assert 'Balances' not in cli_get_info_string
319 assert_equal(cli_get_info['Wallet'], wallets[1])
320 assert_equal(Decimal(cli_get_info['Balance']), amounts[1])
321
322 self.log.info("Test -getinfo -norpcwallet returns the same as -getinfo")
323 # Previously there was a bug where -norpcwallet was treated like -rpcwallet=0
324 assert_equal(self.nodes[0].cli('-getinfo', "-norpcwallet").send_cli(), cli_get_info_string)
325
326 self.log.info("Test -getinfo with -rpcwallet=remaining-non-default-wallet returns only its balance")
327 cli_get_info_string = self.nodes[0].cli('-getinfo', rpcwallet2).send_cli()
328 cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)
329 assert 'Balances' not in cli_get_info_string
330 assert_equal(cli_get_info['Wallet'], wallets[1])
331 assert_equal(Decimal(cli_get_info['Balance']), amounts[1])
332
333 self.log.info("Test -getinfo with -rpcwallet=unloaded wallet returns no balances")
334 cli_get_info_string = self.nodes[0].cli('-getinfo', rpcwallet3).send_cli()
335 cli_get_info_keys = cli_get_info_string_to_dict(cli_get_info_string)
336 assert 'Balance' not in cli_get_info_keys
337 assert 'Balances' not in cli_get_info_string
338
339 # Test bitcoin-cli -generate.
340 n1 = 3
341 n2 = 4
342 w2.walletpassphrase(password, self.rpc_timeout)
343 blocks = self.nodes[0].getblockcount()
344
345 self.log.info('Test -generate with no args')
346 generate = self.nodes[0].cli('-generate').send_cli()
347 assert_equal(set(generate.keys()), {'address', 'blocks'})
348 assert_equal(len(generate["blocks"]), 1)
349 assert_equal(self.nodes[0].getblockcount(), blocks + 1)
350
351 self.log.info('Test -generate with bad args')
352 assert_raises_process_error(1, JSON_PARSING_ERROR, self.nodes[0].cli('-generate', 'foo').echo)
353 assert_raises_process_error(1, BLOCKS_VALUE_OF_ZERO, self.nodes[0].cli('-generate', 0).echo)
354 assert_raises_process_error(1, TOO_MANY_ARGS, self.nodes[0].cli('-generate', 1, 2, 3).echo)
355
356 self.log.info('Test -generate with nblocks')
357 generate = self.nodes[0].cli('-generate', n1).send_cli()
358 assert_equal(set(generate.keys()), {'address', 'blocks'})
359 assert_equal(len(generate["blocks"]), n1)
360 assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n1)
361
362 self.log.info('Test -generate with nblocks and maxtries')
363 generate = self.nodes[0].cli('-generate', n2, 1000000).send_cli()
364 assert_equal(set(generate.keys()), {'address', 'blocks'})
365 assert_equal(len(generate["blocks"]), n2)
366 assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n1 + n2)
367
368 self.log.info('Test -generate -rpcwallet in single-wallet mode')
369 generate = self.nodes[0].cli(rpcwallet2, '-generate').send_cli()
370 assert_equal(set(generate.keys()), {'address', 'blocks'})
371 assert_equal(len(generate["blocks"]), 1)
372 assert_equal(self.nodes[0].getblockcount(), blocks + 2 + n1 + n2)
373
374 self.log.info('Test -generate -rpcwallet=unloaded wallet raises RPC error')
375 assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate').echo)
376 assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate', 'foo').echo)
377 assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate', 0).echo)
378 assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate', 1, 2, 3).echo)
379
380 # Test bitcoin-cli -generate with -rpcwallet in multiwallet mode.
381 self.nodes[0].loadwallet(wallets[2])
382 n3 = 4
383 n4 = 10
384 blocks = self.nodes[0].getblockcount()
385
386 self.log.info('Test -generate -rpcwallet=<filename> raise RPC error')
387 wallet2_path = f'-rpcwallet={self.nodes[0].wallets_path / wallets[2] / self.wallet_data_filename}'
388 assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(wallet2_path, '-generate').echo)
389
390 self.log.info('Test -generate -rpcwallet with no args')
391 generate = self.nodes[0].cli(rpcwallet2, '-generate').send_cli()
392 assert_equal(set(generate.keys()), {'address', 'blocks'})
393 assert_equal(len(generate["blocks"]), 1)
394 assert_equal(self.nodes[0].getblockcount(), blocks + 1)
395
396 self.log.info('Test -generate -rpcwallet with bad args')
397 assert_raises_process_error(1, JSON_PARSING_ERROR, self.nodes[0].cli(rpcwallet2, '-generate', 'foo').echo)
398 assert_raises_process_error(1, BLOCKS_VALUE_OF_ZERO, self.nodes[0].cli(rpcwallet2, '-generate', 0).echo)
399 assert_raises_process_error(1, TOO_MANY_ARGS, self.nodes[0].cli(rpcwallet2, '-generate', 1, 2, 3).echo)
400
401 self.log.info('Test -generate -rpcwallet with nblocks')
402 generate = self.nodes[0].cli(rpcwallet2, '-generate', n3).send_cli()
403 assert_equal(set(generate.keys()), {'address', 'blocks'})
404 assert_equal(len(generate["blocks"]), n3)
405 assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n3)
406
407 self.log.info('Test -generate -rpcwallet with nblocks and maxtries')
408 generate = self.nodes[0].cli(rpcwallet2, '-generate', n4, 1000000).send_cli()
409 assert_equal(set(generate.keys()), {'address', 'blocks'})
410 assert_equal(len(generate["blocks"]), n4)
411 assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n3 + n4)
412
413 self.log.info('Test -generate without -rpcwallet in multiwallet mode raises RPC error')
414 assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate').echo)
415 assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate', 'foo').echo)
416 assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate', 0).echo)
417 assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate', 1, 2, 3).echo)
418 else:
419 self.log.info("*** Wallet not compiled; cli getwalletinfo and -getinfo wallet tests skipped")
420 self.generate(self.nodes[0], 25) # maintain block parity with the wallet_compiled conditional branch
421
422 self.test_netinfo()
423
424 self.log.info("Test -rpcid option sets custom JSON-RPC request ID")
425 with self.nodes[0].assert_debug_log(expected_msgs=['id=myrpcid']):
426 self.nodes[0].cli('-rpcid=myrpcid').getblockcount()
427
428 self.log.info("Test default request logs default id=1")
429 with self.nodes[0].assert_debug_log(expected_msgs=["ThreadRPCServer method=getblockcount", "id=1"]):
430 self.nodes[0].cli.getblockcount()
431
432 self.log.info("Test that request ids with unsafe characters are sanitized in the log")
433 with self.nodes[0].assert_debug_log(expected_msgs=["ThreadRPCServer method=getblockcount", "id=abcdef"]):
434 self.nodes[0].cli('-rpcid=abc<\n>def').getblockcount()
435
436 self.log.info("Test -version with node stopped")
437 self.stop_node(0)
438 cli_response = self.nodes[0].cli('-version').send_cli()
439 assert f"{self.config['environment']['CLIENT_NAME']} RPC client version" in cli_response
440
441 self.log.info("Test -rpcwait option successfully waits for RPC connection")
442 self.nodes[0].start() # start node without RPC connection
443 self.nodes[0].wait_for_cookie_credentials() # ensure cookie file is available to avoid race condition
444 blocks = self.nodes[0].cli('-rpcwait').send_cli('getblockcount')
445 self.nodes[0].wait_for_rpc_connection()
446 assert_equal(blocks, BLOCKS + 25)
447
448 self.log.info("Test -rpcwait option waits at most -rpcwaittimeout seconds for startup")
449 self.stop_node(0) # stop the node so we time out
450 start_time = time.time()
451 assert_raises_process_error(1, "Could not connect to the server", self.nodes[0].cli('-rpcwait', '-rpcwaittimeout=5').echo)
452 assert_greater_than_or_equal(time.time(), start_time + 5)
453
454 self.log.info("Test that only one of -addrinfo, -generate, -getinfo, -netinfo may be specified at a time")
455 assert_raises_process_error(1, "Only one of -getinfo, -netinfo may be specified", self.nodes[0].cli('-getinfo', '-netinfo').send_cli)
456
457 if not self.is_ipc_compiled():
458 # This tests behavior when ENABLE_IPC is off. When it is on,
459 # behavior is checked by the interface_ipc_cli.py test.
460 self.log.info("Test bitcoin-cli -ipcconnect triggers error if not built with IPC support")
461 # node.cli.options includes -rpcconnect which can't be combined with -ipcconnect, so pass just -datadir directly to keep bitcoin-cli on the test's bitcoin.conf
462 args = self.nodes[0].binaries.valgrind_cmd + [self.nodes[0].binaries.paths.bitcoincli, f"-datadir={self.nodes[0].datadir_path}", "-ipcconnect=unix", "-getinfo"]
463 result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
464 assert_equal(result.stdout, "error: bitcoin-cli was not built with IPC support\n")
465 assert_equal(result.stderr, None)
466 assert_equal(result.returncode, 1)
467
468
469 if __name__ == '__main__':
470 TestBitcoinCli(__file__).main()
471