diff --git a/src/app.h b/src/app.h index e5c0a43..7434aa6 100644 --- a/src/app.h +++ b/src/app.h @@ -395,6 +395,12 @@ public: // Wallet encryption helpers void encryptWalletWithPassphrase(const std::string& passphrase); + // Post-encrypt daemon restart: the daemon shuts itself down after + // encryptwallet, so restart it off the main thread. Shared by the + // immediate and deferred encryption continuations. When + // announceRestartStatus is true, connection_status_ is updated first so + // the loading overlay explains the restart. + void restartDaemonAfterEncryption(const char* taskName, bool announceRestartStatus); void unlockWallet(const std::string& passphrase, int timeout); void lockWallet(); void changePassphrase(const std::string& oldPass, const std::string& newPass); diff --git a/src/app_security.cpp b/src/app_security.cpp index de0204c..04d4976 100644 --- a/src/app_security.cpp +++ b/src/app_security.cpp @@ -230,6 +230,34 @@ private: // Wallet encryption helpers // =========================================================================== +// The daemon shuts itself down after encryptwallet. Restart the embedded +// daemon off the main thread (to avoid stalling the UI), or ask the user to +// restart an external daemon. Shared by encryptWalletWithPassphrase() and +// processDeferredEncryption(); must be called on the main thread. +void App::restartDaemonAfterEncryption(const char* taskName, bool announceRestartStatus) { + if (isUsingEmbeddedDaemon()) { + if (announceRestartStatus) { + // Update connection_status_ so the loading overlay explains why + // the daemon is restarting. + connection_status_ = TR("restarting_after_encryption"); + } + // Give daemon a moment to shut down, then restart + // (do this off the main thread to avoid stalling the UI) + async_tasks_.submit(taskName, [this](const util::AsyncTaskManager::Token& token) { + for (int i = 0; i < 20 && !token.cancelled() && !shutting_down_; ++i) + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + if (token.cancelled() || shutting_down_) return; + stopEmbeddedDaemon(); + if (token.cancelled() || shutting_down_) return; + startEmbeddedDaemon(); + // tryConnect will be called by the update loop + }); + } else { + ui::Notifications::instance().warning( + "Please restart your daemon for encryption to take effect."); + } +} + void App::encryptWalletWithPassphrase(const std::string& passphrase) { if (!rpc_ || !rpc_->isConnected()) return; encrypt_in_progress_ = true; @@ -267,23 +295,8 @@ void App::encryptWalletWithPassphrase(const std::string& passphrase) { // The daemon shuts itself down after encryptwallet. // Update connection_status_ so the loading overlay // explains why the daemon is restarting. - if (isUsingEmbeddedDaemon()) { - connection_status_ = TR("restarting_after_encryption"); - // Give daemon a moment to shut down, then restart - // (do this off the main thread to avoid stalling the UI) - async_tasks_.submit("encrypt-daemon-restart", [this](const util::AsyncTaskManager::Token& token) { - for (int i = 0; i < 20 && !token.cancelled() && !shutting_down_; ++i) - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - if (token.cancelled() || shutting_down_) return; - stopEmbeddedDaemon(); - if (token.cancelled() || shutting_down_) return; - startEmbeddedDaemon(); - // tryConnect will be called by the update loop - }); - } else { - ui::Notifications::instance().warning( - "Please restart your daemon for encryption to take effect."); - } + restartDaemonAfterEncryption("encrypt-daemon-restart", + /*announceRestartStatus=*/true); }; } else { std::string err = result.error; @@ -366,20 +379,8 @@ void App::processDeferredEncryption() { wallet_security_.clearDeferredEncryption(); // Restart daemon (it shuts itself down after encryptwallet) - if (isUsingEmbeddedDaemon()) { - async_tasks_.submit("deferred-encrypt-daemon-restart", [this](const util::AsyncTaskManager::Token& token) { - for (int i = 0; i < 20 && !token.cancelled() && !shutting_down_; ++i) - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - if (token.cancelled() || shutting_down_) return; - stopEmbeddedDaemon(); - if (token.cancelled() || shutting_down_) return; - startEmbeddedDaemon(); - // tryConnect will be called by the update loop - }); - } else { - ui::Notifications::instance().warning( - "Please restart your daemon for encryption to take effect."); - } + restartDaemonAfterEncryption("deferred-encrypt-daemon-restart", + /*announceRestartStatus=*/false); }; } else { std::string err = result.error;