wallet_multiwallet.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 multiwallet.
6
7 Verify that a bitcoind node can load multiple wallet files
8 """
9 from threading import Thread
10 import os
11 import platform
12 import shutil
13 import stat
14
15 from test_framework.blocktools import COINBASE_MATURITY
16 from test_framework.test_framework import BitcoinTestFramework
17 from test_framework.test_node import ErrorMatch
18 from test_framework.util import (
19 assert_equal,
20 assert_raises_rpc_error,
21 ensure_for,
22 JSONRPCException,
23 )
24
25 got_loading_error = False
26
27
28 def test_load_unload(node, name):
29 global got_loading_error
30 while True:
31 if got_loading_error:
32 return
33 try:
34 node.loadwallet(name)
35 node.unloadwallet(name)
36 except JSONRPCException as e:
37 if e.error['code'] == -4 and 'Wallet already loading' in e.error['message']:
38 got_loading_error = True
39 return
40
41 def data_dir(node, *p):
42 return os.path.join(node.chain_path, *p)
43
44 def wallet_dir(node, *p):
45 return data_dir(node, 'wallets', *p)
46
47 def get_wallet(node, name):
48 return node.get_wallet_rpc(name)
49
50
51 class MultiWalletTest(BitcoinTestFramework):
52 def set_test_params(self):
53 self.setup_clean_chain = True
54 self.num_nodes = 2
55 self.rpc_timeout = 120
56 self.extra_args = [["-nowallet"], []]
57
58 def skip_test_if_missing_module(self):
59 self.skip_if_no_wallet()
60
61 def wallet_file(self, node, name):
62 if name == self.default_wallet_name:
63 return wallet_dir(node, self.default_wallet_name, self.wallet_data_filename)
64 if os.path.isdir(wallet_dir(node, name)):
65 return wallet_dir(node, name, "wallet.dat")
66 return wallet_dir(node, name)
67
68 def run_test(self):
69 self.check_chmod = True
70 self.check_symlinks = True
71 if platform.system() == 'Windows':
72 # Additional context:
73 # - chmod: Posix has one user per file while Windows has an ACL approach
74 # - symlinks: GCC 13 has FIXME notes for symlinks under Windows:
75 # https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/src/filesystem/ops-common.h;h=ba377905a2e90f7baf30c900b090f1f732397e08;hb=refs/heads/releases/gcc-13#l124
76 self.log.warning('Skipping chmod+symlink checks on Windows: '
77 'chmod works differently due to how access rights work and '
78 'symlink behavior with regard to the standard library is non-standard on cross-built binaries.')
79 self.check_chmod = False
80 self.check_symlinks = False
81 elif os.geteuid() == 0:
82 self.log.warning('Skipping checks involving chmod as they require non-root permissions.')
83 self.check_chmod = False
84
85 node = self.nodes[0]
86
87 assert_equal(node.listwalletdir(), {'wallets': [{'name': self.default_wallet_name, "warnings": []}]})
88
89 self.test_invalid_wallet_names()
90
91 # check wallet.dat is created
92 self.stop_nodes()
93 assert_equal(os.path.isfile(wallet_dir(node, self.default_wallet_name, self.wallet_data_filename)), True)
94
95 self.test_scanning_main_dir_access(node)
96 empty_wallet, empty_created_wallet, wallet_names, in_wallet_dir = self.test_mixed_wallets(node)
97 self.test_scanning_sub_dir(node, in_wallet_dir)
98 self.test_scanning_symlink_levels(node, in_wallet_dir)
99 self.test_init(node, wallet_names)
100 self.test_balances_and_fees(node, wallet_names, in_wallet_dir)
101 w1, w2 = self.test_loading(node, wallet_names)
102 self.test_creation(node, in_wallet_dir)
103 self.test_unloading(node, in_wallet_dir, w1, w2)
104 self.test_backup_and_restore(node, wallet_names, empty_wallet, empty_created_wallet)
105 self.test_lock_file_closed(node)
106
107 def test_scanning_main_dir_access(self, node):
108 if not self.check_chmod:
109 return
110
111 self.log.info("Verify warning is emitted when failing to scan the wallets directory")
112 self.start_node(0)
113 with node.assert_debug_log(unexpected_msgs=['Error scanning directory entries under'], expected_msgs=[]):
114 result = node.listwalletdir()
115 assert_equal(result, {'wallets': [{'name': 'default_wallet', 'warnings': []}]})
116 os.chmod(data_dir(node, 'wallets'), 0)
117 with node.assert_debug_log(expected_msgs=['Error scanning directory entries under']):
118 result = node.listwalletdir()
119 assert_equal(result, {'wallets': []})
120 self.stop_node(0)
121 # Restore permissions
122 os.chmod(data_dir(node, 'wallets'), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
123
124 def test_mixed_wallets(self, node):
125 self.log.info("Test mixed wallets")
126 # create symlink to verify wallet directory path can be referenced
127 # through symlink
128 os.mkdir(wallet_dir(node, 'w7'))
129 os.symlink('w7', wallet_dir(node, 'w7_symlink'))
130
131 if self.check_symlinks:
132 os.symlink('..', wallet_dir(node, 'recursive_dir_symlink'))
133
134 # rename wallet.dat to make sure plain wallet file paths (as opposed to
135 # directory paths) can be loaded
136 # create another dummy wallet for use in testing backups later
137 self.start_node(0)
138 node.createwallet("empty")
139 node.createwallet("plain")
140 node.createwallet("created")
141 self.stop_nodes()
142 empty_wallet = os.path.join(self.options.tmpdir, 'empty.dat')
143 os.rename(self.wallet_file(node, "empty"), empty_wallet)
144 os.rmdir(wallet_dir(node, "empty"))
145 empty_created_wallet = os.path.join(self.options.tmpdir, 'empty.created.dat')
146 os.rename(wallet_dir(node, "created", self.wallet_data_filename), empty_created_wallet)
147 os.rmdir(wallet_dir(node, "created"))
148 os.rename(self.wallet_file(node, "plain"), wallet_dir(node, "w8"))
149 os.rmdir(wallet_dir(node, "plain"))
150
151 # restart node with a mix of wallet names:
152 # w1, w2, w3 - to verify new wallets created when non-existing paths specified
153 # w - to verify wallet name matching works when one wallet path is prefix of another
154 # sub/w5 - to verify relative wallet path is created correctly
155 # extern/w6 - to verify absolute wallet path is created correctly
156 # w7_symlink - to verify symlinked wallet path is initialized correctly
157 # w8 - to verify existing wallet file is loaded correctly. Not tested for SQLite wallets as this is a deprecated BDB behavior.
158 # '' - to verify default wallet file is created correctly
159 to_create = ['w1', 'w2', 'w3', 'w', 'sub/w5', 'w7_symlink']
160 in_wallet_dir = [w.replace('/', os.path.sep) for w in to_create] # Wallets in the wallet dir
161 in_wallet_dir.append('w7') # w7 is not loaded or created, but will be listed by listwalletdir because w7_symlink
162 to_create.append(os.path.join(self.options.tmpdir, 'extern/w6')) # External, not in the wallet dir, so we need to avoid adding it to in_wallet_dir
163 to_load = [self.default_wallet_name]
164 wallet_names = to_create + to_load # Wallet names loaded in the wallet
165 in_wallet_dir += to_load # The loaded wallets are also in the wallet dir
166 self.start_node(0)
167 for wallet_name in to_create:
168 node.createwallet(wallet_name)
169 for wallet_name in to_load:
170 node.loadwallet(wallet_name)
171
172 return empty_wallet, empty_created_wallet, wallet_names, in_wallet_dir
173
174 def test_scanning_sub_dir(self, node, in_wallet_dir):
175 if not self.check_chmod:
176 return
177
178 self.log.info("Test scanning for sub directories")
179 # Baseline, no errors.
180 with node.assert_debug_log(expected_msgs=[], unexpected_msgs=["Error while scanning wallet dir"]):
181 walletlist = node.listwalletdir()['wallets']
182 assert_equal(sorted(map(lambda w: w['name'], walletlist)), sorted(in_wallet_dir))
183
184 # "Permission denied" error.
185 os.mkdir(wallet_dir(node, 'no_access'))
186 os.chmod(wallet_dir(node, 'no_access'), 0)
187 with node.assert_debug_log(expected_msgs=["Error while scanning wallet dir"]):
188 walletlist = node.listwalletdir()['wallets']
189 # Need to ensure access is restored for cleanup
190 os.chmod(wallet_dir(node, 'no_access'), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
191
192 # Verify that we no longer emit errors after restoring permissions
193 with node.assert_debug_log(expected_msgs=[], unexpected_msgs=["Error while scanning wallet dir"]):
194 walletlist = node.listwalletdir()['wallets']
195 assert_equal(sorted(map(lambda w: w['name'], walletlist)), sorted(in_wallet_dir))
196
197 def test_scanning_symlink_levels(self, node, in_wallet_dir):
198 if not self.check_symlinks:
199 return
200
201 self.log.info("Test for errors from too many levels of symbolic links")
202 os.mkdir(wallet_dir(node, 'self_walletdat_symlink'))
203 os.symlink('wallet.dat', wallet_dir(node, 'self_walletdat_symlink/wallet.dat'))
204 with node.assert_debug_log(expected_msgs=["Error while scanning wallet dir"]):
205 walletlist = node.listwalletdir()['wallets']
206 assert_equal(sorted(map(lambda w: w['name'], walletlist)), sorted(in_wallet_dir))
207
208 def test_init(self, node, wallet_names):
209 self.log.info("Test initialization")
210 assert_equal(set(node.listwallets()), set(wallet_names))
211 # check that all requested wallets were created
212 self.stop_node(0)
213 for wallet_name in wallet_names:
214 assert_equal(os.path.isfile(self.wallet_file(node, wallet_name)), True)
215
216 node.assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
217 node.assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir(node))
218 node.assert_start_raises_init_error(['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir(node))
219
220 self.start_node(0, ['-wallet=w1', '-wallet=w1'])
221 self.stop_node(0, 'Warning: Ignoring duplicate -wallet w1.')
222
223 # should not initialize if wallet file is a symlink
224 if self.check_symlinks:
225 os.symlink('w8', wallet_dir(node, 'w8_symlink'))
226 node.assert_start_raises_init_error(['-wallet=w8_symlink'], r'Error: Invalid -wallet path \'w8_symlink\'\. .*', match=ErrorMatch.FULL_REGEX)
227
228 # should not initialize if the specified walletdir does not exist
229 node.assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
230 # should not initialize if the specified walletdir is not a directory
231 not_a_dir = wallet_dir(node, 'notadir')
232 open(not_a_dir, 'a').close()
233 node.assert_start_raises_init_error(['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory')
234
235 # if wallets/ doesn't exist, datadir should be the default wallet dir
236 wallet_dir2 = data_dir(node, 'walletdir')
237 os.rename(wallet_dir(node), wallet_dir2)
238 self.start_node(0)
239 node.createwallet("w4")
240 node.createwallet("w5")
241 assert_equal(set(node.listwallets()), {"w4", "w5"})
242 w5 = get_wallet(node, "w5")
243 self.generatetoaddress(node, nblocks=1, address=w5.getnewaddress(), sync_fun=self.no_op)
244
245 # now if wallets/ exists again, but the rootdir is specified as the walletdir, w4 and w5 should still be loaded
246 os.rename(wallet_dir2, wallet_dir(node))
247 self.restart_node(0, ['-nowallet', '-walletdir=' + data_dir(node)])
248 node.loadwallet("w4")
249 node.loadwallet("w5")
250 assert_equal(set(node.listwallets()), {"w4", "w5"})
251 w5 = get_wallet(node, "w5")
252 assert_equal(w5.getbalances()["mine"]["immature"], 50)
253
254 competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir')
255 os.mkdir(competing_wallet_dir)
256 self.restart_node(0, ['-nowallet', '-walletdir=' + competing_wallet_dir])
257 node.createwallet(self.default_wallet_name)
258 exp_stderr = f"Error: SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of {self.config['environment']['CLIENT_NAME']}?"
259 self.nodes[1].assert_start_raises_init_error(['-walletdir=' + competing_wallet_dir], exp_stderr, match=ErrorMatch.PARTIAL_REGEX)
260
261 def test_balances_and_fees(self, node, wallet_names, in_wallet_dir):
262 self.log.info("Test balances and fees")
263 self.restart_node(0)
264 for wallet_name in wallet_names:
265 node.loadwallet(wallet_name)
266
267 assert_equal(sorted(map(lambda w: w['name'], node.listwalletdir()['wallets'])), sorted(in_wallet_dir))
268
269 wallets = [get_wallet(node, w) for w in wallet_names]
270 wallet_bad = get_wallet(node, "bad")
271
272 # check wallet names and balances
273 self.generatetoaddress(node, nblocks=1, address=wallets[0].getnewaddress(), sync_fun=self.no_op)
274 for wallet_name, wallet in zip(wallet_names, wallets):
275 info = wallet.getwalletinfo()
276 assert_equal(wallet.getbalances()["mine"]["immature"], 50 if wallet is wallets[0] else 0)
277 assert_equal(info['walletname'], wallet_name)
278
279 # accessing invalid wallet fails
280 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", wallet_bad.getwalletinfo)
281
282 # accessing wallet RPC without using wallet endpoint fails
283 assert_raises_rpc_error(-19, "Multiple wallets are loaded. Please select which wallet", node.getwalletinfo)
284
285 w1, w2, w3, w4, *_ = wallets
286 self.generatetoaddress(node, nblocks=COINBASE_MATURITY + 1, address=w1.getnewaddress(), sync_fun=self.no_op)
287 assert_equal(w1.getbalance(), 100)
288 assert_equal(w2.getbalance(), 0)
289 assert_equal(w3.getbalance(), 0)
290 assert_equal(w4.getbalance(), 0)
291
292 w1.sendtoaddress(w2.getnewaddress(), 1)
293 w1.sendtoaddress(w3.getnewaddress(), 2)
294 w1.sendtoaddress(w4.getnewaddress(), 3)
295 self.generatetoaddress(node, nblocks=1, address=w1.getnewaddress(), sync_fun=self.no_op)
296 assert_equal(w2.getbalance(), 1)
297 assert_equal(w3.getbalance(), 2)
298 assert_equal(w4.getbalance(), 3)
299
300 batch = w1.batch([w1.getblockchaininfo.get_request(), w1.getwalletinfo.get_request()])
301 assert_equal(batch[0]["result"]["chain"], self.chain)
302 assert_equal(batch[1]["result"]["walletname"], "w1")
303
304 def test_loading(self, node, wallet_names):
305 self.log.info("Test dynamic wallet loading")
306
307 self.restart_node(0, ['-nowallet'])
308 assert_equal(node.listwallets(), [])
309 assert_raises_rpc_error(-18, "No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)", node.getwalletinfo)
310
311 self.log.info("Load first wallet")
312 loadwallet_name = node.loadwallet(wallet_names[0])
313 assert_equal(loadwallet_name['name'], wallet_names[0])
314 assert_equal(node.listwallets(), wallet_names[0:1])
315 node.getwalletinfo()
316 w1 = get_wallet(node, wallet_names[0])
317 w1.getwalletinfo()
318
319 self.log.info("Load second wallet")
320 loadwallet_name = node.loadwallet(wallet_names[1])
321 assert_equal(loadwallet_name['name'], wallet_names[1])
322 assert_equal(node.listwallets(), wallet_names[0:2])
323 assert_raises_rpc_error(-19, "Multiple wallets are loaded. Please select which wallet", node.getwalletinfo)
324 w2 = get_wallet(node, wallet_names[1])
325 w2.getwalletinfo()
326
327 self.log.info("Concurrent wallet loading")
328 threads = []
329 for _ in range(3):
330 n = node.create_new_rpc_connection()
331 t = Thread(target=test_load_unload, args=(n, wallet_names[2]))
332 t.start()
333 threads.append(t)
334 for t in threads:
335 t.join()
336 global got_loading_error
337 assert_equal(got_loading_error, True)
338
339 self.log.info("Load remaining wallets")
340 for wallet_name in wallet_names[2:]:
341 loadwallet_name = node.loadwallet(wallet_name)
342 assert_equal(loadwallet_name['name'], wallet_name)
343
344 assert_equal(set(node.listwallets()), set(wallet_names))
345
346 # Fail to load if wallet doesn't exist
347 path = wallet_dir(node, "wallets")
348 assert_raises_rpc_error(-18, "Wallet file verification failed. Failed to load database path '{}'. Path does not exist.".format(path), node.loadwallet, 'wallets')
349
350 # Fail to load duplicate wallets
351 assert_raises_rpc_error(-35, "Wallet \"w1\" is already loaded.", node.loadwallet, wallet_names[0])
352 # Fail to load if wallet file is a symlink
353 if self.check_symlinks:
354 assert_raises_rpc_error(-4, "Wallet file verification failed. Invalid -wallet path 'w8_symlink'", node.loadwallet, 'w8_symlink')
355
356 # Fail to load if a directory is specified that doesn't contain a wallet
357 os.mkdir(wallet_dir(node, 'empty_wallet_dir'))
358 path = wallet_dir(node, "empty_wallet_dir")
359 assert_raises_rpc_error(-18, "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(path), node.loadwallet, 'empty_wallet_dir')
360
361 return w1, w2
362
363 def test_creation(self, node, in_wallet_dir):
364 self.log.info("Test dynamic wallet creation")
365
366 # should raise rpc error if wallet path can't be created
367 err_code = -4
368 assert_raises_rpc_error(err_code, "Wallet file verification failed. ", node.createwallet, "w8/bad")
369
370 # Fail to create a wallet if it already exists.
371 path = wallet_dir(node, "w2")
372 assert_raises_rpc_error(-4, "Failed to create database path '{}'. Database already exists.".format(path), node.createwallet, 'w2')
373
374 # Successfully create a wallet with a new name
375 loadwallet_name = node.createwallet('w9')
376 in_wallet_dir.append('w9')
377 assert_equal(loadwallet_name['name'], 'w9')
378 w9 = get_wallet(node, 'w9')
379 assert_equal(w9.getwalletinfo()['walletname'], 'w9')
380
381 assert 'w9' in node.listwallets()
382
383 # Successfully create a wallet using a full path
384 new_wallet_dir = os.path.join(self.options.tmpdir, 'new_walletdir')
385 new_wallet_name = os.path.join(new_wallet_dir, 'w10')
386 loadwallet_name = node.createwallet(new_wallet_name)
387 assert_equal(loadwallet_name['name'], new_wallet_name)
388 w10 = get_wallet(node, new_wallet_name)
389 assert_equal(w10.getwalletinfo()['walletname'], new_wallet_name)
390
391 assert new_wallet_name in node.listwallets()
392
393 def test_unloading(self, node, in_wallet_dir, w1, w2):
394 self.log.info("Test dynamic wallet unloading")
395
396 # Test `unloadwallet` errors
397 assert_raises_rpc_error(-8, "Either the RPC endpoint wallet or the wallet name parameter must be provided", node.unloadwallet)
398 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", node.unloadwallet, "dummy")
399 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", get_wallet(node, "dummy").unloadwallet)
400 assert_raises_rpc_error(-8, "The RPC endpoint wallet and the wallet name parameter specify different wallets", w1.unloadwallet, "w2"),
401
402 # Successfully unload the specified wallet name
403 node.unloadwallet("w1")
404 assert 'w1' not in node.listwallets()
405
406 # Unload w1 again, this time providing the wallet name twice
407 node.loadwallet("w1")
408 assert 'w1' in node.listwallets()
409 w1.unloadwallet("w1")
410 assert 'w1' not in node.listwallets()
411
412 # Successfully unload the wallet referenced by the request endpoint
413 # Also ensure unload works during walletpassphrase timeout
414 w2.encryptwallet('test')
415 w2.walletpassphrase('test', 1)
416 w2.unloadwallet()
417 ensure_for(duration=1.1, f=lambda: 'w2' not in node.listwallets())
418
419 # Successfully unload all wallets
420 for wallet_name in node.listwallets():
421 node.unloadwallet(wallet_name)
422 assert_equal(node.listwallets(), [])
423 assert_raises_rpc_error(-18, "No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)", node.getwalletinfo)
424
425 # Successfully load a previously unloaded wallet
426 node.loadwallet('w1')
427 assert_equal(node.listwallets(), ['w1'])
428 assert_equal(w1.getwalletinfo()['walletname'], 'w1')
429
430 assert_equal(sorted(map(lambda w: w['name'], node.listwalletdir()['wallets'])), sorted(in_wallet_dir))
431
432 def test_backup_and_restore(self, node, wallet_names, empty_wallet, empty_created_wallet):
433 self.log.info("Test wallet backup and restore")
434 self.restart_node(0, ['-nowallet'])
435 for wallet_name in wallet_names:
436 node.loadwallet(wallet_name)
437 for wallet_name in wallet_names:
438 rpc = get_wallet(node, wallet_name)
439 addr = rpc.getnewaddress()
440 backup = os.path.join(self.options.tmpdir, 'backup.dat')
441 if os.path.exists(backup):
442 os.unlink(backup)
443 rpc.backupwallet(backup)
444 node.unloadwallet(wallet_name)
445 shutil.copyfile(empty_created_wallet if wallet_name == self.default_wallet_name else empty_wallet, self.wallet_file(node, wallet_name))
446 node.loadwallet(wallet_name)
447 assert_equal(rpc.getaddressinfo(addr)['ismine'], False)
448 node.unloadwallet(wallet_name)
449 shutil.copyfile(backup, self.wallet_file(node, wallet_name))
450 node.loadwallet(wallet_name)
451 assert_equal(rpc.getaddressinfo(addr)['ismine'], True)
452
453 def test_lock_file_closed(self, node):
454 self.log.info("Test wallet lock file is closed")
455 self.start_node(1)
456 wallet = os.path.join(self.options.tmpdir, 'my_wallet')
457 node.createwallet(wallet)
458 assert_raises_rpc_error(-4, "Unable to obtain an exclusive lock", self.nodes[1].loadwallet, wallet)
459 node.unloadwallet(wallet)
460 self.nodes[1].loadwallet(wallet)
461
462 def test_invalid_wallet_names(self):
463 self.log.info("Test weird paths are not allowed as wallet names")
464 NON_NORMALIZED = ["bad/./path", "bad/../path", "/bad/./path", "/bad/../path", "../", "./", "./wallet", "../wallets/../wallets/wallet"]
465 for name in NON_NORMALIZED:
466 assert_raises_rpc_error(-4, "Wallet name given as a path must be normalized", self.nodes[0].createwallet, name)
467
468 INVALID_RELPATH = ["../wallets/wallet", "..", "."]
469 for name in INVALID_RELPATH:
470 assert_raises_rpc_error(-4, "Wallet name given as a relative path cannot begin with ./ or ../", self.nodes[0].createwallet, name)
471
472 INVALID_ROOT = ["/"]
473 if platform.system() == "Windows":
474 INVALID_ROOT.extend(["C:\\", "C:"])
475 for name in INVALID_ROOT:
476 assert_raises_rpc_error(-4, "Wallet name cannot be the root path", self.nodes[0].createwallet, name)
477
478 if __name__ == '__main__':
479 MultiWalletTest(__file__).main()
480