From 8a20fd2bc84d74917381fb50362b4d1a385256bd Mon Sep 17 00:00:00 2001 From: jeffro256 Date: Wed, 22 Apr 2026 00:27:53 -0500 Subject: [PATCH] blockchain_utilities: fix --data-dir option --- src/blockchain_utilities/blockchain_ancestry.cpp | 5 ++--- src/blockchain_utilities/blockchain_depth.cpp | 5 ++--- src/blockchain_utilities/blockchain_export.cpp | 11 +++-------- src/blockchain_utilities/blockchain_import.cpp | 7 ------- src/blockchain_utilities/blockchain_prune.cpp | 5 ++--- .../blockchain_prune_known_spent_data.cpp | 5 ++--- src/blockchain_utilities/blockchain_stats.cpp | 5 ++--- src/blockchain_utilities/blockchain_usage.cpp | 5 ++--- src/cryptonote_core/cryptonote_core.cpp | 14 +++++++++++--- src/cryptonote_core/cryptonote_core.h | 9 +++++++++ src/daemon/main.cpp | 6 ++---- 11 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/blockchain_utilities/blockchain_ancestry.cpp b/src/blockchain_utilities/blockchain_ancestry.cpp index b0964e4a3..9de1e4420 100644 --- a/src/blockchain_utilities/blockchain_ancestry.cpp +++ b/src/blockchain_utilities/blockchain_ancestry.cpp @@ -358,6 +358,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir); command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); + command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_txid); command_line::add_arg(desc_cmd_sett, arg_output); @@ -400,9 +401,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Starting..."); std::string opt_data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir); - bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET; + const network_type net_type = core::get_network_type_from_args(vm); std::string opt_txid_string = command_line::get_arg(vm, arg_txid); std::string opt_output_string = command_line::get_arg(vm, arg_output); uint64_t opt_height = command_line::get_arg(vm, arg_height); diff --git a/src/blockchain_utilities/blockchain_depth.cpp b/src/blockchain_utilities/blockchain_depth.cpp index b98a1f8e2..0ea6d7df1 100644 --- a/src/blockchain_utilities/blockchain_depth.cpp +++ b/src/blockchain_utilities/blockchain_depth.cpp @@ -66,6 +66,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir); command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); + command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_txid); command_line::add_arg(desc_cmd_sett, arg_height); @@ -102,9 +103,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Starting..."); std::string opt_data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir); - bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET; + const network_type net_type = core::get_network_type_from_args(vm); std::string opt_txid_string = command_line::get_arg(vm, arg_txid); uint64_t opt_height = command_line::get_arg(vm, arg_height); bool opt_include_coinbase = command_line::get_arg(vm, arg_include_coinbase); diff --git a/src/blockchain_utilities/blockchain_export.cpp b/src/blockchain_utilities/blockchain_export.cpp index 82fe524de..2b58124a3 100644 --- a/src/blockchain_utilities/blockchain_export.cpp +++ b/src/blockchain_utilities/blockchain_export.cpp @@ -68,6 +68,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_sett, arg_output_file); command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); + command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_block_start); command_line::add_arg(desc_cmd_sett, arg_block_stop); @@ -105,13 +106,6 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Starting..."); - bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - if (opt_testnet && opt_stagenet) - { - std::cerr << "Can't specify more than one of --testnet and --stagenet" << std::endl; - return 1; - } bool opt_blocks_dat = command_line::get_arg(vm, arg_blocks_dat); std::string m_config_folder; @@ -162,7 +156,8 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Error opening database: " << e.what()); return 1; } - r = core_storage->init(db, opt_testnet ? cryptonote::TESTNET : opt_stagenet ? cryptonote::STAGENET : cryptonote::MAINNET); + const network_type net_type = core::get_network_type_from_args(vm); + r = core_storage->init(db, net_type); if (core_storage->get_blockchain_pruning_seed() && !opt_blocks_dat) { diff --git a/src/blockchain_utilities/blockchain_import.cpp b/src/blockchain_utilities/blockchain_import.cpp index 8a25e1da9..58e152e4d 100644 --- a/src/blockchain_utilities/blockchain_import.cpp +++ b/src/blockchain_utilities/blockchain_import.cpp @@ -668,13 +668,6 @@ int main(int argc, char* argv[]) } } - opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - if (opt_testnet && opt_stagenet) - { - std::cerr << "Error: Can't specify more than one of --testnet and --stagenet" << ENDL; - return 1; - } m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir); mlog_configure(mlog_get_default_log_path("monero-blockchain-import.log"), true); diff --git a/src/blockchain_utilities/blockchain_prune.cpp b/src/blockchain_utilities/blockchain_prune.cpp index 4a91cf7cc..d99a46632 100644 --- a/src/blockchain_utilities/blockchain_prune.cpp +++ b/src/blockchain_utilities/blockchain_prune.cpp @@ -462,6 +462,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir); command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); + command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_db_sync_mode); command_line::add_arg(desc_cmd_sett, arg_copy_pruned_database); @@ -496,9 +497,7 @@ int main(int argc, char* argv[]) MINFO("Starting..."); - bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET; + const network_type net_type = core::get_network_type_from_args(vm); bool opt_copy_pruned_database = command_line::get_arg(vm, arg_copy_pruned_database); std::string data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir); while (boost::ends_with(data_dir, "/") || boost::ends_with(data_dir, "\\")) diff --git a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp index 05aaf42ee..08eb6ee0e 100644 --- a/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp +++ b/src/blockchain_utilities/blockchain_prune_known_spent_data.cpp @@ -115,6 +115,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir); command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); + command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_verbose); command_line::add_arg(desc_cmd_sett, arg_dry_run); @@ -151,9 +152,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Starting..."); std::string opt_data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir); - bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET; + const network_type net_type = core::get_network_type_from_args(vm); bool opt_verbose = command_line::get_arg(vm, arg_verbose); bool opt_dry_run = command_line::get_arg(vm, arg_dry_run); diff --git a/src/blockchain_utilities/blockchain_stats.cpp b/src/blockchain_utilities/blockchain_stats.cpp index 21040a1d8..fc082b4e5 100644 --- a/src/blockchain_utilities/blockchain_stats.cpp +++ b/src/blockchain_utilities/blockchain_stats.cpp @@ -147,6 +147,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_sett, cryptonote::arg_data_dir); command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); + command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_block_start); command_line::add_arg(desc_cmd_sett, arg_block_stop); @@ -189,9 +190,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Starting..."); std::string opt_data_dir = command_line::get_arg(vm, cryptonote::arg_data_dir); - bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET; + const network_type net_type = core::get_network_type_from_args(vm); block_start = command_line::get_arg(vm, arg_block_start); block_stop = command_line::get_arg(vm, arg_block_stop); do_inputs = command_line::get_arg(vm, arg_inputs); diff --git a/src/blockchain_utilities/blockchain_usage.cpp b/src/blockchain_utilities/blockchain_usage.cpp index 129a9be21..1cc785283 100644 --- a/src/blockchain_utilities/blockchain_usage.cpp +++ b/src/blockchain_utilities/blockchain_usage.cpp @@ -96,6 +96,7 @@ int main(int argc, char* argv[]) command_line::add_arg(desc_cmd_sett, cryptonote::arg_testnet_on); command_line::add_arg(desc_cmd_sett, cryptonote::arg_stagenet_on); + command_line::add_arg(desc_cmd_sett, cryptonote::arg_regtest_on); command_line::add_arg(desc_cmd_sett, arg_log_level); command_line::add_arg(desc_cmd_sett, arg_rct_only); command_line::add_arg(desc_cmd_sett, arg_input); @@ -133,9 +134,7 @@ int main(int argc, char* argv[]) LOG_PRINT_L0("Starting..."); - bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - network_type net_type = opt_testnet ? TESTNET : opt_stagenet ? STAGENET : MAINNET; + const network_type net_type = core::get_network_type_from_args(vm); bool opt_rct_only = command_line::get_arg(vm, arg_rct_only); // If we wanted to use the memory pool, we would set up a fake_core. diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 4bada999f..18625de98 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -346,13 +346,21 @@ namespace cryptonote BlockchainDB::init_options(desc); } //----------------------------------------------------------------------------------------------- + network_type core::get_network_type_from_args(const boost::program_options::variables_map& vm) + { + const bool testnet = command_line::get_arg(vm, arg_testnet_on); + const bool stagenet = command_line::get_arg(vm, arg_stagenet_on); + const bool regtest = command_line::get_arg(vm, arg_regtest_on); + if (testnet + stagenet + regtest > 1) + throw std::runtime_error("More than one network type argument was specified"); + return testnet ? TESTNET : stagenet ? STAGENET : regtest ? FAKECHAIN : MAINNET; + } + //----------------------------------------------------------------------------------------------- bool core::handle_command_line(const boost::program_options::variables_map& vm) { if (m_nettype != FAKECHAIN) { - const bool testnet = command_line::get_arg(vm, arg_testnet_on); - const bool stagenet = command_line::get_arg(vm, arg_stagenet_on); - m_nettype = testnet ? TESTNET : stagenet ? STAGENET : MAINNET; + m_nettype = get_network_type_from_args(vm); } m_config_folder = command_line::get_arg(vm, arg_data_dir); diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index 7806fe495..777de3319 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -276,6 +276,15 @@ namespace cryptonote */ static void init_options(boost::program_options::options_description& desc); + /** + * @brief resolves the network type based on command line arguments + * @param vm variables map + * @return network type corresponding to arg_{testnet,stagenet,regtest}_on, defaulting to MAINNET + * @throw std::runtime_error if more than 1 of arg_{testnet,stagenet,regtest}_on is present + * @throw boost::bad_any_cast if arg_{testnet,stagenet,regtest}_on weren't added to vm + */ + static network_type get_network_type_from_args(const boost::program_options::variables_map& vm); + /** * @brief initializes the core as needed * diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 39ef04772..a61fbda6f 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -247,10 +247,8 @@ int main(int argc, char const * argv[]) return 1; } - const bool testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); - const bool stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on); - const bool regtest = command_line::get_arg(vm, cryptonote::arg_regtest_on); - if (testnet + stagenet + regtest > 1) + try { cryptonote::core::get_network_type_from_args(vm); } + catch (const std::runtime_error&) { std::cerr << "Can't specify more than one of --tesnet and --stagenet and --regtest" << ENDL; return 1;