Skip to content

Commit

Permalink
refactor: remove the wallet folder if the restore fails
Browse files Browse the repository at this point in the history
  • Loading branch information
w0xlt committed Dec 15, 2021
1 parent abbb7ec commit 62fa61f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/wallet/wallet.cpp
Expand Up @@ -379,7 +379,14 @@ std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const std::string
auto wallet_file = wallet_path / "wallet.dat";
fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);

return LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
auto wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);

if (!wallet) {
fs::remove(wallet_file);
fs::remove(wallet_path);
}

return wallet;
}

/** @defgroup mapWallet
Expand Down
16 changes: 16 additions & 0 deletions test/functional/wallet_backup.py
Expand Up @@ -110,6 +110,16 @@ def erase_three(self):
os.remove(os.path.join(self.nodes[1].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
os.remove(os.path.join(self.nodes[2].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))

def restore_invalid_wallet(self):
node = self.nodes[3]
invalid_wallet_file = os.path.join(self.nodes[0].datadir, 'invalid_wallet_file.bak')
open(invalid_wallet_file, 'a', encoding="utf8").write('invald wallet')
wallet_name = "res0"
not_created_wallet_file = os.path.join(node.datadir, self.chain, 'wallets', wallet_name)
error_message = "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(not_created_wallet_file)
assert_raises_rpc_error(-18, error_message, node.restorewallet, wallet_name, invalid_wallet_file)
assert not os.path.exists(not_created_wallet_file)

def restore_nonexistent_wallet(self):
node = self.nodes[3]
nonexistent_wallet_file = os.path.join(self.nodes[0].datadir, 'nonexistent_wallet.bak')
Expand All @@ -125,6 +135,7 @@ def restore_wallet_existent_name(self):
wallet_file = os.path.join(node.datadir, self.chain, 'wallets', wallet_name)
error_message = "Failed to create database path '{}'. Database already exists.".format(wallet_file)
assert_raises_rpc_error(-36, error_message, node.restorewallet, wallet_name, backup_file)
assert os.path.exists(wallet_file)

def init_three(self):
self.init_wallet(node=0)
Expand Down Expand Up @@ -181,6 +192,7 @@ def run_test(self):
##
self.log.info("Restoring wallets on node 3 using backup files")

self.restore_invalid_wallet()
self.restore_nonexistent_wallet()

backup_file_0 = os.path.join(self.nodes[0].datadir, 'wallet.bak')
Expand All @@ -191,6 +203,10 @@ def run_test(self):
self.nodes[3].restorewallet("res1", backup_file_1)
self.nodes[3].restorewallet("res2", backup_file_2)

assert os.path.exists(os.path.join(self.nodes[3].datadir, self.chain, 'wallets', "res0"))
assert os.path.exists(os.path.join(self.nodes[3].datadir, self.chain, 'wallets', "res1"))
assert os.path.exists(os.path.join(self.nodes[3].datadir, self.chain, 'wallets', "res2"))

res0_rpc = self.nodes[3].get_wallet_rpc("res0")
res1_rpc = self.nodes[3].get_wallet_rpc("res1")
res2_rpc = self.nodes[3].get_wallet_rpc("res2")
Expand Down

0 comments on commit 62fa61f

Please sign in to comment.