Changeset View
Changeset View
Standalone View
Standalone View
test/functional/wallet_multiwallet.py
Show All 25 Lines | class MultiWalletTest(BitcoinTestFramework): | ||||
def run_test(self): | def run_test(self): | ||||
node = self.nodes[0] | node = self.nodes[0] | ||||
data_dir = lambda *p: os.path.join(node.datadir, 'regtest', *p) | data_dir = lambda *p: os.path.join(node.datadir, 'regtest', *p) | ||||
wallet_dir = lambda *p: data_dir('wallets', *p) | wallet_dir = lambda *p: data_dir('wallets', *p) | ||||
def wallet(name): return node.get_wallet_rpc(name) | def wallet(name): return node.get_wallet_rpc(name) | ||||
def wallet_file(name): | |||||
if os.path.isdir(wallet_dir(name)): | |||||
return wallet_dir(name, "wallet.dat") | |||||
return wallet_dir(name) | |||||
# check wallet.dat is created | # check wallet.dat is created | ||||
self.stop_nodes() | self.stop_nodes() | ||||
assert_equal(os.path.isfile(wallet_dir('wallet.dat')), True) | assert_equal(os.path.isfile(wallet_dir('wallet.dat')), True) | ||||
# create symlink to verify wallet directory path can be referenced | # create symlink to verify wallet directory path can be referenced | ||||
# through symlink | # through symlink | ||||
os.mkdir(wallet_dir('w7')) | os.mkdir(wallet_dir('w7')) | ||||
os.symlink('w7', wallet_dir('w7_symlink')) | os.symlink('w7', wallet_dir('w7_symlink')) | ||||
# rename wallet.dat to make sure plain wallet file paths (as opposed to | # rename wallet.dat to make sure plain wallet file paths (as opposed to | ||||
# directory paths) can be loaded | # directory paths) can be loaded | ||||
os.rename(wallet_dir("wallet.dat"), wallet_dir("w8")) | os.rename(wallet_dir("wallet.dat"), wallet_dir("w8")) | ||||
# create another dummy wallet for use in testing backups later | |||||
self.start_node(0, []) | |||||
self.stop_nodes() | |||||
empty_wallet = os.path.join(self.options.tmpdir, 'empty.dat') | |||||
os.rename(wallet_dir("wallet.dat"), empty_wallet) | |||||
# restart node with a mix of wallet names: | # restart node with a mix of wallet names: | ||||
# w1, w2, w3 - to verify new wallets created when non-existing paths specified | # w1, w2, w3 - to verify new wallets created when non-existing paths specified | ||||
# w - to verify wallet name matching works when one wallet path is prefix of another | # w - to verify wallet name matching works when one wallet path is prefix of another | ||||
# sub/w5 - to verify relative wallet path is created correctly | # sub/w5 - to verify relative wallet path is created correctly | ||||
# extern/w6 - to verify absolute wallet path is created correctly | # extern/w6 - to verify absolute wallet path is created correctly | ||||
# w7_symlink - to verify symlinked wallet path is initialized correctly | # w7_symlink - to verify symlinked wallet path is initialized correctly | ||||
# w8 - to verify existing wallet file is loaded correctly | # w8 - to verify existing wallet file is loaded correctly | ||||
# '' - to verify default wallet file is created correctly | # '' - to verify default wallet file is created correctly | ||||
wallet_names = ['w1', 'w2', 'w3', 'w', 'sub/w5', | wallet_names = ['w1', 'w2', 'w3', 'w', 'sub/w5', | ||||
os.path.join(self.options.tmpdir, 'extern/w6'), 'w7_symlink', 'w8', ''] | os.path.join(self.options.tmpdir, 'extern/w6'), 'w7_symlink', 'w8', ''] | ||||
extra_args = ['-wallet={}'.format(n) for n in wallet_names] | extra_args = ['-wallet={}'.format(n) for n in wallet_names] | ||||
self.start_node(0, extra_args) | self.start_node(0, extra_args) | ||||
assert_equal(set(node.listwallets()), set(wallet_names)) | assert_equal(set(node.listwallets()), set(wallet_names)) | ||||
# check that all requested wallets were created | # check that all requested wallets were created | ||||
self.stop_node(0) | self.stop_node(0) | ||||
for wallet_name in wallet_names: | for wallet_name in wallet_names: | ||||
if os.path.isdir(wallet_dir(wallet_name)): | assert_equal(os.path.isfile(wallet_file(wallet_name)), True) | ||||
assert_equal(os.path.isfile( | |||||
wallet_dir(wallet_name, "wallet.dat")), True) | |||||
else: | |||||
assert_equal(os.path.isfile(wallet_dir(wallet_name)), True) | |||||
# should not initialize if wallet path can't be created | # should not initialize if wallet path can't be created | ||||
exp_stderr = "boost::filesystem::create_directory: (The system cannot find the path specified|Not a directory):" | exp_stderr = "boost::filesystem::create_directory: (The system cannot find the path specified|Not a directory):" | ||||
self.nodes[0].assert_start_raises_init_error( | self.nodes[0].assert_start_raises_init_error( | ||||
['-wallet=wallet.dat/bad'], exp_stderr, match=ErrorMatch.PARTIAL_REGEX) | ['-wallet=wallet.dat/bad'], exp_stderr, match=ErrorMatch.PARTIAL_REGEX) | ||||
self.nodes[0].assert_start_raises_init_error( | self.nodes[0].assert_start_raises_init_error( | ||||
['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist') | ['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist') | ||||
▲ Show 20 Lines • Show All 201 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
assert_raises_rpc_error(-32601, "Method not found (wallet method is disabled because no wallet is loaded)", | assert_raises_rpc_error(-32601, "Method not found (wallet method is disabled because no wallet is loaded)", | ||||
self.nodes[0].getwalletinfo) | self.nodes[0].getwalletinfo) | ||||
# Successfully load a previously unloaded wallet | # Successfully load a previously unloaded wallet | ||||
self.nodes[0].loadwallet('w1') | self.nodes[0].loadwallet('w1') | ||||
assert_equal(self.nodes[0].listwallets(), ['w1']) | assert_equal(self.nodes[0].listwallets(), ['w1']) | ||||
assert_equal(w1.getwalletinfo()['walletname'], 'w1') | assert_equal(w1.getwalletinfo()['walletname'], 'w1') | ||||
# Test backing up and restoring wallets | |||||
self.log.info("Test wallet backup") | |||||
self.restart_node(0, ['-nowallet']) | |||||
for wallet_name in wallet_names: | |||||
self.nodes[0].loadwallet(wallet_name) | |||||
for wallet_name in wallet_names: | |||||
rpc = self.nodes[0].get_wallet_rpc(wallet_name) | |||||
addr = rpc.getnewaddress() | |||||
backup = os.path.join(self.options.tmpdir, 'backup.dat') | |||||
rpc.backupwallet(backup) | |||||
self.nodes[0].unloadwallet(wallet_name) | |||||
shutil.copyfile(empty_wallet, wallet_file(wallet_name)) | |||||
self.nodes[0].loadwallet(wallet_name) | |||||
assert_equal(rpc.getaddressinfo(addr)['ismine'], False) | |||||
self.nodes[0].unloadwallet(wallet_name) | |||||
shutil.copyfile(backup, wallet_file(wallet_name)) | |||||
self.nodes[0].loadwallet(wallet_name) | |||||
assert_equal(rpc.getaddressinfo(addr)['ismine'], True) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
MultiWalletTest().main() | MultiWalletTest().main() |