You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wallet_multiwallet.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017 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. Verify that a bitcoind node can load multiple wallet files
  7. """
  8. import os
  9. import shutil
  10. from test_framework.test_framework import BitcoinTestFramework
  11. from test_framework.util import assert_equal, assert_raises_rpc_error
  12. class MultiWalletTest(BitcoinTestFramework):
  13. def set_test_params(self):
  14. self.setup_clean_chain = True
  15. self.num_nodes = 2
  16. self.extra_args = [['-wallet=w1', '-wallet=w2', '-wallet=w3', '-wallet=w'], []]
  17. self.supports_cli = True
  18. def run_test(self):
  19. node = self.nodes[0]
  20. data_dir = lambda *p: os.path.join(node.datadir, 'regtest', *p)
  21. wallet_dir = lambda *p: data_dir('wallets', *p)
  22. wallet = lambda name: node.get_wallet_rpc(name)
  23. assert_equal(set(node.listwallets()), {"w1", "w2", "w3", "w"})
  24. self.stop_nodes()
  25. self.assert_start_raises_init_error(0, ['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
  26. self.assert_start_raises_init_error(0, ['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir())
  27. self.assert_start_raises_init_error(0, ['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir())
  28. # should not initialize if there are duplicate wallets
  29. self.assert_start_raises_init_error(0, ['-wallet=w1', '-wallet=w1'], 'Error loading wallet w1. Duplicate -wallet filename specified.')
  30. # should not initialize if wallet file is a directory
  31. os.mkdir(wallet_dir('w11'))
  32. self.assert_start_raises_init_error(0, ['-wallet=w11'], 'Error loading wallet w11. -wallet filename must be a regular file.')
  33. # should not initialize if one wallet is a copy of another
  34. shutil.copyfile(wallet_dir('w2'), wallet_dir('w22'))
  35. self.assert_start_raises_init_error(0, ['-wallet=w2', '-wallet=w22'], 'duplicates fileid')
  36. # should not initialize if wallet file is a symlink
  37. os.symlink(wallet_dir('w1'), wallet_dir('w12'))
  38. self.assert_start_raises_init_error(0, ['-wallet=w12'], 'Error loading wallet w12. -wallet filename must be a regular file.')
  39. # should not initialize if the specified walletdir does not exist
  40. self.assert_start_raises_init_error(0, ['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
  41. # should not initialize if the specified walletdir is not a directory
  42. not_a_dir = wallet_dir('notadir')
  43. open(not_a_dir, 'a').close()
  44. self.assert_start_raises_init_error(0, ['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory')
  45. self.log.info("Do not allow -zapwallettxes with multiwallet")
  46. self.assert_start_raises_init_error(0, ['-zapwallettxes', '-wallet=w1', '-wallet=w2'], "Error: -zapwallettxes is only allowed with a single wallet file")
  47. self.assert_start_raises_init_error(0, ['-zapwallettxes=1', '-wallet=w1', '-wallet=w2'], "Error: -zapwallettxes is only allowed with a single wallet file")
  48. self.assert_start_raises_init_error(0, ['-zapwallettxes=2', '-wallet=w1', '-wallet=w2'], "Error: -zapwallettxes is only allowed with a single wallet file")
  49. self.log.info("Do not allow -salvagewallet with multiwallet")
  50. self.assert_start_raises_init_error(0, ['-salvagewallet', '-wallet=w1', '-wallet=w2'], "Error: -salvagewallet is only allowed with a single wallet file")
  51. self.assert_start_raises_init_error(0, ['-salvagewallet=1', '-wallet=w1', '-wallet=w2'], "Error: -salvagewallet is only allowed with a single wallet file")
  52. self.log.info("Do not allow -upgradewallet with multiwallet")
  53. self.assert_start_raises_init_error(0, ['-upgradewallet', '-wallet=w1', '-wallet=w2'], "Error: -upgradewallet is only allowed with a single wallet file")
  54. self.assert_start_raises_init_error(0, ['-upgradewallet=1', '-wallet=w1', '-wallet=w2'], "Error: -upgradewallet is only allowed with a single wallet file")
  55. # if wallets/ doesn't exist, datadir should be the default wallet dir
  56. wallet_dir2 = data_dir('walletdir')
  57. os.rename(wallet_dir(), wallet_dir2)
  58. self.start_node(0, ['-wallet=w4', '-wallet=w5'])
  59. assert_equal(set(node.listwallets()), {"w4", "w5"})
  60. w5 = wallet("w5")
  61. w5.generate(1)
  62. # now if wallets/ exists again, but the rootdir is specified as the walletdir, w4 and w5 should still be loaded
  63. os.rename(wallet_dir2, wallet_dir())
  64. self.restart_node(0, ['-wallet=w4', '-wallet=w5', '-walletdir=' + data_dir()])
  65. assert_equal(set(node.listwallets()), {"w4", "w5"})
  66. w5 = wallet("w5")
  67. w5_info = w5.getwalletinfo()
  68. assert_equal(w5_info['immature_balance'], 50)
  69. competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir')
  70. os.mkdir(competing_wallet_dir)
  71. self.restart_node(0, ['-walletdir='+competing_wallet_dir])
  72. self.assert_start_raises_init_error(1, ['-walletdir='+competing_wallet_dir], 'Error initializing wallet database environment')
  73. self.restart_node(0, self.extra_args[0])
  74. w1 = wallet("w1")
  75. w2 = wallet("w2")
  76. w3 = wallet("w3")
  77. w4 = wallet("w")
  78. wallet_bad = wallet("bad")
  79. w1.generate(1)
  80. # accessing invalid wallet fails
  81. assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", wallet_bad.getwalletinfo)
  82. # accessing wallet RPC without using wallet endpoint fails
  83. assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo)
  84. # check w1 wallet balance
  85. w1_info = w1.getwalletinfo()
  86. assert_equal(w1_info['immature_balance'], 50)
  87. w1_name = w1_info['walletname']
  88. assert_equal(w1_name, "w1")
  89. # check w2 wallet balance
  90. w2_info = w2.getwalletinfo()
  91. assert_equal(w2_info['immature_balance'], 0)
  92. w2_name = w2_info['walletname']
  93. assert_equal(w2_name, "w2")
  94. w3_name = w3.getwalletinfo()['walletname']
  95. assert_equal(w3_name, "w3")
  96. w4_name = w4.getwalletinfo()['walletname']
  97. assert_equal(w4_name, "w")
  98. w1.generate(101)
  99. assert_equal(w1.getbalance(), 100)
  100. assert_equal(w2.getbalance(), 0)
  101. assert_equal(w3.getbalance(), 0)
  102. assert_equal(w4.getbalance(), 0)
  103. w1.sendtoaddress(w2.getnewaddress(), 1)
  104. w1.sendtoaddress(w3.getnewaddress(), 2)
  105. w1.sendtoaddress(w4.getnewaddress(), 3)
  106. w1.generate(1)
  107. assert_equal(w2.getbalance(), 1)
  108. assert_equal(w3.getbalance(), 2)
  109. assert_equal(w4.getbalance(), 3)
  110. batch = w1.batch([w1.getblockchaininfo.get_request(), w1.getwalletinfo.get_request()])
  111. assert_equal(batch[0]["result"]["chain"], "regtest")
  112. assert_equal(batch[1]["result"]["walletname"], "w1")
  113. if __name__ == '__main__':
  114. MultiWalletTest().main()