From 65afd30ede80dca65d62c76dc5106435e1645fc0 Mon Sep 17 00:00:00 2001 From: Michael Maurer Date: Wed, 22 Feb 2023 11:56:55 -0500 Subject: [PATCH] Remove outdated code, add support for seeding Removes identification of build directory, as sending file to build directory has been replaced by priting config file to stdout. Also adds support for seeding by generating a seed key if necessary. Signed-off-by: Michael Maurer --- tests/unit/config_gen_test.cpp | 8 +- tools/config_generator/config_generator.cpp | 85 ++++----------------- tools/config_generator/config_generator.hpp | 5 +- tools/config_generator/generate_config.cpp | 16 +--- 4 files changed, 23 insertions(+), 91 deletions(-) diff --git a/tests/unit/config_gen_test.cpp b/tests/unit/config_gen_test.cpp index 03c2f30a3..0dff5b130 100644 --- a/tests/unit/config_gen_test.cpp +++ b/tests/unit/config_gen_test.cpp @@ -32,11 +32,9 @@ TEST_F(config_generation_validation_test, // Assumes build dir is "build". Cannot find a way around this since unit // tests can be run from either root dir or build dir and we don't // necessarily know which one - std::string build_dir = "build"; cbdc::generate_config::config_generator new_config_gen( template_file_atomizer, - port_num, - build_dir); + port_num); auto cfg_or_err = new_config_gen.generate_configuration_file(); ASSERT_EQ(cfg_or_err, "SUCCESS"); // TODO @@ -49,10 +47,8 @@ TEST_F(config_generation_validation_test, // Assumes build dir is "build". Cannot find a way around this since unit // tests can be run from either root dir or build dir and we don't // necessarily know which one - std::string build_dir = "build"; cbdc::generate_config::config_generator new_config_gen(template_file_2pc, - port_num, - build_dir); + port_num); auto cfg_or_err = new_config_gen.generate_configuration_file(); ASSERT_EQ(cfg_or_err, "SUCCESS"); // TODO diff --git a/tools/config_generator/config_generator.cpp b/tools/config_generator/config_generator.cpp index aad4f53b9..6a334790f 100644 --- a/tools/config_generator/config_generator.cpp +++ b/tools/config_generator/config_generator.cpp @@ -74,77 +74,16 @@ static const std::set log_levels namespace cbdc::generate_config { config_generator::config_generator(std::string& _template_config_file, - const size_t _start_port, - std::string _build_dir) + const size_t _start_port) : m_template_config_file(_template_config_file), m_current_port(_start_port) { // Get Project root dir and build dir std::filesystem::path build_dir = std::filesystem::current_path(); - if(_build_dir.at(_build_dir.size() - 1) == '/') { - _build_dir.erase(_build_dir.end() - 1); - } // This config generator class assumes project root is "opencbdc-tx" - while(build_dir.has_parent_path()) { - if(build_dir.filename() != "opencbdc-tx") { - build_dir = build_dir.parent_path(); - } else { - m_project_root_dir = build_dir; - std::string delimiter = "/"; - std::string tmp_str = _build_dir; - size_t pos = 0; - std::string token; - while((pos = tmp_str.find('/')) != std::string::npos) { - token = tmp_str.substr(0, pos); - build_dir = build_dir.append(token); - tmp_str.erase(0, pos + delimiter.length()); - } - token = tmp_str.substr(0, pos); - build_dir = build_dir.append(token); - tmp_str.erase(0, pos + delimiter.length()); - m_build_dir = build_dir; - std::cerr << "Build directory determined to be " - << m_build_dir.string() << std::endl; - std::cerr << "Project Root directory determined to be " - << m_project_root_dir.string() << std::endl; - break; - } - } - template_file_is_valid = true; if(!std::filesystem::exists(m_template_config_file)) { - template_file_is_valid = false; - std::filesystem::path temp_config_tools_dir = m_project_root_dir; - temp_config_tools_dir.append("config").append("tools"); - std::filesystem::path temp_build_config_tools_dir = m_build_dir; - temp_build_config_tools_dir.append("config").append("tools"); - std::cerr << "Warning: File provided, " << m_template_config_file - << ", does not exist. Attempting to copy it from its " - "original location, " - << temp_config_tools_dir.string() << " to " - << temp_build_config_tools_dir.string() << std::endl; - copy_templates_to_build_dir(); - // Try to use newly copied template files - std::string delimiter = "/"; - std::string tmp_str = m_template_config_file; - size_t pos = 0; - std::string template_filename; - while((pos = tmp_str.find('/')) != std::string::npos) { - template_filename = tmp_str.substr(0, pos); - tmp_str.erase(0, pos + delimiter.length()); - } - template_filename = tmp_str.substr(0, pos); - std::filesystem::path full_template_path_and_filename - = temp_build_config_tools_dir.append(template_filename); - if(std::filesystem::exists(full_template_path_and_filename)) { - m_template_config_file - = full_template_path_and_filename.string(); - template_file_is_valid = true; - std::cerr << "Successfully copied " << template_filename - << " from " << temp_config_tools_dir.string() - << " to " << temp_build_config_tools_dir.string() - << ". Using " - << full_template_path_and_filename.string() - << " as template file." << std::endl; - } + m_template_file_is_valid = false; + std::cerr << "template file cannot be found at " + << _template_config_file << std::endl; } } @@ -749,7 +688,7 @@ namespace cbdc::generate_config { // Cumulative return message std::string return_msg; - if(!template_file_is_valid) { + if(!m_template_file_is_valid) { std::filesystem::path temp_build_dir = m_build_dir; temp_build_dir.append("config").append("tools"); return_msg += "File provided, " + m_template_config_file @@ -855,9 +794,17 @@ namespace cbdc::generate_config { } // add support for pre-seeding // only has effect if pre-seeding is active - std::pair key_pair = create_key_pair(); - set_param_to_config_file(seed_privkey, key_pair.first); - write_generated_config_to_file(); + const auto seed_begin + = get_param_from_template_file(seed_from, config_map); + const auto seed_end + = get_param_from_template_file(seed_to, config_map); + if(!std::holds_alternative(seed_begin) + && !std::holds_alternative(seed_end) + && std::get(seed_begin) != std::get(seed_end)) { + std::pair key_pair = create_key_pair(); + set_param_to_config_file(seed_privkey, key_pair.first); + write_generated_config_to_file(); + } return_msg += "SUCCESS"; return return_msg; } diff --git a/tools/config_generator/config_generator.hpp b/tools/config_generator/config_generator.hpp index 5c1df9e93..7b93554eb 100644 --- a/tools/config_generator/config_generator.hpp +++ b/tools/config_generator/config_generator.hpp @@ -46,8 +46,7 @@ namespace cbdc::generate_config { /// \param _start_port Port to begin using and incrementing from for generated /// configuration file's endpoints config_generator(std::string& _template_config_file, - size_t _start_port, - std::string _build_dir); + size_t _start_port); /// \brief generate_configuration_file /// Main workhorse method of this class. This method will generate a @@ -59,7 +58,7 @@ namespace cbdc::generate_config { private: // Boolean that tells us if file is valid or not - bool template_file_is_valid; + bool m_template_file_is_valid{true}; // Whether to randomize things that can be randomized bool m_randomize{false}; // Template file loaded to create configuration file from diff --git a/tools/config_generator/generate_config.cpp b/tools/config_generator/generate_config.cpp index ecda80bc6..02b7274aa 100644 --- a/tools/config_generator/generate_config.cpp +++ b/tools/config_generator/generate_config.cpp @@ -18,8 +18,8 @@ auto main(int argc, char** argv) -> int { // Help string std::string help_string = "Usage: " + args[0] - + " \n\nPARAM 1, : The relative " + + " > \n\nPARAM 1, : The relative " "path from current working directory to the template configuration " "file including the filename itself.\nPARAM 2, : The first port number to use and increment from. Must be " @@ -41,14 +41,6 @@ auto main(int argc, char** argv) -> int { << std::endl << "Rerun with proper parameters." << std::endl; valid_config = false; - } else if(args.size() == min_param_num) { - // Case where user does not input a build dir - std::cerr << "No build directory name specified as third. Using " - "default name of 'build'" - << std::endl; - } else if(args.size() == max_param_num) { - // Case where user DOES input build dir - build_dir = args[max_param_num - 1]; } if(!valid_config) { return -1; @@ -69,9 +61,7 @@ auto main(int argc, char** argv) -> int { << ", is too large. Exiting..." << std::endl; return -1; } - cbdc::generate_config::config_generator new_config_gen(args[1], - port_num, - build_dir); + cbdc::generate_config::config_generator new_config_gen(args[1], port_num); auto cfg_or_err = new_config_gen.generate_configuration_file(); std::cerr << cfg_or_err << std::endl; std::cout << std::endl;