Skip to content

Commit ee9e88b

Browse files
committed
wallet: Handle duplicate fileid exception
1 parent 9a2b5f2 commit ee9e88b

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

src/wallet/load.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,23 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
6666

6767
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
6868
{
69-
for (const std::string& walletFile : wallet_files) {
70-
std::string error;
71-
std::vector<std::string> warnings;
72-
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile), error, warnings);
73-
if (!warnings.empty()) chain.initWarning(Join(warnings, "\n"));
74-
if (!pwallet) {
75-
chain.initError(error);
76-
return false;
69+
try {
70+
for (const std::string& walletFile : wallet_files) {
71+
std::string error;
72+
std::vector<std::string> warnings;
73+
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile), error, warnings);
74+
if (!warnings.empty()) chain.initWarning(Join(warnings, "\n"));
75+
if (!pwallet) {
76+
chain.initError(error);
77+
return false;
78+
}
79+
AddWallet(pwallet);
7780
}
78-
AddWallet(pwallet);
81+
return true;
82+
} catch (const std::runtime_error& e) {
83+
chain.initError(e.what());
84+
return false;
7985
}
80-
81-
return true;
8286
}
8387

8488
void StartWallets(CScheduler& scheduler)

src/wallet/wallet.cpp

+15-10
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,24 @@ void UnloadWallet(std::shared_ptr<CWallet>&& wallet)
148148

149149
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::vector<std::string>& warnings)
150150
{
151-
if (!CWallet::Verify(chain, location, false, error, warnings)) {
152-
error = "Wallet file verification failed: " + error;
153-
return nullptr;
154-
}
151+
try {
152+
if (!CWallet::Verify(chain, location, false, error, warnings)) {
153+
error = "Wallet file verification failed: " + error;
154+
return nullptr;
155+
}
155156

156-
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location, error, warnings);
157-
if (!wallet) {
158-
error = "Wallet loading failed: " + error;
157+
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location, error, warnings);
158+
if (!wallet) {
159+
error = "Wallet loading failed: " + error;
160+
return nullptr;
161+
}
162+
AddWallet(wallet);
163+
wallet->postInitProcess();
164+
return wallet;
165+
} catch (const std::runtime_error& e) {
166+
error = e.what();
159167
return nullptr;
160168
}
161-
AddWallet(wallet);
162-
wallet->postInitProcess();
163-
return wallet;
164169
}
165170

166171
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::vector<std::string>& warnings)

test/functional/wallet_multiwallet.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ def wallet_file(name):
236236
assert_raises_rpc_error(-4, "Wallet file verification failed: Error loading wallet wallet.dat. Duplicate -wallet filename specified.", self.nodes[0].loadwallet, 'wallet.dat')
237237

238238
# Fail to load if one wallet is a copy of another
239-
assert_raises_rpc_error(-1, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
239+
assert_raises_rpc_error(-4, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
240240

241241
# Fail to load if one wallet is a copy of another, test this twice to make sure that we don't re-introduce #14304
242-
assert_raises_rpc_error(-1, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
242+
assert_raises_rpc_error(-4, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
243243

244244

245245
# Fail to load if wallet file is a symlink

0 commit comments

Comments
 (0)