Skip to content

Commit facec1c

Browse files
author
MarcoFalke
committed
wallet: Avoid showing GUI popups on RPC errors
1 parent 94e6e9f commit facec1c

File tree

14 files changed

+91
-98
lines changed

14 files changed

+91
-98
lines changed

src/dummywallet.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <stdio.h>
65
#include <util/system.h>
76
#include <walletinitinterface.h>
87
#include <support/allocators/secure.h>
@@ -71,12 +70,12 @@ std::vector<std::shared_ptr<CWallet>> GetWallets()
7170
throw std::logic_error("Wallet function called in non-wallet build.");
7271
}
7372

74-
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning)
73+
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::vector<std::string>& warnings)
7574
{
7675
throw std::logic_error("Wallet function called in non-wallet build.");
7776
}
7877

79-
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, std::shared_ptr<CWallet>& result)
78+
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::vector<std::string>& warnings, std::shared_ptr<CWallet>& result)
8079
{
8180
throw std::logic_error("Wallet function called in non-wallet build.");
8281
}

src/interfaces/node.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class CWallet;
4343
fs::path GetWalletDir();
4444
std::vector<fs::path> ListWalletDir();
4545
std::vector<std::shared_ptr<CWallet>> GetWallets();
46-
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning);
47-
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, std::shared_ptr<CWallet>& result);
46+
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::vector<std::string>& warnings);
47+
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::vector<std::string>& warnings, std::shared_ptr<CWallet>& result);
4848

4949
namespace interfaces {
5050

@@ -256,14 +256,14 @@ class NodeImpl : public Node
256256
}
257257
return wallets;
258258
}
259-
std::unique_ptr<Wallet> loadWallet(const std::string& name, std::string& error, std::string& warning) override
259+
std::unique_ptr<Wallet> loadWallet(const std::string& name, std::string& error, std::vector<std::string>& warnings) override
260260
{
261-
return MakeWallet(LoadWallet(*m_interfaces.chain, name, error, warning));
261+
return MakeWallet(LoadWallet(*m_interfaces.chain, name, error, warnings));
262262
}
263-
WalletCreationStatus createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, std::unique_ptr<Wallet>& result) override
263+
WalletCreationStatus createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::vector<std::string>& warnings, std::unique_ptr<Wallet>& result) override
264264
{
265265
std::shared_ptr<CWallet> wallet;
266-
WalletCreationStatus status = CreateWallet(*m_interfaces.chain, passphrase, wallet_creation_flags, name, error, warning, wallet);
266+
WalletCreationStatus status = CreateWallet(*m_interfaces.chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
267267
result = MakeWallet(wallet);
268268
return status;
269269
}

src/interfaces/node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ class Node
200200
//! Attempts to load a wallet from file or directory.
201201
//! The loaded wallet is also notified to handlers previously registered
202202
//! with handleLoadWallet.
203-
virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, std::string& error, std::string& warning) = 0;
203+
virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, std::string& error, std::vector<std::string>& warnings) = 0;
204204

205205
//! Create a wallet from file
206-
virtual WalletCreationStatus createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, std::unique_ptr<Wallet>& result) = 0;
206+
virtual WalletCreationStatus createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::vector<std::string>& warnings, std::unique_ptr<Wallet>& result) = 0;
207207

208208
//! Register handler for init messages.
209209
using InitMessageFn = std::function<void(const std::string& message)>;

src/qt/walletcontroller.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <interfaces/handler.h>
1414
#include <interfaces/node.h>
15+
#include <util/string.h>
1516

1617
#include <algorithm>
1718

@@ -226,7 +227,7 @@ void CreateWalletActivity::finish()
226227
if (!m_error_message.empty()) {
227228
QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message));
228229
} else if (!m_warning_message.empty()) {
229-
QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), QString::fromStdString(m_warning_message));
230+
QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), QString::fromStdString(Join(m_warning_message, "\n")));
230231
}
231232

232233
if (m_wallet_model) Q_EMIT created(m_wallet_model);
@@ -267,7 +268,7 @@ void OpenWalletActivity::finish()
267268
if (!m_error_message.empty()) {
268269
QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message));
269270
} else if (!m_warning_message.empty()) {
270-
QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(m_warning_message));
271+
QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(Join(m_warning_message, "\n")));
271272
}
272273

273274
if (m_wallet_model) Q_EMIT opened(m_wallet_model);

src/qt/walletcontroller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class WalletControllerActivity : public QObject
100100
QProgressDialog* m_progress_dialog{nullptr};
101101
WalletModel* m_wallet_model{nullptr};
102102
std::string m_error_message;
103-
std::string m_warning_message;
103+
std::vector<std::string> m_warning_message;
104104
};
105105

106106

src/wallet/db.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ bool BerkeleyBatch::VerifyEnvironment(const fs::path& file_path, std::string& er
412412
return true;
413413
}
414414

415-
bool BerkeleyBatch::VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc)
415+
bool BerkeleyBatch::VerifyDatabaseFile(const fs::path& file_path, std::vector<std::string>& warnings, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc)
416416
{
417417
std::string walletFile;
418418
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, walletFile);
@@ -424,11 +424,11 @@ bool BerkeleyBatch::VerifyDatabaseFile(const fs::path& file_path, std::string& w
424424
BerkeleyEnvironment::VerifyResult r = env->Verify(walletFile, recoverFunc, backup_filename);
425425
if (r == BerkeleyEnvironment::VerifyResult::RECOVER_OK)
426426
{
427-
warningStr = strprintf(_("Warning: Wallet file corrupt, data salvaged!"
427+
warnings.push_back(strprintf(_("Warning: Wallet file corrupt, data salvaged!"
428428
" Original %s saved as %s in %s; if"
429429
" your balance or transactions are incorrect you should"
430430
" restore from a backup.").translated,
431-
walletFile, backup_filename, walletDir);
431+
walletFile, backup_filename, walletDir));
432432
}
433433
if (r == BerkeleyEnvironment::VerifyResult::RECOVER_FAIL)
434434
{

src/wallet/db.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class BerkeleyBatch
246246
/* verifies the database environment */
247247
static bool VerifyEnvironment(const fs::path& file_path, std::string& errorStr);
248248
/* verifies the database file */
249-
static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);
249+
static bool VerifyDatabaseFile(const fs::path& file_path, std::vector<std::string>& warnings, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);
250250

251251
template <typename K, typename T>
252252
bool Read(const K& key, T& value)

src/wallet/load.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <interfaces/chain.h>
99
#include <scheduler.h>
10+
#include <util/string.h>
1011
#include <util/system.h>
1112
#include <util/translation.h>
1213
#include <wallet/wallet.h>
@@ -53,10 +54,10 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
5354
}
5455

5556
std::string error_string;
56-
std::string warning_string;
57-
bool verify_success = CWallet::Verify(chain, location, salvage_wallet, error_string, warning_string);
57+
std::vector<std::string> warnings;
58+
bool verify_success = CWallet::Verify(chain, location, salvage_wallet, error_string, warnings);
5859
if (!error_string.empty()) chain.initError(error_string);
59-
if (!warning_string.empty()) chain.initWarning(warning_string);
60+
if (!warnings.empty()) chain.initWarning(Join(warnings, "\n"));
6061
if (!verify_success) return false;
6162
}
6263

@@ -66,8 +67,12 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
6667
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
6768
{
6869
for (const std::string& walletFile : wallet_files) {
69-
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile));
70+
std::string error;
71+
std::vector<std::string> warnings;
72+
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile), error, warnings);
73+
if (!warnings.empty()) chain.initWarning(Join(warnings, "\n"));
7074
if (!pwallet) {
75+
chain.initError(error);
7176
return false;
7277
}
7378
AddWallet(pwallet);

src/wallet/rpcwallet.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <util/bip32.h>
2323
#include <util/fees.h>
2424
#include <util/moneystr.h>
25+
#include <util/string.h>
2526
#include <util/system.h>
2627
#include <util/url.h>
2728
#include <util/validation.h>
@@ -2587,13 +2588,14 @@ static UniValue loadwallet(const JSONRPCRequest& request)
25872588
}
25882589
}
25892590

2590-
std::string error, warning;
2591+
std::string error;
2592+
std::vector<std::string> warning;
25912593
std::shared_ptr<CWallet> const wallet = LoadWallet(*g_rpc_interfaces->chain, location, error, warning);
25922594
if (!wallet) throw JSONRPCError(RPC_WALLET_ERROR, error);
25932595

25942596
UniValue obj(UniValue::VOBJ);
25952597
obj.pushKV("name", wallet->GetName());
2596-
obj.pushKV("warning", warning);
2598+
obj.pushKV("warning", Join(warning, "\n"));
25972599

25982600
return obj;
25992601
}
@@ -2699,12 +2701,12 @@ static UniValue createwallet(const JSONRPCRequest& request)
26992701
}
27002702
SecureString passphrase;
27012703
passphrase.reserve(100);
2702-
std::string warning;
2704+
std::vector<std::string> warnings;
27032705
if (!request.params[3].isNull()) {
27042706
passphrase = request.params[3].get_str().c_str();
27052707
if (passphrase.empty()) {
27062708
// Empty string means unencrypted
2707-
warning = "Empty string given as passphrase, wallet will not be encrypted.";
2709+
warnings.emplace_back("Empty string given as passphrase, wallet will not be encrypted.");
27082710
}
27092711
}
27102712

@@ -2713,9 +2715,8 @@ static UniValue createwallet(const JSONRPCRequest& request)
27132715
}
27142716

27152717
std::string error;
2716-
std::string create_warning;
27172718
std::shared_ptr<CWallet> wallet;
2718-
WalletCreationStatus status = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, create_warning, wallet);
2719+
WalletCreationStatus status = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, warnings, wallet);
27192720
switch (status) {
27202721
case WalletCreationStatus::CREATION_FAILED:
27212722
throw JSONRPCError(RPC_WALLET_ERROR, error);
@@ -2726,15 +2727,9 @@ static UniValue createwallet(const JSONRPCRequest& request)
27262727
// no default case, so the compiler can warn about missing cases
27272728
}
27282729

2729-
if (warning.empty()) {
2730-
warning = create_warning;
2731-
} else if (!warning.empty() && !create_warning.empty()){
2732-
warning += "; " + create_warning;
2733-
}
2734-
27352730
UniValue obj(UniValue::VOBJ);
27362731
obj.pushKV("name", wallet->GetName());
2737-
obj.pushKV("warning", warning);
2732+
obj.pushKV("warning", Join(warnings, "\n"));
27382733

27392734
return obj;
27402735
}

0 commit comments

Comments
 (0)