diff options
author | w0xlt <94266259+w0xlt@users.noreply.github.com> | 2021-12-14 20:49:11 -0300 |
---|---|---|
committer | w0xlt <94266259+w0xlt@users.noreply.github.com> | 2021-12-15 18:41:40 -0300 |
commit | 62fa61fa4a33ff4d108a65d656ffe2cac4330824 (patch) | |
tree | 1ba573882ef6bce71e536396e971eae16326f940 | |
parent | abbb7eccef3fc1c36f34756458d2792f6661e29f (diff) | |
download | bitcoin-62fa61fa4a33ff4d108a65d656ffe2cac4330824.tar.xz |
refactor: remove the wallet folder if the restore fails
-rw-r--r-- | src/wallet/wallet.cpp | 9 | ||||
-rwxr-xr-x | test/functional/wallet_backup.py | 16 |
2 files changed, 24 insertions, 1 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f2d1acb71f..1c61d7e981 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -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 diff --git a/test/functional/wallet_backup.py b/test/functional/wallet_backup.py index 4bcfc3aa71..292fe3a310 100755 --- a/test/functional/wallet_backup.py +++ b/test/functional/wallet_backup.py @@ -110,6 +110,16 @@ class WalletBackupTest(BitcoinTestFramework): 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') @@ -125,6 +135,7 @@ class WalletBackupTest(BitcoinTestFramework): 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) @@ -181,6 +192,7 @@ class WalletBackupTest(BitcoinTestFramework): ## 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') @@ -191,6 +203,10 @@ class WalletBackupTest(BitcoinTestFramework): 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") |