Skip to content

Commit 0ae0aa2

Browse files
committed
Merge bitcoin#24678: Prevent wallet unload on GetWalletForJSONRPCRequest
f59959e wallet: Prevent wallet unload on GetWalletForJSONRPCRequest (João Barbosa) Pull request description: Don't extend shared ownership of all wallets to `GetWalletForJSONRPCRequest` scope. ACKs for top commit: achow101: ACK f59959e shaavan: Code Review ACK f59959e theStack: Concept and code-review ACK f59959e Tree-SHA512: 7c0294098b5c32acaab8cc6fcf17a581d580ad1a557ba0602a9506074ac035815739afb4a25b3e61be9132535c7fc3ec7ef5137c1dfc9d4078f13663d508ef55
2 parents 95d4744 + f59959e commit 0ae0aa2

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
@@ -149,6 +149,13 @@ std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context)
149149
return context.wallets;
150150
}
151151

152+
std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count)
153+
{
154+
LOCK(context.wallets_mutex);
155+
count = context.wallets.size();
156+
return count == 1 ? context.wallets[0] : nullptr;
157+
}
158+
152159
std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name)
153160
{
154161
LOCK(context.wallets_mutex);

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ bool AddWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet);
6464
bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start, std::vector<bilingual_str>& warnings);
6565
bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start);
6666
std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context);
67+
std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count);
6768
std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name);
6869
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);
6970
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)