diff options
author | João Barbosa <joao.paulo.barbosa@gmail.com> | 2022-03-25 22:41:21 +0000 |
---|---|---|
committer | João Barbosa <joao.paulo.barbosa@gmail.com> | 2022-03-25 22:47:29 +0000 |
commit | f59959e3818692c5b3c2dfa51c14e515085e940f (patch) | |
tree | 4a12506f5469cedbbc8c0cc825c8b1870ea7aa1e /src | |
parent | 2f0f056e08cd5a1435120592a9ecd212fcdb915b (diff) |
wallet: Prevent wallet unload on GetWalletForJSONRPCRequest
Don't extend shared ownership of all wallets to GetWalletForJSONRPCRequest scope.
Diffstat (limited to 'src')
-rw-r--r-- | src/wallet/rpc/util.cpp | 9 | ||||
-rw-r--r-- | src/wallet/wallet.cpp | 7 | ||||
-rw-r--r-- | src/wallet/wallet.h | 1 |
3 files changed, 12 insertions, 5 deletions
diff --git a/src/wallet/rpc/util.cpp b/src/wallet/rpc/util.cpp index 59683c5fd8..f788d0d2d3 100644 --- a/src/wallet/rpc/util.cpp +++ b/src/wallet/rpc/util.cpp @@ -64,12 +64,11 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques return pwallet; } - std::vector<std::shared_ptr<CWallet>> wallets = GetWallets(context); - if (wallets.size() == 1) { - return wallets[0]; - } + size_t count{0}; + auto wallet = GetDefaultWallet(context, count); + if (wallet) return wallet; - if (wallets.empty()) { + if (count == 0) { throw JSONRPCError( 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)"); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index be64b4cdbb..96b9a69d45 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -151,6 +151,13 @@ std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context) return context.wallets; } +std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count) +{ + LOCK(context.wallets_mutex); + count = context.wallets.size(); + return count == 1 ? context.wallets[0] : nullptr; +} + std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name) { LOCK(context.wallets_mutex); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 0490d321ab..686e102294 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -62,6 +62,7 @@ bool AddWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet); bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start, std::vector<bilingual_str>& warnings); bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start); std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context); +std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count); std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name); 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); 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); |