Skip to content

Commit 0460928

Browse files
committed
rpc: Improve error when wallet is already loaded
1 parent 9fcdb9f commit 0460928

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/wallet/rpc/wallet.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ static RPCHelpMan loadwallet()
226226
bilingual_str error;
227227
std::vector<bilingual_str> warnings;
228228
std::optional<bool> load_on_start = request.params[1].isNull() ? std::nullopt : std::optional<bool>(request.params[1].get_bool());
229+
230+
{
231+
LOCK(context.wallets_mutex);
232+
if (std::any_of(context.wallets.begin(), context.wallets.end(), [&name](const auto& wallet) { return wallet->GetName() == name; })) {
233+
throw JSONRPCError(RPC_WALLET_ALREADY_LOADED, "Wallet \"" + name + "\" is already loaded.");
234+
}
235+
}
236+
229237
std::shared_ptr<CWallet> const wallet = LoadWallet(context, name, load_on_start, options, status, error, warnings);
230238

231239
HandleWalletError(wallet, status, error);

test/functional/wallet_multiwallet.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,8 @@ def wallet_file(name):
302302
assert_raises_rpc_error(-18, "Wallet file verification failed. Failed to load database path '{}'. Path does not exist.".format(path), self.nodes[0].loadwallet, 'wallets')
303303

304304
# Fail to load duplicate wallets
305-
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "w1", "wallet.dat")
306-
if self.options.descriptors:
307-
assert_raises_rpc_error(-4, f"Wallet file verification failed. SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of {self.config['environment']['PACKAGE_NAME']}?", self.nodes[0].loadwallet, wallet_names[0])
308-
else:
309-
assert_raises_rpc_error(-35, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, wallet_names[0])
310-
305+
assert_raises_rpc_error(-35, "Wallet \"w1\" is already loaded.", self.nodes[0].loadwallet, wallet_names[0])
306+
if not self.options.descriptors:
311307
# This tests the default wallet that BDB makes, so SQLite wallet doesn't need to test this
312308
# Fail to load duplicate wallets by different ways (directory and filepath)
313309
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "wallet.dat")

0 commit comments

Comments
 (0)