OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
config.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
12#ifndef OPENCBDC_TX_SRC_COMMON_CONFIG_H_
13#define OPENCBDC_TX_SRC_COMMON_CONFIG_H_
14
15#include "hash.hpp"
16#include "hashmap.hpp"
17#include "keys.hpp"
18#include "logging.hpp"
20
21#include <map>
22#include <optional>
23#include <string>
24#include <unordered_map>
25#include <unordered_set>
26#include <variant>
27#include <vector>
28
29namespace cbdc::config {
31 static constexpr const char* random_source{"/dev/urandom"};
32
34 static const std::string bech32_hrp = "usd";
35
37 static constexpr const char* currency_symbol{"$"};
38
44 static constexpr uint64_t maximum_reservation
45 = static_cast<uint64_t>(1024 * 1024); // 1MiB
46
47 namespace defaults {
48 static constexpr size_t stxo_cache_depth{1};
49 static constexpr size_t window_size{10000};
50 static constexpr size_t shard_completed_txs_cache_size{10000000};
51 static constexpr size_t batch_size{2000};
52 static constexpr size_t target_block_interval{250};
53 static constexpr int32_t election_timeout_upper_bound{4000};
54 static constexpr int32_t election_timeout_lower_bound{2000};
55 static constexpr int32_t heartbeat{1000};
56 static constexpr int32_t raft_max_batch{100000};
57 static constexpr size_t coordinator_max_threads{75};
58 static constexpr size_t initial_mint_count{20000};
59 static constexpr size_t initial_mint_value{100};
60 static constexpr size_t watchtower_block_cache_size{100};
61 static constexpr size_t watchtower_error_cache_size{1000000};
62 static constexpr size_t input_count{2};
63 static constexpr size_t output_count{2};
64 static constexpr double fixed_tx_rate{1.0};
65 static constexpr size_t attestation_threshold{1};
66
67 static constexpr auto log_level = logging::log_level::warn;
68 }
69
70 static constexpr auto endpoint_postfix = "endpoint";
71 static constexpr auto loglevel_postfix = "loglevel";
72 static constexpr auto raft_endpoint_postfix = "raft_endpoint";
73 static constexpr auto stxo_cache_key = "stxo_cache_depth";
74 static constexpr auto shard_count_key = "shard_count";
75 static constexpr auto shard_prefix = "shard";
76 static constexpr auto seed_privkey = "seed_privkey";
77 static constexpr auto seed_value = "seed_value";
78 static constexpr auto seed_from = "seed_from";
79 static constexpr auto seed_to = "seed_to";
80 static constexpr auto atomizer_prefix = "atomizer";
81 static constexpr auto sentinel_count_key = "sentinel_count";
82 static constexpr auto sentinel_prefix = "sentinel";
83 static constexpr auto config_separator = "_";
84 static constexpr auto db_postfix = "db";
85 static constexpr auto start_postfix = "start";
86 static constexpr auto end_postfix = "end";
87 static constexpr auto atomizer_count_key = "atomizer_count";
88 static constexpr auto archiver_prefix = "archiver";
89 static constexpr auto batch_size_key = "batch_size";
90 static constexpr auto window_size_key = "window_size";
91 static constexpr auto target_block_interval_key = "target_block_interval";
92 static constexpr auto election_timeout_upper_key
93 = "election_timeout_upper";
94 static constexpr auto election_timeout_lower_key
95 = "election_timeout_lower";
96 static constexpr auto heartbeat_key = "heartbeat";
97 static constexpr auto snapshot_distance_key = "snapshot_distance";
98 static constexpr auto raft_batch_size_key = "raft_max_batch";
99 static constexpr auto input_count_key = "loadgen_sendtx_input_count";
100 static constexpr auto output_count_key = "loadgen_sendtx_output_count";
101 static constexpr auto invalid_rate_key = "loadgen_invalid_tx_rate";
102 static constexpr auto fixed_tx_rate_key = "loadgen_fixed_tx_rate";
103 static constexpr auto archiver_count_key = "archiver_count";
104 static constexpr auto watchtower_count_key = "watchtower_count";
105 static constexpr auto watchtower_prefix = "watchtower";
106 static constexpr auto watchtower_client_ep_postfix = "client_endpoint";
107 static constexpr auto watchtower_internal_ep_postfix = "internal_endpoint";
108 static constexpr auto watchtower_block_cache_size_key
109 = "watchtower_block_cache_size";
110 static constexpr auto watchtower_error_cache_size_key
111 = "watchtower_error_cache_size";
112 static constexpr auto two_phase_mode = "2pc";
113 static constexpr auto count_postfix = "count";
114 static constexpr auto readonly = "readonly";
115 static constexpr auto coordinator_prefix = "coordinator";
116 static constexpr auto coordinator_count_key = "coordinator_count";
117 static constexpr auto coordinator_max_threads = "coordinator_max_threads";
118 static constexpr auto initial_mint_count_key = "initial_mint_count";
119 static constexpr auto initial_mint_value_key = "initial_mint_value";
120 static constexpr auto loadgen_count_key = "loadgen_count";
121 static constexpr auto shard_completed_txs_cache_size
122 = "shard_completed_txs_cache_size";
123 static constexpr auto wait_for_followers_key = "wait_for_followers";
124 static constexpr auto private_key_postfix = "private_key";
125 static constexpr auto public_key_postfix = "public_key";
126 static constexpr auto attestation_threshold_key = "attestation_threshold";
127
129 using shard_range_t = std::pair<uint8_t, uint8_t>;
130
132 struct options {
134 size_t m_stxo_cache_depth{defaults::stxo_cache_depth};
136 size_t m_window_size{defaults::window_size};
138 size_t m_input_count{defaults::input_count};
140 size_t m_output_count{defaults::output_count};
142 double m_invalid_rate{0.0};
144 double m_fixed_tx_rate{defaults::fixed_tx_rate};
149 defaults::shard_completed_txs_cache_size};
150
152 std::vector<network::endpoint_t> m_atomizer_endpoints;
154 std::vector<network::endpoint_t> m_archiver_endpoints;
156 std::vector<network::endpoint_t> m_sentinel_endpoints;
158 std::vector<network::endpoint_t> m_watchtower_client_endpoints;
160 std::vector<network::endpoint_t> m_watchtower_internal_endpoints;
162 std::vector<network::endpoint_t> m_shard_endpoints;
164 std::vector<network::endpoint_t> m_atomizer_raft_endpoints;
167 size_t m_batch_size{defaults::batch_size};
169 size_t m_target_block_interval{defaults::target_block_interval};
171 std::vector<logging::log_level> m_atomizer_loglevels;
174 defaults::election_timeout_upper_bound};
177 defaults::election_timeout_lower_bound};
179 int32_t m_heartbeat{defaults::heartbeat};
183 int32_t m_raft_max_batch{defaults::raft_max_batch};
185 std::vector<logging::log_level> m_shard_loglevels;
187 std::vector<std::string> m_shard_db_dirs;
190 std::vector<shard_range_t> m_shard_ranges;
191
193 std::optional<privkey_t> m_seed_privkey;
195 size_t m_seed_value{0};
197 size_t m_seed_from{0};
199 size_t m_seed_to{0};
200
202 std::vector<logging::log_level> m_sentinel_loglevels;
204 std::vector<logging::log_level> m_archiver_loglevels;
206 std::vector<logging::log_level> m_watchtower_loglevels;
208 std::vector<std::string> m_archiver_db_dirs;
211 bool m_fixed_tx_mode{false};
213 bool m_twophase_mode{false};
215 std::vector<std::vector<network::endpoint_t>>
219 std::vector<std::vector<network::endpoint_t>>
223 std::vector<std::vector<network::endpoint_t>>
226 std::vector<std::vector<network::endpoint_t>> m_coordinator_endpoints;
229 std::vector<std::vector<network::endpoint_t>>
232 size_t m_coordinator_max_threads{defaults::coordinator_max_threads};
234 std::vector<logging::log_level> m_coordinator_loglevels;
235
237 size_t m_initial_mint_count{defaults::initial_mint_count};
239 size_t m_initial_mint_value{defaults::initial_mint_value};
240
244 defaults::watchtower_block_cache_size};
248 defaults::watchtower_error_cache_size};
249
252
254 std::unordered_map<size_t, privkey_t> m_sentinel_private_keys;
255
257 std::unordered_set<pubkey_t, hashing::null> m_sentinel_public_keys;
258
260 size_t m_attestation_threshold{defaults::attestation_threshold};
261 };
262
268 auto read_options(const std::string& config_file)
269 -> std::variant<options, std::string>;
270
274 auto load_options(const std::string& config_file)
275 -> std::variant<options, std::string>;
276
282 auto check_options(const options& opts) -> std::optional<std::string>;
283
288 auto hash_in_shard_range(const shard_range_t& range, const hash_t& val)
289 -> bool;
290
297 auto loadgen_seed_range(const options& opts, size_t gen_id)
298 -> std::pair<size_t, size_t>;
299
302 auto get_args(int argc, char** argv) -> std::vector<std::string>;
303
323 class parser {
324 public:
327 explicit parser(const std::string& filename);
328
331 explicit parser(std::istream& stream);
332
337 [[nodiscard]] auto get_string(const std::string& key) const
338 -> std::optional<std::string>;
339
344 [[nodiscard]] auto get_ulong(const std::string& key) const
345 -> std::optional<size_t>;
346
351 [[nodiscard]] auto get_endpoint(const std::string& key) const
352 -> std::optional<network::endpoint_t>;
353
358 [[nodiscard]] auto get_loglevel(const std::string& key) const
359 -> std::optional<logging::log_level>;
360
365 [[nodiscard]] auto get_decimal(const std::string& key) const
366 -> std::optional<double>;
367
368 private:
369 using value_t = std::variant<std::string, size_t, double>;
370
371 [[nodiscard]] auto find_or_env(const std::string& key) const
372 -> std::optional<value_t>;
373
374 template<typename T>
375 [[nodiscard]] auto get_val(const std::string& key) const
376 -> std::optional<T> {
377 const auto it = find_or_env(key);
378 if(it) {
379 const auto* val = std::get_if<T>(&it.value());
380 if(val != nullptr) {
381 return *val;
382 }
383 }
384
385 return std::nullopt;
386 }
387
388 void init(std::istream& stream);
389
390 [[nodiscard]] static auto parse_value(const std::string& val)
391 -> value_t;
392
393 std::map<std::string, value_t> m_options;
394 };
395
396 auto parse_ip_port(const std::string& in_str) -> network::endpoint_t;
397}
398
399#endif // OPENCBDC_TX_SRC_COMMON_CONFIG_H_
Reads configuration parameters line-by-line from a file.
Definition config.hpp:323
auto get_endpoint(const std::string &key) const -> std::optional< network::endpoint_t >
Return the value for the given key if its value is an endpoint.
Definition config.cpp:800
auto get_string(const std::string &key) const -> std::optional< std::string >
Returns the given key if its value is a string.
Definition config.cpp:790
auto get_decimal(const std::string &key) const -> std::optional< double >
Return the value for the given key if its value is a double.
Definition config.cpp:819
auto get_ulong(const std::string &key) const -> std::optional< size_t >
Return the value for the given key if its value is a long.
Definition config.cpp:795
parser(const std::string &filename)
Constructor.
Definition config.cpp:765
auto get_loglevel(const std::string &key) const -> std::optional< logging::log_level >
Return the value for the given key if its value is a loglevel.
Definition config.cpp:810
auto check_options(const options &opts) -> std::optional< std::string >
Checks a fully populated options struct for invariants.
Definition config.cpp:680
auto load_options(const std::string &config_file) -> std::variant< options, std::string >
Loads options from the given config file and check for invariants.
Definition config.cpp:668
auto read_options(const std::string &config_file) -> std::variant< options, std::string >
Read options from the given config file without checking invariants.
Definition config.cpp:619
auto parse_ip_port(const std::string &in_str) -> network::endpoint_t
Definition config.cpp:13
auto hash_in_shard_range(const shard_range_t &range, const hash_t &val) -> bool
Checks if a hash is in the given range handled.
Definition config.cpp:736
auto get_args(int argc, char **argv) -> std::vector< std::string >
Converts c-args from an executable's main function into a vector of strings.
Definition config.cpp:751
auto loadgen_seed_range(const options &opts, size_t gen_id) -> std::pair< size_t, size_t >
Calculates the sub-range of total seeded outputs for a particular load generator ID.
Definition config.cpp:741
std::pair< uint8_t, uint8_t > shard_range_t
[start, end] inclusive.
Definition config.hpp:129
@ warn
Potentially unintended, unexpected, or undesirable behavior.
std::pair< ip_address, port_number_t > endpoint_t
[host name, port number].
Definition socket.hpp:19
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
Project-wide configuration options.
Definition config.hpp:132
size_t m_input_count
Number of inputs in fixed-size transactions from atomizer-cli.
Definition config.hpp:138
std::vector< network::endpoint_t > m_watchtower_client_endpoints
List of watchtower client endpoints, ordered by watchtower ID.
Definition config.hpp:158
size_t m_window_size
Maximum number of unconfirmed transactions in atomizer-cli.
Definition config.hpp:136
std::vector< network::endpoint_t > m_archiver_endpoints
List of archiver endpoints, ordered by archiver ID.
Definition config.hpp:154
int32_t m_raft_max_batch
Maximum number of raft log entries to batch into one RPC message.
Definition config.hpp:183
std::vector< network::endpoint_t > m_atomizer_endpoints
List of atomizer endpoints, ordered by atomizer ID.
Definition config.hpp:152
size_t m_loadgen_count
Number of load generators over which to split pre-seeded UTXOs.
Definition config.hpp:251
std::vector< std::vector< network::endpoint_t > > m_locking_shard_readonly_endpoints
List of locking shard read-only endpoints, ordered by shard ID then node ID.
Definition config.hpp:224
std::optional< privkey_t > m_seed_privkey
private key used for initial seed.
Definition config.hpp:193
size_t m_stxo_cache_depth
Depth of the spent transaction cache in the atomizer, in blocks.
Definition config.hpp:134
size_t m_seed_to
ending index for faked input used for initial seed.
Definition config.hpp:199
std::vector< network::endpoint_t > m_shard_endpoints
List of shard endpoints, ordered by shard ID.
Definition config.hpp:162
size_t m_initial_mint_count
Number of outputs in the initial mint transaction.
Definition config.hpp:237
std::vector< logging::log_level > m_coordinator_loglevels
List of coordinator log levels, ordered by coordinator ID.
Definition config.hpp:234
size_t m_coordinator_max_threads
Coordinator thread count limit.
Definition config.hpp:232
size_t m_shard_completed_txs_cache_size
The number of completed transactions that each locking shard (2PC) keeps in memory for responding to ...
Definition config.hpp:148
size_t m_batch_size
Maximum transaction batch size for one log entry in the raft atomizer or one batch in the coordinator...
Definition config.hpp:167
int32_t m_snapshot_distance
Raft snapshot distance, in number of log entries.
Definition config.hpp:181
double m_fixed_tx_rate
Proportion of fixed transactions sent from atomizer-cli.
Definition config.hpp:144
std::vector< shard_range_t > m_shard_ranges
List of shard UHS ID ranges by shard ID.
Definition config.hpp:190
std::vector< network::endpoint_t > m_watchtower_internal_endpoints
List of watchtower internal endpoints, ordered by watchtower ID.
Definition config.hpp:160
size_t m_attestation_threshold
Number of sentinel attestations needed for a compact transaction.
Definition config.hpp:260
std::vector< logging::log_level > m_atomizer_loglevels
List of atomizer log levels by atomizer ID.
Definition config.hpp:171
std::vector< std::string > m_shard_db_dirs
List of shard DB paths by shard ID.
Definition config.hpp:187
int32_t m_election_timeout_lower
Raft election timeout lower bound in milliseconds.
Definition config.hpp:176
std::vector< std::vector< network::endpoint_t > > m_coordinator_endpoints
List of coordinator endpoints, ordered by shard ID then node ID.
Definition config.hpp:226
std::unordered_map< size_t, privkey_t > m_sentinel_private_keys
Private keys for sentinels.
Definition config.hpp:254
std::vector< std::vector< network::endpoint_t > > m_locking_shard_raft_endpoints
List of locking shard raft endpoints, ordered by shard ID then node ID.
Definition config.hpp:220
bool m_twophase_mode
Flag set if the architecture is two-phase commit.
Definition config.hpp:213
size_t m_watchtower_block_cache_size
Number of blocks to store in watchtower block caches.
Definition config.hpp:243
std::vector< std::vector< network::endpoint_t > > m_locking_shard_endpoints
List of locking shard endpoints, ordered by shard ID then node ID.
Definition config.hpp:216
std::vector< logging::log_level > m_sentinel_loglevels
List of sentinel log levels by sentinel ID.
Definition config.hpp:202
std::vector< std::string > m_archiver_db_dirs
List of archiver DB paths by archiver ID.
Definition config.hpp:208
std::unordered_set< pubkey_t, hashing::null > m_sentinel_public_keys
Public keys for sentinels.
Definition config.hpp:257
size_t m_seed_value
output value to use for initial seed.
Definition config.hpp:195
std::vector< std::vector< network::endpoint_t > > m_coordinator_raft_endpoints
List of coordinator raft endpoints, ordered by shard ID then node ID.
Definition config.hpp:230
size_t m_seed_from
starting index for faked input used for initial seed.
Definition config.hpp:197
std::vector< logging::log_level > m_archiver_loglevels
List of archiver log levels by archiver ID.
Definition config.hpp:204
std::vector< logging::log_level > m_shard_loglevels
List of shard log levels by shard ID.
Definition config.hpp:185
double m_invalid_rate
Proportion of invalid transactions sent from atomizer-cli.
Definition config.hpp:142
bool m_fixed_tx_mode
Flag set if m_input_count or m_output_count are greater than zero.
Definition config.hpp:211
size_t m_initial_mint_value
Value for all outputs in the initial mint transaction.
Definition config.hpp:239
int32_t m_election_timeout_upper
Raft election timeout upper bound in milliseconds.
Definition config.hpp:173
int32_t m_heartbeat
Raft heartbeat timeout in milliseconds.
Definition config.hpp:179
size_t m_target_block_interval
Target block creation interval in the atomizer in milliseconds.
Definition config.hpp:169
size_t m_output_count
Number of outputs in fixed-size transactions from atomizer-cli.
Definition config.hpp:140
size_t m_watchtower_error_cache_size
Number of errors to store in watchtower error caches.
Definition config.hpp:247
std::vector< logging::log_level > m_watchtower_loglevels
List of archiver log levels by watchtower ID.
Definition config.hpp:206
std::vector< network::endpoint_t > m_sentinel_endpoints
List of sentinel endpoints, ordered by sentinel ID.
Definition config.hpp:156
std::vector< network::endpoint_t > m_atomizer_raft_endpoints
List of atomizer raft endpoints, ordered by atomizer ID.
Definition config.hpp:164