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