diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index 75a6ea51e..36bd1f80d 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -1,328 +1,335 @@ // Copyright (c) 2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include WalletController::WalletController(ClientModel &client_model, const PlatformStyle *platform_style, QObject *parent) : QObject(parent), m_activity_thread(new QThread(this)), m_activity_worker(new QObject), m_client_model(client_model), m_node(client_model.node()), m_platform_style(platform_style), m_options_model(client_model.getOptionsModel()) { m_handler_load_wallet = m_node.handleLoadWallet( [this](std::unique_ptr wallet) { getOrCreateWallet(std::move(wallet)); }); for (std::unique_ptr &wallet : m_node.getWallets()) { getOrCreateWallet(std::move(wallet)); } m_activity_worker->moveToThread(m_activity_thread); 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() { m_activity_thread->quit(); m_activity_thread->wait(); delete m_activity_worker; } std::vector WalletController::getOpenWallets() const { QMutexLocker locker(&m_mutex); return m_wallets; } std::map WalletController::listWalletDir() const { QMutexLocker locker(&m_mutex); std::map wallets; for (const std::string &name : m_node.listWalletDir()) { wallets[name] = false; } for (WalletModel *wallet_model : m_wallets) { auto it = wallets.find(wallet_model->wallet().getWalletName()); if (it != wallets.end()) { it->second = true; } } return wallets; } void WalletController::closeWallet(WalletModel *wallet_model, QWidget *parent) { QMessageBox box(parent); box.setWindowTitle(tr("Close wallet")); box.setText(tr("Are you sure you wish to close the wallet %1?") .arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName()))); box.setInformativeText( tr("Closing the wallet for too long can result in having to resync the " "entire chain if pruning is enabled.")); box.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); box.setDefaultButton(QMessageBox::Yes); if (box.exec() != QMessageBox::Yes) { return; } // First remove wallet from node. wallet_model->wallet().remove(); // Now release the model. removeAndDeleteWallet(wallet_model); } WalletModel *WalletController::getOrCreateWallet( std::unique_ptr wallet) { QMutexLocker locker(&m_mutex); // Return model instance if exists. if (!m_wallets.empty()) { std::string name = wallet->getWalletName(); for (WalletModel *wallet_model : m_wallets) { if (wallet_model->wallet().getWalletName() == name) { return wallet_model; } } } // Instantiate model and register it. WalletModel *wallet_model = new WalletModel( std::move(wallet), m_client_model, m_platform_style, nullptr); // Handler callback runs in a different thread so fix wallet model thread // affinity. wallet_model->moveToThread(thread()); wallet_model->setParent(this); m_wallets.push_back(wallet_model); // WalletModel::startPollBalance needs to be called in a thread managed by // Qt because of startTimer. Considering the current thread can be a RPC // thread, better delegate the calling to Qt with Qt::AutoConnection. const bool called = QMetaObject::invokeMethod(wallet_model, "startPollBalance"); assert(called); connect( wallet_model, &WalletModel::unload, this, [this, wallet_model] { // Defer removeAndDeleteWallet when no modal widget is active. // TODO: remove this workaround by removing usage of QDiallog::exec. if (QApplication::activeModalWidget()) { connect( qApp, &QApplication::focusWindowChanged, wallet_model, [this, wallet_model]() { if (!QApplication::activeModalWidget()) { removeAndDeleteWallet(wallet_model); } }, Qt::QueuedConnection); } else { removeAndDeleteWallet(wallet_model); } }, Qt::QueuedConnection); // Re-emit coinsSent signal from wallet model. connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent); // Notify walletAdded signal on the GUI thread. Q_EMIT walletAdded(wallet_model); return wallet_model; } void WalletController::removeAndDeleteWallet(WalletModel *wallet_model) { // Unregister wallet model. { QMutexLocker locker(&m_mutex); m_wallets.erase( std::remove(m_wallets.begin(), m_wallets.end(), wallet_model)); } Q_EMIT walletRemoved(wallet_model); // Currently this can trigger the unload since the model can hold the last // CWallet shared pointer. delete wallet_model; } WalletControllerActivity::WalletControllerActivity( WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams) : QObject(wallet_controller), m_wallet_controller(wallet_controller), m_parent_widget(parent_widget), m_chainparams(chainparams) {} WalletControllerActivity::~WalletControllerActivity() { delete m_progress_dialog; } void WalletControllerActivity::showProgressDialog(const QString &label_text) { + assert(!m_progress_dialog); m_progress_dialog = new QProgressDialog(m_parent_widget); m_progress_dialog->setLabelText(label_text); m_progress_dialog->setRange(0, 0); m_progress_dialog->setCancelButton(nullptr); m_progress_dialog->setWindowModality(Qt::ApplicationModal); GUIUtil::PolishProgressDialog(m_progress_dialog); } +void WalletControllerActivity::destroyProgressDialog() { + assert(m_progress_dialog); + delete m_progress_dialog; + m_progress_dialog = nullptr; +} + CreateWalletActivity::CreateWalletActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams) : WalletControllerActivity(wallet_controller, parent_widget, chainparams) { m_passphrase.reserve(MAX_PASSPHRASE_SIZE); } CreateWalletActivity::~CreateWalletActivity() { delete m_create_wallet_dialog; delete m_passphrase_dialog; } void CreateWalletActivity::askPassphrase() { m_passphrase_dialog = new AskPassphraseDialog( AskPassphraseDialog::Encrypt, m_parent_widget, &m_passphrase); m_passphrase_dialog->setWindowModality(Qt::ApplicationModal); m_passphrase_dialog->show(); connect(m_passphrase_dialog, &QObject::destroyed, [this] { m_passphrase_dialog = nullptr; }); connect(m_passphrase_dialog, &QDialog::accepted, [this] { createWallet(); }); connect(m_passphrase_dialog, &QDialog::rejected, [this] { Q_EMIT finished(); }); } void CreateWalletActivity::createWallet() { showProgressDialog( tr("Creating Wallet %1...") .arg(m_create_wallet_dialog->walletName().toHtmlEscaped())); std::string name = m_create_wallet_dialog->walletName().toStdString(); uint64_t flags = 0; if (m_create_wallet_dialog->isDisablePrivateKeysChecked()) { flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS; } if (m_create_wallet_dialog->isMakeBlankWalletChecked()) { flags |= WALLET_FLAG_BLANK_WALLET; } if (m_create_wallet_dialog->isDescriptorWalletChecked()) { flags |= WALLET_FLAG_DESCRIPTORS; } QTimer::singleShot(500, worker(), [this, name, flags] { WalletCreationStatus status; std::unique_ptr wallet = node().createWallet(m_chainparams, m_passphrase, flags, name, m_error_message, m_warning_message, status); if (status == WalletCreationStatus::SUCCESS) { m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet)); } QTimer::singleShot(500, this, &CreateWalletActivity::finish); }); } void CreateWalletActivity::finish() { - m_progress_dialog->hide(); + destroyProgressDialog(); if (!m_error_message.empty()) { QMessageBox::critical( m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message.translated)); } else if (!m_warning_message.empty()) { QMessageBox::warning( m_parent_widget, tr("Create wallet warning"), QString::fromStdString( Join(m_warning_message, Untranslated("\n")).translated)); } if (m_wallet_model) { Q_EMIT created(m_wallet_model); } Q_EMIT finished(); } void CreateWalletActivity::create() { m_create_wallet_dialog = new CreateWalletDialog(m_parent_widget); m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal); m_create_wallet_dialog->show(); connect(m_create_wallet_dialog, &QObject::destroyed, [this] { m_create_wallet_dialog = nullptr; }); connect(m_create_wallet_dialog, &QDialog::rejected, [this] { Q_EMIT finished(); }); connect(m_create_wallet_dialog, &QDialog::accepted, [this] { if (m_create_wallet_dialog->isEncryptWalletChecked()) { askPassphrase(); } else { createWallet(); } }); } OpenWalletActivity::OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams) : WalletControllerActivity(wallet_controller, parent_widget, chainparams) {} void OpenWalletActivity::finish() { - m_progress_dialog->hide(); + destroyProgressDialog(); if (!m_error_message.empty()) { QMessageBox::critical( m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message.translated)); } else if (!m_warning_message.empty()) { QMessageBox::warning( m_parent_widget, tr("Open wallet warning"), QString::fromStdString( Join(m_warning_message, Untranslated("\n")).translated)); } if (m_wallet_model) { Q_EMIT opened(m_wallet_model); } Q_EMIT finished(); } void OpenWalletActivity::open(const std::string &path) { QString name = path.empty() ? QString("[" + tr("default wallet") + "]") : QString::fromStdString(path); showProgressDialog( tr("Opening Wallet %1...").arg(name.toHtmlEscaped())); QTimer::singleShot(0, worker(), [this, path] { std::unique_ptr wallet = node().loadWallet( this->m_chainparams, path, m_error_message, m_warning_message); if (wallet) { m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet)); } QTimer::singleShot(0, this, &OpenWalletActivity::finish); }); } diff --git a/src/qt/walletcontroller.h b/src/qt/walletcontroller.h index 85a01ac1a..d8f5a2a9f 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -1,158 +1,159 @@ // Copyright (c) 2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_QT_WALLETCONTROLLER_H #define BITCOIN_QT_WALLETCONTROLLER_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class ClientModel; class OptionsModel; class PlatformStyle; class WalletModel; namespace interfaces { class Handler; class Node; class Wallet; } // namespace interfaces class AskPassphraseDialog; class CreateWalletActivity; class CreateWalletDialog; class OpenWalletActivity; class WalletControllerActivity; /** * Controller between interfaces::Node, WalletModel instances and the GUI. */ class WalletController : public QObject { Q_OBJECT void removeAndDeleteWallet(WalletModel *wallet_model); public: WalletController(ClientModel &client_model, const PlatformStyle *platform_style, QObject *parent); ~WalletController(); //! Returns wallet models currently open. std::vector getOpenWallets() const; WalletModel *getOrCreateWallet(std::unique_ptr wallet); //! Returns all wallet names in the wallet dir mapped to whether the wallet //! is loaded. std::map listWalletDir() const; void closeWallet(WalletModel *wallet_model, QWidget *parent = nullptr); Q_SIGNALS: void walletAdded(WalletModel *wallet_model); void walletRemoved(WalletModel *wallet_model); void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction); private: QThread *const m_activity_thread; QObject *const m_activity_worker; ClientModel &m_client_model; interfaces::Node &m_node; const PlatformStyle *const m_platform_style; OptionsModel *const m_options_model; mutable QMutex m_mutex; std::vector m_wallets; std::unique_ptr m_handler_load_wallet; friend class WalletControllerActivity; }; class WalletControllerActivity : public QObject { Q_OBJECT public: WalletControllerActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams); virtual ~WalletControllerActivity(); Q_SIGNALS: void finished(); protected: interfaces::Node &node() const { return m_wallet_controller->m_node; } QObject *worker() const { return m_wallet_controller->m_activity_worker; } void showProgressDialog(const QString &label_text); + void destroyProgressDialog(); WalletController *const m_wallet_controller; QWidget *const m_parent_widget; QProgressDialog *m_progress_dialog{nullptr}; WalletModel *m_wallet_model{nullptr}; bilingual_str m_error_message; std::vector m_warning_message; const CChainParams &m_chainparams; }; class CreateWalletActivity : public WalletControllerActivity { Q_OBJECT public: CreateWalletActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams); virtual ~CreateWalletActivity(); void create(); Q_SIGNALS: void created(WalletModel *wallet_model); private: void askPassphrase(); void createWallet(); void finish(); SecureString m_passphrase; CreateWalletDialog *m_create_wallet_dialog{nullptr}; AskPassphraseDialog *m_passphrase_dialog{nullptr}; }; class OpenWalletActivity : public WalletControllerActivity { Q_OBJECT public: OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams); void open(const std::string &path); Q_SIGNALS: void opened(WalletModel *wallet_model); private: void finish(); }; #endif // BITCOIN_QT_WALLETCONTROLLER_H diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp index d09e7f127..ac03dd120 100644 --- a/src/wallet/load.cpp +++ b/src/wallet/load.cpp @@ -1,140 +1,145 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include bool VerifyWallets(const CChainParams &chainParams, interfaces::Chain &chain, const std::vector &wallet_files) { if (gArgs.IsArgSet("-walletdir")) { fs::path wallet_dir = gArgs.GetArg("-walletdir", ""); boost::system::error_code error; // The canonical path cleans the path, preventing >1 Berkeley // environment instances for the same directory fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error); if (error || !fs::exists(wallet_dir)) { chain.initError( strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string())); return false; } else if (!fs::is_directory(wallet_dir)) { chain.initError( strprintf(_("Specified -walletdir \"%s\" is not a directory"), wallet_dir.string())); return false; // The canonical path transforms relative paths into absolute ones, // so we check the non-canonical version } else if (!wallet_dir.is_absolute()) { chain.initError( strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string())); return false; } gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string()); } LogPrintf("Using wallet directory %s\n", GetWalletDir().string()); chain.initMessage(_("Verifying wallet(s)...").translated); // Keep track of each wallet absolute path to detect duplicates. std::set wallet_paths; for (const auto &wallet_file : wallet_files) { WalletLocation location(wallet_file); if (!wallet_paths.insert(location.GetPath()).second) { chain.initError(strprintf(_("Error loading wallet %s. Duplicate " "-wallet filename specified."), wallet_file)); return false; } bilingual_str error_string; std::vector warnings; bool verify_success = CWallet::Verify(chainParams, chain, location, error_string, warnings); if (!warnings.empty()) { chain.initWarning(Join(warnings, Untranslated("\n"))); } if (!verify_success) { chain.initError(error_string); return false; } } return true; } bool LoadWallets(const CChainParams &chainParams, interfaces::Chain &chain, const std::vector &wallet_files) { - for (const std::string &walletFile : wallet_files) { - bilingual_str error; - std::vector warnings; - std::shared_ptr pwallet = CWallet::CreateWalletFromFile( - chainParams, chain, WalletLocation(walletFile), error, warnings); - if (!warnings.empty()) { - chain.initWarning(Join(warnings, Untranslated("\n"))); + try { + for (const std::string &walletFile : wallet_files) { + bilingual_str error; + std::vector warnings; + std::shared_ptr pwallet = CWallet::CreateWalletFromFile( + chainParams, chain, WalletLocation(walletFile), error, + warnings); + if (!warnings.empty()) { + chain.initWarning(Join(warnings, Untranslated("\n"))); + } + if (!pwallet) { + chain.initError(error); + return false; + } + AddWallet(pwallet); } - if (!pwallet) { - chain.initError(error); - return false; - } - AddWallet(pwallet); + return true; + } catch (const std::runtime_error &e) { + chain.initError(Untranslated(e.what())); + return false; } - - return true; } void StartWallets(CScheduler &scheduler, const ArgsManager &args) { for (const std::shared_ptr &pwallet : GetWallets()) { pwallet->postInitProcess(); } // Schedule periodic wallet flushes and tx rebroadcasts if (args.GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) { scheduler.scheduleEvery( [] { MaybeCompactWalletDB(); return true; }, std::chrono::milliseconds{500}); } scheduler.scheduleEvery( [] { MaybeResendWalletTxs(); return true; }, std::chrono::milliseconds{1000}); } void FlushWallets() { for (const std::shared_ptr &pwallet : GetWallets()) { pwallet->Flush(false); } } void StopWallets() { for (const std::shared_ptr &pwallet : GetWallets()) { pwallet->Flush(true); } } void UnloadWallets() { auto wallets = GetWallets(); while (!wallets.empty()) { auto wallet = wallets.back(); wallets.pop_back(); RemoveWallet(wallet); UnloadWallet(std::move(wallet)); } } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index cb63d2d9a..2c3228663 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1,5049 +1,5054 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include