From fbcabb2fc048b481769178418adb74471e47f11b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20W=C3=B3jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 16 Jul 2024 12:09:04 +0200 Subject: [PATCH] Fix wallet logging initialization (#4680) * Adjust annoying logs * Wrap wallet logic in a class --- nano/nano_wallet/entry.cpp | 350 ++++++++++++++++--------------- nano/node/scheduler/priority.cpp | 2 +- 2 files changed, 179 insertions(+), 173 deletions(-) diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index 25b29947fb..5d595c9c04 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -23,215 +23,219 @@ #include #include -namespace +namespace nano { -nano::logger logger{ "wallet_daemon" }; - -void show_error (std::string const & message_a) +class wallet_daemon final { - logger.critical (nano::log::type::daemon, "{}", message_a); - - QMessageBox message (QMessageBox::Critical, "Error starting Nano", message_a.c_str ()); - message.setModal (true); - message.show (); - message.exec (); -} + nano::logger logger{ "wallet_daemon" }; -void show_help (std::string const & message_a) -{ - QMessageBox message (QMessageBox::NoIcon, "Help", "see launch options "); - message.setStyleSheet ("QLabel {min-width: 450px}"); - message.setDetailedText (message_a.c_str ()); - message.show (); - message.exec (); -} - -nano::error write_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a) -{ - nano::tomlconfig wallet_config_toml; - auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a)); - config_a.serialize_toml (wallet_config_toml); +public: + void show_error (std::string const & message_a) + { + logger.critical (nano::log::type::daemon, "{}", message_a); - // Write wallet config. If missing, the file is created and permissions are set. - wallet_config_toml.write (wallet_path); - return wallet_config_toml.get_error (); -} + QMessageBox message (QMessageBox::Critical, "Error starting Nano", message_a.c_str ()); + message.setModal (true); + message.show (); + message.exec (); + } -nano::error read_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a) -{ - nano::tomlconfig wallet_config_toml; - auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a)); - if (!std::filesystem::exists (wallet_path)) + void show_help (std::string const & message_a) { - write_wallet_config (config_a, data_path_a); + QMessageBox message (QMessageBox::NoIcon, "Help", "see launch options "); + message.setStyleSheet ("QLabel {min-width: 450px}"); + message.setDetailedText (message_a.c_str ()); + message.show (); + message.exec (); } - wallet_config_toml.read (wallet_path); - config_a.deserialize_toml (wallet_config_toml); - return wallet_config_toml.get_error (); -} -} - -int run_wallet (QApplication & application, int argc, char * const * argv, std::filesystem::path const & data_path, nano::node_flags const & flags) -{ - nano::logger::initialize (nano::log_config::daemon_default (), data_path, flags.config_overrides); - - logger.info (nano::log::type::daemon_wallet, "Daemon started (wallet)"); - - int result (0); - nano_qt::eventloop_processor processor; - boost::system::error_code error_chmod; - std::filesystem::create_directories (data_path); - nano::set_secure_perm_directory (data_path, error_chmod); - QPixmap pixmap (":/logo.png"); - auto * splash = new QSplashScreen (pixmap); - splash->show (); - QApplication::processEvents (); - splash->showMessage (QSplashScreen::tr ("Remember - Back Up Your Wallet Seed"), Qt::AlignBottom | Qt::AlignHCenter, Qt::darkGray); - QApplication::processEvents (); - - nano::network_params network_params{ nano::network_constants::active_network }; - nano::daemon_config config{ data_path, network_params }; - nano::wallet_config wallet_config; - auto error = nano::read_node_config_toml (data_path, config, flags.config_overrides); - if (!error) + nano::error write_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a) { - error = read_wallet_config (wallet_config, data_path); + nano::tomlconfig wallet_config_toml; + auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a)); + config_a.serialize_toml (wallet_config_toml); + + // Write wallet config. If missing, the file is created and permissions are set. + wallet_config_toml.write (wallet_path); + return wallet_config_toml.get_error (); } - if (!error) + nano::error read_wallet_config (nano::wallet_config & config_a, std::filesystem::path const & data_path_a) { - error = nano::flags_config_conflicts (flags, config.node); + nano::tomlconfig wallet_config_toml; + auto wallet_path (nano::get_qtwallet_toml_config_path (data_path_a)); + if (!std::filesystem::exists (wallet_path)) + { + write_wallet_config (config_a, data_path_a); + } + wallet_config_toml.read (wallet_path); + config_a.deserialize_toml (wallet_config_toml); + return wallet_config_toml.get_error (); } - if (!error) + int run_wallet (QApplication & application, int argc, char * const * argv, std::filesystem::path const & data_path, nano::node_flags const & flags) { - nano::set_use_memory_pools (config.node.use_memory_pools); + nano::logger::initialize (nano::log_config::daemon_default (), data_path, flags.config_overrides); - std::shared_ptr io_ctx = std::make_shared (); + logger.info (nano::log::type::daemon_wallet, "Daemon started (wallet)"); + + int result (0); + nano_qt::eventloop_processor processor; + boost::system::error_code error_chmod; + std::filesystem::create_directories (data_path); + nano::set_secure_perm_directory (data_path, error_chmod); + QPixmap pixmap (":/logo.png"); + auto * splash = new QSplashScreen (pixmap); + splash->show (); + QApplication::processEvents (); + splash->showMessage (QSplashScreen::tr ("Remember - Back Up Your Wallet Seed"), Qt::AlignBottom | Qt::AlignHCenter, Qt::darkGray); + QApplication::processEvents (); - nano::thread_runner runner (io_ctx, logger, config.node.io_threads, nano::thread_role::name::io_daemon); + nano::network_params network_params{ nano::network_constants::active_network }; + nano::daemon_config config{ data_path, network_params }; + nano::wallet_config wallet_config; - std::shared_ptr node; - std::shared_ptr gui; - nano::set_application_icon (application); - auto opencl = nano::opencl_work::create (config.opencl_enable, config.opencl, logger, config.node.network_params.work); - nano::opencl_work_func_t opencl_work_func; - if (opencl) + auto error = nano::read_node_config_toml (data_path, config, flags.config_overrides); + if (!error) { - opencl_work_func = [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic &) { - return opencl->generate_work (version_a, root_a, difficulty_a); - }; + error = read_wallet_config (wallet_config, data_path); } - nano::work_pool work{ config.node.network_params.network, config.node.work_threads, config.node.pow_sleep_interval, opencl_work_func }; - node = std::make_shared (io_ctx, data_path, config.node, work, flags); - if (!node->init_error ()) + + if (!error) + { + error = nano::flags_config_conflicts (flags, config.node); + } + + if (!error) { - auto wallet (node->wallets.open (wallet_config.wallet)); - if (wallet == nullptr) + nano::set_use_memory_pools (config.node.use_memory_pools); + + std::shared_ptr io_ctx = std::make_shared (); + + nano::thread_runner runner (io_ctx, logger, config.node.io_threads, nano::thread_role::name::io_daemon); + + std::shared_ptr node; + std::shared_ptr gui; + nano::set_application_icon (application); + auto opencl = nano::opencl_work::create (config.opencl_enable, config.opencl, logger, config.node.network_params.work); + nano::opencl_work_func_t opencl_work_func; + if (opencl) { - auto existing (node->wallets.items.begin ()); - if (existing != node->wallets.items.end ()) - { - wallet = existing->second; - wallet_config.wallet = existing->first; - } - else - { - wallet = node->wallets.create (wallet_config.wallet); - } + opencl_work_func = [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic &) { + return opencl->generate_work (version_a, root_a, difficulty_a); + }; } - if (wallet_config.account.is_zero () || !wallet->exists (wallet_config.account)) + nano::work_pool work{ config.node.network_params.network, config.node.work_threads, config.node.pow_sleep_interval, opencl_work_func }; + node = std::make_shared (io_ctx, data_path, config.node, work, flags); + if (!node->init_error ()) { - auto transaction (wallet->wallets.tx_begin_write ()); - auto existing (wallet->store.begin (transaction)); - if (existing != wallet->store.end ()) + auto wallet (node->wallets.open (wallet_config.wallet)); + if (wallet == nullptr) { - wallet_config.account = existing->first; + auto existing (node->wallets.items.begin ()); + if (existing != node->wallets.items.end ()) + { + wallet = existing->second; + wallet_config.wallet = existing->first; + } + else + { + wallet = node->wallets.create (wallet_config.wallet); + } } - else + if (wallet_config.account.is_zero () || !wallet->exists (wallet_config.account)) { - wallet_config.account = wallet->deterministic_insert (transaction); + auto transaction (wallet->wallets.tx_begin_write ()); + auto existing (wallet->store.begin (transaction)); + if (existing != wallet->store.end ()) + { + wallet_config.account = existing->first; + } + else + { + wallet_config.account = wallet->deterministic_insert (transaction); + } } - } - debug_assert (wallet->exists (wallet_config.account)); - write_wallet_config (wallet_config, data_path); - node->start (); - nano::ipc::ipc_server ipc (*node, config.rpc); + debug_assert (wallet->exists (wallet_config.account)); + write_wallet_config (wallet_config, data_path); + node->start (); + nano::ipc::ipc_server ipc (*node, config.rpc); - std::unique_ptr rpc_process; - std::shared_ptr rpc; - std::unique_ptr rpc_handler; - if (config.rpc_enable) - { - if (!config.rpc.child_process.enable) + std::unique_ptr rpc_process; + std::shared_ptr rpc; + std::unique_ptr rpc_handler; + if (config.rpc_enable) { - // Launch rpc in-process - nano::rpc_config rpc_config{ config.node.network_params.network }; - error = nano::read_rpc_config_toml (data_path, rpc_config, flags.rpc_config_overrides); - if (error) + if (!config.rpc.child_process.enable) { - splash->hide (); - show_error (error.get_message ()); - std::exit (1); + // Launch rpc in-process + nano::rpc_config rpc_config{ config.node.network_params.network }; + error = nano::read_rpc_config_toml (data_path, rpc_config, flags.rpc_config_overrides); + if (error) + { + splash->hide (); + show_error (error.get_message ()); + std::exit (1); + } + rpc_handler = std::make_unique (*node, ipc, config.rpc); + rpc = nano::get_rpc (io_ctx, rpc_config, *rpc_handler); + rpc->start (); } - rpc_handler = std::make_unique (*node, ipc, config.rpc); - rpc = nano::get_rpc (io_ctx, rpc_config, *rpc_handler); - rpc->start (); - } - else - { - // Spawn a child rpc process - if (!std::filesystem::exists (config.rpc.child_process.rpc_path)) + else { - throw std::runtime_error (std::string ("RPC is configured to spawn a new process however the file cannot be found at: ") + config.rpc.child_process.rpc_path); - } + // Spawn a child rpc process + if (!std::filesystem::exists (config.rpc.child_process.rpc_path)) + { + throw std::runtime_error (std::string ("RPC is configured to spawn a new process however the file cannot be found at: ") + config.rpc.child_process.rpc_path); + } - auto network = node->network_params.network.get_current_network_as_string (); - rpc_process = std::make_unique (config.rpc.child_process.rpc_path, "--daemon", "--data_path", data_path.string (), "--network", network); - } - } - QObject::connect (&application, &QApplication::aboutToQuit, [&] () { - ipc.stop (); - node->stop (); - if (rpc) - { - rpc->stop (); + auto network = node->network_params.network.get_current_network_as_string (); + rpc_process = std::make_unique (config.rpc.child_process.rpc_path, "--daemon", "--data_path", data_path.string (), "--network", network); + } } + QObject::connect (&application, &QApplication::aboutToQuit, [&] () { + ipc.stop (); + node->stop (); + if (rpc) + { + rpc->stop (); + } #if USE_BOOST_PROCESS - if (rpc_process) - { - rpc_process->terminate (); - } + if (rpc_process) + { + rpc_process->terminate (); + } #endif - runner.abort (); - }); - QApplication::postEvent (&processor, new nano_qt::eventloop_event ([&] () { - gui = std::make_shared (application, processor, *node, wallet, wallet_config.account); - splash->close (); - gui->start (); - gui->client_window->show (); - })); - result = QApplication::exec (); - runner.join (); + runner.abort (); + }); + QApplication::postEvent (&processor, new nano_qt::eventloop_event ([&] () { + gui = std::make_shared (application, processor, *node, wallet, wallet_config.account); + splash->close (); + gui->start (); + gui->client_window->show (); + })); + result = QApplication::exec (); + runner.join (); + } + else + { + splash->hide (); + show_error ("Error initializing node"); + } + write_wallet_config (wallet_config, data_path); } else { splash->hide (); - show_error ("Error initializing node"); + show_error ("Error deserializing config: " + error.get_message ()); } - write_wallet_config (wallet_config, data_path); - } - else - { - splash->hide (); - show_error ("Error deserializing config: " + error.get_message ()); - } - logger.info (nano::log::type::daemon_wallet, "Daemon exiting (wallet)"); + logger.info (nano::log::type::daemon_wallet, "Daemon exiting (wallet)"); - return result; + return result; + } +}; } int main (int argc, char * const * argv) @@ -244,6 +248,8 @@ int main (int argc, char * const * argv) QApplication application (argc, const_cast (argv)); + nano::wallet_daemon daemon; + try { boost::program_options::options_description description ("Command line options"); @@ -262,7 +268,7 @@ int main (int argc, char * const * argv) } catch (boost::program_options::error const & err) { - show_error (err.what ()); + daemon.show_error (err.what ()); return 1; } boost::program_options::notify (vm); @@ -273,7 +279,7 @@ int main (int argc, char * const * argv) auto err (nano::network_constants::set_active_network (network->second.as ())); if (err) { - show_error (nano::network_constants::active_network_err_msg); + daemon.show_error (nano::network_constants::active_network_err_msg); std::exit (1); } } @@ -293,7 +299,7 @@ int main (int argc, char * const * argv) std::ostringstream outstream; description.print (outstream); std::string helpstring = outstream.str (); - show_help (helpstring); + daemon.show_help (helpstring); return 1; } else @@ -316,15 +322,15 @@ int main (int argc, char * const * argv) { throw std::runtime_error (flags_ec.message ()); } - result = run_wallet (application, argc, argv, data_path, flags); + result = daemon.run_wallet (application, argc, argv, data_path, flags); } catch (std::exception const & e) { - show_error (boost::str (boost::format ("Exception while running wallet: %1%") % e.what ())); + daemon.show_error (boost::str (boost::format ("Exception while running wallet: %1%") % e.what ())); } catch (...) { - show_error ("Unknown exception while running wallet"); + daemon.show_error ("Unknown exception while running wallet"); } } } @@ -332,11 +338,11 @@ int main (int argc, char * const * argv) } catch (std::exception const & e) { - show_error (boost::str (boost::format ("Exception while initializing %1%") % e.what ())); + daemon.show_error (boost::str (boost::format ("Exception while initializing %1%") % e.what ())); } catch (...) { - show_error (boost::str (boost::format ("Unknown exception while initializing"))); + daemon.show_error (boost::str (boost::format ("Unknown exception while initializing"))); } return 1; } diff --git a/nano/node/scheduler/priority.cpp b/nano/node/scheduler/priority.cpp index 70efead805..054507317e 100644 --- a/nano/node/scheduler/priority.cpp +++ b/nano/node/scheduler/priority.cpp @@ -33,7 +33,7 @@ nano::scheduler::priority::priority (nano::node & node_a, nano::stats & stats_a) build_region (uint128_t{ 1 } << 116, uint128_t{ 1 } << 120, 2); minimums.push_back (uint128_t{ 1 } << 120); - node.logger.info (nano::log::type::election_scheduler, "Number of buckets: {}", minimums.size ()); + node.logger.debug (nano::log::type::election_scheduler, "Number of buckets: {}", minimums.size ()); for (size_t i = 0u, n = minimums.size (); i < n; ++i) {