Skip to content

Commit f59959e

Browse files
committed
wallet: Prevent wallet unload on GetWalletForJSONRPCRequest
Don't extend shared ownership of all wallets to GetWalletForJSONRPCRequest scope.
1 parent 2f0f056 commit f59959e

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

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

src/wallet/wallet.h

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