Skip to content

Commit cb552c5

Browse files
committed
Merge bitcoin/bitcoin#26192: rpc: Improve error when wallet is already loaded
0460928 rpc: Improve error when wallet is already loaded (Aurèle Oulès) Pull request description: Currently, trying to load a descriptor (sqlite) wallet that is already loaded throws the following error: > error code: -4 > error message: > Wallet file verification failed. SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of Bitcoin Core? I don't think it is very clear what it means for a user. While a legacy wallet would throw: > error code: -35 > error message: > Wallet file verification failed. Refusing to load database. Data file '/home/user/.bitcoin/signet/wallets/test_wallet/wallet.dat' is already loaded. This PR changes the error message for both types of wallet to: > error code: -35 > error message: > Wallet file verification failed. Wallet "test_wallet" is already loaded. ACKs for top commit: achow101: ACK 0460928 hernanmarino: ACK 0460928 theStack: Tested ACK 0460928 Tree-SHA512: a8f3d5133bfaef7417a6c05d160910ea08f32ac62bfdf7f5ec305ff5b62e9113b55f385abab4d5a4ad711aabcb1eb7ef746eb41f841b196e8fb5393ab3ccc01e
2 parents 65d7c31 + 0460928 commit cb552c5

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
@@ -303,12 +303,8 @@ def wallet_file(name):
303303
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')
304304

305305
# Fail to load duplicate wallets
306-
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "w1", "wallet.dat")
307-
if self.options.descriptors:
308-
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])
309-
else:
310-
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])
311-
306+
assert_raises_rpc_error(-35, "Wallet \"w1\" is already loaded.", self.nodes[0].loadwallet, wallet_names[0])
307+
if not self.options.descriptors:
312308
# This tests the default wallet that BDB makes, so SQLite wallet doesn't need to test this
313309
# Fail to load duplicate wallets by different ways (directory and filepath)
314310
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "wallet.dat")

0 commit comments

Comments
 (0)