Skip to content

Commit d63ca8a

Browse files
committed
merge bitcoin#24678: Prevent wallet unload on GetWalletForJSONRPCRequest
1 parent 2a880d8 commit d63ca8a

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/wallet/rpc/util.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques
6464
return pwallet;
6565
}
6666

67-
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets(context);
68-
if (wallets.size() == 1) {
69-
return wallets[0];
70-
}
67+
size_t count{0};
68+
auto wallet = GetDefaultWallet(context, count);
69+
if (wallet) return wallet;
7170

72-
if (wallets.empty()) {
71+
if (count == 0) {
7372
throw JSONRPCError(
7473
RPC_WALLET_NOT_FOUND, "No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)");
7574
}

src/wallet/wallet.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context)
166166
return context.wallets;
167167
}
168168

169+
std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count)
170+
{
171+
LOCK(context.wallets_mutex);
172+
count = context.wallets.size();
173+
return count == 1 ? context.wallets[0] : nullptr;
174+
}
175+
169176
std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name)
170177
{
171178
LOCK(context.wallets_mutex);

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ bool AddWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet);
7070
bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start, std::vector<bilingual_str>& warnings);
7171
bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start);
7272
std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context);
73+
std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count);
7374
std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name);
7475
std::shared_ptr<CWallet> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
7576
std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);

0 commit comments

Comments
 (0)