From 3520e17836f193289c7efc5cfbbeaf3f8654c82f Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 2 Jul 2020 23:16:47 +0300 Subject: [PATCH] --stop-after-height implemented --- src/common/command_line.cpp | 2 ++ src/common/command_line.h | 1 + src/currency_core/currency_core.cpp | 13 +++++++++++++ src/currency_core/currency_core.h | 1 + .../currency_protocol_handler_common.h | 2 ++ src/daemon/daemon.cpp | 11 +++++++++++ src/wallet/wallets_manager.cpp | 1 + tests/core_tests/chaingen_main.cpp | 2 ++ 8 files changed, 33 insertions(+) diff --git a/src/common/command_line.cpp b/src/common/command_line.cpp index 645057eb..92a59b61 100644 --- a/src/common/command_line.cpp +++ b/src/common/command_line.cpp @@ -13,6 +13,8 @@ namespace command_line const arg_descriptor arg_help = {"help", "Produce help message"}; const arg_descriptor arg_version = {"version", "Output version information"}; const arg_descriptor arg_data_dir = {"data-dir", "Specify data directory", ""}; + + const arg_descriptor arg_stop_after_height = { "stop-after-height", "If specified, the daemon will stop immediately after a block with the given height is added", 0 }; const arg_descriptor arg_config_file = { "config-file", "Specify configuration file", std::string(CURRENCY_NAME_SHORT ".conf") }; const arg_descriptor arg_os_version = { "os-version", "" }; diff --git a/src/common/command_line.h b/src/common/command_line.h index 9d6bec47..f19a04ae 100644 --- a/src/common/command_line.h +++ b/src/common/command_line.h @@ -198,6 +198,7 @@ namespace command_line extern const arg_descriptor arg_help; extern const arg_descriptor arg_version; extern const arg_descriptor arg_data_dir; + extern const arg_descriptor arg_stop_after_height; extern const arg_descriptor arg_config_file; extern const arg_descriptor arg_os_version; extern const arg_descriptor arg_log_dir; diff --git a/src/currency_core/currency_core.cpp b/src/currency_core/currency_core.cpp index 05f2eeb9..2c397b36 100644 --- a/src/currency_core/currency_core.cpp +++ b/src/currency_core/currency_core.cpp @@ -75,6 +75,11 @@ namespace currency bool core::handle_command_line(const boost::program_options::variables_map& vm) { m_config_folder = command_line::get_arg(vm, command_line::arg_data_dir); + m_stop_after_height = static_cast(command_line::get_arg(vm, command_line::arg_stop_after_height)); + if (m_stop_after_height != 0) + { + LOG_PRINT_YELLOW("Daemon will STOP after block " << m_stop_after_height, LOG_LEVEL_0); + } return true; } //----------------------------------------------------------------------------------------------- @@ -502,6 +507,14 @@ namespace currency { LOG_PRINT_GREEN("Hardfork 1 activated at height " << h, LOG_LEVEL_0); } else if (h == crc.hard_fork_02_starts_after_height + 1) { LOG_PRINT_GREEN("Hardfork 2 activated at height " << h, LOG_LEVEL_0); } + + if (h == m_stop_after_height) + { + LOG_PRINT_YELLOW("Reached block " << h << ", the daemon will now stop as requested", LOG_LEVEL_0); + if (m_critical_error_handler) + return m_critical_error_handler->on_immediate_stop_requested(); + return false; + } } return r; } diff --git a/src/currency_core/currency_core.h b/src/currency_core/currency_core.h index 68d9bead..354fd772 100644 --- a/src/currency_core/currency_core.h +++ b/src/currency_core/currency_core.h @@ -143,6 +143,7 @@ namespace currency miner m_miner; account_public_address m_miner_address; std::string m_config_folder; + uint64_t m_stop_after_height; currency_protocol_stub m_protocol_stub; math_helper::once_a_time_seconds<60*60*12, false> m_prune_alt_blocks_interval; math_helper::once_a_time_seconds<60, true> m_check_free_space_interval; diff --git a/src/currency_protocol/currency_protocol_handler_common.h b/src/currency_protocol/currency_protocol_handler_common.h index 1d965695..2bff506c 100644 --- a/src/currency_protocol/currency_protocol_handler_common.h +++ b/src/currency_protocol/currency_protocol_handler_common.h @@ -47,6 +47,8 @@ namespace currency virtual bool on_critical_time_sync_error() = 0; virtual bool on_critical_low_free_space(uint64_t available, uint64_t required) = 0; + + virtual bool on_immediate_stop_requested() = 0; }; diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index a9b9ad23..8cbaf4f1 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -88,6 +88,16 @@ struct core_critical_error_handler_t : public currency::i_critical_error_handler */ } + virtual bool on_immediate_stop_requested() override + { + LOG_PRINT_L0(ENDL << ENDL << "immediate daemon stop requested, stopping..." << ENDL << ENDL); + + dch.stop_handling(); + p2psrv.send_stop_signal(); + return true; // the caller must stop processing + } + + daemon_commands_handler& dch; p2psrv_t& p2psrv; bool dont_stop_on_time_error; @@ -137,6 +147,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_only, command_line::arg_os_version); // tools::get_default_data_dir() can't be called during static initialization command_line::add_arg(desc_cmd_only, command_line::arg_data_dir, tools::get_default_data_dir()); + command_line::add_arg(desc_cmd_only, command_line::arg_stop_after_height); command_line::add_arg(desc_cmd_only, command_line::arg_config_file); command_line::add_arg(desc_cmd_only, command_line::arg_disable_upnp); diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 042bd9d6..0faa603a 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -117,6 +117,7 @@ bool wallets_manager::init_command_line(int argc, char* argv[]) command_line::add_arg(desc_cmd_only, command_line::arg_os_version); // tools::get_default_data_dir() can't be called during static initialization command_line::add_arg(desc_cmd_only, command_line::arg_data_dir, tools::get_default_data_dir()); + command_line::add_arg(desc_cmd_only, command_line::arg_stop_after_height); command_line::add_arg(desc_cmd_only, command_line::arg_config_file); command_line::add_arg(desc_cmd_sett, command_line::arg_log_dir); diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index afbf28ad..be070ceb 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -666,6 +666,8 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_options, arg_run_single_test); command_line::add_arg(desc_options, arg_enable_debug_asserts); command_line::add_arg(desc_options, command_line::arg_data_dir, std::string(".")); + command_line::add_arg(desc_options, command_line::arg_stop_after_height); + currency::core::init_options(desc_options); tools::db::db_backend_selector::init_options(desc_options);