From 17abc0fd52510bfeb85a7b5be04001075ec78b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Sat, 12 Jan 2019 11:47:04 +0000 Subject: [PATCH 1/8] wallet: Factor out LoadWallet --- src/dummywallet.cpp | 9 +++++++++ src/wallet/rpcwallet.cpp | 16 +++------------- src/wallet/wallet.cpp | 22 ++++++++++++++++++++++ src/wallet/wallet.h | 1 + 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/dummywallet.cpp b/src/dummywallet.cpp index 9211a7596..8a76021a5 100644 --- a/src/dummywallet.cpp +++ b/src/dummywallet.cpp @@ -8,6 +8,10 @@ class CWallet; +namespace interfaces { +class Chain; +} + class DummyWalletInit : public WalletInitInterface { public: @@ -43,6 +47,11 @@ std::vector> GetWallets() throw std::logic_error("Wallet function called in non-wallet build."); } +std::shared_ptr LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning) +{ + throw std::logic_error("Wallet function called in non-wallet build."); +} + namespace interfaces { class Wallet; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c96a9b0af..38942e9fc 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2544,7 +2544,6 @@ static UniValue loadwallet(const JSONRPCRequest& request) }.ToString()); WalletLocation location(request.params[0].get_str()); - std::string error; if (!location.Exists()) { throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Wallet " + location.GetName() + " not found."); @@ -2556,18 +2555,9 @@ static UniValue loadwallet(const JSONRPCRequest& request) } } - std::string warning; - if (!CWallet::Verify(*g_rpc_interfaces->chain, location, false, error, warning)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet file verification failed: " + error); - } - - std::shared_ptr const wallet = CWallet::CreateWalletFromFile(*g_rpc_interfaces->chain, location); - if (!wallet) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet loading failed."); - } - AddWallet(wallet); - - wallet->postInitProcess(); + std::string error, warning; + std::shared_ptr const wallet = LoadWallet(*g_rpc_interfaces->chain, location, error, warning); + if (!wallet) throw JSONRPCError(RPC_WALLET_ERROR, error); UniValue obj(UniValue::VOBJ); obj.pushKV("name", wallet->GetName()); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 9b643be69..2a39b5474 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -130,6 +130,28 @@ void UnloadWallet(std::shared_ptr&& wallet) } } +std::shared_ptr LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::string& warning) +{ + if (!CWallet::Verify(chain, location, false, error, warning)) { + error = "Wallet file verification failed: " + error; + return nullptr; + } + + std::shared_ptr wallet = CWallet::CreateWalletFromFile(chain, location); + if (!wallet) { + error = "Wallet loading failed."; + return nullptr; + } + AddWallet(wallet); + wallet->postInitProcess(); + return wallet; +} + +std::shared_ptr LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning) +{ + return LoadWallet(chain, WalletLocation(name), error, warning); +} + const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000; const uint256 CMerkleTx::ABANDON_HASH(uint256S("0000000000000000000000000000000000000000000000000000000000000001")); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 4776b0eac..4727ddeda 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -66,6 +66,7 @@ bool RemoveWallet(const std::shared_ptr& wallet); bool HasWallets(); std::vector> GetWallets(); std::shared_ptr GetWallet(const std::string& name); +std::shared_ptr LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::string& warning); //! Default for -keypool static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000; From ab288b4e59c52905343a1de628735b5c871d79a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Sat, 12 Jan 2019 11:36:38 +0000 Subject: [PATCH 2/8] interfaces: Add loadWallet to Node --- src/interfaces/node.cpp | 5 +++++ src/interfaces/node.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/interfaces/node.cpp b/src/interfaces/node.cpp index c574f960e..f3e990fa4 100644 --- a/src/interfaces/node.cpp +++ b/src/interfaces/node.cpp @@ -42,6 +42,7 @@ class CWallet; fs::path GetWalletDir(); std::vector ListWalletDir(); std::vector> GetWallets(); +std::shared_ptr LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning); namespace interfaces { @@ -252,6 +253,10 @@ public: } return wallets; } + std::unique_ptr loadWallet(const std::string& name, std::string& error, std::string& warning) override + { + return MakeWallet(LoadWallet(*m_interfaces.chain, name, error, warning)); + } std::unique_ptr handleInitMessage(InitMessageFn fn) override { return MakeHandler(::uiInterface.InitMessage_connect(fn)); diff --git a/src/interfaces/node.h b/src/interfaces/node.h index 54c2d7833..76b93af23 100644 --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -192,6 +192,11 @@ public: //! Return interfaces for accessing wallets (if any). virtual std::vector> getWallets() = 0; + //! Attempts to load a wallet from file or directory. + //! The loaded wallet is also notified to handlers previously registered + //! with handleLoadWallet. + virtual std::unique_ptr loadWallet(const std::string& name, std::string& error, std::string& warning) = 0; + //! Register handler for init messages. using InitMessageFn = std::function; virtual std::unique_ptr handleInitMessage(InitMessageFn fn) = 0; From 32a8c6abfe32d5b89bf4505ca8397bdb44867614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Sat, 12 Jan 2019 11:34:05 +0000 Subject: [PATCH 3/8] gui: Add openWallet and getWalletsAvailableToOpen to WalletController --- src/qt/walletcontroller.cpp | 25 +++++++++++++++++++++++++ src/qt/walletcontroller.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index df2b7a3f9..0730afca9 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -9,6 +9,7 @@ #include +#include #include #include @@ -37,6 +38,30 @@ std::vector WalletController::getWallets() const return m_wallets; } +std::vector WalletController::getWalletsAvailableToOpen() const +{ + QMutexLocker locker(&m_mutex); + std::vector wallets = m_node.listWalletDir(); + for (WalletModel* wallet_model : m_wallets) { + auto it = std::remove(wallets.begin(), wallets.end(), wallet_model->wallet().getWalletName()); + if (it != wallets.end()) wallets.erase(it); + } + return wallets; +} + +WalletModel* WalletController::openWallet(const std::string& name, QWidget* parent) +{ + std::string error, warning; + WalletModel* wallet_model = getOrCreateWallet(m_node.loadWallet(name, error, warning)); + if (!wallet_model) { + QMessageBox::warning(parent, tr("Open Wallet"), QString::fromStdString(error)); + } + if (!warning.empty()) { + QMessageBox::information(parent, tr("Open Wallet"), QString::fromStdString(warning)); + } + return wallet_model; +} + WalletModel* WalletController::getOrCreateWallet(std::unique_ptr wallet) { QMutexLocker locker(&m_mutex); diff --git a/src/qt/walletcontroller.h b/src/qt/walletcontroller.h index 22b71b07f..523e47d27 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -37,6 +37,9 @@ public: ~WalletController(); std::vector getWallets() const; + std::vector getWalletsAvailableToOpen() const; + + WalletModel* openWallet(const std::string& name, QWidget* parent = nullptr); private Q_SLOTS: void addWallet(WalletModel* wallet_model); From 6c49a55b472fa4ec403e48ec718af85bfd402199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Sat, 12 Jan 2019 11:40:30 +0000 Subject: [PATCH 4/8] gui: Add Open Wallet menu --- src/qt/bitcoingui.cpp | 16 ++++++++++++++++ src/qt/bitcoingui.h | 1 + 2 files changed, 17 insertions(+) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index ba7e8c7da..e7279e122 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -334,6 +334,10 @@ void BitcoinGUI::createActions() openAction = new QAction(platformStyle->TextColorIcon(":/icons/open"), tr("Open &URI..."), this); openAction->setStatusTip(tr("Open a bitcoin: URI or payment request")); + m_open_wallet_action = new QAction(tr("Open Wallet"), this); + m_open_wallet_action->setMenu(new QMenu(this)); + m_open_wallet_action->setStatusTip(tr("Open a wallet")); + showHelpMessageAction = new QAction(platformStyle->TextColorIcon(":/icons/info"), tr("&Command-line options"), this); showHelpMessageAction->setMenuRole(QAction::NoRole); showHelpMessageAction->setStatusTip(tr("Show the %1 help message to get a list with possible Bitcoin command-line options").arg(tr(PACKAGE_NAME))); @@ -361,6 +365,16 @@ void BitcoinGUI::createActions() connect(usedSendingAddressesAction, &QAction::triggered, walletFrame, &WalletFrame::usedSendingAddresses); connect(usedReceivingAddressesAction, &QAction::triggered, walletFrame, &WalletFrame::usedReceivingAddresses); connect(openAction, &QAction::triggered, this, &BitcoinGUI::openClicked); + connect(m_open_wallet_action->menu(), &QMenu::aboutToShow, [this] { + m_open_wallet_action->menu()->clear(); + for (std::string path : m_wallet_controller->getWalletsAvailableToOpen()) { + QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path); + QAction* action = m_open_wallet_action->menu()->addAction(name); + connect(action, &QAction::triggered, [this, path] { + setCurrentWallet(m_wallet_controller->openWallet(path)); + }); + } + }); } #endif // ENABLE_WALLET @@ -382,6 +396,8 @@ void BitcoinGUI::createMenuBar() QMenu *file = appMenuBar->addMenu(tr("&File")); if(walletFrame) { + file->addAction(m_open_wallet_action); + file->addSeparator(); file->addAction(openAction); file->addAction(backupWalletAction); file->addAction(signMessageAction); diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index c31cefe60..c22649402 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -147,6 +147,7 @@ private: QAction* openRPCConsoleAction = nullptr; QAction* openAction = nullptr; QAction* showHelpMessageAction = nullptr; + QAction* m_open_wallet_action{nullptr}; QAction* m_wallet_selector_label_action = nullptr; QAction* m_wallet_selector_action = nullptr; From be82dea23c3e6931f9ae7ab8c6a8595ae950587f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Mon, 21 Jan 2019 16:57:22 +0000 Subject: [PATCH 5/8] gui: Add thread to run background activity in WalletController --- src/qt/walletcontroller.cpp | 8 +++++++- src/qt/walletcontroller.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index 0730afca9..a103f4c89 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -26,11 +26,17 @@ WalletController::WalletController(interfaces::Node& node, const PlatformStyle* for (std::unique_ptr& wallet : m_node.getWallets()) { getOrCreateWallet(std::move(wallet)); } + + m_activity_thread.start(); } // Not using the default destructor because not all member types definitions are // available in the header, just forward declared. -WalletController::~WalletController() {} +WalletController::~WalletController() +{ + m_activity_thread.quit(); + m_activity_thread.wait(); +} std::vector WalletController::getWallets() const { diff --git a/src/qt/walletcontroller.h b/src/qt/walletcontroller.h index 523e47d27..1664fa6f3 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -13,6 +13,7 @@ #include #include +#include class OptionsModel; class PlatformStyle; @@ -51,6 +52,7 @@ Q_SIGNALS: void coinsSent(WalletModel* wallet_model, SendCoinsRecipient recipient, QByteArray transaction); private: + QThread m_activity_thread; interfaces::Node& m_node; const PlatformStyle* const m_platform_style; OptionsModel* const m_options_model; From 4c8982a88e3d9fae4f8fc147a2d9f9650a493da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Tue, 22 Jan 2019 15:09:57 +0000 Subject: [PATCH 6/8] interfaces: Avoid interface instance if wallet is null --- src/interfaces/wallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index a2cae2a7a..62b057945 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -523,7 +523,7 @@ public: } // namespace -std::unique_ptr MakeWallet(const std::shared_ptr& wallet) { return MakeUnique(wallet); } +std::unique_ptr MakeWallet(const std::shared_ptr& wallet) { return wallet ? MakeUnique(wallet) : nullptr; } std::unique_ptr MakeWalletClient(Chain& chain, std::vector wallet_filenames) { From 8847cdaaaeb45c1ddee89f43ac4b8fafb20e5c0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Mon, 21 Jan 2019 16:58:20 +0000 Subject: [PATCH 7/8] gui: Add OpenWalletActivity --- src/qt/bitcoin.cpp | 2 +- src/qt/bitcoingui.cpp | 4 +++- src/qt/walletcontroller.cpp | 36 ++++++++++++++++++++++++++---------- src/qt/walletcontroller.h | 27 ++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index 85d79ee26..d6c6fd6e9 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -456,7 +456,7 @@ int GuiMain(int argc, char* argv[]) // IMPORTANT if it is no longer a typedef use the normal variant above qRegisterMetaType< CAmount >("CAmount"); qRegisterMetaType< std::function >("std::function"); - + qRegisterMetaType("QMessageBox::Icon"); /// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these // Command-line options take precedence: node->setupServerArgs(); diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index e7279e122..3622bd565 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -371,7 +371,9 @@ void BitcoinGUI::createActions() QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path); QAction* action = m_open_wallet_action->menu()->addAction(name); connect(action, &QAction::triggered, [this, path] { - setCurrentWallet(m_wallet_controller->openWallet(path)); + OpenWalletActivity* activity = m_wallet_controller->openWallet(path); + connect(activity, &OpenWalletActivity::opened, this, &BitcoinGUI::setCurrentWallet); + connect(activity, &OpenWalletActivity::finished, activity, &QObject::deleteLater); }); } }); diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index a103f4c89..3483c7597 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -55,17 +55,12 @@ std::vector WalletController::getWalletsAvailableToOpen() const return wallets; } -WalletModel* WalletController::openWallet(const std::string& name, QWidget* parent) +OpenWalletActivity* WalletController::openWallet(const std::string& name, QWidget* parent) { - std::string error, warning; - WalletModel* wallet_model = getOrCreateWallet(m_node.loadWallet(name, error, warning)); - if (!wallet_model) { - QMessageBox::warning(parent, tr("Open Wallet"), QString::fromStdString(error)); - } - if (!warning.empty()) { - QMessageBox::information(parent, tr("Open Wallet"), QString::fromStdString(warning)); - } - return wallet_model; + OpenWalletActivity* activity = new OpenWalletActivity(this, name); + activity->moveToThread(&m_activity_thread); + QMetaObject::invokeMethod(activity, "open", Qt::QueuedConnection); + return activity; } WalletModel* WalletController::getOrCreateWallet(std::unique_ptr wallet) @@ -124,3 +119,24 @@ void WalletController::removeAndDeleteWallet(WalletModel* wallet_model) // CWallet shared pointer. delete wallet_model; } + + +OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, const std::string& name) + : m_wallet_controller(wallet_controller) + , m_name(name) +{} + +void OpenWalletActivity::open() +{ + std::string error, warning; + std::unique_ptr wallet = m_wallet_controller->m_node.loadWallet(m_name, error, warning); + if (!warning.empty()) { + Q_EMIT message(QMessageBox::Warning, QString::fromStdString(warning)); + } + if (wallet) { + Q_EMIT opened(m_wallet_controller->getOrCreateWallet(std::move(wallet))); + } else { + Q_EMIT message(QMessageBox::Critical, QString::fromStdString(error)); + } + Q_EMIT finished(); +} diff --git a/src/qt/walletcontroller.h b/src/qt/walletcontroller.h index 1664fa6f3..f19c0e1f3 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -23,6 +24,8 @@ class Handler; class Node; } // namespace interfaces +class OpenWalletActivity; + /** * Controller between interfaces::Node, WalletModel instances and the GUI. */ @@ -40,7 +43,7 @@ public: std::vector getWallets() const; std::vector getWalletsAvailableToOpen() const; - WalletModel* openWallet(const std::string& name, QWidget* parent = nullptr); + OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr); private Q_SLOTS: void addWallet(WalletModel* wallet_model); @@ -59,6 +62,28 @@ private: mutable QMutex m_mutex; std::vector m_wallets; std::unique_ptr m_handler_load_wallet; + + friend class OpenWalletActivity; +}; + +class OpenWalletActivity : public QObject +{ + Q_OBJECT + +public: + OpenWalletActivity(WalletController* wallet_controller, const std::string& name); + +public Q_SLOTS: + void open(); + +Q_SIGNALS: + void message(QMessageBox::Icon icon, const QString text); + void finished(); + void opened(WalletModel* wallet_model); + +private: + WalletController* const m_wallet_controller; + std::string const m_name; }; #endif // BITCOIN_QT_WALLETCONTROLLER_H From 1951ea4342db4122032660139248b79a02c574f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Tue, 22 Jan 2019 13:02:45 +0000 Subject: [PATCH 8/8] gui: Show indeterminate progress dialog while opening walllet --- src/qt/bitcoingui.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 3622bd565..f7a4bad91 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -370,10 +370,29 @@ void BitcoinGUI::createActions() for (std::string path : m_wallet_controller->getWalletsAvailableToOpen()) { QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path); QAction* action = m_open_wallet_action->menu()->addAction(name); - connect(action, &QAction::triggered, [this, path] { + connect(action, &QAction::triggered, [this, name, path] { OpenWalletActivity* activity = m_wallet_controller->openWallet(path); + + QProgressDialog* dialog = new QProgressDialog(this); + dialog->setLabelText(tr("Opening Wallet %1...").arg(name.toHtmlEscaped())); + dialog->setRange(0, 0); + dialog->setCancelButton(nullptr); + dialog->setWindowModality(Qt::ApplicationModal); + dialog->show(); + + connect(activity, &OpenWalletActivity::message, this, [this] (QMessageBox::Icon icon, QString text) { + QMessageBox box; + box.setIcon(icon); + box.setText(tr("Open Wallet Failed")); + box.setInformativeText(text); + box.setStandardButtons(QMessageBox::Ok); + box.setDefaultButton(QMessageBox::Ok); + connect(this, &QObject::destroyed, &box, &QDialog::accept); + box.exec(); + }); connect(activity, &OpenWalletActivity::opened, this, &BitcoinGUI::setCurrentWallet); connect(activity, &OpenWalletActivity::finished, activity, &QObject::deleteLater); + connect(activity, &OpenWalletActivity::finished, dialog, &QObject::deleteLater); }); } });