Skip to content

Commit f120468

Browse files
ykaravasYiannis Karavas
authored and
Yiannis Karavas
committed
Update to config generation branch based on merge comments
Signed-off-by: Yiannis Karavas <[email protected]>
1 parent 21a8d69 commit f120468

7 files changed

+169
-122
lines changed

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -121,38 +121,38 @@ Additionally, you can start the atomizer architecture by passing `--file docker-
121121
## Setup test wallets and test them
122122

123123
The following commands are all performed from within the second container we started in the previous step.
124-
In each of the below commands, you should pass `atomizer-compose.cfg` instead of `2pc-compose.cfg` if you started the atomizer architecture.
124+
In each of the below commands, you should pass `config/general/atomizer-compose.cfg` instead of `config/general/2pc-compose.cfg` if you started the atomizer architecture.
125125

126126
* Mint new coins (e.g., 10 new UTXOs each with a value of 5 atomic units of currency)
127127
```terminal
128-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool0.dat wallet0.dat mint 10 5
128+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool0.dat wallet0.dat mint 10 5
129129
[2021-08-17 15:11:57.686] [WARN ] Existing wallet file not found
130130
[2021-08-17 15:11:57.686] [WARN ] Existing mempool not found
131131
4bc23da407c3a8110145c5b6c38199c8ec3b0e35ea66bbfd78f0ed65304ce6fa
132132
```
133133

134134
If using the atomizer architecture, you'll need to sync the wallet after:
135135
```terminal
136-
# ./build/src/uhs/client/client-cli atomizer-compose.cfg mempool0.dat wallet0.dat sync
136+
# ./build/src/uhs/client/client-cli config/general/atomizer-compose.cfg mempool0.dat wallet0.dat sync
137137
```
138138

139139
* Inspect the balance of a wallet
140140
```terminal
141-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool0.dat wallet0.dat info
141+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool0.dat wallet0.dat info
142142
Balance: $0.50, UTXOs: 10, pending TXs: 0
143143
```
144144

145145
* Make a new wallet
146146
```terminal
147-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool1.dat wallet1.dat newaddress
147+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool1.dat wallet1.dat newaddress
148148
[2021-08-17 15:13:16.148] [WARN ] Existing wallet file not found
149149
[2021-08-17 15:13:16.148] [WARN ] Existing mempool not found
150150
usd1qrw038lx5n4wxx3yvuwdndpr7gnm347d6pn37uywgudzq90w7fsuk52kd5u
151151
```
152152

153153
* Send currency from one wallet to another (e.g., 30 atomic units of currency)
154154
```terminal
155-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool0.dat wallet0.dat send 30 usd1qrw038lx5n4wxx3yvuwdndpr7gnm347d6pn37uywgudzq90w7fsuk52kd5u
155+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool0.dat wallet0.dat send 30 usd1qrw038lx5n4wxx3yvuwdndpr7gnm347d6pn37uywgudzq90w7fsuk52kd5u
156156
tx_id:
157157
cc1f7dc708be5b07e23e125cf0674002ff8546a9342928114bc97031d8b96e75
158158
Data for recipient importinput:
@@ -162,20 +162,20 @@ In each of the below commands, you should pass `atomizer-compose.cfg` instead of
162162

163163
If using the atomizer architecture, you'll need to sync the sending wallet after:
164164
```terminal
165-
# ./build/src/uhs/client/client-cli atomizer-compose.cfg mempool0.dat wallet0.dat sync
165+
# ./build/src/uhs/client/client-cli config/general/atomizer-compose.cfg mempool0.dat wallet0.dat sync
166166
```
167167

168168
* Check that the currency is no longer available in the sending wallet
169169
```terminal
170-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool0.dat wallet0.dat info
170+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool0.dat wallet0.dat info
171171
Balance: $0.20, UTXOs: 4, pending TXs: 0
172172
```
173173

174174
* Import coins to the receiving wallet
175175
```terminal
176-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool1.dat wallet1.dat importinput cc1f7dc708be5b07e23e125cf0674002ff8546a9342928114bc97031d8b96e750000000000000000d0e4f689b550f623e9370edae235de50417860be0f2f8e924eca9f402fcefeaa1e00000000000000
177-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool1.dat wallet1.dat sync
178-
# ./build/src/uhs/client/client-cli 2pc-compose.cfg mempool1.dat wallet1.dat info
176+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool1.dat wallet1.dat importinput cc1f7dc708be5b07e23e125cf0674002ff8546a9342928114bc97031d8b96e750000000000000000d0e4f689b550f623e9370edae235de50417860be0f2f8e924eca9f402fcefeaa1e00000000000000
177+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool1.dat wallet1.dat sync
178+
# ./build/src/uhs/client/client-cli config/general/2pc-compose.cfg mempool1.dat wallet1.dat info
179179
Balance: $0.30, UTXOs: 1, pending TXs: 0
180180
```
181181

config/tools/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
project(config_gen)
22

3-
include_directories(../../src ../../config/tools ../../3rdparty/secp256k1/include)
3+
include_directories(../../src ../../3rdparty/secp256k1/include)
44

55
add_library(config_generator config_generator.cpp)
66

7-
add_executable(generate_config generate_configd.cpp)
7+
add_executable(generate_config generate_config.cpp)
88

99
target_link_libraries(generate_config config_generator
1010
util

config/tools/config_generator.cpp

+72-62
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@
66

77
#include "config_generator.hpp"
88

9+
#include "util/common/config.hpp"
910
#include "util/common/keys.hpp"
11+
#include "util/common/variant_overloaded.hpp"
1012
#include "util/network/tcp_listener.hpp"
1113

1214
#include <algorithm>
1315
#include <cassert>
14-
#include <filesystem>
1516
#include <functional>
1617
#include <random>
1718
#include <set>
1819

1920
// NORMAL CONFIGS
2021

21-
// Integer that determines if this is 2PC or Atomizer. 1 means 2PC
22-
static constexpr auto two_phase_mode = "2pc";
23-
// Number of shards to be created
24-
static constexpr auto shard_count_key = "shard_count";
25-
// Number of sentinels to be created
26-
static constexpr auto sentinel_count_key = "sentinel_count";
27-
// Number of coordinators to be created
28-
static constexpr auto coordinator_count_key = "coordinator_count";
29-
// Number of archivers to be created
30-
static constexpr auto archiver_count_key = "archiver_count";
31-
// Number of atomizers to be created
32-
static constexpr auto atomizer_count_key = "atomizer_count";
33-
// Number of watchtowers to be created
34-
static constexpr auto watchtower_count_key = "watchtower_count";
22+
using namespace cbdc::config;
23+
24+
// Using the following values from src/common/config.hpp:
25+
26+
// "cbdc::config::two_phase_mode" : Name of value in config template file that
27+
// is an integer which determines if this is 2PC or Atomizer. 1 means 2PC
28+
// "cbdc::config::shard_count_key" : Number of shards to be created
29+
// "cbdc::config::sentinel_count_key" : Number of sentinels to be created
30+
// "cbdc::config::coordinator_count_key" : Number of coordinators to be created
31+
// "cbdc::config::archiver_count_key" : Number of archivers to be created
32+
// "cbdc::config::atomizer_count_key" : Number of atomizers to be created
33+
// "cbdc::config::watchtower_count_key" : Number of watchtowers to be created
34+
3535
// Prefix of interest that denotes parameters in the file that are used
3636
// here to help generate the config file but will not be present in the
3737
// final product
@@ -69,9 +69,41 @@ std::set<std::string> log_levels
6969
namespace cbdc::generate_config {
7070

7171
config_generator::config_generator(std::string& _template_config_file,
72-
const size_t _start_port)
72+
const size_t _start_port,
73+
std::string _build_dir)
7374
: m_template_config_file(_template_config_file),
7475
m_current_port(_start_port) {
76+
// Get Project root dir and build dir
77+
std::filesystem::path build_dir = std::filesystem::current_path();
78+
if(_build_dir.at(_build_dir.size() - 1) == '/') {
79+
_build_dir.erase(_build_dir.end() - 1);
80+
}
81+
// This config generator class assumes project root is "opencbdc-tx"
82+
while(build_dir.has_parent_path()) {
83+
if(build_dir.filename() == "opencbdc-tx") {
84+
m_project_root_dir = build_dir;
85+
std::string delimiter = "/";
86+
std::string tmp_str = _build_dir;
87+
size_t pos = 0;
88+
std::string token;
89+
while((pos = tmp_str.find("/")) != std::string::npos) {
90+
token = tmp_str.substr(0, pos);
91+
build_dir = build_dir.append(token);
92+
tmp_str.erase(0, pos + delimiter.length());
93+
}
94+
token = tmp_str.substr(0, pos);
95+
build_dir = build_dir.append(token);
96+
tmp_str.erase(0, pos + delimiter.length());
97+
m_build_dir = build_dir;
98+
std::cout << "Build directory determined to be "
99+
<< m_build_dir.string() << std::endl;
100+
std::cout << "Project Root directory determined to be "
101+
<< m_project_root_dir.string() << std::endl;
102+
break;
103+
} else {
104+
build_dir = build_dir.parent_path();
105+
}
106+
}
75107
template_file_is_valid = true;
76108
if(!std::filesystem::exists(m_template_config_file)) {
77109
template_file_is_valid = false;
@@ -335,7 +367,7 @@ namespace cbdc::generate_config {
335367
[[nodiscard]] auto config_generator::get_param_from_template_file(
336368
const std::string option,
337369
std::map<std::string, std::string>& config_map)
338-
-> std::variant<size_t, double, std::string> {
370+
-> std::variant<std::string, size_t, double> {
339371
auto it = config_map.find(option);
340372
if(it != config_map.end()) {
341373
value_t parsed_val = parse_value(it->second, false);
@@ -346,10 +378,7 @@ namespace cbdc::generate_config {
346378
} else if(std::holds_alternative<std::string>(parsed_val)) {
347379
return std::get<std::string>(parsed_val);
348380
} else {
349-
std::string error_msg
350-
= "Warning: Unrecognized type for param, " + option + ".";
351-
std::cout << error_msg << std::endl;
352-
return error_msg;
381+
__builtin_unreachable();
353382
}
354383
} else {
355384
std::string error_msg
@@ -376,13 +405,12 @@ namespace cbdc::generate_config {
376405

377406
void config_generator::set_log_level(const std::string key,
378407
std::string& log_level) {
379-
if(find_value(key, log_level) == false) {
380-
find_value(tmpl_universal_override_log_level, log_level);
381-
}
382408
if(log_levels.find(log_level) == log_levels.end()) {
383409
log_level = "DEBUG";
384410
std::cout << "Warning: Log level not recognized. Setting to DEBUG"
385411
<< std::endl;
412+
} else if(find_value(key, log_level) == false) {
413+
find_value(tmpl_universal_override_log_level, log_level);
386414
}
387415
}
388416

@@ -642,27 +670,16 @@ namespace cbdc::generate_config {
642670
outFile.close();
643671
}
644672

645-
// This method assumes project root is "opencbdc-tx" and build dir is
646-
// "build"
647673
[[nodiscard]] auto
648674
config_generator::copy_to_build_dir(const std::string filename) -> bool {
649675
std::filesystem::path cwd = std::filesystem::current_path();
650676
cwd.append(filename);
651-
std::filesystem::path build_dir = std::filesystem::current_path();
652-
while(build_dir.has_parent_path()) {
653-
if(build_dir.filename() == "opencbdc-tx") {
654-
build_dir = build_dir.append("build");
655-
break;
656-
} else {
657-
build_dir = build_dir.parent_path();
658-
}
659-
}
660677
if(std::filesystem::exists(filename)) {
661678
const auto copyOptions
662679
= std::filesystem::copy_options::overwrite_existing;
663680
// Copy and remove file if we are not in build currently
664-
if(std::filesystem::current_path().filename() != "build") {
665-
std::filesystem::copy(cwd, build_dir, copyOptions);
681+
if(std::filesystem::current_path() != m_build_dir) {
682+
std::filesystem::copy(cwd, m_build_dir, copyOptions);
666683
std::filesystem::remove(cwd);
667684
}
668685
return true;
@@ -671,24 +688,11 @@ namespace cbdc::generate_config {
671688
}
672689
}
673690

674-
// This method assumes project root is "opencbdc-tx" and build dir is
675-
// "build"
676691
void config_generator::copy_templates_to_build_dir() {
677-
std::filesystem::path config_dir = std::filesystem::current_path();
678-
std::filesystem::path build_dir = std::filesystem::current_path();
679-
while(config_dir.has_parent_path()) {
680-
if(config_dir.filename() == "opencbdc-tx") {
681-
config_dir = config_dir.append("config");
682-
config_dir = config_dir.append("tools");
683-
build_dir = build_dir.append("build");
684-
build_dir = build_dir.append("config");
685-
build_dir = build_dir.append("tools");
686-
break;
687-
} else {
688-
config_dir = config_dir.parent_path();
689-
build_dir = build_dir.parent_path();
690-
}
691-
}
692+
std::filesystem::path config_dir = m_project_root_dir;
693+
config_dir.append("config").append("tools");
694+
std::filesystem::path build_config_dir = m_build_dir;
695+
build_config_dir.append("config").append("tools");
692696
for(auto const& dir_entry :
693697
std::filesystem::directory_iterator{config_dir}) {
694698
std::string filename = dir_entry.path().filename();
@@ -697,7 +701,11 @@ namespace cbdc::generate_config {
697701
if(tmp_str == match_str) {
698702
const auto copyOptions
699703
= std::filesystem::copy_options::overwrite_existing;
700-
std::filesystem::copy(dir_entry, build_dir, copyOptions);
704+
std::filesystem::copy(dir_entry,
705+
build_config_dir,
706+
copyOptions);
707+
std::cout << "Copying " << dir_entry.path().string() << " to "
708+
<< build_config_dir.string() << std::endl;
701709
}
702710
}
703711
}
@@ -728,8 +736,13 @@ namespace cbdc::generate_config {
728736
std::string output_filename = "tmp.cfg";
729737

730738
if(!template_file_is_valid) {
739+
std::filesystem::path temp_build_dir = m_build_dir;
740+
temp_build_dir.append("config").append("tools");
731741
return_msg += "File provided, " + m_template_config_file
732-
+ ", does not exist. Aborting operation. \n";
742+
+ ", did not exist but has likely been copied to "
743+
+ temp_build_dir.string()
744+
+ ". Aborting operation. Please rerun with proper "
745+
"template location \n";
733746
return return_msg;
734747
}
735748
std::map<std::string, std::string> config_map;
@@ -757,8 +770,7 @@ namespace cbdc::generate_config {
757770
config_map_it->first,
758771
std::get<std::string>(parsed_val));
759772
} else {
760-
return_msg += "Warning: Unrecognized type for param, "
761-
+ config_map_it->first + ". \n";
773+
__builtin_unreachable();
762774
}
763775
}
764776
}
@@ -794,8 +806,7 @@ namespace cbdc::generate_config {
794806
return_msg
795807
+= "Warning: Two-phase mode requires at least one "
796808
"configured shard. Fix configuration template "
797-
"and "
798-
"rerun.\n";
809+
"and rerun.\n";
799810
return return_msg;
800811
} else {
801812
create_2pc_component(shard_count_key,
@@ -853,8 +864,7 @@ namespace cbdc::generate_config {
853864
return_msg
854865
+= "Warning: Sentinels require at least one "
855866
"configured shard. Fix configuration template "
856-
"and "
857-
"rerun. \n";
867+
"and rerun. \n";
858868
return return_msg;
859869
} else {
860870
create_atomizer_component(

config/tools/config_generator.hpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "util/common/config.hpp"
1111
#include "util/common/random_source.hpp"
1212

13+
#include <filesystem>
1314
#include <random>
1415
#include <secp256k1.h>
1516

@@ -45,7 +46,8 @@ namespace cbdc::generate_config {
4546
/// \param _start_port Port to begin using and incrementing from for generated
4647
/// configuration file's endpoints
4748
config_generator(std::string& _template_config_file,
48-
size_t _start_port);
49+
size_t _start_port,
50+
std::string _build_dir);
4951

5052
~config_generator() = default;
5153

@@ -64,6 +66,10 @@ namespace cbdc::generate_config {
6466
std::string& m_template_config_file;
6567
// Incrementing port to use in config file for all ports
6668
unsigned short m_current_port;
69+
// Build directory
70+
std::filesystem::path m_build_dir;
71+
// Project root directory
72+
std::filesystem::path m_project_root_dir;
6773
// Map with shard ranges (shard_id, (start range, end_range)
6874
std::vector<ShardInfo> shard_info;
6975
std::default_random_engine generator;
@@ -115,7 +121,7 @@ namespace cbdc::generate_config {
115121
[[nodiscard]] auto get_param_from_template_file(
116122
std::string option,
117123
std::map<std::string, std::string>& config_map)
118-
-> std::variant<size_t, double, std::string>;
124+
-> std::variant<std::string, size_t, double>;
119125
void set_param_to_config_file(std::string key, std::string value);
120126
void set_param_to_config_file(std::string key, size_t value);
121127
void set_param_to_config_file(std::string key, double value);

0 commit comments

Comments
 (0)