-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
320 lines (288 loc) · 9.82 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <vxldollar/lib/blocks.hpp>
#include <vxldollar/lib/config.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>
#include <valgrind/valgrind.h>
namespace
{
// useful for boost_lexical cast to allow conversion of hex strings
template <typename ElemT>
struct HexTo
{
ElemT value;
operator ElemT () const
{
return value;
}
friend std::istream & operator>> (std::istream & in, HexTo & out)
{
in >> std::hex >> out.value;
return in;
}
};
} // namespace
vxldollar::work_thresholds const vxldollar::work_thresholds::publish_full (
//0xffffffc000000000,
//0xfffffff800000000, // 8x higher than epoch_1
//0xfffffe0000000000 // 8x lower than epoch_1
//
0xf000000000000000, // 64x lower
0xf000000000000000, // 64x lower
0xf000000000000000 // 64x lower
);
vxldollar::work_thresholds const vxldollar::work_thresholds::publish_beta (
// 0xfffff00000000000, // 64x lower than publish_full.epoch_1
// 0xfffff00000000000, // same as epoch_1
// 0xffffe00000000000 // 2x lower than epoch_1
//
0xf000000000000000, // 64x lower
0xf000000000000000, // 64x lower
0xf000000000000000 // 64x lower
);
vxldollar::work_thresholds const vxldollar::work_thresholds::publish_dev (
// 0xfe00000000000000, // Very low for tests
// 0xffc0000000000000, // 8x higher than epoch_1
// 0xf000000000000000 // 8x lower than epoch_1
//
0xf000000000000000, // 64x lower
0xf000000000000000, // 64x lower
0xf000000000000000 // 64x lower
);
//
vxldollar::work_thresholds const vxldollar::work_thresholds::publish_test ( //defaults to live network levels
get_env_threshold_or_default ("VXLDOLLAR_TEST_EPOCH_1", 0xf000000000000000),//0xffffffc000000000),
get_env_threshold_or_default ("VXLDOLLAR_TEST_EPOCH_2", 0xf000000000000000),//0xfffffff800000000), // 8x higher than epoch_1
get_env_threshold_or_default ("VXLDOLLAR_TEST_EPOCH_2_RECV", 0xf000000000000000)//0xfffffe0000000000) // 8x lower than epoch_1
);
uint64_t vxldollar::work_thresholds::threshold_entry (vxldollar::work_version const version_a, vxldollar::block_type const type_a) const
{
uint64_t result{ std::numeric_limits<uint64_t>::max () };
if (type_a == vxldollar::block_type::state)
{
switch (version_a)
{
case vxldollar::work_version::work_1:
result = entry;
break;
default:
debug_assert (false && "Invalid version specified to work_threshold_entry");
}
}
else
{
result = epoch_1;
}
return result;
}
#ifndef VXLDOLLAR_FUZZER_TEST
uint64_t vxldollar::work_thresholds::value (vxldollar::root const & root_a, uint64_t work_a) const
{
uint64_t result;
blake2b_state hash;
blake2b_init (&hash, sizeof (result));
blake2b_update (&hash, reinterpret_cast<uint8_t *> (&work_a), sizeof (work_a));
blake2b_update (&hash, root_a.bytes.data (), root_a.bytes.size ());
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&result), sizeof (result));
return result;
}
#else
uint64_t vxldollar::work_thresholds::value (vxldollar::root const & root_a, uint64_t work_a) const
{
return base + 1;
}
#endif
uint64_t vxldollar::work_thresholds::threshold (vxldollar::block_details const & details_a) const
{
static_assert (vxldollar::epoch::max == vxldollar::epoch::epoch_2, "work_v1::threshold is ill-defined");
uint64_t result{ std::numeric_limits<uint64_t>::max () };
switch (details_a.epoch)
{
case vxldollar::epoch::epoch_2:
result = (details_a.is_receive || details_a.is_epoch) ? epoch_2_receive : epoch_2;
break;
case vxldollar::epoch::epoch_1:
case vxldollar::epoch::epoch_0:
result = epoch_1;
break;
default:
debug_assert (false && "Invalid epoch specified to work_v1 ledger work_threshold");
}
return result;
}
uint64_t vxldollar::work_thresholds::threshold (vxldollar::work_version const version_a, vxldollar::block_details const details_a) const
{
uint64_t result{ std::numeric_limits<uint64_t>::max () };
switch (version_a)
{
case vxldollar::work_version::work_1:
result = threshold (details_a);
break;
default:
debug_assert (false && "Invalid version specified to ledger work_threshold");
}
return result;
}
double vxldollar::work_thresholds::normalized_multiplier (double const multiplier_a, uint64_t const threshold_a) const
{
debug_assert (multiplier_a >= 1);
auto multiplier (multiplier_a);
/* Normalization rules
ratio = multiplier of max work threshold (send epoch 2) from given threshold
i.e. max = 0xfe00000000000000, given = 0xf000000000000000, ratio = 8.0
normalized = (multiplier + (ratio - 1)) / ratio;
Epoch 1
multiplier | normalized
1.0 | 1.0
9.0 | 2.0
25.0 | 4.0
Epoch 2 (receive / epoch subtypes)
multiplier | normalized
1.0 | 1.0
65.0 | 2.0
241.0 | 4.0
*/
if (threshold_a == epoch_1 || threshold_a == epoch_2_receive)
{
auto ratio (vxldollar::difficulty::to_multiplier (epoch_2, threshold_a));
debug_assert (ratio >= 1);
multiplier = (multiplier + (ratio - 1.0)) / ratio;
debug_assert (multiplier >= 1);
}
return multiplier;
}
double vxldollar::work_thresholds::denormalized_multiplier (double const multiplier_a, uint64_t const threshold_a) const
{
debug_assert (multiplier_a >= 1);
auto multiplier (multiplier_a);
if (threshold_a == epoch_1 || threshold_a == epoch_2_receive)
{
auto ratio (vxldollar::difficulty::to_multiplier (epoch_2, threshold_a));
debug_assert (ratio >= 1);
multiplier = multiplier * ratio + 1.0 - ratio;
debug_assert (multiplier >= 1);
}
return multiplier;
}
uint64_t vxldollar::work_thresholds::threshold_base (vxldollar::work_version const version_a) const
{
uint64_t result{ std::numeric_limits<uint64_t>::max () };
switch (version_a)
{
case vxldollar::work_version::work_1:
result = base;
break;
default:
debug_assert (false && "Invalid version specified to work_threshold_base");
}
return result;
}
uint64_t vxldollar::work_thresholds::difficulty (vxldollar::work_version const version_a, vxldollar::root const & root_a, uint64_t const work_a) const
{
uint64_t result{ 0 };
switch (version_a)
{
case vxldollar::work_version::work_1:
result = value (root_a, work_a);
break;
default:
debug_assert (false && "Invalid version specified to work_difficulty");
}
return result;
}
uint64_t vxldollar::work_thresholds::difficulty (vxldollar::block const & block_a) const
{
return difficulty (block_a.work_version (), block_a.root (), block_a.block_work ());
}
bool vxldollar::work_thresholds::validate_entry (vxldollar::work_version const version_a, vxldollar::root const & root_a, uint64_t const work_a) const
{
return difficulty (version_a, root_a, work_a) < threshold_entry (version_a, vxldollar::block_type::state);
}
bool vxldollar::work_thresholds::validate_entry (vxldollar::block const & block_a) const
{
return difficulty (block_a) < threshold_entry (block_a.work_version (), block_a.type ());
}
namespace vxldollar
{
char const * network_constants::active_network_err_msg = "Invalid network. Valid values are live, test, beta and dev.";
uint8_t get_major_node_version ()
{
return boost::numeric_cast<uint8_t> (boost::lexical_cast<int> (VXLDOLLAR_MAJOR_VERSION_STRING));
}
uint8_t get_minor_node_version ()
{
return boost::numeric_cast<uint8_t> (boost::lexical_cast<int> (VXLDOLLAR_MINOR_VERSION_STRING));
}
uint8_t get_patch_node_version ()
{
return boost::numeric_cast<uint8_t> (boost::lexical_cast<int> (VXLDOLLAR_PATCH_VERSION_STRING));
}
uint8_t get_pre_release_node_version ()
{
return boost::numeric_cast<uint8_t> (boost::lexical_cast<int> (VXLDOLLAR_PRE_RELEASE_VERSION_STRING));
}
std::string get_env_or_default (char const * variable_name, std::string default_value)
{
auto value = getenv (variable_name);
return value ? value : default_value;
}
uint64_t get_env_threshold_or_default (char const * variable_name, uint64_t const default_value)
{
auto * value = getenv (variable_name);
return value ? boost::lexical_cast<HexTo<uint64_t>> (value) : default_value;
}
uint16_t test_node_port ()
{
auto test_env = vxldollar::get_env_or_default ("VXLDOLLAR_TEST_NODE_PORT", "17155");
return boost::lexical_cast<uint16_t> (test_env);
}
uint16_t test_rpc_port ()
{
auto test_env = vxldollar::get_env_or_default ("VXLDOLLAR_TEST_RPC_PORT", "17156");
return boost::lexical_cast<uint16_t> (test_env);
}
uint16_t test_ipc_port ()
{
auto test_env = vxldollar::get_env_or_default ("VXLDOLLAR_TEST_IPC_PORT", "17157");
return boost::lexical_cast<uint16_t> (test_env);
}
uint16_t test_websocket_port ()
{
auto test_env = vxldollar::get_env_or_default ("VXLDOLLAR_TEST_WEBSOCKET_PORT", "17158");
return boost::lexical_cast<uint16_t> (test_env);
}
std::array<uint8_t, 2> test_magic_number ()
{
auto test_env = get_env_or_default ("VXLDOLLAR_TEST_MAGIC_NUMBER", "RX");
std::array<uint8_t, 2> ret;
std::copy (test_env.begin (), test_env.end (), ret.data ());
return ret;
}
void force_vxldollar_dev_network ()
{
vxldollar::network_constants::set_active_network (vxldollar::networks::vxldollar_dev_network);
}
bool running_within_valgrind ()
{
return (RUNNING_ON_VALGRIND > 0);
}
std::string get_node_toml_config_path (boost::filesystem::path const & data_path)
{
return (data_path / "config-node.toml").string ();
}
std::string get_rpc_toml_config_path (boost::filesystem::path const & data_path)
{
return (data_path / "config-rpc.toml").string ();
}
std::string get_qtwallet_toml_config_path (boost::filesystem::path const & data_path)
{
return (data_path / "config-qtwallet.toml").string ();
}
std::string get_access_toml_config_path (boost::filesystem::path const & data_path)
{
return (data_path / "config-access.toml").string ();
}
std::string get_tls_toml_config_path (boost::filesystem::path const & data_path)
{
return (data_path / "config-tls.toml").string ();
}
} // namespace vxldollar