1#!/usr/bin/env python3 2# Copyright (c) 2017-2018 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 7Verify that a bitcoind node can load multiple wallet files 8""" 9import os 10import shutil 11import time 12 13from test_framework.test_framework import BitcoinTestFramework 14from test_framework.test_node import ErrorMatch 15from test_framework.util import ( 16 assert_equal, 17 assert_raises_rpc_error, 18) 19 20 21class MultiWalletTest(BitcoinTestFramework): 22 def set_test_params(self): 23 self.setup_clean_chain = True 24 self.num_nodes = 2 25 self.supports_cli = True 26 27 def skip_test_if_missing_module(self): 28 self.skip_if_no_wallet() 29 30 def run_test(self): 31 node = self.nodes[0] 32 33 data_dir = lambda *p: os.path.join(node.datadir, 'regtest', *p) 34 wallet_dir = lambda *p: data_dir('wallets', *p) 35 wallet = lambda name: node.get_wallet_rpc(name) 36 37 def wallet_file(name): 38 if os.path.isdir(wallet_dir(name)): 39 return wallet_dir(name, "wallet.dat") 40 return wallet_dir(name) 41 42 assert_equal(self.nodes[0].listwalletdir(), { 'wallets': [{ 'name': '' }] }) 43 44 # check wallet.dat is created 45 self.stop_nodes() 46 assert_equal(os.path.isfile(wallet_dir('wallet.dat')), True) 47 48 # create symlink to verify wallet directory path can be referenced 49 # through symlink 50 os.mkdir(wallet_dir('w7')) 51 os.symlink('w7', wallet_dir('w7_symlink')) 52 53 # rename wallet.dat to make sure plain wallet file paths (as opposed to 54 # directory paths) can be loaded 55 os.rename(wallet_dir("wallet.dat"), wallet_dir("w8")) 56 57 # create another dummy wallet for use in testing backups later 58 self.start_node(0, []) 59 self.stop_nodes() 60 empty_wallet = os.path.join(self.options.tmpdir, 'empty.dat') 61 os.rename(wallet_dir("wallet.dat"), empty_wallet) 62 63 # restart node with a mix of wallet names: 64 # w1, w2, w3 - to verify new wallets created when non-existing paths specified 65 # w - to verify wallet name matching works when one wallet path is prefix of another 66 # sub/w5 - to verify relative wallet path is created correctly 67 # extern/w6 - to verify absolute wallet path is created correctly 68 # w7_symlink - to verify symlinked wallet path is initialized correctly 69 # w8 - to verify existing wallet file is loaded correctly 70 # '' - to verify default wallet file is created correctly 71 wallet_names = ['w1', 'w2', 'w3', 'w', 'sub/w5', os.path.join(self.options.tmpdir, 'extern/w6'), 'w7_symlink', 'w8', ''] 72 extra_args = ['-wallet={}'.format(n) for n in wallet_names] 73 self.start_node(0, extra_args) 74 assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), ['', os.path.join('sub', 'w5'), 'w', 'w1', 'w2', 'w3', 'w7', 'w7_symlink', 'w8']) 75 76 assert_equal(set(node.listwallets()), set(wallet_names)) 77 78 # check that all requested wallets were created 79 self.stop_node(0) 80 for wallet_name in wallet_names: 81 assert_equal(os.path.isfile(wallet_file(wallet_name)), True) 82 83 # should not initialize if wallet path can't be created 84 exp_stderr = "boost::filesystem::create_directory:" 85 self.nodes[0].assert_start_raises_init_error(['-wallet=wallet.dat/bad'], exp_stderr, match=ErrorMatch.PARTIAL_REGEX) 86 87 self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist') 88 self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir()) 89 self.nodes[0].assert_start_raises_init_error(['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir()) 90 91 # should not initialize if there are duplicate wallets 92 self.nodes[0].assert_start_raises_init_error(['-wallet=w1', '-wallet=w1'], 'Error: Error loading wallet w1. Duplicate -wallet filename specified.') 93 94 # should not initialize if one wallet is a copy of another 95 shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy')) 96 exp_stderr = "BerkeleyBatch: Can't open database w8_copy \(duplicates fileid \w+ from w8\)" 97 self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], exp_stderr, match=ErrorMatch.PARTIAL_REGEX) 98 99 # should not initialize if wallet file is a symlink 100 os.symlink('w8', wallet_dir('w8_symlink')) 101 self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Error: Invalid -wallet path \'w8_symlink\'\. .*', match=ErrorMatch.FULL_REGEX) 102 103 # should not initialize if the specified walletdir does not exist 104 self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist') 105 # should not initialize if the specified walletdir is not a directory 106 not_a_dir = wallet_dir('notadir') 107 open(not_a_dir, 'a', encoding="utf8").close() 108 self.nodes[0].assert_start_raises_init_error(['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory') 109 110 self.log.info("Do not allow -zapwallettxes with multiwallet") 111 self.nodes[0].assert_start_raises_init_error(['-zapwallettxes', '-wallet=w1', '-wallet=w2'], "Error: -zapwallettxes is only allowed with a single wallet file") 112 self.nodes[0].assert_start_raises_init_error(['-zapwallettxes=1', '-wallet=w1', '-wallet=w2'], "Error: -zapwallettxes is only allowed with a single wallet file") 113 self.nodes[0].assert_start_raises_init_error(['-zapwallettxes=2', '-wallet=w1', '-wallet=w2'], "Error: -zapwallettxes is only allowed with a single wallet file") 114 115 self.log.info("Do not allow -salvagewallet with multiwallet") 116 self.nodes[0].assert_start_raises_init_error(['-salvagewallet', '-wallet=w1', '-wallet=w2'], "Error: -salvagewallet is only allowed with a single wallet file") 117 self.nodes[0].assert_start_raises_init_error(['-salvagewallet=1', '-wallet=w1', '-wallet=w2'], "Error: -salvagewallet is only allowed with a single wallet file") 118 119 self.log.info("Do not allow -upgradewallet with multiwallet") 120 self.nodes[0].assert_start_raises_init_error(['-upgradewallet', '-wallet=w1', '-wallet=w2'], "Error: -upgradewallet is only allowed with a single wallet file") 121 self.nodes[0].assert_start_raises_init_error(['-upgradewallet=1', '-wallet=w1', '-wallet=w2'], "Error: -upgradewallet is only allowed with a single wallet file") 122 123 # if wallets/ doesn't exist, datadir should be the default wallet dir 124 wallet_dir2 = data_dir('walletdir') 125 os.rename(wallet_dir(), wallet_dir2) 126 self.start_node(0, ['-wallet=w4', '-wallet=w5']) 127 assert_equal(set(node.listwallets()), {"w4", "w5"}) 128 w5 = wallet("w5") 129 node.generatetoaddress(nblocks=1, address=w5.getnewaddress()) 130 131 # now if wallets/ exists again, but the rootdir is specified as the walletdir, w4 and w5 should still be loaded 132 os.rename(wallet_dir2, wallet_dir()) 133 self.restart_node(0, ['-wallet=w4', '-wallet=w5', '-walletdir=' + data_dir()]) 134 assert_equal(set(node.listwallets()), {"w4", "w5"}) 135 w5 = wallet("w5") 136 w5_info = w5.getwalletinfo() 137 assert_equal(w5_info['immature_balance'], 50) 138 139 competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir') 140 os.mkdir(competing_wallet_dir) 141 self.restart_node(0, ['-walletdir=' + competing_wallet_dir]) 142 exp_stderr = "Error: Error initializing wallet database environment \"\S+competing_walletdir\"!" 143 self.nodes[1].assert_start_raises_init_error(['-walletdir=' + competing_wallet_dir], exp_stderr, match=ErrorMatch.PARTIAL_REGEX) 144 145 self.restart_node(0, extra_args) 146 147 assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), ['', os.path.join('sub', 'w5'), 'w', 'w1', 'w2', 'w3', 'w7', 'w7_symlink', 'w8', 'w8_copy']) 148 149 wallets = [wallet(w) for w in wallet_names] 150 wallet_bad = wallet("bad") 151 152 # check wallet names and balances 153 node.generatetoaddress(nblocks=1, address=wallets[0].getnewaddress()) 154 for wallet_name, wallet in zip(wallet_names, wallets): 155 info = wallet.getwalletinfo() 156 assert_equal(info['immature_balance'], 50 if wallet is wallets[0] else 0) 157 assert_equal(info['walletname'], wallet_name) 158 159 # accessing invalid wallet fails 160 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", wallet_bad.getwalletinfo) 161 162 # accessing wallet RPC without using wallet endpoint fails 163 assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo) 164 165 w1, w2, w3, w4, *_ = wallets 166 node.generatetoaddress(nblocks=101, address=w1.getnewaddress()) 167 assert_equal(w1.getbalance(), 100) 168 assert_equal(w2.getbalance(), 0) 169 assert_equal(w3.getbalance(), 0) 170 assert_equal(w4.getbalance(), 0) 171 172 w1.sendtoaddress(w2.getnewaddress(), 1) 173 w1.sendtoaddress(w3.getnewaddress(), 2) 174 w1.sendtoaddress(w4.getnewaddress(), 3) 175 node.generatetoaddress(nblocks=1, address=w1.getnewaddress()) 176 assert_equal(w2.getbalance(), 1) 177 assert_equal(w3.getbalance(), 2) 178 assert_equal(w4.getbalance(), 3) 179 180 batch = w1.batch([w1.getblockchaininfo.get_request(), w1.getwalletinfo.get_request()]) 181 assert_equal(batch[0]["result"]["chain"], "regtest") 182 assert_equal(batch[1]["result"]["walletname"], "w1") 183 184 self.log.info('Check for per-wallet settxfee call') 185 assert_equal(w1.getwalletinfo()['paytxfee'], 0) 186 assert_equal(w2.getwalletinfo()['paytxfee'], 0) 187 w2.settxfee(4.0) 188 assert_equal(w1.getwalletinfo()['paytxfee'], 0) 189 assert_equal(w2.getwalletinfo()['paytxfee'], 4.0) 190 191 self.log.info("Test dynamic wallet loading") 192 193 self.restart_node(0, ['-nowallet']) 194 assert_equal(node.listwallets(), []) 195 assert_raises_rpc_error(-32601, "Method not found", node.getwalletinfo) 196 197 self.log.info("Load first wallet") 198 loadwallet_name = node.loadwallet(wallet_names[0]) 199 assert_equal(loadwallet_name['name'], wallet_names[0]) 200 assert_equal(node.listwallets(), wallet_names[0:1]) 201 node.getwalletinfo() 202 w1 = node.get_wallet_rpc(wallet_names[0]) 203 w1.getwalletinfo() 204 205 self.log.info("Load second wallet") 206 loadwallet_name = node.loadwallet(wallet_names[1]) 207 assert_equal(loadwallet_name['name'], wallet_names[1]) 208 assert_equal(node.listwallets(), wallet_names[0:2]) 209 assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo) 210 w2 = node.get_wallet_rpc(wallet_names[1]) 211 w2.getwalletinfo() 212 213 self.log.info("Load remaining wallets") 214 for wallet_name in wallet_names[2:]: 215 loadwallet_name = self.nodes[0].loadwallet(wallet_name) 216 assert_equal(loadwallet_name['name'], wallet_name) 217 218 assert_equal(set(self.nodes[0].listwallets()), set(wallet_names)) 219 220 # Fail to load if wallet doesn't exist 221 assert_raises_rpc_error(-18, 'Wallet wallets not found.', self.nodes[0].loadwallet, 'wallets') 222 223 # Fail to load duplicate wallets 224 assert_raises_rpc_error(-4, 'Wallet file verification failed: Error loading wallet w1. Duplicate -wallet filename specified.', self.nodes[0].loadwallet, wallet_names[0]) 225 226 # Fail to load duplicate wallets by different ways (directory and filepath) 227 assert_raises_rpc_error(-4, "Wallet file verification failed: Error loading wallet wallet.dat. Duplicate -wallet filename specified.", self.nodes[0].loadwallet, 'wallet.dat') 228 229 # Fail to load if one wallet is a copy of another 230 assert_raises_rpc_error(-1, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy') 231 232 # Fail to load if one wallet is a copy of another, test this twice to make sure that we don't re-introduce #14304 233 assert_raises_rpc_error(-1, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy') 234 235 236 # Fail to load if wallet file is a symlink 237 assert_raises_rpc_error(-4, "Wallet file verification failed: Invalid -wallet path 'w8_symlink'", self.nodes[0].loadwallet, 'w8_symlink') 238 239 # Fail to load if a directory is specified that doesn't contain a wallet 240 os.mkdir(wallet_dir('empty_wallet_dir')) 241 assert_raises_rpc_error(-18, "Directory empty_wallet_dir does not contain a wallet.dat file", self.nodes[0].loadwallet, 'empty_wallet_dir') 242 243 self.log.info("Test dynamic wallet creation.") 244 245 # Fail to create a wallet if it already exists. 246 assert_raises_rpc_error(-4, "Wallet w2 already exists.", self.nodes[0].createwallet, 'w2') 247 248 # Successfully create a wallet with a new name 249 loadwallet_name = self.nodes[0].createwallet('w9') 250 assert_equal(loadwallet_name['name'], 'w9') 251 w9 = node.get_wallet_rpc('w9') 252 assert_equal(w9.getwalletinfo()['walletname'], 'w9') 253 254 assert 'w9' in self.nodes[0].listwallets() 255 256 # Successfully create a wallet using a full path 257 new_wallet_dir = os.path.join(self.options.tmpdir, 'new_walletdir') 258 new_wallet_name = os.path.join(new_wallet_dir, 'w10') 259 loadwallet_name = self.nodes[0].createwallet(new_wallet_name) 260 assert_equal(loadwallet_name['name'], new_wallet_name) 261 w10 = node.get_wallet_rpc(new_wallet_name) 262 assert_equal(w10.getwalletinfo()['walletname'], new_wallet_name) 263 264 assert new_wallet_name in self.nodes[0].listwallets() 265 266 self.log.info("Test dynamic wallet unloading") 267 268 # Test `unloadwallet` errors 269 assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].unloadwallet) 270 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", self.nodes[0].unloadwallet, "dummy") 271 assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", node.get_wallet_rpc("dummy").unloadwallet) 272 assert_raises_rpc_error(-8, "Cannot unload the requested wallet", w1.unloadwallet, "w2"), 273 274 # Successfully unload the specified wallet name 275 self.nodes[0].unloadwallet("w1") 276 assert 'w1' not in self.nodes[0].listwallets() 277 278 # Successfully unload the wallet referenced by the request endpoint 279 # Also ensure unload works during walletpassphrase timeout 280 w2.encryptwallet('test') 281 w2.walletpassphrase('test', 1) 282 w2.unloadwallet() 283 time.sleep(1.1) 284 assert 'w2' not in self.nodes[0].listwallets() 285 286 # Successfully unload all wallets 287 for wallet_name in self.nodes[0].listwallets(): 288 self.nodes[0].unloadwallet(wallet_name) 289 assert_equal(self.nodes[0].listwallets(), []) 290 assert_raises_rpc_error(-32601, "Method not found (wallet method is disabled because no wallet is loaded)", self.nodes[0].getwalletinfo) 291 292 # Successfully load a previously unloaded wallet 293 self.nodes[0].loadwallet('w1') 294 assert_equal(self.nodes[0].listwallets(), ['w1']) 295 assert_equal(w1.getwalletinfo()['walletname'], 'w1') 296 297 assert_equal(sorted(map(lambda w: w['name'], self.nodes[0].listwalletdir()['wallets'])), ['', os.path.join('sub', 'w5'), 'w', 'w1', 'w2', 'w3', 'w7', 'w7_symlink', 'w8', 'w8_copy', 'w9']) 298 299 # Test backing up and restoring wallets 300 self.log.info("Test wallet backup") 301 self.restart_node(0, ['-nowallet']) 302 for wallet_name in wallet_names: 303 self.nodes[0].loadwallet(wallet_name) 304 for wallet_name in wallet_names: 305 rpc = self.nodes[0].get_wallet_rpc(wallet_name) 306 addr = rpc.getnewaddress() 307 backup = os.path.join(self.options.tmpdir, 'backup.dat') 308 rpc.backupwallet(backup) 309 self.nodes[0].unloadwallet(wallet_name) 310 shutil.copyfile(empty_wallet, wallet_file(wallet_name)) 311 self.nodes[0].loadwallet(wallet_name) 312 assert_equal(rpc.getaddressinfo(addr)['ismine'], False) 313 self.nodes[0].unloadwallet(wallet_name) 314 shutil.copyfile(backup, wallet_file(wallet_name)) 315 self.nodes[0].loadwallet(wallet_name) 316 assert_equal(rpc.getaddressinfo(addr)['ismine'], True) 317 318 # Test .walletlock file is closed 319 self.start_node(1) 320 wallet = os.path.join(self.options.tmpdir, 'my_wallet') 321 self.nodes[0].createwallet(wallet) 322 assert_raises_rpc_error(-4, "Error initializing wallet database environment", self.nodes[1].loadwallet, wallet) 323 self.nodes[0].unloadwallet(wallet) 324 self.nodes[1].loadwallet(wallet) 325 326 327if __name__ == '__main__': 328 MultiWalletTest().main() 329