Skip to content

Commit a22b624

Browse files
committed
Merge bitcoin#17070: wallet: Avoid showing GUI popups on RPC errors
facec1c wallet: Avoid showing GUI popups on RPC errors (MarcoFalke) Pull request description: RPC errors and warnings are shown as popups in the GUI instead of being returned to the RPC caller. For example, ``` $ ./src/bitcoin-cli loadwallet $(pwd)/./test/functional/data/wallets/high_minversion/ error code: -4 error message: Wallet loading failed. ``` gives me a GUI popup and no reason why loading the wallet failed. After this pull request: ``` $ ./src/bitcoin-cli loadwallet $(pwd)/./test/functional/data/wallets/high_minversion/ error code: -4 error message: Wallet loading failed: Error loading /home/marco/workspace/btc_bitcoin_core/./test/functional/data/wallets/high_minversion/wallet.dat: Wallet requires newer version of Bitcoin Core ACKs for top commit: laanwj: Code review ACK facec1c Tree-SHA512: c8274bbb02cfcf71676eeec1e773e51fb3538cf93f82e7cb8536f4716d44ed819cdc162dfc039ac7386a4db381a734cdb27fd32567043a1180c02519fbcba194
2 parents a75cb12 + facec1c commit a22b624

14 files changed

+91
-97
lines changed

src/dummywallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ std::vector<std::shared_ptr<CWallet>> GetWallets()
7070
throw std::logic_error("Wallet function called in non-wallet build.");
7171
}
7272

73-
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)
7474
{
7575
throw std::logic_error("Wallet function called in non-wallet build.");
7676
}
7777

78-
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)
7979
{
8080
throw std::logic_error("Wallet function called in non-wallet build.");
8181
}

src/interfaces/node.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class CWallet;
4040
fs::path GetWalletDir();
4141
std::vector<fs::path> ListWalletDir();
4242
std::vector<std::shared_ptr<CWallet>> GetWallets();
43-
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning);
44-
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);
43+
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::vector<std::string>& warnings);
44+
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);
4545

4646
namespace interfaces {
4747

@@ -253,14 +253,14 @@ class NodeImpl : public Node
253253
}
254254
return wallets;
255255
}
256-
std::unique_ptr<Wallet> loadWallet(const std::string& name, std::string& error, std::string& warning) override
256+
std::unique_ptr<Wallet> loadWallet(const std::string& name, std::string& error, std::vector<std::string>& warnings) override
257257
{
258-
return MakeWallet(LoadWallet(*m_interfaces.chain, name, error, warning));
258+
return MakeWallet(LoadWallet(*m_interfaces.chain, name, error, warnings));
259259
}
260-
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
260+
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
261261
{
262262
std::shared_ptr<CWallet> wallet;
263-
WalletCreationStatus status = CreateWallet(*m_interfaces.chain, passphrase, wallet_creation_flags, name, error, warning, wallet);
263+
WalletCreationStatus status = CreateWallet(*m_interfaces.chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
264264
result = MakeWallet(wallet);
265265
return status;
266266
}

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
@@ -244,7 +244,7 @@ class BerkeleyBatch
244244
/* verifies the database environment */
245245
static bool VerifyEnvironment(const fs::path& file_path, std::string& errorStr);
246246
/* verifies the database file */
247-
static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);
247+
static bool VerifyDatabaseFile(const fs::path& file_path, std::vector<std::string>& warnings, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);
248248

249249
template <typename K, typename T>
250250
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
@@ -21,6 +21,7 @@
2121
#include <util/bip32.h>
2222
#include <util/fees.h>
2323
#include <util/moneystr.h>
24+
#include <util/string.h>
2425
#include <util/system.h>
2526
#include <util/url.h>
2627
#include <util/validation.h>
@@ -2581,13 +2582,14 @@ static UniValue loadwallet(const JSONRPCRequest& request)
25812582
}
25822583
}
25832584

2584-
std::string error, warning;
2585+
std::string error;
2586+
std::vector<std::string> warning;
25852587
std::shared_ptr<CWallet> const wallet = LoadWallet(*g_rpc_interfaces->chain, location, error, warning);
25862588
if (!wallet) throw JSONRPCError(RPC_WALLET_ERROR, error);
25872589

25882590
UniValue obj(UniValue::VOBJ);
25892591
obj.pushKV("name", wallet->GetName());
2590-
obj.pushKV("warning", warning);
2592+
obj.pushKV("warning", Join(warning, "\n"));
25912593

25922594
return obj;
25932595
}
@@ -2693,12 +2695,12 @@ static UniValue createwallet(const JSONRPCRequest& request)
26932695
}
26942696
SecureString passphrase;
26952697
passphrase.reserve(100);
2696-
std::string warning;
2698+
std::vector<std::string> warnings;
26972699
if (!request.params[3].isNull()) {
26982700
passphrase = request.params[3].get_str().c_str();
26992701
if (passphrase.empty()) {
27002702
// Empty string means unencrypted
2701-
warning = "Empty string given as passphrase, wallet will not be encrypted.";
2703+
warnings.emplace_back("Empty string given as passphrase, wallet will not be encrypted.");
27022704
}
27032705
}
27042706

@@ -2707,9 +2709,8 @@ static UniValue createwallet(const JSONRPCRequest& request)
27072709
}
27082710

27092711
std::string error;
2710-
std::string create_warning;
27112712
std::shared_ptr<CWallet> wallet;
2712-
WalletCreationStatus status = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, create_warning, wallet);
2713+
WalletCreationStatus status = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, warnings, wallet);
27132714
switch (status) {
27142715
case WalletCreationStatus::CREATION_FAILED:
27152716
throw JSONRPCError(RPC_WALLET_ERROR, error);
@@ -2720,15 +2721,9 @@ static UniValue createwallet(const JSONRPCRequest& request)
27202721
// no default case, so the compiler can warn about missing cases
27212722
}
27222723

2723-
if (warning.empty()) {
2724-
warning = create_warning;
2725-
} else if (!warning.empty() && !create_warning.empty()){
2726-
warning += "; " + create_warning;
2727-
}
2728-
27292724
UniValue obj(UniValue::VOBJ);
27302725
obj.pushKV("name", wallet->GetName());
2731-
obj.pushKV("warning", warning);
2726+
obj.pushKV("warning", Join(warnings, "\n"));
27322727

27332728
return obj;
27342729
}

0 commit comments

Comments
 (0)