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