Skip to content

Commit

Permalink
Remove outdated code, add support for seeding
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
maurermi committed Feb 22, 2023
1 parent 44ab494 commit 65afd30
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 91 deletions.
8 changes: 2 additions & 6 deletions tests/unit/config_gen_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
85 changes: 16 additions & 69 deletions tools/config_generator/config_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,77 +74,16 @@ static const std::set<std::string> 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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -855,9 +794,17 @@ namespace cbdc::generate_config {
}
// add support for pre-seeding
// only has effect if pre-seeding is active
std::pair<std::string, std::string> 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<std::string>(seed_begin)
&& !std::holds_alternative<std::string>(seed_end)
&& std::get<size_t>(seed_begin) != std::get<size_t>(seed_end)) {
std::pair<std::string, std::string> 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;
}
Expand Down
5 changes: 2 additions & 3 deletions tools/config_generator/config_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 3 additions & 13 deletions tools/config_generator/generate_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ auto main(int argc, char** argv) -> int {
// Help string
std::string help_string
= "Usage: " + args[0]
+ " <config template file> <starting port number> <build "
"directory> \n\nPARAM 1, <config template file> : The relative "
+ " <config template file> <starting port number> > <config "
"file name> \n\nPARAM 1, <config template file> : The relative "
"path from current working directory to the template configuration "
"file including the filename itself.\nPARAM 2, <starting port "
"number> : The first port number to use and increment from. Must be "
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 65afd30

Please sign in to comment.