From ee9e88ba2734b81d0ffe23fd45c4f69a970c6494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Sat, 28 Mar 2020 02:14:08 +0000 Subject: [PATCH] wallet: Handle duplicate fileid exception --- src/wallet/load.cpp | 26 +++++++++++++++----------- src/wallet/wallet.cpp | 25 +++++++++++++++---------- test/functional/wallet_multiwallet.py | 4 ++-- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp index 3e92c07d64a..d6e44c7be50 100644 --- a/src/wallet/load.cpp +++ b/src/wallet/load.cpp @@ -66,19 +66,23 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector& wal bool LoadWallets(interfaces::Chain& chain, const std::vector& wallet_files) { - for (const std::string& walletFile : wallet_files) { - std::string error; - std::vector warnings; - std::shared_ptr pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile), error, warnings); - if (!warnings.empty()) chain.initWarning(Join(warnings, "\n")); - if (!pwallet) { - chain.initError(error); - return false; + try { + for (const std::string& walletFile : wallet_files) { + std::string error; + std::vector warnings; + std::shared_ptr pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile), error, warnings); + if (!warnings.empty()) chain.initWarning(Join(warnings, "\n")); + if (!pwallet) { + chain.initError(error); + return false; + } + AddWallet(pwallet); } - AddWallet(pwallet); + return true; + } catch (const std::runtime_error& e) { + chain.initError(e.what()); + return false; } - - return true; } void StartWallets(CScheduler& scheduler) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 98f308f927f..bc9f84a11da 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -148,19 +148,24 @@ void UnloadWallet(std::shared_ptr&& wallet) std::shared_ptr LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::vector& warnings) { - if (!CWallet::Verify(chain, location, false, error, warnings)) { - error = "Wallet file verification failed: " + error; - return nullptr; - } + try { + if (!CWallet::Verify(chain, location, false, error, warnings)) { + error = "Wallet file verification failed: " + error; + return nullptr; + } - std::shared_ptr wallet = CWallet::CreateWalletFromFile(chain, location, error, warnings); - if (!wallet) { - error = "Wallet loading failed: " + error; + std::shared_ptr wallet = CWallet::CreateWalletFromFile(chain, location, error, warnings); + if (!wallet) { + error = "Wallet loading failed: " + error; + return nullptr; + } + AddWallet(wallet); + wallet->postInitProcess(); + return wallet; + } catch (const std::runtime_error& e) { + error = e.what(); return nullptr; } - AddWallet(wallet); - wallet->postInitProcess(); - return wallet; } std::shared_ptr LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::vector& warnings) diff --git a/test/functional/wallet_multiwallet.py b/test/functional/wallet_multiwallet.py index 78ead514a5e..a2c502f280c 100755 --- a/test/functional/wallet_multiwallet.py +++ b/test/functional/wallet_multiwallet.py @@ -236,10 +236,10 @@ class MultiWalletTest(BitcoinTestFramework): assert_raises_rpc_error(-4, "Wallet file verification failed: Error loading wallet wallet.dat. Duplicate -wallet filename specified.", self.nodes[0].loadwallet, 'wallet.dat') # Fail to load if one wallet is a copy of another - assert_raises_rpc_error(-1, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy') + assert_raises_rpc_error(-4, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy') # Fail to load if one wallet is a copy of another, test this twice to make sure that we don't re-introduce #14304 - assert_raises_rpc_error(-1, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy') + assert_raises_rpc_error(-4, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy') # Fail to load if wallet file is a symlink