1 #!/usr/bin/env python3
2 # Copyright (c) 2017-2022 The Limenka 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 limenkad node can load multiple wallet files
8 """
9 from decimal import Decimal
10 from threading import Thread
11 import os
12 import platform
13 import shutil
14 import stat
15 16 from test_framework.authproxy import JSONRPCException
17 from test_framework.blocktools import COINBASE_MATURITY
18 from test_framework.test_framework import LimenkaTestFramework
19 from test_framework.test_node import ErrorMatch
20 from test_framework.util import (
21 assert_equal,
22 assert_raises_rpc_error,
23 ensure_for,
24 get_rpc_proxy,
25 )
26 27 got_loading_error = False
28 29 30 def test_load_unload(node, name):
31 global got_loading_error
32 while True:
33 if got_loading_error:
34 return
35 try:
36 node.loadwallet(name)
37 node.unloadwallet(name)
38 except JSONRPCException as e:
39 if e.error['code'] == -4 and 'Wallet already loading' in e.error['message']:
40 got_loading_error = True
41 return
42 43 44 class MultiWalletTest(LimenkaTestFramework):
45 def set_test_params(self):
46 self.setup_clean_chain = True
47 self.num_nodes = 2
48 self.rpc_timeout = 120
49 self.extra_args = [["-nowallet"], []]
50 51 def skip_test_if_missing_module(self):
52 self.skip_if_no_wallet()
53 54 def add_options(self, parser):
55 self.add_wallet_options(parser)
56 parser.add_argument(
57 '--data_wallets_dir',
58 default=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/wallets/'),
59 help='Test data with wallet directories (default: %(default)s)',
60 )
61 62 def run_test(self):
63 node = self.nodes[0]
64 65 data_dir = lambda *p: os.path.join(node.chain_path, *p)
66 wallet_dir = lambda *p: data_dir('wallets', *p)
67 wallet = lambda name: node.get_wallet_rpc(name)
68 69 def wallet_file(name):
70 if name == self.default_wallet_name:
71 return wallet_dir(self.default_wallet_name, self.wallet_data_filename)
72 if os.path.isdir(wallet_dir(name)):
73 return wallet_dir(name, "wallet.dat")
74 return wallet_dir(name)
75 76 assert_equal(self.nodes[0].listwalletdir(), {'wallets': [{'name': self.default_wallet_name}]})
77 78 # check wallet.dat is created
79 self.stop_nodes()
80 assert_equal(os.path.isfile(wallet_dir(self.default_wallet_name, self.wallet_data_filename)), True)
81 82 self.log.info("Verify warning is emitted when failing to scan the wallets directory")
83 if platform.system() == 'Windows':
84 self.log.warning('Skipping test involving chmod as Windows does not support it.')
85 elif os.geteuid() == 0:
86 self.log.warning('Skipping test involving chmod as it requires a non-root user.')
87 else:
88 self.start_node(0)
89 with self.nodes[0].assert_debug_log(unexpected_msgs=['Error scanning directory entries under'], expected_msgs=[]):
90 result = self.nodes[0].listwalletdir()
91 assert_equal(result, {'wallets': [{'name': self.default_wallet_name}]})
92 os.chmod(data_dir('wallets'), 0)
93 with self.nodes[0].assert_debug_log(expected_msgs=['Error scanning directory entries under']):
94 result = self.nodes[0].listwalletdir()
95 assert_equal(result, {'wallets': []})
96 self.stop_node(0)
97 # Restore permissions
98 os.chmod(data_dir('wallets'), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
99 100 # create symlink to verify wallet directory path can be referenced
101 # through symlink
102 os.mkdir(wallet_dir('w7'))
103 os.symlink('w7', wallet_dir('w7_symlink'))
104 105 os.symlink('..', wallet_dir('recursive_dir_symlink'))
106 107 os.mkdir(wallet_dir('self_walletdat_symlink'))
108 os.symlink('wallet.dat', wallet_dir('self_walletdat_symlink/wallet.dat'))
109 110 # rename wallet.dat to make sure plain wallet file paths (as opposed to
111 # directory paths) can be loaded
112 # create another dummy wallet for use in testing backups later
113 self.start_node(0)
114 node.createwallet("empty")
115 node.createwallet("plain")
116 node.createwallet("created")
117 self.stop_nodes()
118 empty_wallet = os.path.join(self.options.tmpdir, 'empty.dat')
119 os.rename(wallet_file("empty"), empty_wallet)
120 shutil.rmtree(wallet_dir("empty"))
121 empty_created_wallet = os.path.join(self.options.tmpdir, 'empty.created.dat')
122 os.rename(wallet_dir("created", self.wallet_data_filename), empty_created_wallet)
123 shutil.rmtree(wallet_dir("created"))
124 os.rename(wallet_file("plain"), wallet_dir("w8"))
125 shutil.rmtree(wallet_dir("plain"))
126 127 # restart node with a mix of wallet names:
128 # w1, w2, w3 - to verify new wallets created when non-existing paths specified
129 # w - to verify wallet name matching works when one wallet path is prefix of another
130 # sub/w5 - to verify relative wallet path is created correctly
131 # extern/w6 - to verify absolute wallet path is created correctly
132 # w7_symlink - to verify symlinked wallet path is initialized correctly
133 # w8 - to verify existing wallet file is loaded correctly. Not tested for SQLite wallets as this is a deprecated BDB behavior.
134 # '' - to verify default wallet file is created correctly
135 to_create = ['w1', 'w2', 'w3', 'w', 'sub/w5', 'w7_symlink']
136 in_wallet_dir = [w.replace('/', os.path.sep) for w in to_create] # Wallets in the wallet dir
137 in_wallet_dir.append('w7') # w7 is not loaded or created, but will be listed by listwalletdir because w7_symlink
138 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
139 to_load = [self.default_wallet_name]
140 if not self.options.descriptors:
141 to_load.append('w8')
142 wallet_names = to_create + to_load # Wallet names loaded in the wallet
143 in_wallet_dir += to_load # The loaded wallets are also in the wallet dir
144 self.start_node(0)
145 for wallet_name in to_create:
146 self.nodes[0].createwallet(wallet_name)
147 for wallet_name in to_load:
148 self.nodes[0].loadwallet(wallet_name)
149 150 os.mkdir(wallet_dir('no_access'))
151 os.chmod(wallet_dir('no_access'), 0)
152 try:
153 with self.nodes[0].assert_debug_log(expected_msgs=["Error while scanning wallet dir"]):
154 walletlist = self.nodes[0].listwalletdir()['wallets']
155 finally:
156 # Need to ensure access is restored for cleanup
157 os.chmod(wallet_dir('no_access'), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
158 assert_equal(sorted(map(lambda w: w['name'], walletlist)), sorted(in_wallet_dir))
159 160 assert_equal(set(node.listwallets()), set(wallet_names))
161 162 # should raise rpc error if wallet path can't be created
163 err_code = -4
164 assert_raises_rpc_error(err_code, "filesystem error:" if platform.system() != 'Windows' else "create_directories:", self.nodes[0].createwallet, "w8/bad")
165 166 # check that all requested wallets were created
167 self.stop_node(0)
168 for wallet_name in wallet_names:
169 assert_equal(os.path.isfile(wallet_file(wallet_name)), True)
170 171 self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
172 self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir())
173 self.nodes[0].assert_start_raises_init_error(['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir())
174 175 self.start_node(0, ['-wallet=w1', '-wallet=w1'])
176 self.stop_node(0, 'Warning: Ignoring duplicate -wallet w1.')
177 178 if not self.options.descriptors:
179 # Only BDB doesn't open duplicate wallet files. SQLite does not have this limitation. While this may be desired in the future, it is not necessary
180 # should not initialize if one wallet is a copy of another
181 shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy'))
182 in_wallet_dir.append('w8_copy')
183 exp_stderr = r"BerkeleyDatabase: Can't open database w8_copy \(duplicates fileid \w+ from w8\)"
184 self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], exp_stderr, match=ErrorMatch.PARTIAL_REGEX)
185 186 # should not initialize if wallet file is a symlink
187 os.symlink('w8', wallet_dir('w8_symlink'))
188 self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], r'Error: Invalid -wallet path \'w8_symlink\'\. .*', match=ErrorMatch.FULL_REGEX)
189 190 # should not initialize if the specified walletdir does not exist
191 self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
192 # should not initialize if the specified walletdir is not a directory
193 not_a_dir = wallet_dir('notadir')
194 open(not_a_dir, 'a', encoding="utf8").close()
195 self.nodes[0].assert_start_raises_init_error(['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory')
196 197 self.log.info("Do not allow -upgradewallet with multiwallet")
198 self.nodes[0].assert_start_raises_init_error(['-upgradewallet'], "Error: Error parsing command line arguments: Invalid parameter -upgradewallet")
199 200 # if wallets/ doesn't exist, datadir should be the default wallet dir
201 wallet_dir2 = data_dir('walletdir')
202 os.rename(wallet_dir(), wallet_dir2)
203 self.start_node(0)
204 self.nodes[0].createwallet("w4")
205 self.nodes[0].createwallet("w5")
206 assert_equal(set(node.listwallets()), {"w4", "w5"})
207 w5 = wallet("w5")
208 self.generatetoaddress(node, nblocks=1, address=w5.getnewaddress(), sync_fun=self.no_op)
209 210 # now if wallets/ exists again, but the rootdir is specified as the walletdir, w4 and w5 should still be loaded
211 os.rename(wallet_dir2, wallet_dir())
212 self.restart_node(0, ['-nowallet', '-walletdir=' + data_dir()])
213 self.nodes[0].loadwallet("w4")
214 self.nodes[0].loadwallet("w5")
215 assert_equal(set(node.listwallets()), {"w4", "w5"})
216 w5 = wallet("w5")
217 w5_info = w5.getwalletinfo()
218 assert_equal(w5_info['immature_balance'], 50)
219 220 competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir')
221 os.mkdir(competing_wallet_dir)
222 self.restart_node(0, ['-nowallet', '-walletdir=' + competing_wallet_dir])
223 self.nodes[0].createwallet(self.default_wallet_name)
224 if self.options.descriptors:
225 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']}?"
226 else:
227 exp_stderr = r"Error: Error initializing wallet database environment \"\S+competing_walletdir\S*\"!"
228 self.nodes[1].assert_start_raises_init_error(['-walletdir=' + competing_wallet_dir], exp_stderr, match=ErrorMatch.PARTIAL_REGEX)
229 230 self.restart_node(0)
231 for wallet_name in wallet_names:
232 self.nodes[0].loadwallet(wallet_name)
233 234 assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), sorted(in_wallet_dir))
235 236 wallets = [wallet(w) for w in wallet_names]
237 wallet_bad = wallet("bad")
238 239 # check wallet names and balances
240 self.generatetoaddress(node, nblocks=1, address=wallets[0].getnewaddress(), sync_fun=self.no_op)
241 for wallet_name, wallet in zip(wallet_names, wallets):
242 info = wallet.getwalletinfo()
243 assert_equal(info['immature_balance'], 50 if wallet is wallets[0] else 0)
244 assert_equal(info['walletname'], wallet_name)
245 246 # accessing invalid wallet fails
247 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", wallet_bad.getwalletinfo)
248 249 # accessing wallet RPC without using wallet endpoint fails
250 assert_raises_rpc_error(-19, "Multiple wallets are loaded. Please select which wallet", node.getwalletinfo)
251 252 w1, w2, w3, w4, *_ = wallets
253 self.generatetoaddress(node, nblocks=COINBASE_MATURITY + 1, address=w1.getnewaddress(), sync_fun=self.no_op)
254 assert_equal(w1.getbalance(), 100)
255 assert_equal(w2.getbalance(), 0)
256 assert_equal(w3.getbalance(), 0)
257 assert_equal(w4.getbalance(), 0)
258 259 w1.sendtoaddress(w2.getnewaddress(), 1)
260 w1.sendtoaddress(w3.getnewaddress(), 2)
261 w1.sendtoaddress(w4.getnewaddress(), 3)
262 self.generatetoaddress(node, nblocks=1, address=w1.getnewaddress(), sync_fun=self.no_op)
263 assert_equal(w2.getbalance(), 1)
264 assert_equal(w3.getbalance(), 2)
265 assert_equal(w4.getbalance(), 3)
266 267 batch = w1.batch([w1.getblockchaininfo.get_request(), w1.getwalletinfo.get_request()])
268 assert_equal(batch[0]["result"]["chain"], self.chain)
269 assert_equal(batch[1]["result"]["walletname"], "w1")
270 271 self.log.info('Test per-wallet setfeerate and settxfee calls')
272 assert_equal(w1.getwalletinfo()['paytxfee'], 0)
273 assert_equal(w2.getwalletinfo()['paytxfee'], 0)
274 w2.setfeerate(200)
275 assert_equal(w1.getwalletinfo()['paytxfee'], 0)
276 assert_equal(w2.getwalletinfo()['paytxfee'], Decimal('0.00200000'))
277 w2.settxfee(0.001)
278 assert_equal(w1.getwalletinfo()['paytxfee'], 0)
279 assert_equal(w2.getwalletinfo()['paytxfee'], Decimal('0.00100000'))
280 w1.setfeerate(30)
281 assert_equal(w1.getwalletinfo()['paytxfee'], Decimal('0.00030000'))
282 assert_equal(w2.getwalletinfo()['paytxfee'], Decimal('0.00100000'))
283 284 self.log.info("Test dynamic wallet loading")
285 286 self.restart_node(0, ['-nowallet'])
287 assert_equal(node.listwallets(), [])
288 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)
289 290 self.log.info("Load first wallet")
291 loadwallet_name = node.loadwallet(wallet_names[0])
292 assert_equal(loadwallet_name['name'], wallet_names[0])
293 assert_equal(node.listwallets(), wallet_names[0:1])
294 node.getwalletinfo()
295 w1 = node.get_wallet_rpc(wallet_names[0])
296 w1.getwalletinfo()
297 298 self.log.info("Load second wallet")
299 loadwallet_name = node.loadwallet(wallet_names[1])
300 assert_equal(loadwallet_name['name'], wallet_names[1])
301 assert_equal(node.listwallets(), wallet_names[0:2])
302 assert_raises_rpc_error(-19, "Multiple wallets are loaded. Please select which wallet", node.getwalletinfo)
303 w2 = node.get_wallet_rpc(wallet_names[1])
304 w2.getwalletinfo()
305 306 self.log.info("Concurrent wallet loading")
307 threads = []
308 for _ in range(3):
309 n = node.cli if self.options.usecli else get_rpc_proxy(node.url, 1, timeout=600, coveragedir=node.coverage_dir)
310 t = Thread(target=test_load_unload, args=(n, wallet_names[2]))
311 t.start()
312 threads.append(t)
313 for t in threads:
314 t.join()
315 global got_loading_error
316 assert_equal(got_loading_error, True)
317 318 self.log.info("Load remaining wallets")
319 for wallet_name in wallet_names[2:]:
320 loadwallet_name = self.nodes[0].loadwallet(wallet_name)
321 assert_equal(loadwallet_name['name'], wallet_name)
322 323 assert_equal(set(self.nodes[0].listwallets()), set(wallet_names))
324 325 # Fail to load if wallet doesn't exist
326 path = wallet_dir("wallets")
327 assert_raises_rpc_error(-18, "Wallet file verification failed. Failed to load database path '{}'. Path does not exist.".format(path), self.nodes[0].loadwallet, 'wallets')
328 329 # Fail to load duplicate wallets
330 assert_raises_rpc_error(-35, "Wallet \"w1\" is already loaded.", self.nodes[0].loadwallet, wallet_names[0])
331 if not self.options.descriptors:
332 # This tests the default wallet that BDB makes, so SQLite wallet doesn't need to test this
333 # Fail to load duplicate wallets by different ways (directory and filepath)
334 path = wallet_dir("wallet.dat")
335 assert_raises_rpc_error(-35, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, 'wallet.dat')
336 337 # Only BDB doesn't open duplicate wallet files. SQLite does not have this limitation. While this may be desired in the future, it is not necessary
338 # Fail to load if one wallet is a copy of another
339 assert_raises_rpc_error(-4, "BerkeleyDatabase: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
340 341 # Fail to load if one wallet is a copy of another, test this twice to make sure that we don't re-introduce #14304
342 assert_raises_rpc_error(-4, "BerkeleyDatabase: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
343 344 # Fail to load if wallet file is a symlink
345 assert_raises_rpc_error(-4, "Wallet file verification failed. Invalid -wallet path 'w8_symlink'", self.nodes[0].loadwallet, 'w8_symlink')
346 347 # Fail to load if a directory is specified that doesn't contain a wallet
348 os.mkdir(wallet_dir('empty_wallet_dir'))
349 path = wallet_dir("empty_wallet_dir")
350 assert_raises_rpc_error(-18, "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(path), self.nodes[0].loadwallet, 'empty_wallet_dir')
351 352 self.log.info("Test dynamic wallet creation.")
353 354 # Fail to create a wallet if it already exists.
355 path = wallet_dir("w2")
356 assert_raises_rpc_error(-4, "Failed to create database path '{}'. Database already exists.".format(path), self.nodes[0].createwallet, 'w2')
357 358 # Successfully create a wallet with a new name
359 loadwallet_name = self.nodes[0].createwallet('w9')
360 in_wallet_dir.append('w9')
361 assert_equal(loadwallet_name['name'], 'w9')
362 w9 = node.get_wallet_rpc('w9')
363 assert_equal(w9.getwalletinfo()['walletname'], 'w9')
364 365 assert 'w9' in self.nodes[0].listwallets()
366 367 # Successfully create a wallet using a full path
368 new_wallet_dir = os.path.join(self.options.tmpdir, 'new_walletdir')
369 new_wallet_name = os.path.join(new_wallet_dir, 'w10')
370 loadwallet_name = self.nodes[0].createwallet(new_wallet_name)
371 assert_equal(loadwallet_name['name'], new_wallet_name)
372 w10 = node.get_wallet_rpc(new_wallet_name)
373 assert_equal(w10.getwalletinfo()['walletname'], new_wallet_name)
374 375 assert new_wallet_name in self.nodes[0].listwallets()
376 377 self.log.info("Test dynamic wallet unloading")
378 379 # Test `unloadwallet` errors
380 assert_raises_rpc_error(-8, "Either the RPC endpoint wallet or the wallet name parameter must be provided", self.nodes[0].unloadwallet)
381 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", self.nodes[0].unloadwallet, "dummy")
382 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", node.get_wallet_rpc("dummy").unloadwallet)
383 assert_raises_rpc_error(-8, "The RPC endpoint wallet and the wallet name parameter specify different wallets", w1.unloadwallet, "w2"),
384 385 # Successfully unload the specified wallet name
386 self.nodes[0].unloadwallet("w1")
387 assert 'w1' not in self.nodes[0].listwallets()
388 389 # Unload w1 again, this time providing the wallet name twice
390 self.nodes[0].loadwallet("w1")
391 assert 'w1' in self.nodes[0].listwallets()
392 w1.unloadwallet("w1")
393 assert 'w1' not in self.nodes[0].listwallets()
394 395 # Successfully unload the wallet referenced by the request endpoint
396 # Also ensure unload works during walletpassphrase timeout
397 w2.encryptwallet('test')
398 w2.walletpassphrase('test', 1)
399 w2.unloadwallet()
400 ensure_for(duration=1.1, f=lambda: 'w2' not in self.nodes[0].listwallets())
401 402 # Successfully unload all wallets
403 for wallet_name in self.nodes[0].listwallets():
404 self.nodes[0].unloadwallet(wallet_name)
405 assert_equal(self.nodes[0].listwallets(), [])
406 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)", self.nodes[0].getwalletinfo)
407 408 # Successfully load a previously unloaded wallet
409 self.nodes[0].loadwallet('w1')
410 assert_equal(self.nodes[0].listwallets(), ['w1'])
411 assert_equal(w1.getwalletinfo()['walletname'], 'w1')
412 413 assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), sorted(in_wallet_dir))
414 415 # Test backing up and restoring wallets
416 self.log.info("Test wallet backup")
417 self.restart_node(0, ['-nowallet'])
418 for wallet_name in wallet_names:
419 self.nodes[0].loadwallet(wallet_name)
420 for wallet_name in wallet_names:
421 rpc = self.nodes[0].get_wallet_rpc(wallet_name)
422 addr = rpc.getnewaddress()
423 backup = os.path.join(self.options.tmpdir, 'backup.dat')
424 if os.path.exists(backup):
425 os.unlink(backup)
426 rpc.backupwallet(backup)
427 self.nodes[0].unloadwallet(wallet_name)
428 shutil.copyfile(empty_created_wallet if wallet_name == self.default_wallet_name else empty_wallet, wallet_file(wallet_name))
429 self.nodes[0].loadwallet(wallet_name)
430 assert_equal(rpc.getaddressinfo(addr)['ismine'], False)
431 self.nodes[0].unloadwallet(wallet_name)
432 shutil.copyfile(backup, wallet_file(wallet_name))
433 self.nodes[0].loadwallet(wallet_name)
434 assert_equal(rpc.getaddressinfo(addr)['ismine'], True)
435 436 # Test .walletlock file is closed
437 self.start_node(1)
438 wallet = os.path.join(self.options.tmpdir, 'my_wallet')
439 self.nodes[0].createwallet(wallet)
440 if self.options.descriptors:
441 assert_raises_rpc_error(-4, "Unable to obtain an exclusive lock", self.nodes[1].loadwallet, wallet)
442 else:
443 assert_raises_rpc_error(-4, "Error initializing wallet database environment", self.nodes[1].loadwallet, wallet)
444 self.nodes[0].unloadwallet(wallet)
445 self.nodes[1].loadwallet(wallet)
446 447 448 if __name__ == '__main__':
449 MultiWalletTest(__file__).main()
450